コード例 #1
0
 /**
  * {@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);
 }
コード例 #2
0
 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);
 }
コード例 #3
0
 public function testOverridingOfTwoArgumentsBehavior()
 {
     $gen = new BackgroundGenerator();
     $proxy = PhpClass::create('BlahBlan' . sha1(microtime(true)));
     $refl = new \ReflectionClass('Nfx\\AsyncBundle\\Generator\\BackgroundGenerator');
     $method = $refl->getMethod('override');
     $origCodeIn = $gen->override($method, new Background(array('value' => 'foobar')));
     $gen->generate($refl, $proxy);
     $this->assertTrue($proxy->hasMethod('override'));
     $this->assertTrue($proxy->hasMethod($origCodeIn));
 }
コード例 #4
0
 /**
  * @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);
 }
 /**
  * @dataProvider conditionDataProvider
  *
  * @param string $oldMethodBody
  * @param mixed  $value
  * @param string $condition
  * @param string $expectedMethodBody
  */
 public function testVisit($oldMethodBody, $value, $condition, $expectedMethodBody)
 {
     $conditionObject = new SimpleContextValueComparisonConditionVisitor('valueToCompare', $condition, $value);
     $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));
     $conditionObject->startVisit($visitContext);
     $visitContext->getUpdateMethodWriter()->writeln($oldMethodBody);
     $conditionObject->endVisit($visitContext);
     $method->setBody($visitContext->getUpdateMethodWriter()->getContent());
     $phpClass->setMethod($method);
     $this->assertSame($expectedMethodBody, $method->getBody());
 }
コード例 #6
0
ファイル: EntityGenerator.php プロジェクト: xamin123/platform
 /**
  * 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));
 }
コード例 #7
0
    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()));
    }
コード例 #8
0
 /**
  * @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);
 }
コード例 #9
0
ファイル: ProxyGenerator.php プロジェクト: pugx/aop
 /**
  * Creates a new enhanced class
  *
  * @throws \RuntimeException
  * @return string
  */
 public final function generateClass()
 {
     static $docBlock;
     if (empty($docBlock)) {
         $writer = new Writer();
         $writer->writeln('/**')->writeln(' * CG library enhanced proxy class.')->writeln(' *')->writeln(' * This code was generated automatically by the CG library, manual changes to it')->writeln(' * will be lost upon next generation.')->writeln(' */')->writeln('');
         $docBlock = $writer->getContent() . $this->class->getDocComment() . "\n";
     }
     $this->generatedClass = PhpClass::create()->setDocblock($docBlock)->setParentClassName($this->class->name);
     $proxyClassName = $this->getClassName($this->class);
     if (false === strpos($proxyClassName, NamingStrategyInterface::SEPARATOR)) {
         throw new \RuntimeException(sprintf('The proxy class name must be suffixed with "%s" and an optional string, but got "%s".', NamingStrategyInterface::SEPARATOR, $proxyClassName));
     }
     $this->generatedClass->setName($proxyClassName);
     foreach ($this->aspectGenerators as $generator) {
         $generator->generate($this->class, $this->generatedClass);
     }
     return $this->generateCode($this->generatedClass);
 }
コード例 #10
0
    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()));
    }
コード例 #11
0
 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));
 }
コード例 #12
0
 /**
  * Creates a new enhanced class
  *
  * @return string
  */
 public final function generateClass()
 {
     static $docBlock;
     if (empty($docBlock)) {
         $writer = new Writer();
         $writer->writeln('/**')->writeln(' * CG library enhanced proxy class.')->writeln(' *')->writeln(' * This code was generated automatically by the CG library, manual changes to it')->writeln(' * will be lost upon next generation.')->writeln(' */');
         $docBlock = $writer->getContent();
     }
     $this->generatedClass = PhpClass::create()->setDocblock($docBlock)->setParentClassName($this->class->name);
     $proxyClassName = $this->getClassName($this->class);
     if (false === strpos($proxyClassName, NamingStrategyInterface::SEPARATOR)) {
         throw new \RuntimeException(sprintf('The proxy class name must be suffixed with "%s" and an optional string, but got "%s".', NamingStrategyInterface::SEPARATOR, $proxyClassName));
     }
     $this->generatedClass->setName($proxyClassName);
     if (!empty($this->interfaces)) {
         $this->generatedClass->setInterfaceNames(array_map(function ($v) {
             return '\\' . $v;
         }, $this->interfaces));
         foreach ($this->getInterfaceMethods() as $method) {
             $method = PhpMethod::fromReflection($method);
             $method->setAbstract(false);
             $this->generatedClass->setMethod($method);
         }
     }
     if (!empty($this->generators)) {
         foreach ($this->generators as $generator) {
             $generator->generate($this->class, $this->generatedClass);
         }
     }
     return $this->generateCode($this->generatedClass);
 }
コード例 #13
0
ファイル: VisitContextTest.php プロジェクト: Maksold/platform
 /**
  * @return PhpClass
  */
 protected function getClass()
 {
     return PhpClass::create(uniqid('testClassName', true));
 }
コード例 #14
0
 private function getClass()
 {
     $class = PhpClass::create()->setName('GenerationTestClass')->setMethod(PhpMethod::create('a'))->setMethod(PhpMethod::create('b'))->setProperty(PhpProperty::create('a'))->setProperty(PhpProperty::create('b'))->setConstant('a', 'foo')->setConstant('b', 'bar');
     return $class;
 }