コード例 #1
0
 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()));
 }
コード例 #2
0
 /**
  * Generates the necessary methods in the class.
  *
  * @param  \ReflectionClass $originalClass
  * @param  PhpClass         $class
  * @return void
  */
 public function generate(\ReflectionClass $originalClass, PhpClass $class)
 {
     $methods = ReflectionUtils::getOverrideableMethods($originalClass, true);
     // no public, non final methods
     if (empty($methods)) {
         return;
     }
     if (null !== $this->markerInterface) {
         $class->setImplementedInterfaces(array_merge($class->getImplementedInterfaces(), array($this->markerInterface)));
     }
     $initializer = new PhpProperty();
     $initializer->setName($this->prefix . 'lazyInitializer');
     $initializer->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
     $class->setProperty($initializer);
     $initialized = new PhpProperty();
     $initialized->setName($this->prefix . 'initialized');
     $initialized->setDefaultValue(false);
     $initialized->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
     $class->setProperty($initialized);
     $initializerSetter = new PhpMethod();
     $initializerSetter->setName($this->prefix . 'setLazyInitializer');
     $initializerSetter->setBody('$this->' . $this->prefix . 'lazyInitializer = $initializer;');
     $parameter = new PhpParameter();
     $parameter->setName('initializer');
     $parameter->setType('\\CG\\Proxy\\LazyInitializerInterface');
     $initializerSetter->addParameter($parameter);
     $class->setMethod($initializerSetter);
     $this->addMethods($class, $methods);
     $initializingMethod = new PhpMethod();
     $initializingMethod->setName($this->prefix . 'initialize');
     $initializingMethod->setVisibility(PhpMethod::VISIBILITY_PRIVATE);
     $initializingMethod->setBody($this->writer->reset()->writeln('if (null === $this->' . $this->prefix . 'lazyInitializer) {')->indent()->writeln('throw new \\RuntimeException("' . $this->prefix . 'setLazyInitializer() must be called prior to any other public method on this object.");')->outdent()->write("}\n\n")->writeln('$this->' . $this->prefix . 'lazyInitializer->initializeObject($this);')->writeln('$this->' . $this->prefix . 'initialized = true;')->getContent());
     $class->setMethod($initializingMethod);
 }
コード例 #3
0
 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);
     }
 }
コード例 #4
0
 public function generate(\ReflectionClass $original, PhpClass $proxy)
 {
     if (empty($this->overrides)) {
         throw new \RuntimeException('No overriding methods defined');
     }
     $writer = new Writer();
     $proxy->setProperty(PhpProperty::create('__MessagingAdapter')->setVisibility(PhpProperty::VISIBILITY_PRIVATE))->setMethod(PhpMethod::create('__setMessagingAdapter')->addParameter(PhpParameter::create('adapter')->setType('Nfx\\AsyncBundle\\Adapter\\AdapterInterface'))->setBody($writer->reset()->writeln('$this->__MessagingAdapter = $adapter;')->getContent()))->setProperty(PhpProperty::create('__defaultTransform')->setVisibility(PhpProperty::VISIBILITY_PRIVATE))->setMethod(PhpMethod::create('__setDefaultTransform')->addParameter(PhpParameter::create('transform')->setType('Nfx\\AsyncBundle\\Message\\Transform\\TransformInterface'))->setBody($writer->reset()->writeln('$this->__defaultTransform = $transform;')->getContent()));
     foreach ($this->overrides as $closure) {
         $closure($proxy);
     }
 }
コード例 #5
0
 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));
     }
 }
コード例 #6
0
 /**
  * @param PhpClass $class
  *
  * @return string
  */
 public function generate(PhpClass $class)
 {
     $dispatcherContainer = new PhpProperty('dispatcher');
     $dispatcherContainer->setVisibility('protected');
     $class->setProperty($dispatcherContainer);
     try {
         $method = $class->getMethod('setDispatcher');
         $method->setBody('$this->dispatcher=$dispatcher;');
     } catch (\Exception $e) {
     }
     $this->generateAopMethodProxy($class, PhpClass::fromReflection(new \ReflectionClass($class->getParentClassName())));
     $this->visitor->reset();
     $this->navigator->accept($this->visitor, $class);
     return $this->visitor->getContent();
 }
コード例 #7
0
ファイル: AspectGenerator.php プロジェクト: pugx/aop
 /**
  * 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;
 }
コード例 #8
0
 /**
  * @param string   $propertyType
  * @param array    $schema
  * @param PhpClass $class
  */
 protected function generateProperties($propertyType, array $schema, PhpClass $class)
 {
     foreach ($schema[$propertyType] as $fieldName => $config) {
         $class->setProperty(PhpProperty::create($fieldName)->setVisibility('protected'));
         $isPrivate = is_array($config) && isset($config['private']) && $config['private'];
         if (!$isPrivate) {
             $class->setMethod($this->generateClassMethod($this->generateGetMethodName($fieldName), 'return $this->' . $fieldName . ';'))->setMethod($this->generateClassMethod($this->generateSetMethodName($fieldName), $this->getSetterBody($fieldName, $schema), ['value']));
         }
     }
 }
コード例 #9
0
 /**
  * @param string   $propertyType
  * @param array    $schema
  * @param PhpClass $class
  */
 protected function generateProperties($propertyType, array $schema, PhpClass $class)
 {
     foreach ($schema[$propertyType] as $fieldName => $config) {
         $class->setProperty(PhpProperty::create($fieldName)->setVisibility('protected'))->setMethod($this->generateClassMethod('get' . ucfirst(Inflector::camelize($fieldName)), 'return $this->' . $fieldName . ';'))->setMethod($this->generateClassMethod('set' . ucfirst(Inflector::camelize($fieldName)), '$this->' . $fieldName . ' = $value; return $this;', ['value']));
     }
 }
コード例 #10
0
 private function tryParsingProperties(\SimpleXMLElement $doc, PhpClass $class)
 {
     foreach ($doc->xpath('//search:fieldsynopsis') as $fieldElem) {
         $type = (string) $fieldElem->type;
         $name = (string) $fieldElem->varname;
         $modifiers = array();
         foreach ($fieldElem->modifier as $modifierElem) {
             $modifiers[] = (string) $modifierElem;
         }
         if (in_array('const', $modifiers, true)) {
             if (false !== strpos($name, '::')) {
                 list(, $name) = explode('::', $name);
             }
             $constant = new PhpConstant($name);
             $class->setConstant($constant);
             $constant->setAttribute('type', $type);
             $initializer = $fieldElem->initializer;
             settype($initializer, $type);
             $constant->setValue($initializer);
         } else {
             $visibility = array_intersect(array('public', 'protected', 'private'), $modifiers);
             if (!$visibility) {
                 $visibility = array('public');
             }
             $property = new \CG\Generator\PhpProperty($name);
             $property->setVisibility(reset($visibility));
             if ('' !== $type) {
                 $property->setAttribute('type', $type);
             }
             $class->setProperty($property);
             $this->typeRefiner->refinePropertyType($class, $property);
         }
     }
 }