fromReflection() public static method

public static fromReflection ( Zend\Code\Reflection\MethodReflection $reflectionMethod ) : MethodGenerator
$reflectionMethod Zend\Code\Reflection\MethodReflection
return MethodGenerator
 /**
  * Build a Code Generation Php Object from a Class Reflection
  *
  * @param  ClassReflection $classReflection
  * @return InterfaceGenerator
  */
 public static function fromReflection(ClassReflection $classReflection)
 {
     if (!$classReflection->isInterface()) {
         throw new Exception\InvalidArgumentException(sprintf('Class %s is not a interface', $classReflection->getName()));
     }
     // class generator
     $cg = new static($classReflection->getName());
     $methods = [];
     $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());
     }
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $methods[] = MethodGenerator::fromReflection($reflectionMethod);
         }
     }
     foreach ($classReflection->getConstants() as $name => $value) {
         $cg->addConstant($name, $value);
     }
     $cg->addMethods($methods);
     return $cg;
 }
Beispiel #2
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;
 }
Beispiel #3
0
    public function testMethodFromReflection()
    {
        $ref = new MethodReflection('ZendTest\\Code\\Generator\\TestAsset\\TestSampleSingleClass', 'someMethod');
        $methodGenerator = MethodGenerator::fromReflection($ref);
        $target = <<<EOS
    /**
     * Enter description here...
     *
     * @return bool
     */
    public function someMethod()
    {
        /* test test */
    }

EOS;
        $this->assertEquals($target, (string) $methodGenerator);
    }
 /**
  * @return MethodGenerator
  */
 private function generateProxyMethod(\ReflectionMethod $method, $preSource, $postSource, $exceptionSource)
 {
     $methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName());
     $methodGenerator = MethodGenerator::fromReflection($methodReflection);
     $parametersString = '(';
     $i = count($method->getParameters());
     foreach ($method->getParameters() as $parameter) {
         $parametersString .= '$' . $parameter->getName() . (--$i > 0 ? ',' : '');
     }
     $parametersString .= ')';
     if ('' === $preSource && '' === $postSource && '' === $exceptionSource) {
         $body = 'return $this->proxy_realSubject->' . $method->getName() . $parametersString . ";\n";
     } else {
         $body = "try {\n" . $preSource . "\n" . "\$data = \$this->proxy_realSubject->" . $method->getName() . $parametersString . ";\n" . $postSource . "\n" . "return \$data;\n" . "} catch(\\Exception \$e){\n" . $exceptionSource . "\n" . "throw \$e;\n" . '};';
     }
     $methodGenerator->setBody($body);
     return $methodGenerator;
 }
 /**
  * {@inheritDoc}
  */
 public static function fromReflection(MethodReflection $reflectionMethod)
 {
     $method = parent::fromReflection($reflectionMethod);
     $method->setInterface(false);
     return $method;
 }
