Пример #1
0
 /**
  * Fetch the compiled template for a model
  *
  * @param  string $template Path to template
  * @param  string $className
  *
  * @return string Compiled template
  */
 protected function getTemplate($template, $className)
 {
     $this->template = $this->file->get($template);
     $relationModelList = GeneratorsServiceProvider::getRelationsModelVarsList(GeneratorsServiceProvider::splitFields($this->cache->getFields(), true));
     // Replace template vars
     $modelVars = GeneratorsServiceProvider::getModelVars($this->cache->getModelName());
     $this->template = GeneratorsServiceProvider::replaceModelVars($this->template, $modelVars);
     $fields = $this->cache->getFields() ?: [];
     $fields = GeneratorsServiceProvider::splitFields(implode(',', $fields), SCOPED_EXPLODE_WANT_ID_RECORD);
     $this->template = GeneratorsServiceProvider::replaceTemplateLines($this->template, '{{translations:line}}', function ($line, $fieldVar) use($fields, $relationModelList) {
         $fieldTexts = [];
         foreach ($fields + ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'] as $field => $type) {
             if (array_key_exists($field, $relationModelList)) {
                 // add the foreign model translation
                 $foreignName = $relationModelList[$field]['dash-model'];
                 $foreignNameText = $relationModelList[$field]['Space Model'];
                 $fieldTexts[] = str_replace($fieldVar, "'{$foreignName}' => '{$foreignNameText}',", $line);
                 if (trim_suffix($field, "_id") !== $foreignName) {
                     $foreignName = trim_suffix($field, "_id");
                     $foreignNameText = GeneratorsServiceProvider::getModelVars($foreignName)['Space Model'];
                     $fieldTexts[] = str_replace($fieldVar, "'{$foreignName}' => '{$foreignNameText}',", $line);
                 }
             }
             if (is_array($type) && ($bitset = hasIt($type, 'bitset', HASIT_WANT_PREFIX | HASIT_WANT_VALUE))) {
                 $params = preg_match('/bitset\\((.*)\\)/', $bitset, $matches) ? $matches[1] : '';
                 if ($params === '') {
                     $params = $field;
                 }
                 $params = explode(',', $params);
                 foreach ($params as $param) {
                     $bitFieldNameText = GeneratorsServiceProvider::getModelVars($param)['Space Model'];
                     $fieldTexts[] = str_replace($fieldVar, "'{$param}' => '{$bitFieldNameText}',", $line);
                 }
             }
             $modelVars = GeneratorsServiceProvider::getModelVars($field);
             $fieldNameTrans = $field !== Pluralizer::plural($field) ? $modelVars['Space Model'] : $modelVars['Space Models'];
             $fieldTexts[] = str_replace($fieldVar, "'{$field}' => '{$fieldNameTrans}',", $line);
         }
         sort($fieldTexts);
         return implode("\n", $fieldTexts) . "\n";
     });
     $this->template = $this->replaceStandardParams($this->template);
     return $this->template;
 }
