Пример #1
0
    public function testGenerate()
    {
        $generator = new TraitGenerator('Test');
        $code = <<<EOF
trait Test
{


}

EOF;
        $this->assertEquals($code, $generator->generate());
    }
    /**
     * Build generators
     *
     * @param  State|\Scaffold\State $state
     * @return \Scaffold\State|void
     */
    public function build(State $state)
    {
        $model = $this->model;
        $generator = new TraitGenerator($model->getName());
        $generator->addUse($state->getRepositoryModel()->getName());
        $property = lcfirst($state->getRepositoryModel()->getClassName());
        $class = $state->getRepositoryModel()->getClassName();
        $entity = $state->getEntityModel()->getName();
        $code = <<<EOF
if (null === \$this->{$property}) {
    \$this->{$property} = \$this->getEntityManager()->getRepository('{$entity}');
}
return \$this->{$property};
EOF;
        $this->addProperty($generator, $property, $class);
        $this->addSetter($generator, $property, $class);
        $getter = $this->getGetter($property, $class);
        $getter->setBody($code);
        $generator->addMethodFromGenerator($getter);
        //
        $model->setGenerator($generator);
    }
    /**
     * Build generators
     *
     * @param  State|\Scaffold\State $state
     * @return \Scaffold\State|void
     */
    public function build(State $state)
    {
        $model = $state->getModel('options-trait');
        $options = $state->getModel('options');
        $generator = new TraitGenerator($model->getName());
        $generator->addUse($options->getName());
        $generator->addUse($state->getModel('RuntimeException')->getName());
        $generator->addUse('Zend\\ServiceManager\\ServiceLocatorAwareInterface');
        $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface');
        $property = lcfirst($options->getClassName());
        $class = $options->getClassName();
        $alias = $options->getServiceName();
        $code = <<<EOF
if (null === \$this->{$property}) {
    if (\$this instanceof ServiceLocatorAwareInterface || method_exists(\$this, 'getServiceLocator')) {
        \$this->{$property} = \$this->getServiceLocator()->get('{$alias}');
    } else {
        if (property_exists(\$this, 'serviceLocator')
            && \$this->serviceLocator instanceof ServiceLocatorInterface
        ) {
            \$this->{$property} = \$this->serviceLocator->get('{$alias}');
        } else {
            throw new RuntimeException('Service locator not found');
        }
    }
}
return \$this->{$property};
EOF;
        $this->addProperty($generator, $property, $class);
        $this->addSetter($generator, $property, $class);
        $getter = $this->getGetter($property, $class);
        $getter->setBody($code);
        $getter->getDocBlock()->setTag(new Tag(['name' => 'throws', 'description' => $state->getModel('RuntimeException')->getClassName()]));
        $generator->addMethodFromGenerator($getter);
        //
        $model->setGenerator($generator);
    }