示例#1
0
    public function testFromReflection()
    {
        if (PHP_VERSION_ID < 70000) {
            $this->markTestSkipped("Test is only valid for PHP >=7");
        }
        $class = new PhpClass();
        $class->setName('CG\\Tests\\Generator\\Fixture\\EntityPhp7')->setDocblock('/**
 * Doc Comment.
 *
 * @author Johannes M. Schmitt <*****@*****.**>
 */')->setProperty(PhpProperty::create('id')->setVisibility('private')->setDefaultValue(0)->setDocblock('/**
 * @var integer
 */'));
        $class->setMethod(PhpMethod::create()->setName('getId')->setDocblock('/**
 * @return int
 */')->setVisibility('public')->setReturnType('int'));
        $class->setMethod(PhpMethod::create()->setName('setId')->setVisibility('public')->setDocBlock('/**
 * @param int $id
 * @return EntityPhp7
 */')->addParameter(PhpParameter::create()->setName('id')->setType('int')->setDefaultValue(null))->setReturnType('self'));
        $class->setMethod(PhpMethod::create()->setName('getTime')->setVisibility('public')->setReturnType('DateTime'));
        $class->setMethod(PhpMethod::create()->setName('getTimeZone')->setVisibility('public')->setReturnType('DateTimeZone'));
        $class->setMethod(PhpMethod::create()->setName('setTime')->setVisibility('public')->addParameter(PhpParameter::create()->setName('time')->setType('DateTime')));
        $class->setMethod(PhpMethod::create()->setName('setTimeZone')->setVisibility('public')->addParameter(PhpParameter::create()->setName('timezone')->setType('DateTimeZone')));
        $class->setMethod(PhpMethod::create()->setName('setArray')->setVisibility('public')->setReturnType('array')->addParameter(PhpParameter::create()->setName('array')->setDefaultValue(null)->setPassedByReference(true)->setType('array')));
        $class->setMethod(PhpMethod::create()->setName('getFoo')->setReturnType('CG\\Tests\\Generator\\Fixture\\SubFixture\\Foo'));
        $class->setMethod(PhpMethod::create()->setName('getBar')->setReturnType('CG\\Tests\\Generator\\Fixture\\SubFixture\\Bar'));
        $class->setMethod(PhpMethod::create()->setName('getBaz')->setReturnType('CG\\Tests\\Generator\\Fixture\\SubFixture\\Baz'));
        $this->assertEquals($class, PhpClass::fromReflection(new \ReflectionClass('CG\\Tests\\Generator\\Fixture\\EntityPhp7')));
    }
 public function generate(\ReflectionClass $originalClass, PhpClass $genClass)
 {
     $methods = ReflectionUtils::getOverrideableMethods($originalClass);
     if (null !== $this->filter) {
         $methods = array_filter($methods, $this->filter);
     }
     if (empty($methods)) {
         return;
     }
     if (!empty($this->requiredFile)) {
         $genClass->addRequiredFile($this->requiredFile);
     }
     $interceptorLoader = new PhpProperty();
     $interceptorLoader->setName($this->prefix . 'loader')->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
     $genClass->setProperty($interceptorLoader);
     $loaderSetter = new PhpMethod();
     $loaderSetter->setName($this->prefix . 'setLoader')->setVisibility(PhpMethod::VISIBILITY_PUBLIC)->setBody('$this->' . $this->prefix . 'loader = $loader;');
     $genClass->setMethod($loaderSetter);
     $loaderParam = new PhpParameter();
     $loaderParam->setName('loader')->setType('CG\\Proxy\\InterceptorLoaderInterface');
     $loaderSetter->addParameter($loaderParam);
     $interceptorCode = '$ref = new \\ReflectionMethod(%s, %s);' . "\n" . '$interceptors = $this->' . $this->prefix . 'loader->loadInterceptors($ref, $this, array(%s));' . "\n" . '$invocation = new \\CG\\Proxy\\MethodInvocation($ref, $this, array(%s), $interceptors);' . "\n\n" . 'return $invocation->proceed();';
     foreach ($methods as $method) {
         $params = array();
         foreach ($method->getParameters() as $param) {
             $params[] = '$' . $param->name;
         }
         $params = implode(', ', $params);
         $genMethod = PhpMethod::fromReflection($method)->setBody(sprintf($interceptorCode, var_export(ClassUtils::getUserClass($method->class), true), var_export($method->name, true), $params, $params))->setDocblock(null);
         $genClass->setMethod($genMethod);
     }
 }
 public function generate(\ReflectionClass $original, PhpClass $proxy)
 {
     $writer = new Writer();
     // copy over all public methods
     foreach ($original->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         if ($method->isStatic()) {
             continue;
         }
         $writer->reset()->write('return $this->delegate->')->write($method->name)->write('(');
         $first = true;
         foreach ($method->getParameters() as $param) {
             if (!$first) {
                 $writer->write(', ');
             }
             $first = false;
             $writer->write('$')->write($param->name);
         }
         $writer->write(');');
         $proxyMethod = PhpMethod::fromReflection($method)->setBody($writer->getContent());
         $proxy->setMethod($proxyMethod);
     }
     $proxy->setProperty(PhpProperty::create('delegate')->setVisibility('private'));
     $proxy->setProperty(PhpProperty::create('container')->setVisibility('private'));
     $proxy->setMethod(PhpMethod::create('__construct')->setVisibility('public')->addParameter(PhpParameter::create('objectManager')->setType('Doctrine\\Common\\Persistence\\ObjectManager'))->addParameter(PhpParameter::create('container')->setType('Symfony\\Component\\DependencyInjection\\ContainerInterface'))->setBody($writer->reset()->writeln('$this->delegate = $objectManager;')->writeln('$this->container = $container;')->getContent()));
     $proxy->setMethod(PhpMethod::fromReflection($original->getMethod('getRepository'))->setParameters(array(PhpParameter::create('className')))->setBody($writer->reset()->writeln('$repository = $this->delegate->getRepository($className);' . "\n")->writeln('if (null !== $metadata = $this->container->get("jms_di_extra.metadata.metadata_factory")->getMetadataForClass(get_class($repository))) {')->indent()->writeln('foreach ($metadata->classMetadata as $classMetadata) {')->indent()->writeln('foreach ($classMetadata->methodCalls as $call) {')->indent()->writeln('list($method, $arguments) = $call;')->writeln('call_user_func_array(array($repository, $method), $this->prepareArguments($arguments));')->outdent()->writeln('}')->outdent()->writeln('}')->outdent()->writeln('}' . "\n")->writeln('return $repository;')->getContent()));
     $proxy->setMethod(PhpMethod::create('prepareArguments')->setVisibility('private')->addParameter(PhpParameter::create('arguments')->setType('array'))->setBody($writer->reset()->writeln('$processed = array();')->writeln('foreach ($arguments as $arg) {')->indent()->writeln('if ($arg instanceof \\Symfony\\Component\\DependencyInjection\\Reference) {')->indent()->writeln('$processed[] = $this->container->get((string) $arg, $arg->getInvalidBehavior());')->outdent()->writeln('} else if ($arg instanceof \\Symfony\\Component\\DependencyInjection\\Parameter) {')->indent()->writeln('$processed[] = $this->container->getParameter((string) $arg);')->outdent()->writeln('} else {')->indent()->writeln('$processed[] = $arg;')->outdent()->writeln('}')->outdent()->writeln('}' . "\n")->writeln('return $processed;')->getContent()));
 }
 public function generate(\ReflectionClass $class, PhpClass $genClass)
 {
     if (!empty($this->requiredFile)) {
         $genClass->addRequiredFile($this->requiredFile);
     }
     $genClass->setProperty(PhpProperty::create()->setName(self::PREFIX . 'container')->setVisibility('private'));
     $genClass->setMethod(PhpMethod::create()->setName(self::PREFIX . 'setContainer')->addParameter(PhpParameter::create()->setName('container')->setType('Symfony\\Component\\DependencyInjection\\ContainerInterface'))->setBody('$this->' . self::PREFIX . 'container = $container;'));
     $genClass->addInterfaceName('JMS\\DiExtraBundle\\DependencyInjection\\LookupMethodClassInterface');
     $genClass->setMethod(PhpMethod::create()->setName(self::PREFIX . 'getOriginalClassName')->setFinal(true)->setBody('return ' . var_export($class->name, true) . ';'));
     foreach ($this->getLookupMethods() as $name => $value) {
         $genClass->setMethod(PhpMethod::fromReflection($class->getMethod($name))->setAbstract(false)->setBody('return ' . $this->dumpValue($value) . ';')->setDocblock(null));
     }
 }
示例#5
0
    public function testFromReflection()
    {
        $class = new PhpClass();
        $class->setName('CG\\Tests\\Generator\\Fixture\\Entity')->setAbstract(true)->setDocblock('/**
 * Doc Comment.
 *
 * @author Johannes M. Schmitt <*****@*****.**>
 */')->setProperty(PhpProperty::create('id')->setVisibility('private')->setDocblock('/**
 * @var integer
 */'))->setProperty(PhpProperty::create('enabled')->setVisibility('private')->setDefaultValue(false));
        $method = PhpMethod::create()->setName('__construct')->setFinal(true)->addParameter(new PhpParameter('a'))->addParameter(PhpParameter::create()->setName('b')->setType('array')->setPassedByReference(true))->addParameter(PhpParameter::create()->setName('c')->setType('stdClass'))->addParameter(PhpParameter::create()->setName('d')->setDefaultValue('foo'))->setDocblock('/**
 * Another doc comment.
 *
 * @param unknown_type $a
 * @param array        $b
 * @param \\stdClass    $c
 * @param string       $d
 */');
        $class->setMethod($method);
        $class->setMethod(PhpMethod::create()->setName('foo')->setAbstract(true)->setVisibility('protected'));
        $class->setMethod(PhpMethod::create()->setName('bar')->setStatic(true)->setVisibility('private'));
        $this->assertEquals($class, PhpClass::fromReflection(new \ReflectionClass('CG\\Tests\\Generator\\Fixture\\Entity')));
    }
示例#6
0
 /**
  * Add the required properties and setters for the aspects used by the generated class
  *
  * @param \ReflectionClass $originalClass
  * @param \CG\Generator\PhpClass $generatedClass
  * @return \CG\Generator\PhpClass
  */
 protected function prepareAspectInjection(\ReflectionClass $originalClass, PhpClass $generatedClass)
 {
     $aspects = $this->getRequiredAspects();
     $generatedClass->setMethod($this->generateMethod($originalClass->getConstructor(), $aspects));
     $generator = new AspectCodeGenerator($originalClass->name);
     foreach ($aspects as $aspect) {
         $generatedClass->setProperty($this->generateAspectProperty($aspect, $generator));
         $generatedClass->setMethod($this->generateAspectSetter($aspect, $generator));
     }
     return $generatedClass;
 }
 /**
  * @param array    $schema
  * @param PhpClass $class
  */
 protected function generateCollectionMethods(array $schema, PhpClass $class)
 {
     foreach ($schema['addremove'] as $fieldName => $config) {
         $addMethodBody = ['if (!$this->' . $fieldName . '->contains($value)) {', '    $this->' . $fieldName . '->add($value);'];
         $removeMethodBody = ['if ($this->' . $fieldName . ' && $this->' . $fieldName . '->contains($value)) {', '    $this->' . $fieldName . '->removeElement($value);'];
         if (isset($config['target'])) {
             if ($config['is_target_addremove']) {
                 $addMethodBody[] = "    \$value->{$this->generateAddMethodName($config['target'])}(\$this);";
                 $removeMethodBody[] = "    \$value->{$this->generateRemoveMethodName($config['target'])}(\$this);";
             } else {
                 $addMethodBody[] = "    \$value->{$this->generateSetMethodName($config['target'])}(\$this);";
                 $removeMethodBody[] = "    \$value->{$this->generateSetMethodName($config['target'])}(null);";
             }
         }
         $addMethodBody[] = '}';
         $removeMethodBody[] = '}';
         $class->setMethod($this->generateClassMethod($this->generateAddMethodName($config['self']), implode("\n", $addMethodBody), ['value']))->setMethod($this->generateClassMethod($this->generateRemoveMethodName($config['self']), implode("\n", $removeMethodBody), ['value']));
     }
 }
 /**
  * @param array    $schema
  * @param PhpClass $class
  *
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function generateManyToManyAssociationMethods(array $schema, PhpClass $class)
 {
     $prefix = $this->getAssociationKind();
     $prefix = Inflector::classify(null === $prefix ? '' : $prefix);
     $supportMethodName = sprintf('support%sTarget', $prefix);
     $getMethodName = sprintf('get%sTargets', $prefix);
     $hasMethodName = sprintf('has%sTarget', $prefix);
     $addMethodName = sprintf('add%sTarget', $prefix);
     $removeMethodName = sprintf('remove%sTarget', $prefix);
     $supportMethodBody = ['$className = \\Doctrine\\Common\\Util\\ClassUtils::getRealClass($targetClass);'];
     $getMethodBody = ['$className = \\Doctrine\\Common\\Util\\ClassUtils::getRealClass($targetClass);'];
     $hasMethodBody = ['$className = \\Doctrine\\Common\\Util\\ClassUtils::getClass($target);'];
     $addMethodBody = ['$className = \\Doctrine\\Common\\Util\\ClassUtils::getClass($target);'];
     $removeMethodBody = ['$className = \\Doctrine\\Common\\Util\\ClassUtils::getClass($target);'];
     foreach ($schema['relationData'] as $relationData) {
         if (!$this->isSupportedRelation($relationData)) {
             continue;
         }
         /** @var FieldConfigId $fieldConfigId */
         $fieldConfigId = $relationData['field_id'];
         $fieldName = $fieldConfigId->getFieldName();
         $targetClassName = $relationData['target_entity'];
         $supportMethodBody[] = sprintf('if ($className === \'%s\') { return true; }', $targetClassName);
         $getMethodBody[] = sprintf('if ($className === \'%s\') { return $this->%s; }', $targetClassName, $fieldName);
         $hasMethodBody[] = str_replace(['{class}', '{field}'], [$targetClassName, $fieldName], "if (\$className === '{class}') { return \$this->{field}->contains(\$target); }");
         $addMethodBody[] = str_replace(['{class}', '{field}'], [$targetClassName, $fieldName], "if (\$className === '{class}') {\n" . "    if (!\$this->{field}->contains(\$target)) { \$this->{field}->add(\$target); }\n" . "    return \$this;\n}");
         $removeMethodBody[] = str_replace(['{class}', '{field}'], [$targetClassName, $fieldName], "if (\$className === '{class}') {\n" . "    if (\$this->{field}->contains(\$target)) { \$this->{field}->removeElement(\$target); }\n" . "    return \$this;\n}");
     }
     $throwStmt = 'throw new \\RuntimeException(' . 'sprintf(\'The association with "%s" entity was not configured.\', $className));';
     $supportMethodBody[] = 'return false;';
     $getMethodBody[] = $throwStmt;
     $hasMethodBody[] = 'return false;';
     $addMethodBody[] = $throwStmt;
     $removeMethodBody[] = $throwStmt;
     $supportMethodDocblock = "/**\n" . " * Checks if an entity of the given type can be associated with this entity\n" . " *\n" . " * @param string \$targetClass The class name of the target entity\n" . " * @return bool\n" . " */";
     $getMethodDocblock = "/**\n" . " * Gets entities of the given type associated with this entity\n" . " *\n" . " * @param string \$targetClass The class name of the target entity\n" . " * @return object[]\n" . " */";
     $hasMethodDocblock = "/**\n" . " * Checks is the given entity is associated with this entity\n" . " *\n" . " * @param object \$target Any configurable entity that can be associated with this type of entity\n" . " * @return bool\n" . " */";
     $addMethodDocblock = "/**\n" . " * Associates the given entity with this entity\n" . " *\n" . " * @param object \$target Any configurable entity that can be associated with this type of entity\n" . " * @return object This object\n" . " */";
     $removeMethodDocblock = "/**\n" . " * Removes the association of the given entity and this entity\n" . " *\n" . " * @param object \$target Any configurable entity that can be associated with this type of entity\n" . " * @return object This object\n" . " */";
     $class->setMethod($this->generateClassMethod($supportMethodName, implode("\n", $supportMethodBody), ['targetClass'])->setDocblock($supportMethodDocblock))->setMethod($this->generateClassMethod($getMethodName, implode("\n", $getMethodBody), ['targetClass'])->setDocblock($getMethodDocblock))->setMethod($this->generateClassMethod($hasMethodName, implode("\n", $hasMethodBody), ['target'])->setDocblock($hasMethodDocblock))->setMethod($this->generateClassMethod($addMethodName, implode("\n", $addMethodBody), ['target'])->setDocblock($addMethodDocblock))->setMethod($this->generateClassMethod($removeMethodName, implode("\n", $removeMethodBody), ['target'])->setDocblock($removeMethodDocblock));
 }
 protected function addDependentFixtureInterface(PhpClass $class, ClassMetadata $metadata, array $options)
 {
     $class->addInterfaceName('Doctrine\\Common\\DataFixtures\\DependentFixtureInterface');
     $writer = new Writer();
     $method = PhpMethod::create('getDependencies');
     $writer->writeln("return array(");
     $associations = array();
     foreach ($metadata->getAssociationNames() as $assocName) {
         $targetClass = $metadata->getAssociationTargetClass($assocName);
         $associations[] = sprintf("'%s\\%s'", $options['namespace'], $this->namingStrategy->fixtureName($this->getManager()->getClassMetadata($targetClass)));
     }
     $writer->indent();
     $writer->writeln(implode(",\n", $associations));
     $writer->outdent();
     $writer->writeln(");");
     $method->setBody($writer->getContent());
     $class->setMethod($method);
 }
 private function addMethods(PhpClass $class, array $methods)
 {
     foreach ($methods as $method) {
         $initializingCode = 'if (false === $this->' . $this->prefix . 'initialized) {' . "\n" . '    $this->' . $this->prefix . 'initialize();' . "\n" . '}';
         if ($class->hasMethod($method->name)) {
             $genMethod = $class->getMethod($method->name);
             $genMethod->setBody($initializingCode . "\n" . $genMethod->getBody());
             continue;
         }
         $genMethod = PhpMethod::fromReflection($method);
         $genMethod->setBody($initializingCode . "\n\n" . 'return ' . GeneratorUtils::callMethod($method) . ';');
         $class->setMethod($genMethod);
     }
 }
 /**
  * @param array    $schema
  * @param PhpClass $class
  */
 protected function generateCollectionMethods(array $schema, PhpClass $class)
 {
     foreach ($schema['addremove'] as $fieldName => $config) {
         $addMethodBody = ['if (!$this->' . $fieldName . '->contains($value)) {', '    $this->' . $fieldName . '->add($value);'];
         $removeMethodBody = ['if ($this->' . $fieldName . ' && $this->' . $fieldName . '->contains($value)) {', '    $this->' . $fieldName . '->removeElement($value);'];
         if (isset($config['target'])) {
             $addMethodBody[] = '    $value->' . ($config['is_target_addremove'] ? 'add' : 'set') . ucfirst(Inflector::camelize($config['target'])) . '($this);';
             $removeMethodBody[] = '    $value->' . ($config['is_target_addremove'] ? 'remove' : 'set') . ucfirst(Inflector::camelize($config['target'])) . '(' . ($config['is_target_addremove'] ? '$this' : 'null') . ');';
         }
         $addMethodBody[] = '}';
         $removeMethodBody[] = '}';
         $class->setMethod($this->generateClassMethod('add' . ucfirst(Inflector::camelize($config['self'])), implode("\n", $addMethodBody), ['value']))->setMethod($this->generateClassMethod('remove' . ucfirst(Inflector::camelize($config['self'])), implode("\n", $removeMethodBody), ['value']));
     }
 }
示例#12
0
 /**
  * Generate aop methods proxies
  *
  * @param PhpClass $aopClass
  * @param PhpClass $parentClass
  */
 private function generateAopMethodProxy(PhpClass $aopClass, PhpClass $parentClass)
 {
     foreach ($parentClass->getMethods() as $method) {
         /* @var $method \CG\Generator\PhpMethod */
         try {
             $aopClass->getMethod($method->getName());
         } catch (\InvalidArgumentException $e) {
             try {
                 $proxyMethod = new ProxyMethod($method);
                 $aopClass->setMethod($proxyMethod);
             } catch (ProxyMethodException $e) {
             }
         }
     }
     if ($parentClass->getParentClassName() != null) {
         $this->generateAopMethodProxy($aopClass, PhpClass::fromReflection(new \ReflectionClass($parentClass->getParentClassName())));
     }
 }