/** * Add a selectWith() method if table has an external dependency * * @return MethodGenerator */ protected function addSelectWithMethod() { $foreignKeys = $this->loadedTables[$this->tableName]['foreignKeys']; if (empty($foreignKeys)) { return true; } $body = []; /** @var ConstraintObject $foreignKey */ foreach ($foreignKeys as $foreignKey) { $refTableName = $foreignKey->getReferencedTableName(); $refTableColumns = $this->loadedTables[$refTableName]['columns']; $body[] = '$select->join('; $body[] = ' \'' . $refTableName . '\','; $body[] = ' \'' . $this->tableName . '.' . $foreignKey->getColumns()[0] . ' = ' . $refTableName . '.' . $foreignKey->getReferencedColumns()[0] . '\','; $body[] = ' ['; /** @var ColumnObject $column */ foreach ($refTableColumns as $column) { $body[] = ' \'' . $refTableName . '.' . $column->getName() . '\' => \'' . $column->getName() . '\','; } $body[] = ' ]'; $body[] = ');'; $body[] = ''; } $body[] = 'return parent::selectWith($select);'; $body = implode(AbstractGenerator::LINE_FEED, $body); $this->addUse('Zend\\Db\\ResultSet\\ResultSetInterface'); $this->addUse('Zend\\Db\\Sql\\Select'); $selectMethod = new MethodGenerator('selectWith'); $selectMethod->addFlag(MethodGenerator::FLAG_PUBLIC); $selectMethod->setParameter(new ParameterGenerator('select', 'Select')); $selectMethod->setDocBlock(new DocBlockGenerator('Add join tables', null, [['name' => 'param', 'description' => 'Select $select'], ['name' => 'return', 'description' => 'ResultSetInterface']])); $selectMethod->setBody($body); $this->addMethodFromGenerator($selectMethod); return true; }
/** * Add a getOptions() method if table has an external dependency * * @param $moduleName * * @return MethodGenerator */ protected function addGetOptionsMethod($moduleName) { $entityClass = $this->filterUnderscoreToCamelCase($this->tableName) . 'Entity'; /** @var ConstraintObject $primaryKey */ $primaryKey = $this->loadedTables[$this->tableName]['primaryKey']; $body = []; $body[] = '$options = [];'; $body[] = ''; $body[] = '/** @var ' . $entityClass . ' $entity */'; $body[] = 'foreach ($this->fetchAllEntities() as $entity) {'; $body[] = ' $columns = ['; foreach ($this->loadedTables[$this->tableName]['columns'] as $columnName => $columnType) { if (in_array($columnName, $primaryKey->getColumns())) { continue; } $getMethod = 'get' . ucfirst($this->filterUnderscoreToCamelCase($columnName)); $body[] = ' $entity->' . $getMethod . '(),'; } $body[] = ' ];'; $body[] = ''; $body[] = ' $options[$entity->getIdentifier()] = implode(\' \', $columns);'; $body[] = '}'; $body[] = ''; $body[] = 'return $options;'; $body = implode(AbstractGenerator::LINE_FEED, $body); $this->addUse($moduleName . '\\' . $this->config['namespaceEntity'] . '\\' . $entityClass); $selectMethod = new MethodGenerator('getOptions'); $selectMethod->addFlag(MethodGenerator::FLAG_PUBLIC); $selectMethod->setDocBlock(new DocBlockGenerator('Get option list', null, [['name' => 'return', 'description' => 'array']])); $selectMethod->setBody($body); $this->addMethodFromGenerator($selectMethod); return true; }
/** * @param string $columnName * @param ConstraintObject $foreignKey */ protected function addOptionsSetter($columnName, ConstraintObject $foreignKey) { $body = '$this->' . $columnName . 'Options = $' . $columnName . 'Options;'; $parameter = new ParameterGenerator($columnName . 'Options', 'array'); $setMethodName = 'set' . ucfirst($columnName) . 'Options'; $setMethod = new MethodGenerator($setMethodName); $setMethod->addFlag(MethodGenerator::FLAG_PUBLIC); $setMethod->setParameter($parameter); $setMethod->setDocBlock(new DocBlockGenerator('Set ' . $columnName . ' options', null, [['name' => 'param', 'description' => 'array $' . $columnName . 'Options']])); $setMethod->setBody($body); $this->addMethodFromGenerator($setMethod); }
/** * @return MethodGenerator */ protected function generateNormalToStringMethod() { $body = ['return $this->__toString();']; $body = implode(AbstractGenerator::LINE_FEED, $body); $toStringMethod = new MethodGenerator('toString'); $toStringMethod->addFlag(MethodGenerator::FLAG_PUBLIC); $toStringMethod->setDocBlock(new DocBlockGenerator('Output entity', null, [['name' => 'return', 'description' => 'string']])); $toStringMethod->setBody($body); return $toStringMethod; }
/** * Add a selectWith() method if table has an external dependency * * @return MethodGenerator */ protected function addSelectWithMethod() { /** @var TableObject $currentTable */ $currentTable = $this->tableObjects[$this->tableName]; $foreignKeys = array(); /** @var $tableConstraint ConstraintObject */ foreach ($currentTable->getConstraints() as $tableConstraint) { if (!$tableConstraint->isForeignKey()) { continue; } $foreignKeys[] = $tableConstraint; } if (empty($foreignKeys)) { return true; } $body = array(); /** @var ConstraintObject $foreignKey */ foreach ($foreignKeys as $foreignKey) { $refTableName = $foreignKey->getReferencedTableName(); /** @var TableObject $refTableObject */ $refTableObject = $this->tableObjects[$refTableName]; $body[] = '$select->join('; $body[] = ' \'' . $refTableName . '\','; $body[] = ' \'' . $this->tableName . '.' . $foreignKey->getColumns()[0] . ' = ' . $refTableName . '.' . $foreignKey->getReferencedColumns()[0] . '\','; $body[] = ' array('; /** @var ColumnObject $column */ foreach ($refTableObject->getColumns() as $column) { $body[] = ' \'' . $refTableName . '.' . $column->getName() . '\' => \'' . $column->getName() . '\','; } $body[] = ' )'; $body[] = ');'; $body[] = ''; } $body[] = 'return parent::selectWith($select);'; $body = implode(AbstractGenerator::LINE_FEED, $body); $this->addUse('Zend\\Db\\ResultSet\\ResultSetInterface'); $this->addUse('Zend\\Db\\Sql\\Select'); $selectMethod = new MethodGenerator('selectWith'); $selectMethod->addFlag(MethodGenerator::FLAG_PUBLIC); $selectMethod->setParameter(new ParameterGenerator('select', 'Select')); $selectMethod->setDocBlock(new DocBlockGenerator('Add join tables', null, array(array('name' => 'param', 'description' => 'Select $select'), array('name' => 'return', 'description' => 'ResultSetInterface')))); $selectMethod->setBody($body); $this->addMethodFromGenerator($selectMethod); return true; }
/** * Add indexAction() method for DeleteController * * @param $repositoryClass * @param $formClass */ protected function addDeleteControllerAction($repositoryClass, $formClass) { // prepare some params $formParam = lcfirst($formClass); $underscoredParam = strtolower(StaticFilter::execute(str_replace('Entity', '', $this->entityClass), 'WordCamelCaseToUnderscore')); $dashedParam = strtolower(StaticFilter::execute($this->paramModule, 'WordCamelCaseToDash')); $dashedModule = strtolower(StaticFilter::execute($this->paramModule, 'WordCamelCaseToUnderscore')); $noFoundMessage = $dashedModule . '_message_' . $underscoredParam . '_not_found'; $deleteMessage = $dashedModule . '_message_' . $underscoredParam . '_deleting_possible'; $successMessage = $dashedModule . '_message_' . $underscoredParam . '_deleting_success'; $failedMessage = $dashedModule . '_message_' . $underscoredParam . '_deleting_failed'; // prepare entity params $entityParam = lcfirst($this->entityClass); $body = ['$id = $this->params()->fromRoute(\'id\');', '', 'if (!$id) {', ' $this->flashMessenger()->addErrorMessage(\'' . $noFoundMessage . '\');', ' ', ' return $this->redirect()->toRoute(\'' . $dashedParam . '\');', '}', '', '$' . $entityParam . ' = $this->' . lcfirst($repositoryClass) . '->getEntityById($id);', '', 'if (!$' . $entityParam . ') {', ' $this->flashMessenger()->addErrorMessage(\'' . $noFoundMessage . '\');', ' ', ' return $this->redirect()->toRoute(\'' . $dashedParam . '\');', '}', '', '$' . $formParam . ' = $this->' . $formParam . ';', '', 'if ($this->params()->fromPost(\'delete_' . $underscoredParam . '\')) {', ' if ($this->' . lcfirst($repositoryClass) . '->removeEntity($' . $entityParam . ')) {', ' $this->flashMessenger()->addSuccessMessage(\'' . $successMessage . '\');', ' ', ' return $this->redirect()->toRoute(\'' . $dashedParam . '\');', ' } else {', ' $this->flashMessenger()->addErrorMessage(\'' . $failedMessage . '\');', ' }', '} else {', ' $this->flashMessenger()->addInfoMessage(\'' . $deleteMessage . '\');', '}', '', '$viewModel = new ViewModel(', ' [', ' \'' . $entityParam . '\' => $' . $entityParam . ',', ' \'' . $formParam . '\' => $' . $formParam . ',', ' ]', ');', '', 'return $viewModel;']; $body = implode(AbstractGenerator::LINE_FEED, $body); $indexAction = new MethodGenerator('indexAction'); $indexAction->addFlag(MethodGenerator::FLAG_PUBLIC); $indexAction->setDocBlock(new DocBlockGenerator('Index action for DeleteController', null, [new ReturnTag(['ViewModel'])])); $indexAction->setBody($body); $this->addMethodFromGenerator($indexAction); }
/** * generates propertiy, getter, setter and other methods... * * @param \Zend\Code\Generator\ClassGenerator $class * @param \VisioCrudModeler\Descriptor\Db\DbDataSetDescriptor $dataSet */ protected function generateConstructor(ClassGenerator $class, \VisioCrudModeler\Descriptor\AbstractDataSetDescriptor $dataSet) { $constructor = new MethodGenerator("__construct"); $constructor->addFlag(MethodGenerator::FLAG_PUBLIC); $methodBody = $this->codeLibrary()->get("filter.constructor.body.begin"); foreach ($dataSet->listGenerator() as $column) { $methodBody .= $this->generateFilterForColumn($column); } $constructor->setBody($methodBody); $class->addMethodFromGenerator($constructor); }
/** * @param $columnName * @param $columnType * * @return MethodGenerator */ protected function generateGetMethod($columnName, $columnType) { $getMethodName = 'get' . ucfirst($columnName); $getMethod = new MethodGenerator($getMethodName); $getMethod->addFlag(MethodGenerator::FLAG_PUBLIC); $getMethod->setDocBlock(new DocBlockGenerator('Get ' . $columnName, null, array(array('name' => 'return', 'description' => $columnType)))); $getMethod->setBody('return $this->' . $columnName . ';'); return $getMethod; }