Beispiel #1
0
            public PhpFunctionGroup(string @namespace, MethodGroup o)
            {
                var functions = o.Methods.Select(m => new PhpOperation(m));

                Class = PHP.Class(
                    name: Class.CreateName(@namespace, o.Name),
                    constructor: PHP.Constructor(
                        parameters: GroupConstructorParameters,
                        body: functions.SelectMany(f => f.ConstructorStatements)),
                    functions: functions.Select(f => f.Function),
                    properties: functions.Select(f => f.Property));

                Property = PHP.Property(Class.Name, "_" + o.Name + "_group");

                Create = PHP
                         .This
                         .Arrow(Property)
                         .Assign(PHP.New(Class.Name, ClientParameterRef))
                         .Statement();

                Function = PHP.Function(
                    name: $"get{o.Name}",
                    @return: Class,
                    body: PHP.Statements(PHP.Return(PHP.This.Arrow(Property))));
            }
Beispiel #2
0
            public PhpOperation(Method m)
            {
                var name = "_" + m.Name + "_operation";

                Property = PHP.Property(new ClassName(MicrosoftRestOperationInterface), name);

                var thisProperty = PHP.This.Arrow(Property);

                ConstructorStatements = OperationInfoInit(thisProperty, m);

                var parameters = m.Parameters
                                 .Where(p => !p.IsConstant &&
                                        !p.IsApiVersion() &&
                                        p.SerializedName != "subscriptionId");

                var call = PHP.Return(thisProperty.Call(
                                          CallFunction,
                                          PHP.CreateArray(parameters.Select(p => PHP.KeyValue(
                                                                                p.SerializedName,
                                                                                new ObjectName(p.SerializedName).Ref())))));

                Function = PHP.Function(
                    name: m.Name,
                    description: m.Description,
                    @return: m.ReturnType.Body == null ? null : SchemaObject.Create(m.ReturnType.Body).ToPhpType(),
                    parameters: parameters.Select(p => {
                    var phpType = SchemaObject.Create(p.ModelType).ToPhpType();
                    return(PHP.Parameter(
                               p.IsRequired ? phpType : new Nullable(phpType),
                               new ObjectName(p.SerializedName)));
                }),
                    body: PHP.Statements(call));
            }
Beispiel #3
0
        static IEnumerable <Statement> OperationInfoInit(
            Expression0 property, Method m)
        {
            // $_client->createOperation({...operationId...})
            var operationInfoCreate = ClientParameterRef.Call(
                CreateOperation,
                PHP.StringConst(m.SerializedName));

            // $this->{...operation...} =
            return(PHP.Statements(property.Assign(operationInfoCreate).Statement()));
        }
Beispiel #4
0
        public override async Task Generate(CodeModel codeModel)
        {
            var @namespace = Class.CreateName(codeModel.Namespace);

            var phpGroups = codeModel.Operations
                            .Where(o => o.Name.RawValue != string.Empty)
                            .Select(o => new PhpFunctionGroup(@namespace, o));

            var phpFunctions = codeModel.Operations
                               .Where(o => o.Name.RawValue == string.Empty)
                               .SelectMany(o => o.Methods.Select(m => new PhpOperation(m)));

            var swaggerObjectData = PHP.Const(
                SwaggerObjectData,
                PHP.FromJson(SwaggerObject.Create(codeModel)));

            var client = PHP.Class(
                name: Class.CreateName(@namespace, codeModel.Name),
                constructor: PHP.Constructor(
                    parameters: ClientConstructorParameters,
                    body: PHP
                    .Statements(CreateClient)
                    .Concat(phpGroups.Select(g => g.Create))
                    .Concat(phpFunctions.SelectMany(f => f.ConstructorStatements))),
                functions: phpGroups
                .Select(o => o.Function)
                .Concat(phpFunctions.Select(f => f.Function)),
                properties: phpGroups
                .Select(o => o.Property)
                .Concat(phpFunctions.Select(f => f.Property)),
                consts: PHP.Consts(swaggerObjectData));

            foreach (var class_ in phpGroups
                     .Select(o => o.Class)
                     .Concat(ImmutableArray.Create(client)))
            {
                await Write(
                    string.Join("\n", class_.ToCodeText(Indent)),
                    class_.Name.FileName,
                    false);
            }
        }