Пример #2
0
    /**
     * Get template for a scaffold
     *
     * @param $className
     *
     * @return string
     */
    protected function getScaffoldedModel($className)
    {
        if (!($fields = $this->cache->getFields())) {
            return str_replace('{{rules}}', '', $this->template);
        }
        $template = $this->template;
        $template = str_replace('{{__construct}}', <<<'PHP'
protected $table = '{{snake_models}}';

    public function __construct($attributes = [])
    {
        {{prefixdef}}$this->table = {{prefix}}'{{snake_models}}';
        parent::__construct($attributes);
    }

PHP
, $this->template);
        $prefix = $this->options('prefix');
        $package = $this->options('bench');
        $template = GeneratorsServiceProvider::replacePrefixTemplate($prefix, $package, $template);
        $fieldRawText = implode(',', $fields);
        $fields = GeneratorsServiceProvider::splitFields($fieldRawText, true);
        $modelVars = GeneratorsServiceProvider::getModelVars($this->cache->getModelName());
        // Replace template vars
        $template = GeneratorsServiceProvider::replaceModelVars($template, $modelVars);
        $relationModelList = GeneratorsServiceProvider::getRelationsModelVarsList($fields);
        if (strpos($template, '{{relations') !== false) {
            $relations = '';
            foreach ($fields as $field) {
                // add foreign keys
                $name = $field->name;
                if (array_key_exists($name, $relationModelList)) {
                    $foreignModelVars = $relationModelList[$name];
                    $relations .= <<<PHP
    /**
     * @return \\Illuminate\\Database\\Eloquent\\Relations\\Relation
     */
    public
    function {$foreignModelVars['field_no_id']}()
    {
        return \$this->belongsTo('{{app_namespace}}\\{$foreignModelVars['CamelModel']}', '{$name}', '{$foreignModelVars['id']}');
    }


PHP;
                }
            }
            $template = str_replace('{{relations}}', $relations, $template);
            if ($relationModelList) {
                $relationsVars = [];
                foreach ($relationModelList as $name => $relationModelVars) {
                    foreach ($relationModelVars as $relationModel => $relationModelVar) {
                        // append
                        if (array_key_exists($relationModel, $relationsVars)) {
                            $relationsVars[$relationModel] .= ", '{$relationModelVar}'";
                        } else {
                            $relationsVars[$relationModel] = "'{$relationModelVar}'";
                        }
                    }
                }
                $template = GeneratorsServiceProvider::replaceModelVars($template, $relationsVars, '{{relations:', '}}');
                $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{relation:line}}', function ($line, $fieldVar) use($fields, $relationModelList) {
                    $fieldText = '';
                    $line = str_replace($fieldVar, '', $line);
                    foreach ($fields as $field) {
                        // add foreign keys
                        $name = $field->name;
                        if (array_key_exists($name, $relationModelList)) {
                            $relationsVars = $relationModelList[$name];
                            $fieldText .= GeneratorsServiceProvider::replaceModelVars($line, $relationsVars, '{{relation:', '}}') . "\n";
                        }
                    }
                    return $fieldText;
                });
            } else {
                $emptyVars = $modelVars;
                array_walk($emptyVars, function (&$val) {
                    $val = '';
                });
                $template = GeneratorsServiceProvider::replaceModelVars($template, $emptyVars, '{{relations:', '}}');
                $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{relation:line}}', function ($line, $fieldVar) {
                    return '';
                });
            }
        }
        if (strpos($template, '{{field:unique}}') !== false) {
            $uniqueField = '';
            $keyindices = [0];
            // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list
            $fieldIndex = 0;
            foreach ($fields as $field) {
                $fieldIndex++;
                foreach ($field->options as $option) {
                    $isKey = null;
                    $isUnique = null;
                    if (($isUnique = strpos($option, 'unique') === 0) || ($isKey = strpos($option, 'keyindex') === 0)) {
                        MigrationGenerator::processIndexOption($keyindices, $option, $field->name, $fieldIndex);
                    }
                }
            }
            // we now have the key indices, we can take the first one
            if (count($keyindices) >= 1) {
                // skip the auto counter
                array_shift($keyindices);
                $indexValues = array_values($keyindices);
                if ($indexValues) {
                    $anyIndex = $indexValues[0];
                    $uniqueField = "'" . implode("','", array_values($anyIndex)) . "'";
                }
            } else {
                foreach ($fields as $field) {
                    if (hasIt($field->options, 'unique', HASIT_WANT_PREFIX)) {
                        $uniqueField = "'" . $field->name . "'";
                        break;
                    }
                }
            }
            if ($uniqueField === '') {
                $uniqueField = "'id'";
            }
            $template = str_replace('{{field:unique}}', $uniqueField, $template);
        }
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{field:line}}', function ($line, $fieldVar) use($fields) {
            $fieldText = '';
            foreach ($fields as $field) {
                $fieldText .= str_replace($fieldVar, $field->name, $line) . "\n";
            }
            if ($fieldText === '') {
                $fieldText = "''";
            }
            return $fieldText;
        });
        // add only unique lines
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{relations:line:with_model}}', function ($line, $fieldVar) use($fields, $relationModelList, $modelVars) {
            // we don't need the marker
            $line = str_replace($fieldVar, '', $line);
            $fieldText = '';
            $fieldTexts = [];
            if ($modelVars) {
                // add model
                $text = GeneratorsServiceProvider::replaceModelVars($line, $modelVars, '{{relations:', '}}') . "\n";
                if (array_search($text, $fieldTexts) === false) {
                    $fieldText .= $text;
                    $fieldTexts[] = $text;
                }
            }
            foreach ($fields as $field => $type) {
                // here we override for foreign keys
                if (array_key_exists($field, $relationModelList)) {
                    $relationModelVars = $relationModelList[$field];
                    // Replace template vars
                    $text = GeneratorsServiceProvider::replaceModelVars($line, $relationModelVars, '{{relations:', '}}') . "\n";
                    if (array_search($text, $fieldTexts) === false) {
                        $fieldText .= $text;
                        $fieldTexts[] = $text;
                    }
                }
            }
            return $fieldText;
        });
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{field:line:bool}}', function ($line, $fieldVar) use($fields, $modelVars) {
            $fieldText = '';
            foreach ($fields as $field) {
                if (GeneratorsServiceProvider::isFieldBoolean($field->type)) {
                    $fieldText .= str_replace($fieldVar, $field->name, $line) . "\n";
                }
            }
            return $fieldText;
        });
        $rules = [];
        $guarded = [];
        $hidden = [];
        $notrail = [];
        $notrailonly = [];
        $dates = [];
        $defaults = [];
        $fieldText = '';
        $bitsets = [];
        foreach ($fields as $field) {
            if ($field->name !== 'id') {
                if ($fieldText) {
                    $fieldText .= ', ';
                }
                $fieldText .= $field->name . ":" . implode(':', $field->options);
            }
            if (GeneratorsServiceProvider::isFieldDateTimeType($field->type)) {
                $dates[] = "'{$field->name}'";
            }
            if (!hasIt($field->options, ['hidden', 'guarded'], HASIT_WANT_PREFIX)) {
                $ruleBits = [];
                if ($rule = hasIt($field->options, 'rule', HASIT_WANT_PREFIX | HASIT_WANT_VALUE)) {
                    $rule = substr($rule, strlen('rule('), -1);
                    $ruleBits[] = $rule;
                }
                if ($default = hasIt($field->options, 'default', HASIT_WANT_PREFIX | HASIT_WANT_VALUE)) {
                    $default = substr($default, strlen('default('), -1);
                    $defaults[$field->name] = $default;
                } elseif (hasIt($field->options, 'nullable', HASIT_WANT_PREFIX)) {
                    $defaults[$field->name] = null;
                }
                if (array_search('sometimes', $ruleBits) === false && !array_key_exists($field->name, $defaults) && !GeneratorsServiceProvider::isFieldBoolean($field->type) && !hasIt($field->options, ['nullable', 'hidden', 'guarded'], HASIT_WANT_PREFIX)) {
                    $ruleBits[] = 'required';
                }
                if ($field->name === 'email') {
                    array_unshift($ruleBits, 'email');
                }
                $ruleType = GeneratorsServiceProvider::getFieldRuleType($field->type);
                if ($ruleType) {
                    array_unshift($ruleBits, $ruleType);
                }
                if (hasIt($field->options, ['unique'], HASIT_WANT_PREFIX)) {
                    $ruleBits[] = "unique:{$modelVars['snake_models']},{$field->name},{{id}}";
                }
                // here we override for foreign keys
                if (array_key_exists($field->name, $relationModelList)) {
                    $relationsVars = $relationModelList[$field->name];
                    $table_name = $relationsVars['snake_models'];
                    $ruleBits[] = "exists:{$table_name},id";
                }
                // Laravel 5.3 compatibility requirement
                if ($ruleBits && !array_search('required', $ruleBits) && array_key_exists($field->name, $defaults) && ($defaults[$field->name] === null || strtolower($defaults[$field->name]) === 'null')) {
                    array_unshift($ruleBits, 'nullable');
                }
                $rules[$field->name] = "'{$field->name}' => '" . implode('|', $ruleBits) . "'";
            }
            // bitset is an option so that the size of the field can be set with normal type
            if ($bitset = hasIt($field->options, 'bitset', HASIT_WANT_PREFIX | HASIT_WANT_VALUE)) {
                $bitsets[$field->name] = [];
                $params = preg_match('/bitset\\((.*)\\)/', $bitset, $matches) ? $matches[1] : '';
                if ($params === '') {
                    $params = $field->name;
                }
                $params = explode(',', $params);
                $bitMask = 1;
                foreach ($params as $param) {
                    $bitsets[$field->name][trim($param)] = $bitMask;
                    $bitMask <<= 1;
                }
            }
            if (hasIt($field->options, 'notrail', HASIT_WANT_PREFIX)) {
                $notrail[] = "'{$field->name}'";
            }
            if (hasIt($field->options, 'hidden', HASIT_WANT_PREFIX)) {
                $hidden[] = "'{$field->name}'";
            }
            if (hasIt($field->options, 'guarded', HASIT_WANT_PREFIX)) {
                $guarded[] = "'{$field->name}'";
            }
            if (hasIt($field->options, 'notrailonly', HASIT_WANT_PREFIX)) {
                $notrailonly[] = "'{$field->name}'";
            }
        }
        $defaultValues = [];
        foreach ($defaults as $field => $value) {
            if ($value === null || strtolower($value) === 'null') {
                $value = 'null';
            } elseif (!(GeneratorsServiceProvider::isFieldNumeric($fields[$field]->type) || GeneratorsServiceProvider::isFieldBoolean($fields[$field]->type))) {
                if (!(str_starts_with($value, "'") && str_ends_with($value, "'") || str_starts_with($value, '"') && str_ends_with($value, '"'))) {
                    $value = str_replace("'", "\\'", $value);
                    $value = "'{$value}'";
                }
            }
            $defaultValues[] = "'{$field}' => {$value}";
        }
        if (strpos($template, '{{bitset:') !== false) {
            // create the data for processing bitsets
            $bitsetData = '';
            $bitsetFields = [];
            $bitsetMaps = [];
            $bitsetAttributes = '';
            foreach ($bitsets as $bitset => $bits) {
                $bitsetName = strtoupper($bitset);
                $bitsetData .= "const {$bitsetName}_NONE = '';\n";
                foreach ($bits as $bit => $bitMask) {
                    $bitName = strtoupper($bit);
                    $bitsetData .= "\tconst {$bitsetName}_{$bitName} = '{$bit}';\n";
                    $bitModelVars = GeneratorsServiceProvider::getModelVars($bit);
                    $bitAttribute = $bitModelVars['CamelModel'];
                    $bitsetAttributes .= <<<PHP
    /**
     * @return boolean
     */
    public
    function get{$bitAttribute}Attribute()
    {
        return !!(\$this->{$bitset} & self::{$bitsetName}_{$bitName}_MASK);
    }

    /**
     * @param boolean \$value
     */
    public
    function set{$bitAttribute}Attribute(\$value)
    {
        if (\$value) {
            \$this->{$bitset} |= self::{$bitsetName}_{$bitName}_MASK;
        } else {
            \$this->{$bitset} &= ~self::{$bitsetName}_{$bitName}_MASK;
        }
    }

PHP;
                }
                $bitsetData .= "\n\tconst {$bitsetName}_NONE_MASK = 0;\n";
                foreach ($bits as $bit => $bitMask) {
                    $bitName = strtoupper($bit);
                    $bitsetData .= "\tconst {$bitsetName}_{$bitName}_MASK = {$bitMask};\n";
                }
                $bitsetData .= "\n\tpublic static \${$bitset}_bitset = [\n";
                foreach ($bits as $bit => $bitMask) {
                    $bitName = strtoupper($bit);
                    $bitsetData .= "\t\tself::{$bitsetName}_{$bitName} => self::{$bitsetName}_{$bitName}_MASK,\n";
                }
                $bitsetData .= "\t];\n";
                $bitsetFields[] = "'{$bitset}'";
                $bitsetMaps[] = "'{$bitset}' => self::\${$bitset}_bitset";
            }
            $template = str_replace('{{bitset:data}}', $bitsetData, $template);
            $template = str_replace('{{bitset:fields}}', implode(',', $bitsetFields), $template);
            $template = str_replace('{{bitset:maps}}', implode(',', $bitsetMaps), $template);
            $template = str_replace('{{bitset:attributes}}', $bitsetAttributes, $template);
            if (strpos($template, '{{bitset:line}}') !== false) {
                $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{bitset:line}}', function ($line, $fieldVar) use($fields, $modelVars) {
                    $line = str_replace($fieldVar, '', $line);
                    $text = '';
                    foreach ($fields as $field => $type) {
                        if (hasIt($type->options, 'bitset', HASIT_WANT_PREFIX | HASIT_WANT_VALUE)) {
                            $fieldModelVars = GeneratorsServiceProvider::getModelVars($field);
                            $allVars = array_merge($modelVars, $fieldModelVars);
                            $allVars['field'] = $field;
                            $text .= GeneratorsServiceProvider::replaceModelVars($line, $allVars, '{{bitset:', '}}') . "\n";
                        }
                    }
                    return $text;
                });
            }
        }
        $template = str_replace('{{fields}}', $fieldRawText, $template);
        $template = str_replace('{{rules}}', $this->implodeOneLineExpansion($rules), $template);
        $template = str_replace('{{hidden}}', $this->implodeOneLineExpansion($hidden), $template);
        $template = str_replace('{{guarded}}', $this->implodeOneLineExpansion($guarded), $template);
        $template = str_replace('{{dates}}', $this->implodeOneLineExpansion($dates), $template);
        $template = str_replace('{{notrail}}', $this->implodeOneLineExpansion($notrail), $template);
        $template = str_replace('{{notrailonly}}', $this->implodeOneLineExpansion($notrailonly), $template);
        $template = str_replace('{{defaults}}', $this->implodeOneLineExpansion($defaultValues), $template);
        return $template;
    }