Beispiel #6
0
 /**
  * fromReflection() - build a Code Generation Php Object from a Class Reflection
  *
  * @param ReflectionClass $classReflection
  * @return ClassGenerator
  */
 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()));
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var $parentClass \Zend\Code\Reflection\ReflectionClass */
     if ($parentClass = $classReflection->getParentClass()) {
         $cg->setExtendedClass($parentClass->getName());
         $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces());
     } else {
         $interfaces = $classReflection->getInterfaces();
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var $interface \Zend\Code\Reflection\ReflectionClass */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         /* @var $reflectionProperty \PropertyReflection\Code\Reflection\ReflectionProperty */
         if ($reflectionProperty->getDeclaringClass()->getName() == $cg->getName()) {
             $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
         }
     }
     $cg->setProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         /* @var $reflectionMethod \MethodReflection\Code\Reflection\ReflectionMethod */
         if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getName()) {
             $methods[] = MethodGenerator::fromReflection($reflectionMethod);
         }
     }
     $cg->setMethods($methods);
     return $cg;
 }
 /**
  * Build a Code Generation Php Object from a Class Reflection
  *
  * @param  ClassReflection $classReflection
  * @return ClassGenerator
  */
 public static function fromReflection(ClassReflection $classReflection)
 {
     $cg = new static($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock()));
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var \Zend\Code\Reflection\ClassReflection $parentClass */
     $parentClass = $classReflection->getParentClass();
     $interfaces = $classReflection->getInterfaces();
     if ($parentClass) {
         $cg->setExtendedClass($parentClass->getName());
         $interfaces = array_diff($interfaces, $parentClass->getInterfaces());
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var \Zend\Code\Reflection\ClassReflection $interface */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $properties[] = PropertyGenerator::fromReflection($reflectionProperty);
         }
     }
     $cg->addProperties($properties);
     $constants = array();
     foreach ($classReflection->getConstants() as $name => $value) {
         $constants[] = array('name' => $name, 'value' => $value);
     }
     $cg->addConstants($constants);
     $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;
 }
 /**
  * Create a new subclass and factory to override a factory-generated
  * service.
  *
  * @param mixed  $factory Factory configuration for class to extend
  * @param string $module  Module in which to create the new factory
  *
  * @return string
  * @throws \Exception
  */
 protected function cloneFactory($factory, $module)
 {
     // Make sure we can figure out how to handle the factory; it should
     // either be a [controller, method] array or a "controller::method"
     // string; anything else will cause a problem.
     $parts = is_string($factory) ? explode('::', $factory) : $factory;
     if (!is_array($parts) || count($parts) != 2 || !class_exists($parts[0]) || !method_exists($parts[0], $parts[1])) {
         throw new \Exception('Unexpected factory configuration format.');
     }
     list($factoryClass, $factoryMethod) = $parts;
     $newFactoryClass = $this->generateLocalClassName($factoryClass, $module);
     if (!class_exists($newFactoryClass)) {
         $this->createClassInModule($newFactoryClass, $module);
         $skipBackup = true;
     } else {
         $skipBackup = false;
     }
     if (method_exists($newFactoryClass, $factoryMethod)) {
         throw new \Exception("{$newFactoryClass}::{$factoryMethod} already exists.");
     }
     $oldReflection = new ClassReflection($factoryClass);
     $newReflection = new ClassReflection($newFactoryClass);
     $generator = ClassGenerator::fromReflection($newReflection);
     $method = MethodGenerator::fromReflection($oldReflection->getMethod($factoryMethod));
     $this->createServiceClassAndUpdateFactory($method, $oldReflection->getNamespaceName(), $module);
     $generator->addMethodFromGenerator($method);
     $this->writeClass($generator, $module, true, $skipBackup);
     return $newFactoryClass . '::' . $factoryMethod;
 }
Beispiel #9
0
 /**
  * Copied from ClassGenerator::fromReflection and tweaked slightly
  * @param ClassReflection $classReflection
  *
  * @return ClassGenerator
  */
 public function getGeneratorFromReflection(ClassReflection $classReflection)
 {
     // class generator
     $cg = new ClassGenerator($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock());
         $docblock->setIndentation(Generator::$indentation);
         $cg->setDocBlock($docblock);
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var \Zend\Code\Reflection\ClassReflection $parentClass */
     $parentClass = $classReflection->getParentClass();
     if ($parentClass) {
         $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\'));
         $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces());
     } else {
         $interfaces = $classReflection->getInterfaces();
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var \Zend\Code\Reflection\ClassReflection $interface */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $property = PropertyGenerator::fromReflection($reflectionProperty);
             $property->setIndentation(Generator::$indentation);
             $properties[] = $property;
         }
     }
     $cg->addProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $method = MethodGenerator::fromReflection($reflectionMethod);
             $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody()));
             $method->setIndentation(Generator::$indentation);
             $methods[] = $method;
         }
     }
     $cg->addMethods($methods);
     return $cg;
 }