Наследование: extends Zend\Code\Generator\AbstractMemberGenerator
Пример #1
0
 /**
  * Constructor
  *
  * @param PropertyGenerator   $initializerProperty
  * @param ZendMethodGenerator $callInitializer
  */
 public function __construct(PropertyGenerator $initializerProperty, ZendMethodGenerator $callInitializer)
 {
     parent::__construct('initializeProxy');
     $this->setDocblock('{@inheritDoc}');
     $this->setReturnType('bool');
     $this->setBody('return $this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'initializeProxy\', []);');
 }
    /**
     * @param  State|\Scaffold\State $state
     * @return State|void
     */
    public function build(State $state)
    {
        $model = $state->getModel('options-factory');
        $generator = new ClassGenerator($model->getName());
        $generator->setImplementedInterfaces(['FactoryInterface']);
        $model->setGenerator($generator);
        $generator->addUse('Zend\\ServiceManager\\FactoryInterface');
        $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface');
        $options = $state->getModel('options');
        $key = $options->getServiceName();
        $key = substr($key, 0, -7);
        $body = <<<EOF
\$config = \$serviceLocator->get('Config');
return new {$options->getClassName()}(
    isset(\$config['{$key}'])
        ? \$config['{$key}']
        : []
);
EOF;
        $method = new MethodGenerator('createService');
        $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface'));
        $method->setBody($body);
        $doc = new DocBlockGenerator('');
        $doc->setTag(new Tag(['name' => 'inhertidoc']));
        $method->setDocBlock($doc);
        $generator->addMethodFromGenerator($method);
    }
Пример #3
0
    public function addToString()
    {
        $classGenerator = $this->getClassGenerator();
        $methodGenerator = new CodeGenerator\MethodGenerator();
        $methodGenerator->setName('__toString');
        $idAttribute = 'id';
        $body = <<<METHODBODY
\$attributes = array(\$this->{$idAttribute});

METHODBODY;
        foreach (static::$secondaryProperties as $attributeName => $propertyName) {
            $body .= <<<METHODBODY
if (!empty(\$this->{$propertyName})) {
    \$attributes[] = "{$attributeName}={\$this->{$propertyName}}";
}

METHODBODY;
        }
        $body .= <<<METHODBODY
foreach (\$this->attributes as \$attributeName => \$value) {
    if (!empty(\$value)) {
        \$attributes[] = "{\$attributeName}={\$value}";
    }
}
return implode('!', \$attributes);
METHODBODY;
        $methodGenerator->setBody($body);
        $classGenerator->addMethodFromGenerator($methodGenerator);
    }
Пример #4
0
 protected function getConstructor()
 {
     $defaultValue = new ValueGenerator([], ValueGenerator::TYPE_ARRAY);
     $setterParam = new ParameterGenerator('properties', 'array', $defaultValue);
     $methodGenerator = new MethodGenerator('__construct', [$setterParam]);
     $methodGenerator->setBody('$this->properties = $properties;');
     return $methodGenerator;
 }
 /**
  * @param ReflectionClass  $originalClass
  * @param ClassGenerator   $classGenerator
  * @param MethodGenerator  $generatedMethod
  *
  * @return void|false
  */
 public static function addMethodIfNotFinal(ReflectionClass $originalClass, ClassGenerator $classGenerator, MethodGenerator $generatedMethod)
 {
     $methodName = $generatedMethod->getName();
     if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) {
         return false;
     }
     $classGenerator->addMethodFromGenerator($generatedMethod);
 }
Пример #6
0
 /**
  * Build method factory
  *
  * @param ClassGenerator $generator
  */
 public function buildFactory(ClassGenerator $generator)
 {
     $docBlock = new DocBlockGenerator('@return ' . $this->config->getName());
     $factory = new MethodGenerator();
     $factory->setDocBlock($docBlock);
     $factory->setName('factory');
     $factory->setBody('return new ' . $this->config->getName() . '();');
     $generator->addMethodFromGenerator($factory);
 }
Пример #7
0
 /**
  * {@inheritDoc}
  */
 protected function setUp()
 {
     $this->initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator');
     $this->initMethod = $this->getMock('Zend\\Code\\Generator\\MethodGenerator');
     $this->publicProperties = $this->getMockBuilder('ProxyManager\\ProxyGenerator\\PropertyGenerator\\PublicPropertiesMap')->disableOriginalConstructor()->getMock();
     $this->initializer->expects($this->any())->method('getName')->will($this->returnValue('foo'));
     $this->initMethod->expects($this->any())->method('getName')->will($this->returnValue('baz'));
     $this->publicProperties->expects($this->any())->method('isEmpty')->will($this->returnValue(false));
     $this->publicProperties->expects($this->any())->method('getName')->will($this->returnValue('bar'));
 }
