/**
  * {@inheritdoc}
  */
 public function generate($className, GeneratorData $data, VisitorCollection $visitorCollection = null)
 {
     $this->visitorCollection = $visitorCollection ?: new VisitorCollection();
     $this->prepare($data, $this->visitorCollection);
     $this->validate($data);
     $class = PhpClass::create($className);
     $visitContext = new VisitContext($class);
     if ($data->getFilename()) {
         $writer = $visitContext->createWriter();
         $writer->writeln('/**')->writeln(' * Filename: ' . $data->getFilename())->writeln(' */');
         $class->setDocblock($writer->getContent());
     }
     $class->addInterfaceName('Oro\\Component\\Layout\\LayoutUpdateInterface');
     $method = PhpMethod::create(LayoutUpdateGeneratorInterface::UPDATE_METHOD_NAME);
     $manipulatorParameter = PhpParameter::create(LayoutUpdateGeneratorInterface::PARAM_LAYOUT_MANIPULATOR);
     $manipulatorParameter->setType('Oro\\Component\\Layout\\LayoutManipulatorInterface');
     $method->addParameter($manipulatorParameter);
     $layoutItemParameter = PhpParameter::create(LayoutUpdateGeneratorInterface::PARAM_LAYOUT_ITEM);
     $layoutItemParameter->setType('Oro\\Component\\Layout\\LayoutItemInterface');
     $method->addParameter($layoutItemParameter);
     /** @var VisitorInterface $condition */
     foreach ($this->visitorCollection as $condition) {
         $condition->startVisit($visitContext);
     }
     $writer = $visitContext->getUpdateMethodWriter();
     $writer->writeLn(trim($this->doGenerateBody($data)));
     /** @var VisitorInterface $condition */
     foreach ($this->visitorCollection as $condition) {
         $condition->endVisit($visitContext);
     }
     $method->setBody($writer->getContent());
     $class->setMethod($method);
     $strategy = new DefaultGeneratorStrategy();
     return "<?php\n\n" . $strategy->generate($class);
 }
 public function testGenerate()
 {
     $schema = ['relationData' => [['field_id' => new FieldConfigId('extend', 'Test\\Entity', ExtendHelper::buildAssociationName('Test\\TargetEntity1', ActivityScope::ASSOCIATION_KIND), 'manyToMany'), 'target_entity' => 'Test\\TargetEntity1'], ['field_id' => new FieldConfigId('extend', 'Test\\Entity', ExtendHelper::buildAssociationName('Test\\TargetEntity2', ActivityScope::ASSOCIATION_KIND), 'manyToMany'), 'target_entity' => 'Test\\TargetEntity2'], ['field_id' => new FieldConfigId('extend', 'Test\\Entity', ExtendHelper::buildAssociationName('Test\\TargetEntity3', ActivityScope::ASSOCIATION_KIND), 'manyToOne'), 'target_entity' => 'Test\\TargetEntity3'], ['field_id' => new FieldConfigId('extend', 'Test\\Entity', 'testField', 'manyToMany'), 'target_entity' => 'Test\\TargetEntity4']]];
     $class = PhpClass::create('Test\\Entity');
     $this->extension->generate($schema, $class);
     $strategy = new DefaultGeneratorStrategy();
     $classBody = $strategy->generate($class);
     $expectedBody = file_get_contents(__DIR__ . '/Fixtures/generationResult.txt');
     $this->assertEquals(trim($expectedBody), $classBody);
 }
 public function testVisitClassWithPhp7Features()
 {
     if (PHP_VERSION_ID < 70000) {
         $this->markTestSkipped('Test only valid for PHP >=7.0');
     }
     $ref = new \ReflectionClass('CG\\Tests\\Generator\\Fixture\\EntityPhp7');
     $class = PhpClass::fromReflection($ref);
     $generator = new DefaultGeneratorStrategy();
     $content = $generator->generate($class);
     $this->assertEquals($this->getContent('php7_class.php'), $content);
 }
 /**
  * @param string $expectedFile
  * @param array  $schema
  * @param bool   $dump
  */
 protected function assertGeneration($expectedFile, $schema, $dump = false)
 {
     $class = PhpClass::create('Test\\Entity');
     $this->extension->generate($schema, $class);
     $strategy = new DefaultGeneratorStrategy();
     $classBody = $strategy->generate($class);
     if ($dump) {
         print_r("\n" . $classBody . "\n");
     }
     $expectedBody = file_get_contents(__DIR__ . '/../Fixtures/' . $expectedFile);
     $this->assertEquals(trim($expectedBody), $classBody);
 }
 public function testGenerateChangedConstantOrder()
 {
     $strategy = new DefaultGeneratorStrategy();
     $strategy->setConstantSortFunc(function ($a, $b) {
         return -1 * strcasecmp($a, $b);
     });
     $strategy->setMethodSortFunc($func = function ($a, $b) {
         return strcasecmp($a->getName(), $b->getName());
     });
     $strategy->setPropertySortFunc($func);
     $this->assertEquals($this->getContent('GenerationTestClass_B.php'), $strategy->generate($this->getClass()));
 }
