Ejemplo n.º 1
0
    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);
    }
Ejemplo n.º 2
0
    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);
    }
Ejemplo n.º 3
0
    /**
     * @group ZF-6444
     */
    public function testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract()
    {
        $methodGenerator = new MethodGenerator();
        $methodGenerator->setName('foo');
        $methodGenerator->setParameters(array('one'));
        $methodGenerator->setFinal(true);
        $methodGenerator->setAbstract(true);

        $expected = <<<EOS
    abstract public function foo(\$one)
    {
    }

EOS;
        $this->assertEquals($expected, $methodGenerator->generate());
    }
Ejemplo n.º 4
0
    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);
    }
Ejemplo n.º 5
0
    protected function viewStub($part)
    {
        /**
         * @var $part \Model\Generator\Part\Model
         */
        /**
         * @var $file \Model\Code\Generator\FileGenerator
         */
        $file = $part->getFile();
        $table = $part->getTable();
        $tableNameAsCamelCase = $table->getNameAsCamelCase();
        $entity = $tableNameAsCamelCase . 'Entity';
        $collection = $tableNameAsCamelCase . 'Collection';
        $p = new \Zend\Code\Generator\ParameterGenerator('cond');
        $p->setType('Cond');
        $p->setDefaultValue(null);
        $params[] = $p;
        $docblock = new DocBlockGenerator('Получить объект условий в виде представления \'Extended\'

$param Cond $cond
$return Cond
        ');
        $method = new MethodGenerator();
        $method->setName('getCondAsExtendedView');
        $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC);
        $method->setFinal(false);
        $method->setDocBlock($docblock);
        $method->setParameters($params);
        $method->setBody(<<<EOS
\$cond = \$this->prepareCond(\$cond);
\$cond->where(array('status' => 'active'));
return \$cond;
EOS
);
        $file->getClass()->addMethodFromGenerator($method);
        $p = new \Zend\Code\Generator\ParameterGenerator('cond');
        $p->setType('Cond');
        $p->setDefaultValue(null);
        $params[] = $p;
        $docblock = new DocBlockGenerator('Получить элемент в виде представления \'Extended\'

@param Cond $cond
@return ' . $entity);
        $method = new MethodGenerator();
        $method->setName('getAsExtendedView');
        $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC);
        $method->setFinal(false);
        $method->setDocBlock($docblock);
        $method->setParameters($params);
        $method->setBody(<<<EOS
\$cond = \$this->getCondAsExtendedView(\$cond);
return \$this->get(\$cond);
EOS
);
        $file->getClass()->addMethodFromGenerator($method);
        $p = new \Zend\Code\Generator\ParameterGenerator('cond');
        $p->setType('Cond');
        $p->setDefaultValue(null);
        $params[] = $p;
        $docblock = new DocBlockGenerator('Получить коллекцию в виде представления \'Extended\'

@param Cond $cond
@return ' . $collection);
        $method = new MethodGenerator();
        $method->setName('getCollectionAsExtendedView');
        $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC);
        $method->setFinal(false);
        $method->setDocBlock($docblock);
        $method->setParameters($params);
        $method->setBody(<<<EOS
\$cond = \$this->getCondAsExtendedView(\$cond);
return \$this->getCollection(\$cond);
EOS
);
        $file->getClass()->addMethodFromGenerator($method);
    }
Ejemplo n.º 6
0
 private function buildClass($replacement)
 {
     $type = $this->splitNsandClass($replacement['originalFullyQualifiedType']);
     $class = new ClassGenerator();
     $class->setName($type['class']);
     $class->setNamespaceName($type['ns']);
     $class->setExtendedClass('\\Brads\\Ppm\\Proxy');
     $properties = [];
     $methods = [];
     $implementedInterfaces = [];
     foreach ($versions as $version => $info) {
         foreach ($info['files'] as $file) {
             echo "Parsing: " . $this->vendorDir . '/' . $package . '/' . $version . '/' . $file . "\n";
             $parsedFile = new ReflectionFile($this->vendorDir . '/' . $package . '/' . $version . '/' . $file);
             $parsedClass = $parsedFile->getFileNamespace($info['toNs'])->getClass($info['toNs'] . '\\' . $type['class']);
             if ($parsedClass->getInterfaceNames() != null) {
                 $implementedInterfaces = array_merge($implementedInterfaces, $parsedClass->getInterfaceNames());
             }
             foreach ($parsedClass->getMethods() as $method) {
                 if ($method->isPublic()) {
                     $generatedMethod = new MethodGenerator();
                     $generatedMethod->setName($method->name);
                     $generatedMethod->setBody('echo "Hello world!";');
                     $generatedMethod->setAbstract($method->isAbstract());
                     $generatedMethod->setFinal($method->isFinal());
                     $generatedMethod->setStatic($method->isStatic());
                     $generatedMethod->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
                     foreach ($method->getParameters() as $param) {
                         $generatedParam = new ParameterGenerator();
                         $generatedParam->setName($param->name);
                         if ($param->hasType()) {
                             $generatedParam->setType($param->getType());
                         }
                         //$generatedParam->setDefaultValue($param->getDefaultValue());
                         $generatedParam->setPosition($param->getPosition());
                         $generatedParam->setVariadic($param->isVariadic());
                         $generatedParam->setPassedByReference($param->isPassedByReference());
                         $generatedMethod->setParameter($generatedParam);
                     }
                     $existingMethod = Linq::from($methods)->firstOrDefault(null, function (MethodGenerator $v) use($method) {
                         return $v->getName() == $method->name;
                     });
                     if ($existingMethod != null) {
                         $existingParams = $existingMethod->getParameters();
                         foreach ($generatedMethod->getParameters() as $newParam) {
                             $existingParam = Linq::from($existingParams)->firstOrDefault(function (ParameterGenerator $v) {
                                 return $v->getName() == $newParam->getName();
                             });
                             if ($existingParam == null) {
                                 $existingMethod->setParameter($newParam);
                             }
                         }
                     } else {
                         $methods[] = $generatedMethod;
                     }
                 }
             }
             foreach ($parsedClass->getProperties() as $prop) {
                 //$properties[] = PropertyGenerator::fromReflection($prop);
             }
         }
     }
     $class->setImplementedInterfaces($implementedInterfaces);
     $class->addMethods($methods);
     $class->addProperties($properties);
     return (new FileGenerator(['classes' => [$class]]))->generate();
 }