Пример #3
0
    protected function replaceLines($template, $modelVars)
    {
        $relationModelList = GeneratorsServiceProvider::getRelationsModelVarsList(GeneratorsServiceProvider::splitFields($this->cache->getFields(), true));
        $fields = GeneratorsServiceProvider::splitFields($this->cache->getFields(), SCOPED_EXPLODE_WANT_ID_RECORD | SCOPED_EXPLODE_WANT_TEXT);
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{field:line}}', function ($line, $fieldVar) use($fields) {
            $fieldText = '';
            foreach ($fields as $field => $type) {
                $fieldText .= str_replace($fieldVar, $field, $line) . "\n";
            }
            if ($fieldText === '') {
                $fieldText = "''";
            }
            return $fieldText;
        });
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{field:line:bool}}', function ($line, $fieldVar) use($fields) {
            $fieldText = '';
            foreach ($fields as $field => $type) {
                if (preg_match('/\\bboolean\\b/', $type)) {
                    $fieldText .= str_replace('{{field:line:bool}}', $field, $line) . "\n";
                }
            }
            return $fieldText;
        });
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{field:line:nobool}}', function ($line, $fieldVar) use($fields) {
            $fieldText = '';
            foreach ($fields as $field => $type) {
                if (!preg_match('/\\bboolean\\b/', $type)) {
                    $fieldText .= str_replace('{{field:line:nobool}}', $field, $line) . "\n";
                }
            }
            return $fieldText;
        });
        // add only unique lines
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{relations:line}}', function ($line, $fieldVar) use($fields, $relationModelList) {
            // we don't need the marker
            $line = str_replace($fieldVar, '', $line);
            $fieldText = '';
            $fieldTexts = [];
            foreach ($fields as $field => $type) {
                // here we override for foreign keys
                if (array_key_exists($field, $relationModelList)) {
                    $modelVars = $relationModelList[$field];
                    // Replace template vars
                    $text = GeneratorsServiceProvider::replaceModelVars($line, $modelVars, '{{relations:', '}}') . "\n";
                    if (array_search($text, $fieldTexts) === false) {
                        $fieldText .= $text;
                        $fieldTexts[] = $text;
                    }
                }
            }
            return $fieldText;
        });
        // add only unique lines
        $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{relations:line:with_model}}', function ($line, $fieldVar) use($fields, $relationModelList, $modelVars) {
            // we don't need the marker
            $line = str_replace($fieldVar, '', $line);
            $fieldText = '';
            $fieldTexts = [];
            if ($modelVars) {
                // add model
                $text = GeneratorsServiceProvider::replaceModelVars($line, $modelVars, '{{relations:', '}}') . "\n";
                if (array_search($text, $fieldTexts) === false) {
                    $fieldText .= $text;
                    $fieldTexts[] = $text;
                }
            }
            foreach ($fields as $field => $type) {
                // here we override for foreign keys
                if (array_key_exists($field, $relationModelList)) {
                    $relationModelVars = $relationModelList[$field];
                    // Replace template vars
                    $text = GeneratorsServiceProvider::replaceModelVars($line, $relationModelVars, '{{relations:', '}}') . "\n";
                    if (array_search($text, $fieldTexts) === false) {
                        $fieldText .= $text;
                        $fieldTexts[] = $text;
                    }
                }
            }
            return $fieldText;
        });
        if (strpos($template, '{{relations') !== false) {
            $relations = '';
            $foreignModels = [];
            foreach ($fields as $field => $type) {
                if (array_key_exists($field, $relationModelList)) {
                    $relationModelVars = $relationModelList[$field];
                    if (array_search($relationModelVars['camelModels'], $foreignModels) === false) {
                        $foreignField = trim_suffix($field, "_id");
                        $foreignModels[] = $relationModelVars['camelModels'];
                        $foreignFieldList = "'{$relationModelVars['name']}', '{$relationModelVars['id']}'";
                        $relations .= <<<PHP
    /**
     * @param \\{{app_namespace}}\\{$modelVars['CamelModel']} \${$modelVars['camelModel']}
     *
     * @return array {$relationModelVars['CamelModel']}
     */
    public
    function {$relationModelVars['camelModels']}List({$modelVars['CamelModel']} \${$modelVars['camelModel']} = null)
    {
        // fill the foreign list for {$relationModelVars['CamelModel']}
        if (\${$modelVars['camelModel']} !== null) {
            \${$relationModelVars['camelModels']} = !\${$modelVars['camelModel']}->{$foreignField} ? [] : [ \${$modelVars['camelModel']}->{$foreignField}->{$relationModelVars['id']} => \${$modelVars['camelModel']}->{$foreignField}->{$relationModelVars['name']} ];
        } else {
            \${$relationModelVars['camelModels']} = {$relationModelVars['CamelModel']}::query()->get([{$foreignFieldList}])->pluck({$foreignFieldList})->all();
        }
        return \${$relationModelVars['camelModels']};
    }

PHP;
                    }
                }
            }
            $template = str_replace('{{relations}}', $relations, $template);
            if ($relationModelList) {
                $relationsVars = [];
                foreach ($relationModelList as $fieldName => $relationModelVars) {
                    foreach ($relationModelVars as $relationModel => $relationModelVar) {
                        // append
                        if (array_key_exists($relationModel, $relationsVars)) {
                            $relationsVars[$relationModel] .= ", '{$relationModelVar}' => \${$relationModelVar}";
                        } else {
                            $relationsVars[$relationModel] = "'{$relationModelVar}' => \${$relationModelVar}";
                        }
                    }
                }
                $template = GeneratorsServiceProvider::replaceModelVars($template, $relationsVars, '{{relations:compact:', '}}');
            }
        }
        if (strpos($template, '{{auto}}') !== false) {
            $relations = '';
            foreach ($fields as $field => $type) {
                $options = scopedExplode(':', ['(' => ')', '[' => ']', '{' => '}'], $type, null);
                foreach ($options as $option) {
                    if (str_starts_with($option, 'auto')) {
                        $auto = substr($option, 5, -1);
                        $relations .= <<<PHP
        \$input['{$field}'] = {$auto};

PHP;
                    }
                }
            }
            $template = str_replace('{{auto}}', $relations, $template);
        }
        if (strpos($template, '{{bitset:line}}') !== false) {
            $template = GeneratorsServiceProvider::replaceTemplateLines($template, '{{bitset:line}}', function ($line, $fieldVar) use($fields, $modelVars) {
                $line = str_replace($fieldVar, '', $line);
                $text = '';
                foreach ($fields as $field => $type) {
                    if (preg_match('/\\bbitset\\b/', $type)) {
                        $fieldModelVars = GeneratorsServiceProvider::getModelVars($field);
                        $allVars = array_merge($modelVars, $fieldModelVars);
                        $allVars['field'] = $field;
                        $text .= GeneratorsServiceProvider::replaceModelVars($line, $allVars, '{{bitset:', '}}') . "\n";
                    }
                }
                return $text;
            });
        }
        return $template;
    }
