Example #1
0
 public function build()
 {
     // Check name (table name)
     if (!$this->_options['table_name']) {
         throw new BuilderException("You must specify the table name");
     }
     // Get config
     $path = '';
     if (isset($this->_options['config_path'])) {
         $path = $this->_options['config_path'];
     } elseif (isset($this->_options['app_path'])) {
         $path = $this->_options['app_path'];
     } elseif (isset($this->_options['module_path'])) {
         $path = $this->_options['module_path'];
     }
     $config = $this->_getConfig($path);
     // build options
     $this->buildOptions($this->_options['table_name'], $config, Component::OPTION_FORM, $this->type);
     // Prepare DB connection
     if (!$this->prepareDbConnection($config)) {
         return false;
     }
     // Check if table exist in database
     $table = $this->_options['table_name'];
     if ($this->db->tableExists($table, $config->database->dbname)) {
         $fields = $this->db->describeColumns($table, $config->database->dbname);
         $rows = $this->db->fetchAll("SELECT * FROM `information_schema`.`columns` WHERE `table_schema` = '" . $config->database->dbname . "' and `table_name` = '" . $table . "'");
         $fullFields = [];
         foreach ($rows as $row) {
             $fullFields[$row['COLUMN_NAME']] = $row;
         }
     } else {
         throw new BuilderException('Table "' . $table . '" does not exists');
     }
     // Set extender class template
     switch ($this->type) {
         case self::TYPE_SIMPLE:
             $extends = $this->templateSimpleFormExtends;
             break;
         case self::TYPE_EXTJS:
             $extends = $this->templateExtJsFormExtends;
             break;
         default:
             $extends = $this->templateSimpleFormExtends;
             break;
     }
     // Set action template
     if ($this->type !== self::TYPE_EXTJS) {
         $nameSpace = $this->_builderOptions['namespaceClear'];
         $pieces = explode('\\', $nameSpace);
         array_shift($pieces);
         array_shift($pieces);
         $nameSpace = implode('-', $pieces);
         $action = '/' . $this->_builderOptions['moduleName'] . '/form/' . Inflector::slug($nameSpace . '-' . $this->_builderOptions['className']);
     }
     $initFields = '';
     foreach ($fields as $field) {
         $type = $this->getType($field->getType());
         $fieldName = $field->getName();
         if ($fieldName == 'id' || $field->isPrimary()) {
             $initFields .= sprintf($this->templateShortFormSimpleField, $fieldName, 'Primary', Inflector::humanize($fieldName));
         } elseif ($fieldName == 'title' || $fieldName == 'name') {
             $initFields .= sprintf($this->templateShortFormSimpleField, $fieldName, 'Name', Inflector::humanize($fieldName));
         } elseif ($this->isEnum($this->_options['table_name'], $fieldName)) {
             $templateArray = "[%s]";
             $templateArrayPair = "%s => '%s',";
             $enumVals = $this->getEnumValues($this->_options['table_name'], $fieldName);
             $enumValsContent = '';
             $i = 0;
             foreach ($enumVals as $enumVal) {
                 $enumValsContent .= sprintf($templateArrayPair, $i, $enumVal);
                 $i++;
             }
             $templateArray = sprintf($templateArray, $enumValsContent);
             $initFields .= sprintf($this->templateSimpleFormComplexField, $fieldName, 'ArrayToSelect', Inflector::humanize($fieldName), $fieldName, $templateArray);
         } else {
             preg_match('/^(.*)\\_i{1}d{1}$/', $fieldName, $matches);
             if (!empty($matches)) {
                 $pieces = explode('_', $fieldName);
                 if (count($pieces) > 2) {
                     array_shift($pieces);
                 }
                 array_pop($pieces);
                 $camelize = function ($pieces) {
                     $c = array();
                     foreach ($pieces as $piece) {
                         $c[] = ucfirst($piece);
                     }
                     return $c;
                 };
                 $modelName = implode('\\', $camelize($pieces));
                 $fieldName = implode('_', $pieces);
                 $initFields .= sprintf($this->templateSimpleFormSimpleField, $fieldName, 'ManyToOne', Inflector::humanize($fieldName), $this->getNameSpace($table, self::OPTION_MODEL)[1] . '\\' . $modelName);
             } else {
                 $fieldComment = $fullFields[$fieldName]['COLUMN_COMMENT'];
                 $options = explode(";", $fieldComment);
                 if (count($options) < 2) {
                     $options = explode(",", $fieldComment);
                 }
                 $vals = [];
                 $colectionType = false;
                 if (count($rows) > 1) {
                     foreach ($options as $option) {
                         if (strpos($option, ":") === false) {
                             $colectionType = false;
                             break;
                         }
                         list($key, $value) = explode(":", $option);
                         $vals[$key] = $value;
                         $colectionType = true;
                     }
                 }
                 if ($colectionType) {
                     $templateArray = "[%s]";
                     $templateArrayPair = "'%s' => '%s'";
                     $valsContent = [];
                     foreach ($vals as $key => $value) {
                         $valsContent[] = sprintf($templateArrayPair, $key, $value);
                     }
                     $templateArray = sprintf($templateArray, implode(", ", $valsContent));
                     $initFields .= sprintf($this->templateSimpleFormComplexField, $fieldName, 'ArrayToSelect', Inflector::humanize($fieldName), $fieldName, $templateArray);
                 } else {
                     $initFields .= sprintf($this->templateSimpleFormSimpleField, $fieldName, $type, Inflector::humanize($fieldName), $fieldName);
                 }
             }
         }
     }
     // Set init fields method
     $templateInitFields = sprintf($this->templateSimpleFormInitFields, $initFields);
     // Prepare class content
     $content = '';
     switch ($this->type) {
         case self::TYPE_SIMPLE:
             $content .= sprintf($this->templateSimpleFormTitle, $this->_builderOptions['className']);
             $content .= sprintf($this->templateSimpleFormContainerModel, $this->getNameSpace($table, self::OPTION_MODEL)[1] . '\\' . $this->_builderOptions['className']);
             $content .= sprintf($this->templateSimpleFormAction, $action);
             $content .= $templateInitFields;
             break;
         case self::TYPE_EXTJS:
             $content .= sprintf($this->templateExtJsFormKey, Inflector::underscore($this->_builderOptions['className']));
             $content .= $this->templateExtJsFormModulePrefix;
             $content .= sprintf($this->templateExtJsFormModuleName, $this->_builderOptions['moduleName']);
             $content .= sprintf($this->templateSimpleFormTitle, $this->_builderOptions['className']);
             $content .= sprintf($this->templateSimpleFormContainerModel, $this->getNameSpace($table, self::OPTION_MODEL)[1] . '\\' . $this->_builderOptions['className']);
             $content .= $templateInitFields;
             break;
         default:
             $content .= sprintf($this->templateSimpleFormTitle, $this->_builderOptions['className']);
             $content .= sprintf($this->templateSimpleFormContainerModel, $this->getNameSpace($table, self::OPTION_MODEL)[1] . '\\' . $this->_builderOptions['className']);
             $content .= sprintf($this->templateSimpleFormAction, $action);
             $content .= $templateInitFields;
             break;
     }
     $code = sprintf($this->templateClassFullStack, '', $this->_builderOptions['namespace'], $this->_builderOptions['use'], $this->_builderOptions['head'], $this->_builderOptions['className'], $extends, $content);
     file_put_contents($this->_builderOptions['path'], $code);
     print Color::success('Form "' . $this->_builderOptions['className'] . '" was successfully created.') . PHP_EOL;
 }