Ejemplo n.º 7
0
 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);
     $linkList = $table->getLink();
     /*if ($table->getColumn('parent_id')) {
                 echo $table->getName();
     
                 print_r($linkList);
                 die;
             }*/
     $relation = array();
     foreach ($linkList as $link) {
         $linkLocalColumnName = $link->getLocalColumn()->getName();
         if ($link->getForeignTable() == $link->getLocalTable() && $link->getLocalColumn() == $link->getForeignColumn()) {
             continue;
         }
         $foreignAliasName = $link->getForeignEntity();
         $rel = $link->toArray();
         unset($rel['name']);
         switch ($link->getLinkType()) {
             case AbstractLink::LINK_TYPE_ONE_TO_ONE:
                 $rel['type'] = new ValueGenerator('AbstractModel::ONE_TO_ONE', ValueGenerator::TYPE_CONSTANT);
                 break;
             case AbstractLink::LINK_TYPE_ONE_TO_MANY:
                 $rel['type'] = new ValueGenerator('AbstractModel::ONE_TO_MANY', ValueGenerator::TYPE_CONSTANT);
                 break;
             case AbstractLink::LINK_TYPE_MANY_TO_ONE:
                 $rel['type'] = new ValueGenerator('AbstractModel::MANY_TO_ONE', ValueGenerator::TYPE_CONSTANT);
                 break;
             case AbstractLink::LINK_TYPE_MANY_TO_MANY:
                 $rel['type'] = new ValueGenerator('AbstractModel::MANY_TO_MANY', ValueGenerator::TYPE_CONSTANT);
                 break;
         }
         if ($link->getLocalColumn()->getName() != 'id' && !$link->getLinkTable() || $link->getLocalColumn()->getName() == 'id' && !$link->getLinkTable() && !$link->getLocalColumn()->isAutoincrement()) {
             $rel['required_link'] = !$link->getLocalColumn()->isNullable();
             $rel['link_method'] = 'link' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase();
             $rel['unlink_method'] = 'deleteLink' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase();
         } else {
             if ($table->getName() == 'tag') {
                 if ($link->getLocalEntityAlias() != $link->getLocalTable()->getName()) {
                     $foreignAliasName .= '_as_' . $link->getLocalEntityAlias();
                 }
             }
             $rel['required_link'] = false;
             $rel['link_method'] = 'link' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase();
             $rel['unlink_method'] = 'deleteLink' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase();
         }
         $rel['local_entity'] = $link->getLocalEntity();
         $rel['foreign_entity'] = $link->getForeignEntity();
         $relation[$foreignAliasName] = $rel;
         $relation[$foreignAliasName]['foreign_model'] = '\\Model\\' . $link->getForeignTable()->getNameAsCamelCase() . 'Model';
     }
     $property = new PropertyGenerator('relation', $relation, PropertyGenerator::FLAG_PROTECTED);
     //$property->setDocBlock($docblock);
     $method = new MethodGenerator();
     $method->setName('initRelation');
     $method->setFinal(true);
     $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED);
     $body = preg_replace("#^(\\s*)protected #", "\\1", $property->generate()) . "\n";
     $body .= "\$this->setRelation(\$relation); \n";
     $body .= "\$this->setupRelation();";
     $docblock = new DocBlockGenerator('Настройка связей');
     $method->setDocBlock($docblock);
     $method->setBody($body);
     //$file->getClass()->addPropertyFromGenerator($property);
     $file->getClass()->addMethodFromGenerator($method);
 }
Ejemplo n.º 8
0
    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);
    }