/** * @param State|\Scaffold\State $state * @return State|void */ public function build(State $state) { $model = $state->getModel('options-factory'); $generator = new ClassGenerator($model->getName()); $generator->setImplementedInterfaces(['FactoryInterface']); $model->setGenerator($generator); $generator->addUse('Zend\\ServiceManager\\FactoryInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $options = $state->getModel('options'); $key = $options->getServiceName(); $key = substr($key, 0, -7); $body = <<<EOF \$config = \$serviceLocator->get('Config'); return new {$options->getClassName()}( isset(\$config['{$key}']) ? \$config['{$key}'] : [] ); EOF; $method = new MethodGenerator('createService'); $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface')); $method->setBody($body); $doc = new DocBlockGenerator(''); $doc->setTag(new Tag(['name' => 'inhertidoc'])); $method->setDocBlock($doc); $generator->addMethodFromGenerator($method); }
/** * Build generators * * @param State|State $state */ public function build(State $state) { $model = $state->getModel($this->name); $data = file_get_contents(SCAFFOLD_ROOT . "/data/template/" . $this->name); $data = str_replace('__NAMESPACE_PLACEHOLDER__', ucfirst($this->config->getModule()), $data); $model->setGenerator(new BinGenerator($data)); }
/** * @param ClassGenerator $generator * @param State $state */ public function addLoadById(ClassGenerator $generator, State $state) { $entity = $state->getEntityModel()->getClassName(); $repository = ucfirst($state->getRepositoryModel()->getClassName()); $code = <<<EOF \$id = 123; \$entity = new {$entity}(); \$object = \$this->getObject(); \$object->get{$repository}()->expects(\$this->once())->method('find') ->with(\$id)->will(\$this->returnValue(\$entity)); \$this->assertSame(\$entity, \$object->loadById(\$id)); EOF; $generator->addMethodFromGenerator(new MethodGenerator('testLoadById', [], MethodGenerator::FLAG_PUBLIC, $code)); $doc = '@expectedException \\' . $state->getModel('NotFoundException')->getName(); $code = <<<EOF \$id = 123; \$object = \$this->getObject(); \$object->get{$repository}()->expects(\$this->once())->method('find') ->with(\$id)->will(\$this->returnValue(false)); \$object->loadById(\$id); EOF; $generator->addMethodFromGenerator(new MethodGenerator('testLoadByIdFailed', [], MethodGenerator::FLAG_PUBLIC, $code, $doc)); }
protected function write(State $state, InputInterface $input, OutputInterface $output) { $writeState = new State($this->configWriter); $writeState->addModel($state->getEntityModel()); $writeState->addModel($state->getModel('entity-test')); parent::write($writeState, $input, $output); }
/** * @param State|\Scaffold\State $state * @return State|void */ public function build(State $state) { $model = $state->getModel('options'); $generator = new ClassGenerator($model->getName()); $generator->setExtendedClass('AbstractOptions'); $model->setGenerator($generator); $generator->addUse('Zend\\Stdlib\\AbstractOptions'); }
protected function write(State $state, InputInterface $input, OutputInterface $output) { $writeState = new State($this->configWriter); if (!$input->getOption('no-repository')) { $writeState->addModel($state->getRepositoryModel()); } if (!$input->getOption('no-trait')) { $writeState->addModel($state->getModel('repository-trait')); } parent::write($writeState, $input, $output); }
/** * Build generators * * @param State|State $state */ public function build(State $state) { $model = $state->getModel('module'); if ($this->config->getBare()) { $data = file_get_contents(SCAFFOLD_ROOT . "/data/template/src/Module.bare.php"); } else { $data = file_get_contents(SCAFFOLD_ROOT . "/data/template/src/Module.php"); } $data = str_replace('__NAMESPACE_PLACEHOLDER__', ucfirst($this->config->getModule()), $data); $data = substr($data, 7); $model->setGenerator(new RawGenerator($data)); }
/** * 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); }
/** * Build generators * * @param State|\Scaffold\State $state * @return \Scaffold\State|void */ public function build(State $state) { $model = $state->getServiceModel(); $generator = new ClassGenerator($model->getName()); $generator->setImplementedInterfaces(['ServiceLocatorAwareInterface']); $generator->addUse('Doctrine\\ORM\\EntityManager'); $generator->addUse($state->getEntityModel()->getName()); $generator->addUse($state->getModel('NotFoundException')->getName()); $generator->addUse($state->getModel('repository-trait')->getName()); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorAwareInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorAwareTrait'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $generator->addTrait('ServiceLocatorAwareTrait'); $generator->addTrait($state->getModel('repository-trait')->getClassName()); $this->addProperty($generator, 'entityManager', 'EntityManager'); $this->buildConstructor($generator); $this->buildLoadById($generator, $state); $this->buildSearch($generator, $state); $this->buildSave($generator, $state); $this->buildDelete($generator, $state); $this->buildEntityManager($generator); $this->buildFactory($generator, $state); $model->setGenerator($generator); }
/** * @expectedException \Scaffold\Exception\RuntimeException */ public function testGetModelNotFound() { $state = new State($this->getConfigWriter()); $state->getModel('test'); }