argument('name'); $schema = $this->option('schema'); $this->migration($name, $schema); $this->model($name, $schema); $this->controller($name); $this->request($name, $schema); $this->view($name, $schema); } protected function getStub($type) { return file_get_contents(resource_path("stubs/$type.stub")); } protected function migration($name, $schema) { $schema_parent = ''; $schema_child = ''; if($schema) { $schema = explode(';', $schema); foreach ($schema as $field) { $options = explode(':', $field); if($options[2] != "translate") { $schema_parent .= "\n\t\t\t\$table->$options[1]('$options[0]');"; } else { $schema_child .= "\n\t\t\t\$table->$options[1]('$options[0]');"; } } $schema_parent .= "\n"; $schema_child .= "\n"; } $migrationTemplate = str_replace( [ '{{modelNamePlural}}', '{{modelNamePluralLowerCase}}', '{{schema}}' ], [ str_plural($name), strtolower(str_plural($name)), $schema_parent ], $this->getStub('Migration') ); file_put_contents(database_path("migrations/".date('Y_m_d_His')."_create_".strtolower(str_plural($name))."_table.php"), $migrationTemplate); sleep(1); $migrationTemplate = str_replace( [ '{{modelName}}', '{{modelNameLowerCase}}', '{{modelNamePluralLowerCase}}', '{{schema}}' ], [ $name, strtolower($name), strtolower(str_plural($name)), $schema_child ], $this->getStub('MigrationTranslation') ); file_put_contents(database_path("migrations/".date('Y_m_d_His')."_create_".strtolower($name)."_translations_table.php"), $migrationTemplate); } protected function model($name, $schema) { $fillable_parent = ''; $fillable_child = ''; if($schema) { $schema = explode(';', $schema); foreach ($schema as $field) { $options = explode(':', $field); if($options[2] != "translate") { $fillable_parent .= ", '$options[0]'"; } else { $fillable_child .= ", '$options[0]'"; } } } $modelTemplate = str_replace( [ '{{modelName}}', '{{fillable_parent}}', '{{fillable_child}}' ], [ $name, $fillable_parent, $fillable_child ], $this->getStub('Model') ); file_put_contents(app_path("/{$name}.php"), $modelTemplate); $modelTemplate = str_replace( [ '{{modelName}}', '{{fillable_child}}' ], [ $name, $fillable_child ], $this->getStub('ModelTranslation') ); file_put_contents(app_path("/{$name}Translation.php"), $modelTemplate); } protected function controller($name) { $controllerTemplate = str_replace( [ '{{modelName}}', '{{modelNamePluralLowerCase}}', '{{modelNameSingularLowerCase}}' ], [ $name, strtolower(str_plural($name)), strtolower($name) ], $this->getStub('Controller') ); file_put_contents(app_path("/Http/Controllers/{$name}Controller.php"), $controllerTemplate); } protected function request($name, $schema) { $validation_parent = ''; $validation_child = ''; if($schema) { $schema = explode(';', $schema); foreach ($schema as $field) { $options = explode(':', $field); // check if validation rules are present if($options[3]) { if($options[2] != "translate") { $validation_parent .= "'$options[0]' => '$options[3]',\n\t\t"; } else { $validation_child .= "'$options[0]' => '$options[3]',\n\t\t"; } } } } $requestTemplate = str_replace( [ '{{modelName}}', '{{validation_parent}}', '{{validation_child}}' ], [ $name, $validation_parent, $validation_child ], $this->getStub('CreateRequest') ); file_put_contents(app_path("/Http/Requests/Create{$name}Request.php"), $requestTemplate); $requestTemplate = str_replace( [ '{{modelName}}', '{{validation_parent}}', '{{validation_child}}' ], [ $name, $validation_parent, $validation_child ], $this->getStub('UpdateRequest') ); file_put_contents(app_path("/Http/Requests/Update{$name}Request.php"), $requestTemplate); } protected function view($name, $schema) { $fields_parent = ''; $fields_child = ''; $path = resource_path('views/' . strtolower($name)); if(!File::exists($path)) { mkdir($path, 0777, true); } $files = File::allFiles(resource_path('stubs/views')); foreach($files as $file) { $filename = pathinfo($file, PATHINFO_FILENAME); // add form inputs if($filename == '_form' && $schema) { $schema = explode(';', $schema); foreach ($schema as $field) { $options = explode(':', $field); if($options[2] != "translate") { switch ($options[1]) { case 'string': $fields_parent .= "
has('$options[0]') ? ' has-error' : '' }}\"> {!! Form::label('$options[0]', '$options[0] *', ['class' => 'control-label']) !!} {!! Form::text('$options[0]', null, ['class' => 'form-control']) !!} @if(\$errors->has('$options[0]')) {{ \$errors->first('$options[0]') }} @endif
"; break; case 'text': $fields_parent .= "
has('$options[0]') ? ' has-error' : '' }}\"> {!! Form::label('$options[0]', '$options[0] *', ['class' => 'control-label']) !!} {!! Form::textarea('$options[0]', null, ['class' => 'form-control tinymce']) !!} @if(\$errors->has('$options[0]')) {{ \$errors->first('$options[0]') }} @endif
"; break; case 'date': $fields_parent .= "
has('$options[0]') ? ' has-error' : '' }}\"> {!! Form::label('$options[0]', '$options[0] *', ['class' => 'control-label']) !!} {!! Form::text('$options[0]', null, ['class' => 'form-control datepicker', 'autocomplete' => 'off']) !!} @if(\$errors->has('$options[0]')) {{ \$errors->first('$options[0]') }} @endif
"; break; default: break; } } else { switch ($options[1]) { case 'string': $fields_child .= "
has(\$locale.'.$options[0]') ? ' has-error' : '' }}\"> {!! Form::label(\$locale.'[$options[0]]', '$options[0] *', ['class' => 'control-label']) !!} {!! Form::text(\$locale.'[$options[0]]', \$".strtolower($name)."->translateOrNew(\$locale)->$options[0], ['class' => 'form-control']) !!} @if(\$errors->has(\$locale.'.$options[0]')) {{ \$errors->first(\$locale.'.$options[0]') }} @endif
"; break; case 'text': $fields_child .= "
has(\$locale.'.$options[0]') ? ' has-error' : '' }}\"> {!! Form::label(\$locale.'[$options[0]]', '$options[0] *', ['class' => 'control-label']) !!} {!! Form::textarea(\$locale.'[$options[0]]', \$".strtolower($name)."->translateOrNew(\$locale)->$options[0], ['class' => 'form-control tinymce']) !!} @if(\$errors->has(\$locale.'.$options[0]')) {{ \$errors->first(\$locale.'.$options[0]') }} @endif
"; break; case 'date': $fields_child .= "
has(\$locale.'.$options[0]') ? ' has-error' : '' }}\"> {!! Form::label(\$locale.'[$options[0]]', '$options[0] *', ['class' => 'control-label']) !!} {!! Form::text(\$locale.'[$options[0]]', \$".strtolower($name)."->translateOrNew(\$locale)->$options[0], ['class' => 'form-control datepicker', 'autocomplete' => 'off']) !!} @if(\$errors->has(\$locale.'.$options[0]')) {{ \$errors->first(\$locale.'.$options[0]') }} @endif
"; break; default: break; } } } } $viewTemplate = str_replace( [ '{{modelName}}', '{{modelNameSingularLowerCase}}', '{{fields_parent}}', '{{fields_child}}' ], [ $name, strtolower($name), $fields_parent, $fields_child ], $this->getStub("views/$filename") ); file_put_contents(resource_path("views/" . strtolower($name) . "/" . $filename . ".blade.php"), $viewTemplate); } // File::copyDirectory(resource_path('stubs/views'), resource_path('views/' . strtolower($name))); } }