Example #2
0
 public function build()
 {
     // Check name (table name)
     if (!$this->_options['table_name']) {
         throw new BuilderException("You must specify the table name");
     }
     // Get config
     $path = '';
     if (isset($this->_options['config_path'])) {
         $path = $this->_options['config_path'];
     } elseif (isset($this->_options['app_path'])) {
         $path = $this->_options['app_path'];
     } elseif (isset($this->_options['module_path'])) {
         $path = $this->_options['module_path'];
     }
     $config = $this->_getConfig($path);
     // build options
     $this->buildOptions($this->_options['table_name'], $config);
     // Prepare DB connection
     if (!$this->prepareDbConnection($config)) {
         return false;
     }
     // Check if table exist in database
     $table = $this->_options['table_name'];
     if ($this->db->tableExists($table, $config->database->dbname)) {
         $fields = $this->db->describeColumns($table, $config->database->dbname);
     } else {
         throw new BuilderException('Table "' . $table . '" does not exists');
     }
     // Set extender class template
     switch ($this->type) {
         case self::TYPE_SIMPLE:
         case self::TYPE_EXTJS:
         default:
             $extends = $this->templateSimpleModelExtends;
             break;
     }
     $attributes = array();
     $belongsTo = array();
     foreach ($fields as $field) {
         $type = $this->getPHPType($field->getType());
         $attributes[] = sprintf($this->templateEmptyAttribute, $type, 'public', $field->getName());
         // Build belongsTo relations
         preg_match('/^(.*)\\_i{1}d{1}$/', $field->getName(), $matches);
         if (!empty($matches)) {
             $belongsTo[] = sprintf($this->templateModelRelation, 'belongsTo', $matches[0], ucfirst($this->_builderOptions['moduleName']) . '\\Model\\' . Inflector::modelize($matches[1]), 'id', $this->_buildRelationOptions(['alias' => $this->getAlias($matches[1])]));
         }
         if ($field->getName() == 'id' || $field->isPrimary()) {
             $this->_options['primary_column'] = $field->getName();
             $this->_options['order_expr'] = $field->getName();
         }
         if ($field->getName() == 'title' || $field->getName() == 'name') {
             $this->_options['name_expr'] = $field->getName();
             $this->_options['order_expr'] = $field->getName();
         }
     }
     // Model::initialize() code
     $initializeCode = "";
     if (count($belongsTo) > 0) {
         foreach ($belongsTo as $rel) {
             $initializeCode .= $rel . "\n";
         }
     }
     // Join attributes to content
     $content = join('', $attributes);
     // Join engine properties
     if (isset($this->_options['primary_column'])) {
         $content .= sprintf($this->templateModelPrimaryColumn, $this->_options['primary_column']);
     }
     // Join engine name_expr
     if (isset($this->_options['name_expr'])) {
         $content .= sprintf($this->templateModelDefaultTitleColumn, $this->_options['name_expr']);
     }
     // Join engine attributes
     if (isset($this->_options['attributes']) && is_array($this->_options['attributes'])) {
         $content .= sprintf($this->templateModelAttribute, $this->_options['attributes']);
     }
     // Join engine orderExpr
     if (isset($this->_options['order_expr'])) {
         $content .= sprintf($this->templateModelOrderExpr, $this->_options['order_expr']);
     }
     // Join engine orderAsc
     if (isset($this->_options['order_asc']) && is_bool($this->_options['order_asc'])) {
         $content .= sprintf($this->templateModelOrder, $this->_options['order_asc']);
     } else {
         $content .= sprintf($this->templateModelOrder, 'true');
     }
     // Join initialize code to content
     $content .= '';
     if (!empty($initializeCode)) {
         $content .= sprintf($this->templateInitialize, $initializeCode);
     }
     // Join Model::getSource() code to content
     $content .= sprintf($this->templateModelGetSource, $this->_options['table_name']);
     if (isset($this->_options['mapColumn'])) {
         $content .= $this->_genColumnMapCode($fields);
     }
     $code = sprintf($this->templateClassFullStack, '', $this->_builderOptions['namespace'], $this->_builderOptions['use'], $this->_builderOptions['head'], $this->_builderOptions['className'], $extends, $content);
     file_put_contents($this->_builderOptions['path'], $code);
     print Color::success('Model "' . $this->_builderOptions['className'] . '" was successfully created.') . PHP_EOL;
 }