Пример #8
0
 /**
  * @param string $name
  * @param Code $code
  * @return PhpMethod
  */
 public function addMethod($name, Code $code)
 {
     $this->code[$name] = $code;
     $method = new MethodGenerator();
     $method->setName($name);
     $method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
     // $code remains object until turning to string
     $method->setBody($code);
     $this->addMethodFromGenerator($method);
     return $method;
 }
Пример #9
0
    public function addTestIdMethod(ClassGenerator $generator, State $state)
    {
        $method = new MethodGenerator('testGetSetId');
        $class = $state->getEntityModel()->getClassName();
        $code = <<<EOF
\$object = new {$class}();
\$object->setId(123);
\$this->assertEquals(123, \$object->getId());
EOF;
        $method->setBody($code);
        $generator->addMethodFromGenerator($method);
    }
Пример #10
0
 /**
  * Generate an init method
  */
 protected function addInitMethod()
 {
     // set action body
     $body = ['// add form elements and form configuration here'];
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('init');
     $method->setBody($body);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Generate form by adding elements'));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return \Zend\Code\Generator\GeneratorInterface
  */
 protected function generateClearMethod(Entity $entity)
 {
     $lines = $this->generateBody($entity);
     $body = implode(PHP_EOL, $lines);
     $method = MethodGenerator::fromArray(['name' => 'clear', 'body' => $body, 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
     return $method;
 }
Пример #12
0
 /**
  * @param \rg\injektor\generators\FactoryGenerator $factoryGenerator
  */
 public function __construct(FactoryGenerator $factoryGenerator)
 {
     parent::__construct('getInstance');
     $this->factoryGenerator = $factoryGenerator;
     $parameter = new \Zend\Code\Generator\ParameterGenerator('parameters', 'array', array());
     $this->setParameter($parameter);
 }
 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return string
  */
 protected function generateMethod(Entity $entity)
 {
     $lines = $this->generateBody($entity);
     $body = implode(PHP_EOL, $lines);
     $method = MethodGenerator::fromArray(['name' => 'fromArray', 'body' => $body, 'static' => true, 'parameters' => [['name' => 'values', 'type' => 'array']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
     return $method;
 }
 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return \Zend\Code\Generator\GeneratorInterface
  */
 protected function generateConstructorMethod(Entity $entity)
 {
     $lines = $this->generateBody($entity);
     $body = implode(PHP_EOL, $lines);
     $method = MethodGenerator::fromArray(['name' => '__construct', 'body' => $body, 'parameters' => [['name' => 'stream', 'type' => 'mixed', 'defaultValue' => null], ['name' => 'configuration', 'type' => '\\Protobuf\\Configuration', 'defaultValue' => null]], 'docblock' => ['shortDescription' => '{@inheritdoc}']]);
     return $method;
 }
Пример #15
0
 /**
  * Build a Code Generation Php Object from a Class Reflection
  *
  * @param  ClassReflection $classReflection
  * @return TraitGenerator
  */
 public static function fromReflection(ClassReflection $classReflection)
 {
     // class generator
     $cg = new static($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock()));
     }
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
         }
     }
     $cg->addProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $methods[] = MethodGenerator::fromReflection($reflectionMethod);
         }
     }
     $cg->addMethods($methods);
     return $cg;
 }
