Appzcoder\CrudGenerator\Commands\CrudMigrationCommand::buildClass PHP Метод

buildClass() защищенный Метод

Build the model class with the given name.
protected buildClass ( string $name ) : string
$name string
Результат string
    protected function buildClass($name)
    {
        $stub = $this->files->get($this->getStub());
        $tableName = $this->argument('name');
        $className = 'Create' . str_replace(' ', '', ucwords(str_replace('_', ' ', $tableName))) . 'Table';
        $fieldsToIndex = trim($this->option('indexes')) != '' ? explode(',', $this->option('indexes')) : [];
        $foreignKeys = trim($this->option('foreign-keys')) != '' ? explode(',', $this->option('foreign-keys')) : [];
        $schema = rtrim($this->option('schema'), ';');
        $fields = explode(';', $schema);
        $data = array();
        if ($schema) {
            $x = 0;
            foreach ($fields as $field) {
                $fieldArray = explode('#', $field);
                $data[$x]['name'] = trim($fieldArray[0]);
                $data[$x]['type'] = trim($fieldArray[1]);
                $data[$x]['modifier'] = '';
                $modifierLookup = ['comment', 'default', 'first', 'nullable', 'unsigned'];
                if (isset($fieldArray[2]) && in_array(trim($fieldArray[2]), $modifierLookup)) {
                    $data[$x]['modifier'] = "->" . trim($fieldArray[2]) . "()";
                }
                $x++;
            }
        }
        $tabIndent = '    ';
        $schemaFields = '';
        foreach ($data as $item) {
            if (isset($this->typeLookup[$item['type']])) {
                $type = $this->typeLookup[$item['type']];
                $schemaFields .= "\$table->" . $type . "('" . $item['name'] . "')";
            } else {
                $schemaFields .= "\$table->string('" . $item['name'] . "')";
            }
            // Append column modifier
            $schemaFields .= $item['modifier'];
            $schemaFields .= ";\n" . $tabIndent . $tabIndent . $tabIndent;
        }
        // add indexes and unique indexes as necessary
        foreach ($fieldsToIndex as $fldData) {
            $line = trim($fldData);
            // is a unique index specified after the #?
            // if no hash present, we append one to make life easier
            if (strpos($line, '#') === false) {
                $line .= '#';
            }
            // parts[0] = field name (or names if pipe separated)
            // parts[1] = unique specified
            $parts = explode('#', $line);
            if (strpos($parts[0], '|') !== 0) {
                $fieldNames = "['" . implode("', '", explode('|', $parts[0])) . "']";
                // wrap single quotes around each element
            } else {
                $fieldNames = trim($parts[0]);
            }
            if (count($parts) > 1 && $parts[1] == 'unique') {
                $schemaFields .= "\$table->unique(" . trim($fieldNames) . ")";
            } else {
                $schemaFields .= "\$table->index(" . trim($fieldNames) . ")";
            }
            $schemaFields .= ";\n" . $tabIndent . $tabIndent . $tabIndent;
        }
        // foreign keys
        foreach ($foreignKeys as $fk) {
            $line = trim($fk);
            $parts = explode('#', $line);
            // if we don't have three parts, then the foreign key isn't defined properly
            // --foreign-keys="foreign_entity_id#id#foreign_entity#onDelete#onUpdate"
            if (count($parts) == 3) {
                $schemaFields .= "\$table->foreign('" . trim($parts[0]) . "')" . "->references('" . trim($parts[1]) . "')->on('" . trim($parts[2]) . "')";
            } elseif (count($parts) == 4) {
                $schemaFields .= "\$table->foreign('" . trim($parts[0]) . "')" . "->references('" . trim($parts[1]) . "')->on('" . trim($parts[2]) . "')" . "->onDelete('" . trim($parts[3]) . "')" . "->onUpdate('" . trim($parts[3]) . "')";
            } elseif (count($parts) == 5) {
                $schemaFields .= "\$table->foreign('" . trim($parts[0]) . "')" . "->references('" . trim($parts[1]) . "')->on('" . trim($parts[2]) . "')" . "->onDelete('" . trim($parts[3]) . "')" . "->onUpdate('" . trim($parts[4]) . "')";
            } else {
                continue;
            }
            $schemaFields .= "\$table->foreign('" . trim($parts[0]) . "')" . "->references('" . trim($parts[1]) . "')->on('" . trim($parts[2]) . "')";
            $schemaFields .= ";\n" . $tabIndent . $tabIndent . $tabIndent;
        }
        $primaryKey = $this->option('pk');
        $schemaUp = "Schema::create('" . $tableName . "', function(Blueprint \$table) {\n            \$table->increments('" . $primaryKey . "');\n            " . $schemaFields . "\$table->timestamps();\n        });";
        $schemaDown = "Schema::drop('" . $tableName . "');";
        return $this->replaceSchemaUp($stub, $schemaUp)->replaceSchemaDown($stub, $schemaDown)->replaceClass($stub, $className);
    }