public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tags = array(array('name' => 'return', 'description' => 'array')); $docblock = new DocBlockGenerator('Initialize indexes'); $docblock->setTags($tags); $resultIndexList = array(); $indexList = $table->getIndex(); foreach ($indexList as $index) { $resIndex = $index->toArray(); $resIndex['column_list'] = array(); switch ($index->getType()) { case AbstractIndex::TYPE_PRIMARY: $resIndex['type'] = new ValueGenerator('AbstractModel::INDEX_PRIMARY', ValueGenerator::TYPE_CONSTANT); break; case AbstractIndex::TYPE_KEY: $resIndex['type'] = new ValueGenerator('AbstractModel::INDEX_KEY', ValueGenerator::TYPE_CONSTANT); break; case AbstractIndex::TYPE_UNIQUE: $resIndex['type'] = new ValueGenerator('AbstractModel::INDEX_UNIQUE', ValueGenerator::TYPE_CONSTANT); break; } foreach ($resIndex['columns'] as $col) { $resIndex['column_list'][] = $col['column_name']; } unset($resIndex['columns']); $resultIndexList[$index->getName()] = $resIndex; } $property = new PropertyGenerator('indexList', $resultIndexList, PropertyGenerator::FLAG_PROTECTED); $body = preg_replace("#^(\\s*)protected #", "\\1", $property->generate()) . "\n"; $method = new MethodGenerator(); $method->setName('initIndexList'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setFinal(true); $method->setDocBlock($docblock); $method->setBody(<<<EOS {$body} \$this->indexList = \$indexList; \$this->setupIndexList(); EOS ); $file->getClass()->addMethodFromGenerator($method); }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tags = array(array('name' => 'var', 'description' => 'array значения по-умолчанию для полей')); $docblock = new \Zend\Code\Generator\DocBlockGenerator('Значения по-умолчанию для полей'); $docblock->setTags($tags); $columnCollection = $table->getColumn(); if (!$columnCollection) { return; } $defaults = ''; /** @var $column Column */ foreach ($columnCollection as $column) { $columnName = $column->getName(); $defaultValue = $column->getColumnDefault(); if (substr($columnName, -5) == '_date') { $defaults .= "\$this->setDefaultRule('" . $columnName . "', date('Y-m-d H:i:s'));\n "; } elseif ($defaultValue == 'CURRENT_TIMESTAMP') { $defaults .= "\$this->setDefaultRule('" . $columnName . "', date('Y-m-d H:i:s'));\n "; } elseif (!empty($defaultValue)) { $defaults .= '$this->setDefaultRule(\'' . $columnName . '\', \'' . (string) $defaultValue . '\');' . "\n "; } elseif (is_null($defaultValue) && $column->isNullable()) { $defaults .= '$this->setDefaultRule(\'' . $columnName . '\', null);' . "\n "; } } $tags = array(array('name' => 'return', 'description' => 'void')); $docblock = new DocBlockGenerator('Инициализация значений по-умолчанию'); $docblock->setTags($tags); $method = new MethodGenerator(); $method->setName('initDefaultsRules'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setFinal(true); $method->setDocBlock($docblock); $method->setBody(<<<EOS {$defaults} \$this->setupDefaultsRules(); EOS ); $file->getClass()->addMethodFromGenerator($method); }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $tags = array(array('name' => 'version', 'description' => '$Rev:$'), array('name' => 'license', 'description' => 'MIT'), array('name' => 'author', 'description' => 'Model_Generator'), array('name' => 'author', 'description' => 'Eugene Myazin <*****@*****.**>'), array('name' => 'author', 'description' => 'Mikhail Rybalka <*****@*****.**>'), array('name' => 'author', 'description' => 'Vadim Slutsky <*****@*****.**>'), array('name' => 'author', 'description' => 'Anton Sedyshev <*****@*****.**>')); if ($file->getClass()->getDocblock()) { $file->getClass()->getDocblock()->setTags($tags); } else { $docblock = new DocBlockGenerator('Entity ' . $tableNameAsCamelCase); $docblock->setTags($tags); $file->getClass()->setDocblock($docblock); } }
/** * @param \Model\Generator\Part\Model|\Model\Generator\Part\PartInterface $part */ public function preRun(PartInterface $part) { /** * @var Table $table */ $table = $part->getTable(); /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); /** @var array|AbstractLink[] $linkList */ $linkList = $table->getLink(); foreach ($linkList as $link) { $_name = strtoupper($link->getForeignEntity()); $name = 'JOIN_' . $_name; $property = new PropertyGenerator($name, strtolower($_name), PropertyGenerator::FLAG_CONSTANT); $tags = array(array('name' => 'const'), array('name' => 'var', 'description' => 'string')); $docblock = new DocBlockGenerator('JOIN сущность ' . $_name); $docblock->setTags($tags); $property->setDocBlock($docblock); $this->_data[$table->getName()][$name] = $property; } }
/** * @param \Model\Generator\Part\Model $part */ public function generateEnumConstantList($part) { $file = $part->getFile(); $table = $part->getTable(); foreach ($table->getColumn() as $column) { if ($column->getColumnType() == 'enum') { $enumList = $column->getEnumValuesAsArray(); // пропускаем флаги и enum поля с числом параметров >10 if (substr($column->getName(), 0, 3) == 'is_' || $enumList == array('y', 'n') || count($enumList) > 10) { continue; } foreach ($enumList as $enumValue) { $columnName = $column->getName(); $name = strtoupper($columnName . '_' . $enumValue); $property = new PropertyGenerator($name, $enumValue, PropertyGenerator::FLAG_CONSTANT); $tags = array(array('name' => 'const'), array('name' => 'var', 'description' => 'string')); $docblock = new DocBlockGenerator("Значение {$enumValue} поля {$columnName}"); $docblock->setTags($tags); $property->setDocBlock($docblock); $file->getClass()->addPropertyFromGenerator($property); } } } }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $tags = array(array('name' => 'return', 'description' => '\\Model\\' . $tableNameAsCamelCase . 'Model экземпляр модели')); $docblock = new DocBlockGenerator('Получить экземпляр модели ' . $tableNameAsCamelCase); $docblock->setTags($tags); $method = new MethodGenerator(); $method->setName('getInstance'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setStatic(true); $method->setDocBlock($docblock); $method->setBody(<<<EOS return parent::getInstance(); EOS ); $file->getClass()->addMethodFromGenerator($method); }
/** * @param PartInterface $part */ public function preRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); /** * @var $table \Model\Cluster\Schema\Table */ $table = $part->getTable(); /** @var $columnList Column[] */ $columnList = $table->getColumn(); foreach ($columnList as $column) { $columnName = $column->getName(); $columnComment = $column->getComment(); if ($columnComment) { $shortDescription = "Get " . mb_strtolower($columnComment, 'UTF-8') . ' (' . $column->getTable()->getName() . '.' . $columnName . ')'; } else { $shortDescription = 'Get ' . $column->getTable()->getName() . '.' . $columnName; } $docBlock = new DocBlockGenerator($shortDescription); $docBlock->setTags(array(array('name' => 'return', 'description' => $column->getTypeAsPhp()))); $method = new MethodGenerator(); $method->setName('get' . $column->getNameAsCamelCase()); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docBlock); $method->setBody(<<<EOS return \$this->get('{$columnName}'); EOS ); $part->getFile()->getClass()->addMethodFromGenerator($method); } }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $columnCollection = $part->getTable()->getColumn(); $template = ''; /** @var $column Column */ foreach ($columnCollection as $column) { $name = $column->getName(); $requiredFlag = !($column->isNullable() || $column->getName() == 'id'); if ($columnConfig = $part->getColumntConfig($column)) { if ($columnConfig && isset($columnConfig['validators'])) { foreach ($columnConfig['validators'] as $validator) { if (isset($validator['params'])) { $validatorParams = $this->prepareValidatorParams($validator['params'], $column); $validatorParams = $this->varExportMin($validatorParams, true); } else { $validatorParams = null; } if ($validatorParams && $validatorParams != 'NULL') { $template .= "\$this->addValidatorRule('{$name}', Model::getValidatorAdapter()->getValidatorInstance('{$validator['name']}', {$validatorParams}), " . ($requiredFlag ? 'true' : 'false') . ");\n"; } else { $template .= "\$this->addValidatorRule('{$name}', Model::getValidatorAdapter()->getValidatorInstance('{$validator['name']}'), " . ($requiredFlag ? 'true' : 'false') . ");\n"; } } } } } $template = rtrim($template, "\r\n, "); //$tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $tags = array(array('name' => 'return', 'description' => 'array Model массив с фильтрами по полям')); $docblock = new DocBlockGenerator('Получить правила для фильтрации '); $docblock->setTags($tags); $method = new MethodGenerator(); $method->setName('initValidatorRules'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setStatic(false); $method->setFinal(true); $method->setDocBlock($docblock); $method->setBody(<<<EOS {$template} \$this->setupValidatorRules(); EOS ); $file->getClass()->addMethodFromGenerator($method); }
protected function validatorStub($file) { $tags = array(array('name' => 'param', 'description' => 'boolean $required true - при добавлении, в остальных случаях false'), array('name' => 'return', 'description' => 'void')); $docblock = new DocBlockGenerator('Инициализация правил валидации Третий параметр у addValidatorRule это обязателен ли этот валидатор при добавлении данных в базу. '); $docblock->setTags($tags); $p = new \Zend\Code\Generator\ParameterGenerator('required'); $p->setDefaultValue(true); $params[] = $p; $method = new MethodGenerator(); $method->setName('setupValidatorRules'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); $method->setFinal(false); $method->setParameters($params); $method->setDocBlock($docblock); $method->setBody(<<<EOS // \$this->addValidatorRule('field', Validator::getValidatorInstance('Zend\\Filter\\Int'), true or false); EOS ); $file->getClass()->addMethodFromGenerator($method); }
/** * @param AbstractLink $link * @return MethodGenerator */ protected function getDeleteLinkMethodWithoutLinkTable(AbstractLink $link) { $localEntity = $link->getLocalEntity(); $localEntityAsVar = $link->getLocalEntityAsVar(); $localEntityAsCamelCase = $link->getLocalEntityAsCamelCase(); $foreignTableAsCamelCase = $link->getForeignTable()->getNameAsCamelCase(); $foreignEntity = $link->getForeignEntity(); $foreignEntityAsVar = $link->getForeignEntityAsVar(); $foreignEntityAsCamelCase = $link->getForeignEntityAsCamelCase(); $localColumn = $link->getLocalColumn()->getName(); $foreignColumn = $link->getForeignColumn()->getName(); $tags = array(array('name' => 'param', 'description' => 'mixed $' . $localEntityAsVar), array('name' => 'param', 'description' => 'mixed $' . $foreignEntityAsVar), array('name' => 'return', 'description' => '\\Model\\Result\\Result')); $docblock = new DocBlockGenerator('Отвязать ' . $localEntityAsCamelCase . ' от ' . $foreignEntityAsCamelCase); $docblock->setTags($tags); $nullValue = new ValueGenerator('array()', ValueGenerator::TYPE_CONSTANT); $method = new MethodGenerator(); $method->setName('deleteLink' . $localEntityAsCamelCase . 'To' . $foreignEntityAsCamelCase); $method->setParameter(new ParameterGenerator($localEntityAsVar, 'mixed', $nullValue)); $method->setParameter(new ParameterGenerator($foreignEntityAsVar, 'mixed', $nullValue)); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); if ($link->isDirect()) { $method->setBody(<<<EOS \${$localEntityAsVar}Ids = array_unique(\$this->getIdsFromMixed(\${$localEntityAsVar})); \${$foreignEntityAsVar}Ids = array_unique({$foreignTableAsCamelCase}Model::getInstance()->getIdsFromMixed(\${$foreignEntityAsVar})); \$result = new Result(); \$result->setResult(true); if (count(\${$localEntityAsVar}Ids) == 0 && count(\${$foreignEntityAsVar}Ids) == 0) { return \$result; } \$cond = array(); if (count(\${$localEntityAsVar}Ids) != 0) { \$cond['{$foreignColumn}'] = \${$localEntityAsVar}Ids; } if (count(\${$foreignEntityAsVar}Ids) != 0) { \$cond['{$localColumn}'] = \${$foreignEntityAsVar}Ids; } try { \$this->getDb()->update(\$this->getRawName(), array('{$localColumn}' => null), \$cond); } catch (\\Exception \$e) { \$result->setResult(false); \$result->addError("Delete link {$localEntity} to {$foreignEntity} failed", 'delete_link_failed'); } return \$result; EOS ); } else { $method->setBody(<<<EOS return {$foreignTableAsCamelCase}Model::getInstance()->deleteLink{$foreignEntityAsCamelCase}To{$localEntityAsCamelCase}(\${$foreignEntityAsVar}, \${$localEntityAsVar}); EOS ); } return $method; }
public function generateMethodsByLink($part) { /** @var $part \Model\Generator\Part\Model */ /** @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $indexList = $table->getIndex(); /* $userStat = $part->getTable()->getSchema()->getTable('user'); $indexList = $userStat->getIndex(); foreach ($indexList as $index) { print_r($index->toArray()); $column = reset($index); if ($index->count() > 1 || !($link = $table->getLinkByColumn($column))) { } } die;*/ $methods = array(); foreach ($indexList as $index) { if ($index->getName() == 'PRIMARY') { continue; } $column = reset($index); if ($index->count() > 1 || !($link = $table->getLinkByColumn($column, $table->getName()))) { continue; } if ($link->getLocalTable() == $table->getName()) { $direct = true; } else { $direct = false; } $columnAsCamelCase = $link->getForeignTable()->getnameAsCamelCase(); $columnAsVar = $link->getForeignTable()->getnameAsVar(); $columnName = $link->getForeignTable()->getName(); $localColumnName = $link->getLocalColumn()->getName(); $localTableName = $link->getLocalTable()->getName(); $type = "{$columnAsCamelCase}Entity|{$columnAsCamelCase}Collection|array|string|integer"; $file->addUse('\\Model\\Entity\\' . $columnAsCamelCase . 'Entity'); $file->addUse('\\Model\\Collection\\' . $columnAsCamelCase . 'Collection'); $tags[] = array('name' => 'param', 'description' => "{$type} \$" . $columnAsVar); $params[] = new \Zend\Code\Generator\ParameterGenerator($columnAsVar); $docblock = new DocBlockGenerator('Получить один элемен по '); $docblock->setTags($tags); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('getBy' . $columnAsCamelCase); $method->setParameters($params); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setStatic(true); $method->setDocBlock($docblock); // print_r($link->toArray());z if (($link->getLinkType() == AbstractLink::LINK_TYPE_ONE_TO_ONE || $link->getLinkType() == AbstractLink::LINK_TYPE_MANY_TO_ONE) && $direct) { $method->setBody(<<<EOS \$cond = \$this->prepareCond(\$cond); \${$columnAsVar}Ids = {$columnAsCamelCase}Model::getInstance()->getIdsFromMixed(\${$columnAsVar}); if (!\${$columnAsVar}Ids) { return \$cond->getEmptySelectResult(); } \$cond->where(array('`{$localTableName}`.`{$localColumnName}`' => \${$columnAsVar}Ids)); return \$this->get(\$cond); EOS ); } elseif (($link->getLinkType() == AbstractLink::LINK_TYPE_ONE_TO_ONE || $link->getLinkType() == AbstractLink::LINK_TYPE_MANY_TO_ONE) && !$direct) { $localTableName = $link->getLocalTable()->getName(); $localColumnName = $link->getLocalColumn()->getName(); $localTableNameAsVar = $link->getLocalTable()->getNameAsVar(); $localTableNameAsCamelCase = $link->getLocalTable()->getNameAsCamelCase(); $method->setBody(<<<EOS \$cond = \$this->prepareCond(\$cond); \${$columnAsVar}Collection = {$columnAsCamelCase}Model::getInstance()->getCollectionBy{$columnAsCamelCase}(\${$columnAsVar}); \${$localTableNameAsVar}Ids = array(); foreach (\${$columnAsVar}Collection as \${$columnAsVar}) { \${$localTableNameAsVar}Ids[] = \${$columnAsVar}->get{$localTableNameAsCamelCase}Id(); } \${$localTableNameAsVar}Ids = \$this->getIdsFromMixed(\${$localTableNameAsVar}Ids); if (!\${$localTableNameAsVar}Ids) { return \$cond->getEmptySelectResult(); } \$cond->where(array('`{$localTableName}`.`{$localColumnName}`' => \${$localTableNameAsVar}Ids)); return \$this->get(\$cond); EOS ); } elseif ($link->getLinkType() == AbstractLink::LINK_TYPE_MANY_TO_MANY) { die('ok'); } $methods[] = $method; } return $methods; }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $file->addUse('Model\\Filter\\Filter'); $columnCollection = $part->getTable()->getColumn(); $template = ''; /** @var $column Column */ foreach ($columnCollection as $column) { $name = $column->getName(); if ($columnConfig = $part->getColumntConfig($column)) { if ($columnConfig && isset($columnConfig['filters'])) { foreach ($columnConfig['filters'] as $filter) { $filterParams = isset($validator['params']) ? $this->varExportMin($validator['params'], true) : null; if ($filterParams && $filterParams != 'NULL') { $template .= "\$this->addFilterRule('{$name}', Filter::getFilterInstance('{$filter['name']}', {$filterParams}));\n"; } else { $template .= "\$this->addFilterRule('{$name}', Filter::getFilterInstance('{$filter['name']}'));\n"; } } } } /* $filterArray = $column->getFilter(); foreach ($filterArray as $filter) { if (empty($filter['params'])) { $template .= "\$this->addFilterRule('$name', Filter::getFilterInstance('{$filter['name']}'));\n"; } else { $filterParams = $this->varExportMin($filter['params'], true); $template .= "\$this->addFilterRule('$name', Filter::getFilterInstance('{$filter['name']}', {$filterParams}));\n"; } } */ if ($column->isNullable()) { $template .= "\$this->addFilterRule('{$name}', Filter::getFilterInstance('\\Model\\Filter\\Null'));\n"; } } //$tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $tags = array(array('name' => 'return', 'description' => 'array Model массив с фильтрами по полям')); $docblock = new DocBlockGenerator('Получить правила для фильтрации '); $docblock->setTags($tags); $method = new MethodGenerator(); $method->setName('initFilterRules'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setStatic(false); $method->setFinal(true); $method->setDocBlock($docblock); $method->setBody(<<<EOS {$template} \$this->setupFilterRules(); return \$this->getFilterRules(); EOS ); $file->getClass()->addMethodFromGenerator($method); }