Пример #16
0
 /**
  * Generate an init method
  */
 protected function addInitMethod()
 {
     // set action body
     $body = array('// add input objects here');
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('init');
     $method->setBody($body);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Generate input filter by adding inputs'));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
Пример #17
0
 /**
  * Generate an __invoke method
  */
 protected function addInvokeMethod()
 {
     // set action body
     $body = ['// add view helper code here', '$output = \'\';', '', 'return $output;'];
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('__invoke');
     $method->setBody($body);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Called when view helper is executed', null, [new ReturnTag(['string'])]));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
Пример #18
0
 /**
  * @param \ReflectionClass                                                   $originalClass
  * @param \Zend\Code\Generator\PropertyGenerator                             $initializerProperty
  * @param \Zend\Code\Generator\MethodGenerator                               $callInitializer
  * @param \ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap $publicProperties
  */
 public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, MethodGenerator $callInitializer, PublicPropertiesMap $publicProperties)
 {
     parent::__construct($originalClass, '__get', array(new ParameterGenerator('name')));
     $override = $originalClass->hasMethod('__get');
     $callParent = '';
     $this->setDocblock(($override ? "{@inheritDoc}\n" : '') . '@param string $name');
     if (!$publicProperties->isEmpty()) {
         $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . '    return $this->$name;' . "\n}\n\n";
     }
     if ($override) {
         $callParent .= 'return parent::__get($name);';
     } else {
         $callParent .= PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'name');
     }
     $this->setBody('$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'__get\', array(\'name\' => $name));' . "\n\n" . $callParent);
 }
 /**
  * Generate an __invoke method
  */
 protected function addInvokeMethod()
 {
     // set action body
     $body = ['// add controller plugin code here'];
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('__invoke');
     $method->setBody($body);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Called when controller plugin is executed', null, [new ReturnTag(['mixed'])]));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return MethodGenerator
  */
 protected function generateMethod(Entity $entity)
 {
     $lines = $this->generateBody($entity);
     $body = implode(PHP_EOL, $lines);
     $method = MethodGenerator::fromArray(['name' => 'merge', 'body' => $body, 'parameters' => [['name' => 'message', 'type' => '\\Protobuf\\Message']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
     return $method;
 }
Пример #21
0
 /**
  * Generate a isValid method
  */
 protected function addIsValidMethod()
 {
     // set action body
     $body = ['$this->setValue((string) $value);', '', '// add validation code here', '$isValid = true;', '', 'if (!$isValid) {', '    $this->error(self::INVALID);', '    return false;', '}', '', 'return true;'];
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('isValid');
     $method->setBody($body);
     $method->setParameters([new ParameterGenerator('value', 'mixed')]);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Called when validator is executed', null, [new ParamTag('value', ['mixed']), new ReturnTag(['mixed'])]));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
 /**
  * @param \Zend\Code\Reflection\MethodReflection $originalMethod
  * @param \Zend\Code\Generator\PropertyGenerator $initializerProperty
  * @param \Zend\Code\Generator\MethodGenerator   $callInitializer
  *
  * @return LazyLoadingMethodInterceptor|static
  */
 public static function generateMethod(MethodReflection $originalMethod, PropertyGenerator $initializerProperty, ZendMethodGenerator $callInitializer)
 {
     /* @var $method self */
     $method = static::fromReflection($originalMethod);
     $parameters = $originalMethod->getParameters();
     $methodName = $originalMethod->getName();
     $initializerParams = array();
     $forwardedParams = array();
     foreach ($parameters as $parameter) {
         $parameterName = $parameter->getName();
         $initializerParams[] = var_export($parameterName, true) . ' => $' . $parameterName;
         $forwardedParams[] = '$' . $parameterName;
     }
     $method->setBody('$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(' . var_export($methodName, true) . ', array(' . implode(', ', $initializerParams) . "));\n\n" . 'return parent::' . $methodName . '(' . implode(', ', $forwardedParams) . ');');
     $method->setDocblock('{@inheritDoc}');
     return $method;
 }
 private function generate($version)
 {
     $generator = new ClassGenerator();
     $docblock = DocBlockGenerator::fromArray(array('shortDescription' => 'PDO Simple Migration Class', 'longDescription' => 'Add your queries below'));
     $generator->setName('PDOSimpleMigration\\Migrations\\Migration' . $version)->setExtendedClass('AbstractMigration')->addUse('PDOSimpleMigration\\Library\\AbstractMigration')->setDocblock($docblock)->addProperties(array(array('description', 'Migration description', PropertyGenerator::FLAG_STATIC)))->addMethods(array(MethodGenerator::fromArray(array('name' => 'up', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate up', 'longDescription' => null)))), MethodGenerator::fromArray(array('name' => 'down', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate down', 'longDescription' => null))))));
     $file = FileGenerator::fromArray(array('classes' => array($generator)));
     return $file->generate();
 }
Пример #24
0
 /**
  * Generate a filter method
  */
 protected function addFilterMethod()
 {
     // set action body
     $body = ['// add filter code here', 'return $value;'];
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('filter');
     $method->setBody($body);
     $method->setParameters([new ParameterGenerator('value', 'mixed')]);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Called when filter is executed', null, [new ParamTag('value', ['mixed']), new ReturnTag(['mixed'])]));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
 /**
  * @param \Protobuf\Compiler\Entity              $entity
  * @param \google\protobuf\MethodDescriptorProto $method
  *
  * @return string
  */
 protected function generateMethod(Entity $entity, MethodDescriptorProto $method)
 {
     $inputClass = $this->getMethodInputTypeHint($method);
     $inputDoc = $this->getMethodInputDocblock($method);
     $outputDoc = $this->getMethodOutputDocblock($method);
     $methodName = $this->getCamelizedValue($method->getName());
     $method = MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [['name' => 'input', 'type' => $inputClass]], 'docblock' => ['tags' => [['name' => 'param', 'description' => $inputDoc . ' $input'], ['name' => 'return', 'description' => $outputDoc]]]]);
     return $method;
 }
 /**
  * Build generators
  *
  * @param  State|\Scaffold\State $state
  * @return \Scaffold\State|void
  */
 public function build(State $state)
 {
     $model = $this->model;
     $generator = new ClassGenerator($model->getName());
     $generator->addUse('Zend\\ServiceManager\\FactoryInterface');
     $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface');
     $generator->addUse('Zend\\ServiceManager\\ServiceManager');
     $generator->addUse($state->getServiceModel()->getName());
     $generator->setImplementedInterfaces(['FactoryInterface']);
     $method = new MethodGenerator('createService');
     $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface'));
     $method->setBody('return new ' . $state->getServiceModel()->getClassName() . '($serviceLocator);');
     $doc = new DocBlockGenerator('Create service');
     $doc->setTag(new Tag\GenericTag('param', 'ServiceLocatorInterface|ServiceManager $serviceLocator'));
     $doc->setTag(new Tag\GenericTag('return', $state->getServiceModel()->getClassName()));
     $method->setDocBlock($doc);
     $generator->addMethodFromGenerator($method);
     $model->setGenerator($generator);
 }
Пример #27
0
 /**
  * @param ContextInterface $context
  * @param ClassGenerator   $class
  * @param Property         $property
  *
  * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  */
 private function implementGetResult(ContextInterface $context, ClassGenerator $class, Property $property)
 {
     $useAssembler = new UseAssembler($this->wrapperClass ?: ResultInterface::class);
     if ($useAssembler->canAssemble($context)) {
         $useAssembler->assemble($context);
     }
     $methodName = 'getResult';
     $class->removeMethod($methodName);
     $class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => $this->generateGetResultBody($property), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => $this->generateGetResultReturnTag($property)]]])]));
 }
 /**
  * Generate the create service method
  *
  * @param string $className
  * @param string $moduleName
  * @param string $tableName
  */
 protected function addCreateServiceMethod($className, $moduleName, $tableName)
 {
     $managerName = 'serviceLocator';
     $tableGatewayName = ucfirst($tableName) . 'TableGateway';
     $tableGatewayService = $moduleName . '\\' . $this->config['namespaceTableGateway'] . '\\' . ucfirst($tableName);
     // set action body
     $body = ['/** @var ' . $tableGatewayName . ' $tableGateway */', '$tableGateway = $serviceLocator->get(\'' . $tableGatewayService . '\');', '', '$instance = new ' . $className . '($tableGateway);', '', 'return $instance;'];
     $body = implode(AbstractGenerator::LINE_FEED, $body);
     // create method
     $method = new MethodGenerator();
     $method->setName('createService');
     $method->setBody($body);
     $method->setParameters([new ParameterGenerator($managerName, 'ServiceLocatorInterface')]);
     // check for api docs
     if ($this->config['flagAddDocBlocks']) {
         $method->setDocBlock(new DocBlockGenerator('Create service', null, [new ParamTag($managerName, ['ServiceLocatorInterface']), new ReturnTag([$className])]));
     }
     // add method
     $this->addMethodFromGenerator($method);
 }