Пример #4
0
    /**
     * Add Laravel methods, as string,
     * for the fields
     *
     * @param string $modelVars
     *
     * @param bool   $disable
     *
     * @param bool   $onlyBoolean
     * @param bool   $noBoolean
     * @param bool   $useOp
     *
     * @return string
     * @internal param $model
     */
    public function makeFormElements($modelVars, $disable = false, $onlyBoolean = false, $noBoolean = false, $useOp = false, $filterRows = false)
    {
        $formMethods = [];
        $relationModelList = GeneratorsServiceProvider::getRelationsModelVarsList(GeneratorsServiceProvider::splitFields($this->cache->getFields(), true));
        $fields = GeneratorsServiceProvider::splitFields($this->cache->getFields(), SCOPED_EXPLODE_WANT_ID_TYPE_OPTIONS | SCOPED_EXPLODE_WANT_TEXT);
        $models = $modelVars['models'];
        $model = $modelVars['model'];
        $camelModel = $modelVars['camelModel'];
        $CamelModel = $modelVars['CamelModel'];
        $narrowText = " input-narrow";
        $dash_models = $modelVars['dash-models'];
        foreach ($fields as $name => $values) {
            $type = $values['type'];
            $options = $values['options'];
            $nameModelVars = GeneratorsServiceProvider::getModelVars($name);
            if (strpos($options, 'hidden') !== false) {
                continue;
            }
            $nullable = strpos($options, 'nullable') !== false;
            if (strpos($options, 'guarded') !== false) {
                if ($useOp) {
                    $readonly = true ? "['readonly', " : '[';
                    $readonlyHalf = '[true ? \'readonly\' : ';
                    $readonlyClose = ']';
                    $disabled = true ? "'disabled', " : '';
                } else {
                    $readonly = $disable ? "['readonly', " : '[';
                    $readonlyHalf = "[{$disable} ? 'readonly' : ";
                    $readonlyClose = ']';
                    $disabled = $disable ? "'disabled', " : '';
                }
            } else {
                if ($useOp) {
                    $readonly = '[isViewOp($op) ? \'readonly\' : \'\',';
                    $readonlyHalf = '[isViewOp($op) ? \'readonly\' : ';
                    $readonlyClose = ']';
                    $disabled = 'isViewOp($op) ? \'disabled\' : \'\',';
                } else {
                    $readonly = $disable ? "['readonly', " : '[';
                    $readonlyHalf = "[{$disable} ? 'readonly' : ";
                    $readonlyClose = ']';
                    $disabled = $disable ? "'disabled', " : '';
                }
            }
            $limit = null;
            $useShort = false;
            if (str_contains($type, '[')) {
                if (preg_match('/([^\\[]+?)\\[(\\d+)(?:\\,\\d+)?\\]/', $type, $matches)) {
                    $type = $matches[1];
                    // string
                    $limit = $matches[2];
                    // 50{,...,}
                }
            }
            if (preg_match('/\\btextarea\\b/', $options)) {
                // treat it as text with multiple rows
                $type = 'text';
            }
            $foreignTable = '';
            if (preg_match('/\\btable\\(([^)]+)\\)\\b/', $options, $matches)) {
                // treat it as text with multiple rows
                $foreignTable = $matches[1];
            }
            $is_bitset_field = preg_match('/\\bbitset\\b/', $options);
            if (($type === 'boolean' || $is_bitset_field) && $noBoolean) {
                continue;
            }
            if ($type !== 'boolean' && !$is_bitset_field && $onlyBoolean) {
                continue;
            }
            $trans_name = $name;
            $labelName = $trans_name;
            $labelGroup = $dash_models;
            $afterElement = '';
            $afterElementFilter = '';
            $wrapRow = !$onlyBoolean;
            $inputNarrow = GeneratorsServiceProvider::isFieldNumeric($type) || $type === 'string' && $limit < 32 ? $narrowText : '';
            if ($is_bitset_field) {
                if ($filterRows) {
                    $formMethods[] = <<<PHP
                @foreach({{app_namespace}}\\{$CamelModel}::\${$name}_bitset as \$type => \$flag)
                <td>{!! \\Form::select(\$type, ['' => '&nbsp;', '0' => '0', '1' => '1', ], Input::get(\$type), ['form' => 'filter-{$models}', 'class' => 'form-control', ]) !!}</td>
                @endforeach
PHP;
                } else {
                    if ($wrapRow) {
                        $formMethods[] = <<<PHP
@foreach({{app_namespace}}\\{$CamelModel}::\${$name}_bitset as \$type => \$flag)
            <div class="row">
                <label>
                    {!! Form::checkbox(\$type, 1, Input::old(\$type), [{$disabled}]) !!} @lang('{$labelGroup}.{$labelName}')&nbsp;&nbsp;
                </label>
            </div>
                @endforeach
PHP;
                    } else {
                        $formMethods[] = <<<PHP
@foreach({{app_namespace}}\\{$CamelModel}::\${$name}_bitset as \$type => \$flag)
                <label>
                    {!! Form::checkbox(\$type, 1, Input::old(\$type), [{$disabled}]) !!} @lang('{$labelGroup}.{$labelName}')&nbsp;&nbsp;
                </label>
                @endforeach
PHP;
                    }
                }
            } else {
                switch ($type) {
                    case 'mediumInteger':
                    case 'smallInteger':
                    case 'tinyInteger':
                        $element = "{!! Form::input('number', '{$name}', Input::old('{$name}'), {$readonly}'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), {$readonlyClose}) !!}";
                        $elementFilter = "{!! Form::input('number', '{$name}', Input::get('{$name}'), ['form' => 'filter-{$models}', 'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), ]) !!}";
                        break;
                    case 'bigInteger':
                    case 'integer':
                        if (array_key_exists($name, $relationModelList)) {
                            // assume foreign key
                            $afterElement = "";
                            $foreignModelVars = $relationModelList[$name];
                            $foreignModels = $foreignModelVars['camelModels'];
                            $foreignmodels = $foreignModelVars['models'];
                            $foreign_model = $foreignModelVars['snake_model'];
                            $foreign_models = $foreignModelVars['snake_models'];
                            $foreign_display = $foreignModelVars['name'];
                            $plainName = trim_suffix($name, '_id');
                            $labelName = $plainName;
                            $element = "{!! Form::select('{$name}', [0 => ''], \${$foreignModels},  Input::old('{$name}'), [{$disabled}'class' => 'form-control', ]) !!}";
                            if ($nullable) {
                                $element .= "\n{!! Form::text('{$plainName}', \${$camelModel} ? (\${$camelModel}->{$plainName} ? \${$camelModel}->{$plainName}->{$foreign_display} : '') : '', {$readonlyHalf}'data-vsch_completion'=>'{$foreign_models}:{$foreign_display};id:{$name}','class' => 'form-control', {$readonlyClose}) !!}";
                            } else {
                                $element .= "\n{!! Form::text('{$plainName}', \${$camelModel} ? \${$camelModel}->{$plainName}->{$foreign_display} : '', {$readonlyHalf}'data-vsch_completion'=>'{$foreign_models}:{$foreign_display};id:{$name}','class' => 'form-control', {$readonlyClose}) !!}";
                            }
                            $elementFilter = "{!! Form::text('{$foreign_model}', Input::get('{$foreign_model}'), ['form' => 'filter-{$models}', 'data-vsch_completion'=>'{$foreign_models}:{$foreign_display};id:{$name}','class'=>'form-control', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), ]) !!}";
                            if ($filterRows) {
                                $afterElementFilter .= "{!! Form::hidden('{$name}', Input::old('{$name}'), ['form' => 'filter-{$models}', 'id'=>'{$name}']) !!}";
                            } else {
                                $afterElementFilter .= "{!! Form::hidden('{$name}', Input::old('{$name}'), ['id'=>'{$name}']) !!}";
                            }
                            //$labelName = $foreignModelVars['model'];
                            //$labelGroup = $foreignModelVars['dash-models'];
                            if ($useOp) {
                                $afterElement .= "\n\t\n@if(\$op === 'create' || \$op === 'edit')";
                            }
                            $afterElement .= "\n\t<div class='form-group col-sm-1'>\n\t\t\t<label>&nbsp;</label>\n\t\t\t<br>\n\t\t\t<a href=\"@route('{$foreignmodels}.create')\" @linkAsButton('warning')>@lang('messages.create')</a>\n</div>";
                            if ($useOp) {
                                $afterElement .= "\n@endif";
                            }
                            $element .= "\n\t\t\t" . $afterElementFilter;
                        } else {
                            $element = "{!! Form::input('number', '{$name}', Input::old('{$name}'), {$readonly}'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), {$readonlyClose}) !!}";
                            $elementFilter = "{!! Form::input('number', '{$name}', Input::get('{$name}'), ['form' => 'filter-{$models}', 'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), ]) !!}";
                        }
                        break;
                    case 'text':
                        $limit = empty($limit) ? 256 : $limit;
                        $rowAttr = (int) ($limit / 64) ?: 1;
                        $element = "{!! Form::textarea('{$name}', Input::old('{$name}'), {$readonly}'class'=>'form-control', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), 'rows'=>'{$rowAttr}', {$readonlyClose}) !!}";
                        $elementFilter = "{!! Form::text('{$name}', Input::get('{$name}'), ['form' => 'filter-{$models}', 'class'=>'form-control', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), ]) !!}";
                        break;
                    case 'boolean':
                        $element = "{!! Form::checkbox('{$name}', 1, Input::old('{$name}'), [{$disabled}]) !!}";
                        $elementFilter = "{!! Form::select('{$name}', ['' => '&nbsp;', '0' => '0', '1' => '1', ], Input::get('{$name}'), ['form' => 'filter-{$models}', 'class' => 'form-control', ]) !!}";
                        $useShort = true;
                        break;
                    case 'date':
                    case 'dateTime':
                        $element = <<<HTML
<div class="input-group input-group-sm date">
    {!! Form::text('{$name}', Input::old('{$name}'), {$readonly}'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), {$readonlyClose}) !!}
    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar" aria-hidden="true"></span></span>
</div>
HTML;
                        $elementFilter = <<<HTML
<div class="input-group date">
    {!! Form::text('{$name}', Input::get('{$name}'), ['form' => 'filter-{$models}', 'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), ]) !!}
    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar" aria-hidden="true"></span></span>
</div>
HTML;
                        break;
                    case 'decimal':
                    case 'double':
                    case 'float':
                    case 'time':
                    case 'string':
                    default:
                        $element = "{!! Form::text('{$name}', Input::old('{$name}'), {$readonly}'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), {$readonlyClose}) !!}";
                        $elementFilter = "{!! Form::text('{$name}', Input::get('{$name}'), ['form' => 'filter-{$models}', 'class'=>'form-control{$inputNarrow}', 'placeholder'=>noEditTrans('{$dash_models}.{$trans_name}'), ]) !!}";
                        break;
                }
                if ($filterRows) {
                    $afterElementFilter = $afterElementFilter ? "\n\t\t\t\t" . $afterElementFilter : $afterElementFilter;
                    $frag = "\t\t\t\t<td>{$elementFilter}{$afterElementFilter}</td>";
                } elseif ($useShort) {
                    if ($wrapRow) {
                        $frag = <<<EOT
        <div class="row">
            <label>
                  {$element} @lang('{$labelGroup}.{$labelName}')
                  &nbsp;&nbsp;
            </label>{$afterElement}
        </div>

EOT;
                    } else {
                        $frag = <<<EOT
            <label>
                  {$element} @lang('{$labelGroup}.{$labelName}')
                  &nbsp;&nbsp;
            </label>{$afterElement}
EOT;
                    }
                } else {
                    if ($wrapRow) {
                        $frag = <<<EOT
        <div class="row">
            <div class="form-group col-sm-3">
                <label for="{$name}">@lang('{$labelGroup}.{$labelName}'):</label>
                  {$element}
            </div>
        </div>
        {$afterElement}
EOT;
                    } else {
                        $frag = <<<EOT
        <div class="form-group">
            <label for="{$name}">@lang('{$labelGroup}.{$labelName}'):</label>
              {$element}{$afterElement}
        </div>

EOT;
                    }
                }
                $formMethods[] = $frag;
            }
        }
        return implode(PHP_EOL, $formMethods);
    }
Пример #5
0
 /**
  * If Schema fields are specified, parse
  * them into an array of objects.
  *
  * So: name:string, age:integer
  * Becomes: [ ((object)['name' => 'string'], (object)['age' => 'integer'] ]
  *
  * @returns mixed
  */
 protected function convertFieldsToArray()
 {
     $fields = $this->fields;
     if (!$fields) {
         return;
     }
     $fields = GeneratorsServiceProvider::splitFields($fields, true);
     $indices = [0];
     // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list
     $keyindices = $indices;
     // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list
     $primaryindices = $indices;
     // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list
     $dropIndices = [];
     $foreignKeys = [];
     $relationsModelList = GeneratorsServiceProvider::getRelationsModelVarsList($fields);
     $fieldIndex = 0;
     foreach ($fields as $field) {
         $fieldIndex++;
         // If there is a third key, then
         // the user is setting any number
         // of options
         $options = $field->options;
         $field->options = '';
         $hadUnsigned = false;
         $hadNullable = false;
         $hadDefault = false;
         $hadOnDelete = false;
         $foreignTable = null;
         foreach ($options as $option) {
             $isKey = null;
             $isUnique = null;
             if (($isPrimary = strpos($option, 'primary') === 0) || ($isUnique = strpos($option, 'unique') === 0) || ($isKey = strpos($option, 'keyindex') === 0) || strpos($option, 'index') === 0) {
                 if ($isPrimary) {
                     $keyIndex =& $primaryindices;
                 } elseif ($isKey || $isUnique) {
                     $keyIndex =& $keyindices;
                 } else {
                     $keyIndex =& $indices;
                 }
                 self::processIndexOption($keyIndex, $option, $field->name, $fieldIndex);
             }
             if ($option === 'ondelete' || $option === 'ondelete') {
                 $hadOnDelete = true;
             }
             if (GeneratorsServiceProvider::isFieldHintOption($option)) {
                 continue;
             }
             if ($option === 'unsigned' || $option === 'unsigned()') {
                 $hadUnsigned = true;
             }
             if ($option === 'nullable' || $option === 'nullable()') {
                 $hadNullable = true;
             }
             if ($option === 'default' || starts_with($option, 'default(')) {
                 if ($option === 'default') {
                     $option = 'default(null)';
                 }
                 $hadDefault = true;
             }
             $field->options .= str_contains($option, '(') ? "->{$option}" : "->{$option}()";
         }
         // add foreign keys
         $name = $field->name;
         if (array_key_exists($name, $relationsModelList)) {
             $table_name = $relationsModelList[$name]['snake_models'];
             if (!$hadUnsigned) {
                 $field->options .= "->unsigned()";
             }
             $indexName = "ixf_{$this->tableName}_{$name}_{$table_name}_id";
             if (strlen($indexName) > 64) {
                 $indexName = substr($indexName, 0, 64);
             }
             $onDeleteCascade = $hadOnDelete ? "->onDelete('cascade')" : '';
             $foreignKeys[] = "\$table->foreign('{$name}','{$indexName}')->references('id')->on({{prefix}}'{$table_name}'){$onDeleteCascade}";
             $dropIndices[] = "\$table->dropIndex('{$indexName}')";
         }
         if ($hadNullable && !$hadDefault && !$field->type === 'text') {
             $field->options .= "->default(null)";
         }
     }
     // now append the indices
     $inds = $foreignKeys;
     $inds = array_merge($inds, $this->generateIndex('index', $indices, $dropIndices));
     $inds = array_merge($inds, $this->generateIndex('unique', $keyindices, $dropIndices));
     $inds = array_merge($inds, $this->generateIndex('primary', $primaryindices, $dropIndices));
     $fields[] = [$inds, $dropIndices];
     return $fields;
 }