setName() public méthode

public setName ( string $name ) : ParameterGenerator
$name string
Résultat ParameterGenerator
 public function generate()
 {
     $modelBuilder = $this->controller->getModelBuilder();
     $className = $modelBuilder->getName() . 'Controller';
     $class = new ClassGenerator();
     $class->setName($className);
     $class->setExtendedClass('CrudController');
     $param = new ParameterGenerator();
     $param->setName('fb')->setType('FormBuilder');
     $body = $this->generateFormBuilderBody();
     $docblock = '@param FormBuilder $fb';
     $class->addMethod('buildForm', array($param), MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     $param = new ParameterGenerator();
     $param->setName('mb')->setType('ModelBuilder');
     $body = '';
     $docblock = '@param ModelBuilder $mb';
     $class->addMethod('buildModel', array($param), MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     $param = new ParameterGenerator();
     $param->setName('ob')->setType('OverviewBuilder');
     $body = '';
     $docblock = '@param OverviewBuilder $ob';
     $class->addMethod('buildOverview', array($param), MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     $this->generator->setClass($class);
     $this->generator->setUses(array('Boyhagemann\\Crud\\CrudController', 'Boyhagemann\\Form\\FormBuilder', 'Boyhagemann\\Model\\ModelBuilder', 'Boyhagemann\\Overview\\OverviewBuilder'));
     return $this->generator->generate();
 }
 public function generate()
 {
     if ($this->controller) {
         $modelBuilder = $this->controller->getModelBuilder();
         $className = $modelBuilder->getName();
     } else {
         $className = $this->class;
     }
     $modelClass = $this->modelClass ? $this->modelClass : $this->class;
     $class = new ClassGenerator();
     $class->setName($className);
     $class->setExtendedClass('CrudController');
     $class->addUse('Boyhagemann\\Crud\\CrudController');
     $class->addUse('Boyhagemann\\Form\\FormBuilder');
     $class->addUse('Boyhagemann\\Model\\ModelBuilder');
     $class->addUse('Boyhagemann\\Overview\\OverviewBuilder');
     $param = new ParameterGenerator();
     $param->setName('fb')->setType('FormBuilder');
     $body = $this->generateFormBuilderBody();
     $docblock = '@param FormBuilder $fb';
     $class->addMethod('buildForm', array($param), MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     $param = new ParameterGenerator();
     $param->setName('mb')->setType('ModelBuilder');
     $body = sprintf('$mb->name(\'%s\')->table(\'%s\');' . PHP_EOL, $modelClass, strtolower(str_replace('\\', '_', $modelClass)));
     $docblock = '@param ModelBuilder $mb';
     $class->addMethod('buildModel', array($param), MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     $param = new ParameterGenerator();
     $param->setName('ob')->setType('OverviewBuilder');
     $body = '';
     $docblock = '@param OverviewBuilder $ob';
     $class->addMethod('buildOverview', array($param), MethodGenerator::FLAG_PUBLIC, $body, $docblock);
     $this->generator->setClass($class);
     return $this->generator->generate();
 }
 /**
  * @param  ParameterReflection $reflectionParameter
  * @return ParameterGenerator
  */
 public static function fromReflection(ParameterReflection $reflectionParameter)
 {
     $param = new ParameterGenerator();
     $param->setName($reflectionParameter->getName());
     if ($reflectionParameter->isArray()) {
         $param->setType('array');
     } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
         $param->setType('callable');
     } else {
         $typeClass = $reflectionParameter->getClass();
         if ($typeClass) {
             $parameterType = $typeClass->getName();
             $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName();
             if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) {
                 $parameterType = substr($parameterType, strlen($currentNamespace) + 1);
             } else {
                 $parameterType = '\\' . trim($parameterType, '\\');
             }
             $param->setType($parameterType);
         }
     }
     $param->setPosition($reflectionParameter->getPosition());
     if ($reflectionParameter->isOptional()) {
         $param->setDefaultValue($reflectionParameter->getDefaultValue());
     }
     $param->setPassedByReference($reflectionParameter->isPassedByReference());
     return $param;
 }
    /**
     * @param  ParameterReflection $reflectionParameter
     * @return ParameterGenerator
     */
    public static function fromReflection(ParameterReflection $reflectionParameter)
    {
        $param = new ParameterGenerator();
        $param->setName($reflectionParameter->getName());

        if ($reflectionParameter->isArray()) {
            $param->setType('array');
        } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
            $param->setType('callable');
        } else {
            $typeClass = $reflectionParameter->getClass();
            if ($typeClass) {
                $param->setType($typeClass->getName());
            }
        }

        $param->setPosition($reflectionParameter->getPosition());

        if ($reflectionParameter->isOptional()) {
            $param->setDefaultValue($reflectionParameter->getDefaultValue());
        }
        $param->setPassedByReference($reflectionParameter->isPassedByReference());

        return $param;
    }
 public function testGenerateIsCorrect()
 {
     $parameterGenerator = new ParameterGenerator();
     $parameterGenerator->setType('Foo');
     $parameterGenerator->setName('bar');
     $parameterGenerator->setDefaultValue(15);
     $this->assertEquals('Foo $bar = 15', $parameterGenerator->generate());
     $parameterGenerator->setDefaultValue('foo');
     $this->assertEquals('Foo $bar = \'foo\'', $parameterGenerator->generate());
 }
 public function generatePropertyAdder($propertyName)
 {
     $parameterName = rtrim($propertyName, 's');
     $methodName = 'add' . ucfirst($parameterName);
     $setterGenerator = clone $this->getPropertySetterGenerator();
     $setterGenerator->setName($methodName);
     $parameter = new CodeGenerator\ParameterGenerator();
     $parameter->setName($parameterName);
     $setterGenerator->setParameter($parameter);
     $valueParameter = new CodeGenerator\ParameterGenerator();
     $valueParameter->setName('value');
     $setterGenerator->setParameter($valueParameter);
     $setterGenerator->setBody("\$this->{$propertyName}[\${$parameterName}] = \$value;\nreturn \$this;");
     return $setterGenerator;
 }
 /**
  * @param  ParameterReflection $reflectionParameter
  * @return ParameterGenerator
  */
 public static function fromReflection(ParameterReflection $reflectionParameter)
 {
     $param = new ParameterGenerator();
     $param->setName($reflectionParameter->getName());
     if ($type = self::extractFQCNTypeFromReflectionType($reflectionParameter)) {
         $param->setType($type);
     }
     $param->setPosition($reflectionParameter->getPosition());
     $variadic = method_exists($reflectionParameter, 'isVariadic') && $reflectionParameter->isVariadic();
     $param->setVariadic($variadic);
     if (!$variadic && $reflectionParameter->isOptional()) {
         try {
             $param->setDefaultValue($reflectionParameter->getDefaultValue());
         } catch (\ReflectionException $e) {
             $param->setDefaultValue(null);
         }
     }
     $param->setPassedByReference($reflectionParameter->isPassedByReference());
     return $param;
 }
 public function transform()
 {
     $classGenerator = $this->getService()->getClassGenerator();
     if ($createContactBatch = $classGenerator->getMethod('CreateContactBatch')) {
         $newMethod = new CodeGenerator\MethodGenerator();
         $newMethod->setName($createContactBatch->getName());
         $docblock = $createContactBatch->getDocBlock();
         $docblock->setTag((new CodeGenerator\DocBlock\Tag\ParamTag())->setParamName('id')->setDataType('int'));
         $newMethod->setDocBlock($docblock);
         $parameters = $createContactBatch->getParameters();
         $idParameter = new CodeGenerator\ParameterGenerator();
         $idParameter->setName('id');
         $createContactBatch->setParameter($idParameter);
         $newMethod->setParameters(array("id" => $idParameter, "CreateContactBatch" => $parameters['CreateContactBatch']));
         $body = $createContactBatch->getBody();
         $body = str_replace('array()', 'array($id)', $body);
         $newMethod->setBody($body);
         $classGenerator->removeMethod('CreateContactBatch');
         $classGenerator->addMethodFromGenerator($newMethod);
     }
 }
 public function generatePropertyAdder($propertyName, $withIndex = true, $proxyPropertyName = null)
 {
     $proxyPropertyName = $proxyPropertyName ?: $propertyName;
     list($parameterName, $methodName) = $this->generatePropertyAdderName($proxyPropertyName);
     $setterGenerator = clone $this->getPropertySetterGenerator();
     $setterGenerator->setName($methodName);
     if ($withIndex) {
         $parameter = new CodeGenerator\ParameterGenerator();
         $parameter->setName($parameterName);
         $setterGenerator->setParameter($parameter);
     }
     $valueParameter = new CodeGenerator\ParameterGenerator();
     $valueParameter->setName('value');
     $setterGenerator->setParameter($valueParameter);
     if ($withIndex) {
         $setterGenerator->setBody("\$this->{$propertyName}[\${$parameterName}] = \$value;\nreturn \$this;");
     } else {
         $setterGenerator->setBody("\$this->{$propertyName}[] = \$value;\nreturn \$this;");
     }
     return $setterGenerator;
 }
 public function generatePropertySetter($property)
 {
     $property = lcfirst($property);
     $methodName = 'set' . ucfirst($property);
     $setterGenerator = clone $this->getPropertySetterGenerator();
     $setterGenerator->setName($methodName);
     $parameter = new CodeGenerator\ParameterGenerator();
     $parameter->setName($property);
     $setterGenerator->setParameter($parameter);
     $setterGenerator->setBody("\$this->{$property} = \${$property};\nreturn \$this;");
     return $setterGenerator;
 }
Exemple #11
0
 /**
  * Construct, configure, and return a PHP class file code generation object
  *
  * Creates a Zend\Code\Generator\FileGenerator object that has
  * created the specified class and service locator methods.
  *
  * @param  null|string                         $filename
  * @throws \Zend\Di\Exception\RuntimeException
  * @return FileGenerator
  */
 public function getCodeGenerator($filename = null)
 {
     $injector = $this->injector;
     $im = $injector->instanceManager();
     $indent = '    ';
     $aliases = $this->reduceAliases($im->getAliases());
     $caseStatements = array();
     $getters = array();
     $definitions = $injector->definitions();
     $fetched = array_unique(array_merge($definitions->getClasses(), $im->getAliases()));
     foreach ($fetched as $name) {
         $getter = $this->normalizeAlias($name);
         $meta = $injector->get($name);
         $params = $meta->getParams();
         // Build parameter list for instantiation
         foreach ($params as $key => $param) {
             if (null === $param || is_scalar($param) || is_array($param)) {
                 $string = var_export($param, 1);
                 if (strstr($string, '::__set_state(')) {
                     throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
                 }
                 $params[$key] = $string;
             } elseif ($param instanceof GeneratorInstance) {
                 /* @var $param GeneratorInstance */
                 $params[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
             } else {
                 $message = sprintf('Unable to use object arguments when building containers. Encountered with "%s", parameter of type "%s"', $name, get_class($param));
                 throw new Exception\RuntimeException($message);
             }
         }
         // Strip null arguments from the end of the params list
         $reverseParams = array_reverse($params, true);
         foreach ($reverseParams as $key => $param) {
             if ('NULL' === $param) {
                 unset($params[$key]);
                 continue;
             }
             break;
         }
         // Create instantiation code
         $constructor = $meta->getConstructor();
         if ('__construct' != $constructor) {
             // Constructor callback
             $callback = var_export($constructor, 1);
             if (strstr($callback, '::__set_state(')) {
                 throw new Exception\RuntimeException('Unable to build containers that use callbacks requiring object instances');
             }
             if (count($params)) {
                 $creation = sprintf('$object = call_user_func(%s, %s);', $callback, implode(', ', $params));
             } else {
                 $creation = sprintf('$object = call_user_func(%s);', $callback);
             }
         } else {
             // Normal instantiation
             $className = '\\' . ltrim($name, '\\');
             $creation = sprintf('$object = new %s(%s);', $className, implode(', ', $params));
         }
         // Create method call code
         $methods = '';
         foreach ($meta->getMethods() as $methodData) {
             if (!isset($methodData['name']) && !isset($methodData['method'])) {
                 continue;
             }
             $methodName = isset($methodData['name']) ? $methodData['name'] : $methodData['method'];
             $methodParams = $methodData['params'];
             // Create method parameter representation
             foreach ($methodParams as $key => $param) {
                 if (null === $param || is_scalar($param) || is_array($param)) {
                     $string = var_export($param, 1);
                     if (strstr($string, '::__set_state(')) {
                         throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
                     }
                     $methodParams[$key] = $string;
                 } elseif ($param instanceof GeneratorInstance) {
                     $methodParams[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
                 } else {
                     $message = sprintf('Unable to use object arguments when generating method calls. Encountered with class "%s", method "%s", parameter of type "%s"', $name, $methodName, get_class($param));
                     throw new Exception\RuntimeException($message);
                 }
             }
             // Strip null arguments from the end of the params list
             $reverseParams = array_reverse($methodParams, true);
             foreach ($reverseParams as $key => $param) {
                 if ('NULL' === $param) {
                     unset($methodParams[$key]);
                     continue;
                 }
                 break;
             }
             $methods .= sprintf("\$object->%s(%s);\n", $methodName, implode(', ', $methodParams));
         }
         // Generate caching statement
         $storage = '';
         if ($im->hasSharedInstance($name, $params)) {
             $storage = sprintf("\$this->services['%s'] = \$object;\n", $name);
         }
         // Start creating getter
         $getterBody = '';
         // Create fetch of stored service
         if ($im->hasSharedInstance($name, $params)) {
             $getterBody .= sprintf("if (isset(\$this->services['%s'])) {\n", $name);
             $getterBody .= sprintf("%sreturn \$this->services['%s'];\n}\n\n", $indent, $name);
         }
         // Creation and method calls
         $getterBody .= sprintf("%s\n", $creation);
         $getterBody .= $methods;
         // Stored service
         $getterBody .= $storage;
         // End getter body
         $getterBody .= "return \$object;\n";
         $getterDef = new MethodGenerator();
         $getterDef->setName($getter);
         $getterDef->setBody($getterBody);
         $getters[] = $getterDef;
         // Get cases for case statements
         $cases = array($name);
         if (isset($aliases[$name])) {
             $cases = array_merge($aliases[$name], $cases);
         }
         // Build case statement and store
         $statement = '';
         foreach ($cases as $value) {
             $statement .= sprintf("%scase '%s':\n", $indent, $value);
         }
         $statement .= sprintf("%sreturn \$this->%s();\n", str_repeat($indent, 2), $getter);
         $caseStatements[] = $statement;
     }
     // Build switch statement
     $switch = sprintf("switch (%s) {\n%s\n", '$name', implode("\n", $caseStatements));
     $switch .= sprintf("%sdefault:\n%sreturn parent::get(%s, %s);\n", $indent, str_repeat($indent, 2), '$name', '$params');
     $switch .= "}\n\n";
     // Build get() method
     $nameParam = new ParameterGenerator();
     $nameParam->setName('name');
     $paramsParam = new ParameterGenerator();
     $paramsParam->setName('params')->setType('array')->setDefaultValue(array());
     $get = new MethodGenerator();
     $get->setName('get');
     $get->setParameters(array($nameParam, $paramsParam));
     $get->setBody($switch);
     // Create getters for aliases
     $aliasMethods = array();
     foreach ($aliases as $class => $classAliases) {
         foreach ($classAliases as $alias) {
             $aliasMethods[] = $this->getCodeGenMethodFromAlias($alias, $class);
         }
     }
     // Create class code generation object
     $container = new ClassGenerator();
     $container->setName($this->containerClass)->setExtendedClass('ServiceLocator')->addMethodFromGenerator($get)->addMethods($getters)->addMethods($aliasMethods);
     // Create PHP file code generation object
     $classFile = new FileGenerator();
     $classFile->setUse('Zend\\Di\\ServiceLocator')->setClass($container);
     if (null !== $this->namespace) {
         $classFile->setNamespace($this->namespace);
     }
     if (null !== $filename) {
         $classFile->setFilename($filename);
     }
     return $classFile;
 }
Exemple #12
0
 private function buildClass($replacement)
 {
     $type = $this->splitNsandClass($replacement['originalFullyQualifiedType']);
     $class = new ClassGenerator();
     $class->setName($type['class']);
     $class->setNamespaceName($type['ns']);
     $class->setExtendedClass('\\Brads\\Ppm\\Proxy');
     $properties = [];
     $methods = [];
     $implementedInterfaces = [];
     foreach ($versions as $version => $info) {
         foreach ($info['files'] as $file) {
             echo "Parsing: " . $this->vendorDir . '/' . $package . '/' . $version . '/' . $file . "\n";
             $parsedFile = new ReflectionFile($this->vendorDir . '/' . $package . '/' . $version . '/' . $file);
             $parsedClass = $parsedFile->getFileNamespace($info['toNs'])->getClass($info['toNs'] . '\\' . $type['class']);
             if ($parsedClass->getInterfaceNames() != null) {
                 $implementedInterfaces = array_merge($implementedInterfaces, $parsedClass->getInterfaceNames());
             }
             foreach ($parsedClass->getMethods() as $method) {
                 if ($method->isPublic()) {
                     $generatedMethod = new MethodGenerator();
                     $generatedMethod->setName($method->name);
                     $generatedMethod->setBody('echo "Hello world!";');
                     $generatedMethod->setAbstract($method->isAbstract());
                     $generatedMethod->setFinal($method->isFinal());
                     $generatedMethod->setStatic($method->isStatic());
                     $generatedMethod->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
                     foreach ($method->getParameters() as $param) {
                         $generatedParam = new ParameterGenerator();
                         $generatedParam->setName($param->name);
                         if ($param->hasType()) {
                             $generatedParam->setType($param->getType());
                         }
                         //$generatedParam->setDefaultValue($param->getDefaultValue());
                         $generatedParam->setPosition($param->getPosition());
                         $generatedParam->setVariadic($param->isVariadic());
                         $generatedParam->setPassedByReference($param->isPassedByReference());
                         $generatedMethod->setParameter($generatedParam);
                     }
                     $existingMethod = Linq::from($methods)->firstOrDefault(null, function (MethodGenerator $v) use($method) {
                         return $v->getName() == $method->name;
                     });
                     if ($existingMethod != null) {
                         $existingParams = $existingMethod->getParameters();
                         foreach ($generatedMethod->getParameters() as $newParam) {
                             $existingParam = Linq::from($existingParams)->firstOrDefault(function (ParameterGenerator $v) {
                                 return $v->getName() == $newParam->getName();
                             });
                             if ($existingParam == null) {
                                 $existingMethod->setParameter($newParam);
                             }
                         }
                     } else {
                         $methods[] = $generatedMethod;
                     }
                 }
             }
             foreach ($parsedClass->getProperties() as $prop) {
                 //$properties[] = PropertyGenerator::fromReflection($prop);
             }
         }
     }
     $class->setImplementedInterfaces($implementedInterfaces);
     $class->addMethods($methods);
     $class->addProperties($properties);
     return (new FileGenerator(['classes' => [$class]]))->generate();
 }
 public function generateParameterSetter($parameterName, $swaggerParameter)
 {
     $methodName = 'set' . ucfirst($parameterName);
     $setterGenerator = clone $this->getParameterSetterGenerator();
     $setterGenerator->setName($methodName);
     $parameter = new CodeGenerator\ParameterGenerator();
     $parameter->setName($parameterName);
     $setterGenerator->setParameter($parameter);
     $setterGenerator->setBody("\$this->{$parameterName} = \${$parameterName};\nreturn \$this;");
     return $setterGenerator;
 }
 /**
  * Generate method
  *
  * @param string $methodName
  * @return void
  */
 protected function generateMethod($methodName)
 {
     $methodReflection = $this->_method[$methodName];
     $docBlock = new DocBlockGenerator();
     $docBlock->setShortDescription("Delicate {$methodName}() to __call() method ");
     if ($methodReflection->getDocComment()) {
         $docBlockReflection = new DocBlockReflection($methodReflection);
         $docBlock->fromReflection($docBlockReflection);
     }
     $method = new MethodGenerator();
     $method->setName($methodName);
     $method->setDocBlock($docBlock);
     $method->setBody(sprintf(self::METHOD_TEMPLATE, $methodName));
     if ($methodReflection->isPublic()) {
         $method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
     } else {
         if ($methodReflection->isProtected()) {
             $method->setVisibility(MethodGenerator::VISIBILITY_PROTECTED);
         } else {
             if ($methodReflection->isPrivate()) {
                 $method->setVisibility(MethodGenerator::VISIBILITY_PRIVATE);
             }
         }
     }
     foreach ($methodReflection->getParameters() as $parameter) {
         $parameterGenerator = new ParameterGenerator();
         $parameterGenerator->setPosition($parameter->getPosition());
         $parameterGenerator->setName($parameter->getName());
         $parameterGenerator->setPassedByReference($parameter->isPassedByReference());
         if ($parameter->isDefaultValueAvailable()) {
             $parameterGenerator->setDefaultValue($parameter->getDefaultValue());
         }
         if ($parameter->isArray()) {
             $parameterGenerator->setType('array');
         }
         if ($typeClass = $parameter->getClass()) {
             $parameterGenerator->setType($typeClass->getName());
         }
         $method->setParameter($parameterGenerator);
     }
     $this->addMethodFromGenerator($method);
 }
 public function getConstructorGenerator()
 {
     if (!$this->constructorGenerator && ($extendedClass = $this->getClassGenerator()->getExtendedClass())) {
         $constructorGenerator = new CodeGenerator\MethodGenerator();
         $constructorGenerator->setName('__construct');
         $wsdlParameter = new CodeGenerator\ParameterGenerator();
         $wsdlParameter->setName('wsdl');
         $wsdlParameter->setDefaultValue($this->getWsdl());
         $constructorGenerator->setParameter($wsdlParameter);
         $optionsParameter = new CodeGenerator\ParameterGenerator();
         $optionsParameter->setName('options');
         $optionsParameter->setDefaultValue(array());
         $constructorGenerator->setParameter($optionsParameter);
         $constructorGenerator->setBody("parent::__construct(\$wsdl, \$options);");
         $this->constructorGenerator = $constructorGenerator;
     }
     return $this->constructorGenerator;
 }
 /**
  * @group 6023
  *
  * @coversNothing
  */
 public function testGeneratedParametersHaveEscapedDefaultValues()
 {
     $parameter = new ParameterGenerator();
     $parameter->setName('foo');
     $parameter->setDefaultValue("\\'");
     $parameter->setType('stdClass');
     $this->assertSame("stdClass \$foo = '\\\\\\''", $parameter->generate());
 }