Пример #29
0
 /**
  * @param ContextInterface|PropertyContext $context
  *
  * @throws AssemblerException
  */
 public function assemble(ContextInterface $context)
 {
     $class = $context->getClass();
     $property = $context->getProperty();
     try {
         $methodName = Normalizer::generatePropertyMethod('get', $property->getName());
         $class->removeMethod($methodName);
         $class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => sprintf('return $this->%s;', $property->getName()), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => $property->getType()]]])]));
     } catch (\Exception $e) {
         throw AssemblerException::fromException($e);
     }
 }
Пример #30
0
 /**
  * @param Type $type
  *
  * @return MethodGenerator
  * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  */
 private function assembleConstructor(Type $type)
 {
     $body = [];
     $constructor = MethodGenerator::fromArray(['name' => '__construct', 'visibility' => MethodGenerator::VISIBILITY_PUBLIC]);
     $docblock = DocBlockGenerator::fromArray(['shortdescription' => 'Constructor']);
     foreach ($type->getProperties() as $property) {
         $body[] = sprintf('$this->%1$s = $%1$s;', $property->getName());
         $constructor->setParameter(['name' => $property->getName()]);
         $docblock->setTag(['name' => 'var', 'description' => sprintf('%s $%s', $property->getType(), $property->getName())]);
     }
     $constructor->setDocBlock($docblock);
     $constructor->setBody(implode($constructor::LINE_FEED, $body));
     return $constructor;
 }