Beispiel #6
0
 /**
  * Generate php and yml files for schema
  *
  * @param array $schema
  */
 public function generateSchemaFiles(array $schema)
 {
     // generate PHP code
     $class = PhpClass::create($schema['entity']);
     foreach ($this->getExtensions() as $extension) {
         if ($extension->supports($schema)) {
             $extension->generate($schema, $class);
         }
     }
     $className = ExtendHelper::getShortClassName($schema['entity']);
     // write PHP class to the file
     $strategy = new DefaultGeneratorStrategy();
     file_put_contents($this->entityCacheDir . DIRECTORY_SEPARATOR . $className . '.php', "<?php\n\n" . $strategy->generate($class));
     // write doctrine metadata in separate yaml file
     file_put_contents($this->entityCacheDir . DIRECTORY_SEPARATOR . $className . '.orm.yml', Yaml::dump($schema['doctrine'], 5));
 }
    public function testVisit()
    {
        $conditionObject = new ElementDependentVisitor('header');
        $phpClass = PhpClass::create('LayoutUpdateClass');
        $visitContext = new VisitContext($phpClass);
        $conditionObject->startVisit($visitContext);
        $conditionObject->endVisit($visitContext);
        $strategy = new DefaultGeneratorStrategy();
        $this->assertSame(<<<CONTENT
class LayoutUpdateClass implements \\Oro\\Component\\Layout\\Loader\\Generator\\ElementDependentLayoutUpdateInterface
{
    public function getElement()
    {
        return 'header';
    }
}
CONTENT
, $strategy->generate($visitContext->getClass()));
    }
 /**
  * @param string $expectedFile
  * @param array  $schema
  * @param bool   $dump
  */
 protected function assertGeneration($expectedFile, $schema, $dump = false)
 {
     $class = PhpClass::create('Test\\Entity');
     $this->extension->generate($schema, $class);
     $strategy = new DefaultGeneratorStrategy();
     $classBody = $strategy->generate($class);
     if ($dump) {
         print_r("\n" . $classBody . "\n");
     }
     $expectedBody = file_get_contents(__DIR__ . '/../Fixtures/' . $expectedFile);
     /**
      * Support different line endings.
      */
     $expectedBody = str_replace(PHP_EOL, "\n", $expectedBody);
     $classBody = str_replace(PHP_EOL, "\n", $classBody);
     $expectedBody = trim($expectedBody);
     $classBody = trim($classBody);
     $this->assertEquals($expectedBody, $classBody);
 }
 /**
  * {@inheritdoc}
  */
 protected function doGenerate(ClassMetadata $metadata, array $data, array $options = array())
 {
     static $docBlock;
     if (empty($docBlock)) {
         $writer = new Writer();
         $writer->writeln('/**')->writeln(' * This code was generated automatically by the FixtureDumper library, manual changes to it')->writeln(' * may be lost upon next generation.')->writeln(' */');
         $docBlock = $writer->getContent();
     }
     $fixtureClass = new PhpClass($options['namespace'] . '\\' . $this->namingStrategy->fixtureName($metadata));
     $fixtureClass->setDocblock($docBlock);
     $fixtureClass->setParentClassName('Doctrine\\Common\\DataFixtures\\AbstractFixture');
     $fixtureClass->addUseStatement($metadata->getName());
     if (count($metadata->getAssociationNames()) !== 0) {
         $this->addDependentFixtureInterface($fixtureClass, $metadata, $options);
     }
     $this->generateLoadMethod($fixtureClass, $metadata, $data);
     $generator = new DefaultGeneratorStrategy();
     $content = $generator->generate($fixtureClass);
     return $content;
 }
    public function testVisit()
    {
        $condition = new ConfigExpressionConditionVisitor(new Condition\True());
        $phpClass = PhpClass::create('LayoutUpdateClass');
        $visitContext = new VisitContext($phpClass);
        $method = PhpMethod::create(LayoutUpdateGeneratorInterface::UPDATE_METHOD_NAME);
        $method->addParameter(PhpParameter::create(LayoutUpdateGeneratorInterface::PARAM_LAYOUT_MANIPULATOR));
        $method->addParameter(PhpParameter::create(LayoutUpdateGeneratorInterface::PARAM_LAYOUT_ITEM));
        $condition->startVisit($visitContext);
        $visitContext->getUpdateMethodWriter()->writeln('echo 123;');
        $condition->endVisit($visitContext);
        $method->setBody($visitContext->getUpdateMethodWriter()->getContent());
        $phpClass->setMethod($method);
        $strategy = new DefaultGeneratorStrategy();
        $this->assertSame(<<<CLASS
class LayoutUpdateClass implements \\Oro\\Component\\ConfigExpression\\ExpressionFactoryAwareInterface
{
    private \$expressionFactory;

    public function updateLayout(\$layoutManipulator, \$item)
    {
        if (null === \$this->expressionFactory) {
            throw new \\RuntimeException('Missing expression factory for layout update');
        }

        \$expr = \$this->expressionFactory->create('true', []);
        \$context = ['context' => \$item->getContext()];
        if (\$expr->evaluate(\$context)) {
            echo 123;
        }
    }

    public function setExpressionFactory(\\Oro\\Component\\ConfigExpression\\ExpressionFactoryInterface \$expressionFactory)
    {
        \$this->expressionFactory = \$expressionFactory;
    }
}
CLASS
, $strategy->generate($visitContext->getClass()));
    }
 protected function generateClass($item)
 {
     $this->writer = new Writer();
     $class = PhpClass::create($item['entity']);
     if ($item['type'] == 'Extend') {
         if (isset($item['inherit'])) {
             $class->setParentClassName($item['inherit']);
         }
     } else {
         $class->setProperty(PhpProperty::create('id')->setVisibility('protected'));
         $class->setMethod($this->generateClassMethod('getId', 'return $this->id;'));
         /**
          * TODO
          * custom entity instance as manyToOne relation
          * find the way to show it on view
          * we should mark some field as title
          */
         $toString = array();
         foreach ($item['property'] as $propKey => $propValue) {
             if ($item['doctrine'][$item['entity']]['fields'][$propKey]['type'] == 'string') {
                 $toString[] = '$this->get' . ucfirst(Inflector::camelize($propValue)) . '()';
             }
         }
         $toStringBody = 'return (string) $this->getId();';
         if (count($toString) > 0) {
             $toStringBody = 'return (string)' . implode(' . ', $toString) . ';';
         }
         $class->setMethod($this->generateClassMethod('__toString', $toStringBody));
     }
     $class->setInterfaceNames(array('Oro\\Bundle\\EntityExtendBundle\\Entity\\ExtendEntityInterface'));
     $this->generateClassMethods($item, $class);
     $classArray = explode('\\', $item['entity']);
     $className = array_pop($classArray);
     $filePath = $this->entityCacheDir . '/' . $className . '.php';
     $strategy = new DefaultGeneratorStrategy();
     file_put_contents($filePath, "<?php\n\n" . $strategy->generate($class));
 }