/**
  * {@inheritDoc}
  */
 public function setUp()
 {
     $this->generatedClassName = UniqueIdentifierGenerator::getIdentifier('foo');
     $this->config = new Configuration($this->generatedClassName);
     $generatorStrategy = new EvaluatingGeneratorStrategy();
     $reflection = new ReflectionClass('GeneratedHydratorTestAsset\\ClassWithMixedProperties');
     $generator = new ClassBuilder();
     $traverser = new NodeTraverser();
     $renamer = new ClassRenamerVisitor($reflection, $this->generatedClassName);
     $traverser->addVisitor($renamer);
     // evaluating the generated class
     //die(var_dump($traverser->traverse($generator->fromReflection($reflection))));
     $ast = $traverser->traverse($generator->fromReflection($reflection));
     $generatorStrategy->generate($ast);
     $this->config->setGeneratorStrategy($generatorStrategy);
 }
 /**
  * Generates an AST of {@see \PhpParser\Node[]} out of a given reflection class
  * and a map of properties to be used to
  *
  * @param \ReflectionClass $originalClass
  *
  * @return \PhpParser\Node[]
  */
 public function generate(ReflectionClass $originalClass)
 {
     $builder = new ClassBuilder();
     $ast = $builder->fromReflection($originalClass);
     // step 1: remove methods that are not used
     $cleaner = new NodeTraverser();
     $cleaner->addVisitor(new MethodDisablerVisitor(function () {
         return false;
     }));
     $ast = $cleaner->traverse($ast);
     // step 2: implement new methods and interfaces, extend original class
     $implementor = new NodeTraverser();
     $implementor->addVisitor(new HydratorMethodsVisitor($originalClass));
     $implementor->addVisitor(new ClassExtensionVisitor($originalClass->getName(), $originalClass->getName()));
     $implementor->addVisitor(new ClassImplementorVisitor($originalClass->getName(), array('Zend\\Stdlib\\Hydrator\\HydratorInterface')));
     return $implementor->traverse($ast);
 }
Exemplo n.º 3
0
 /**
  * Generates an AST out of a given reflection class and a target hydrator name
  *
  * @param string $realClass
  * @param string $hydratorClass
  *
  * @return \PhpParser\Node[]
  */
 protected function generateAst($realClass, $hydratorClass)
 {
     $originalClass = new \ReflectionClass($realClass);
     $builder = new ClassBuilder();
     $traverser = new NodeTraverser();
     $ast = $builder->fromReflection($originalClass);
     // Remove unused methods
     $traverser->addVisitor(new MethodDisablerVisitor(function () {
         return false;
     }));
     // Implement new methods and interfaces, extend original class
     $this->addMethodVisitors($originalClass, $traverser);
     $traverser->addVisitor(new ClassExtensionVisitor($realClass, $realClass));
     $traverser->addVisitor(new ClassImplementorVisitor($realClass, ['Indigo\\Hydra\\Hydrator']));
     // Renames class
     $traverser->addVisitor(new ClassRenamerVisitor($originalClass, $hydratorClass));
     return $traverser->traverse($ast);
 }