Mercurial > packages > magicforger
changeset 7:769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
| author | luka |
|---|---|
| date | Wed, 18 Oct 2023 21:04:11 -0400 |
| parents | b46922d4a301 |
| children | 4216c0dc638c |
| files | .php-cs-fixer.cache src/Generator/BaseGenerator.php src/Generator/Controller/ControllerGenerator.php src/Generator/Controller/stubs/controller.stub src/Generator/Generator.php src/Generator/Model/ModelGenerator.php src/Generator/Requests/RequestGenerator.php src/Generator/Requests/StoreRequestGenerator.php src/Generator/Requests/UpdateRequestGenerator.php src/Generator/Route/RouteGenerator.php src/MagicForgerServiceProvider.php src/Replacer/Replacer.php src/Replacer/TableReplacer.php |
| diffstat | 13 files changed, 140 insertions(+), 108 deletions(-) [+] |
line wrap: on
line diff
--- a/.php-cs-fixer.cache Thu Oct 12 19:44:22 2023 -0400 +++ b/.php-cs-fixer.cache Wed Oct 18 21:04:11 2023 -0400 @@ -1,1 +1,1 @@ -{"php":"8.2.10","version":"3.19.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"curly_braces_position":{"allow_single_line_empty_anonymous_classes":true},"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\/Generator\/Route\/RouteGenerator.php":"4145e6208d253b08e9415339655c43e8","src\/Generator\/Controller\/ControllerGenerator.php":"cda3b688502c4427a50680c41b3a0f9d","src\/Generator\/Model\/ModelGenerator.php":"ded0f28024f6b6cbbfda97fbe14fda5a","src\/Generator\/Generator.php":"32b046d691dd2c24388e0741f875b184","src\/Generator\/Requests\/UpdateRequestGenerator.php":"ed745b244a7151bf0c12439af236f99b","src\/Generator\/Requests\/RequestGenerator.php":"29917ee19288caa73d92b46645c54905","src\/Generator\/Requests\/StoreRequestGenerator.php":"04b9c38f3339e02be4dc2d1c7545a370","src\/Generator\/BaseGenerator.php":"e59b94348fbc89dabdfea4098d4dfacb","src\/MagicForgerServiceProvider.php":"38024b0e807b2bc4cdd756ab456965a0","src\/Replacer\/Replacer.php":"830af5da740df6688a6def60966d38d7","src\/Replacer\/TableReplacer.php":"52736f1760b4c556cb3390e3d51a8812"}} \ No newline at end of file +{"php":"8.2.11","version":"3.19.1","indent":" ","lineEnding":"\n","rules":{"align_multiline_comment":true,"array_syntax":true,"backtick_to_shell_exec":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":{"method":"one"}},"class_definition":{"single_line":true},"class_reference_name_casing":true,"clean_namespace":true,"concat_space":true,"curly_braces_position":{"allow_single_line_anonymous_functions":true,"allow_single_line_empty_anonymous_classes":true},"declare_parentheses":true,"echo_tag_syntax":true,"empty_loop_body":{"style":"braces"},"empty_loop_condition":true,"fully_qualified_strict_types":true,"function_typehint_space":true,"general_phpdoc_tag_rename":{"replacements":{"inheritDocs":"inheritDoc"}},"global_namespace_import":{"import_classes":false,"import_constants":false,"import_functions":false},"include":true,"increment_style":true,"integer_literal_case":true,"lambda_not_used_import":true,"linebreak_after_opening_tag":true,"magic_constant_casing":true,"magic_method_casing":true,"method_argument_space":{"on_multiline":"ignore"},"native_function_casing":true,"native_function_type_declaration_casing":true,"no_alias_language_construct_call":true,"no_alternative_syntax":true,"no_binary_string":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["attribute","case","continue","curly_brace_block","default","extra","parenthesis_brace_block","square_brace_block","switch","throw","use"]},"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_null_property_initialization":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_superfluous_phpdoc_tags":{"remove_inheritdoc":true},"no_trailing_comma_in_singleline":true,"no_unneeded_control_parentheses":{"statements":["break","clone","continue","echo_print","others","return","switch_case","yield","yield_from"]},"no_unneeded_curly_braces":{"namespaces":true},"no_unneeded_import_alias":true,"no_unset_cast":true,"no_unused_imports":true,"no_useless_concat_operator":true,"no_useless_nullsafe_operator":true,"no_whitespace_before_comma_in_array":true,"normalize_index_brace":true,"nullable_type_declaration_for_default_null_value":{"use_nullable_type_declaration":false},"object_operator_without_whitespace":true,"operator_linebreak":{"only_booleans":true},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"alpha"},"php_unit_fqcn_annotation":true,"php_unit_method_casing":true,"phpdoc_align":true,"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag_normalizer":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_order":{"order":["param","return","throws"]},"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_tag_type":{"tags":{"inheritDoc":"inline"}},"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"semicolon_after_instruction":true,"simple_to_complex_string_variable":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"single_line_comment_spacing":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_space_around_construct":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"switch_continue_to_break":true,"trailing_comma_in_multiline":true,"trim_array_spaces":true,"types_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true,"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"return_type_declaration":true,"short_scalar_cast":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true},"hashes":{"src\/Generator\/Route\/RouteGenerator.php":"02bf90516b88a1b0b00a84afb10d8a45","src\/Generator\/Controller\/ControllerGenerator.php":"d549016b293838cc11788b320e1041c8","src\/Generator\/Model\/ModelGenerator.php":"31743678bf017f63680db19e9682df45","src\/Generator\/Generator.php":"c7cd32166a24be83f6575d33f6c4ba80","src\/Generator\/Requests\/UpdateRequestGenerator.php":"63475513d258b0727637db19629d4ac6","src\/Generator\/Requests\/RequestGenerator.php":"93fbc04a1ea18b794ee7f24902ae9cc1","src\/Generator\/Requests\/StoreRequestGenerator.php":"42bac2b426288d9a08216dfc5fe45faf","src\/Generator\/BaseGenerator.php":"d2d270339d1874b935f9c7ab81eabc03","src\/MagicForgerServiceProvider.php":"00c1321ad894d1e0807036dbe9114bf2","src\/Replacer\/Replacer.php":"464172a18e964c41a5fc019e5028f92f","src\/Replacer\/TableReplacer.php":"dc289802665136521abf6ffea0203732","src\/ConfigHelper.php":"5fa51fb9ad2615629a3f40809b8e9a42","mf_config.php":"f9a0ec14e629791d3d523fd915f7fa69"}} \ No newline at end of file
--- a/src/Generator/BaseGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/BaseGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -3,19 +3,17 @@ namespace Wizzard\MagicForger\Generator; use DB; - use Illuminate\Console\GeneratorCommand; -use Illuminate\Support\Str; -use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; - use Wizzard\MagicForger\Replacer\Replacer; +use Wizzard\MagicForger\Replacer\TableReplacer; abstract class BaseGenerator extends GeneratorCommand { use Replacer; + use TableReplacer; /** * The schema of the database. @@ -27,7 +25,7 @@ /** * The tables available in the schema. * - * @var array + * @var array */ protected $tables; @@ -43,10 +41,8 @@ */ public function handle() { - - // First we need to ensure that the table exists, then we can - if (! $this->tableExists($this->getTableInput())) { + if (!$this->tableExists($this->getTableInput())) { $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); return false; @@ -58,10 +54,14 @@ $file = $this->apply_replacements($file); + $file = $this->apply_inserts($file); + $this->makeDirectory($path); $this->files->put($path, $this->sortImports($file)); + $this->format_file($path); + $info = $this->type; $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); @@ -69,15 +69,14 @@ /** * Override the original so that we can prompt for a table with autocomplete. - * */ protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) { $prompted = false; - if(is_null($input->getArgument('table'))) { + if (is_null($input->getArgument('table'))) { $prompted = true; $table = null; - while ($table === null) { + while (null === $table) { $table = $this->components->askWithCompletion( 'What Table should we use?', $this->possibleTables() @@ -90,7 +89,7 @@ parent::promptForMissingArguments($input, $output); // This will get missed if we prompt here but not in the parent - if($prompted) { + if ($prompted) { $this->afterPromptingForMissingArguments($input, $output); } } @@ -140,7 +139,7 @@ } /** - * Determines if the file exists + * Determines if the file exists. */ protected function fileExists(string $path): bool { @@ -151,22 +150,20 @@ * Gets the file that will be worked on. If there is already an existing file * then we can open that. However if we are forcing the operation, then we * will start with an empty stub. - * */ protected function getFile($name) { - if ((! $this->hasOption('fresh') || - ! $this->option('fresh')) && - $this->fileExists($name)) { - //Working with an existing file + if (!($this->hasOption('fresh') + && $this->option('fresh')) + && $this->fileExists($name)) { + // Working with an existing file return $this->files->get($name); } - //Working with a stub + // Working with a stub return $this->files->get($this->getStub()); } - /** * Get the desired class table from the input. * @@ -177,9 +174,8 @@ return trim($this->argument('table')); } - /** - * Determines if the table exists in the current database + * Determines if the table exists in the current database. */ protected function tableExists(string $table_name): bool { @@ -195,7 +191,7 @@ } /** - * Get the tables in the schema + * Get the tables in the schema. */ protected function getTables() { @@ -207,12 +203,12 @@ } /** - * Get the database schema for DB interactions + * Get the database schema for DB interactions. */ protected function getSchema() { if (is_null($this->schema)) { - $this->schema = DB::connection()->getDoctrineSchemaManager(); + $this->schema = \DB::connection()->getDoctrineSchemaManager(); } return $this->schema; @@ -231,9 +227,14 @@ protected function setCurrentTable(string $table_name) { $table = null; - if(!is_null($table_name) && trim($table_name) !== '') { + if (!is_null($table_name) && '' !== trim($table_name)) { $table = $this->getTable($table_name); } $this->currentTable = $table; } + + protected function format_file(string $path) + { + exec('php-cs-fixer fix '.$path); + } }
--- a/src/Generator/Controller/ControllerGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Controller/ControllerGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -4,7 +4,6 @@ use Symfony\Component\Console\Attribute\AsCommand; use Wizzard\MagicForger\Generator\BaseGenerator; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:controller')] class ControllerGenerator extends BaseGenerator @@ -35,7 +34,7 @@ */ public function handle() { - $table = $this->getTable($this->getTableInput()); + $this->setCurrentTable($this->getTableInput()); parent::handle(); } @@ -52,7 +51,8 @@ /** * Resolve the fully-qualified path to the stub. * - * @param string $stub + * @param string $stub + * * @return string */ protected function resolveStubPath($stub) @@ -74,6 +74,6 @@ */ protected function getPath($name = null) { - return str_replace(['App\\', '\\'], ['app/', '/'], $this->getControllerNamespace() . '/' . $this->controller_name($this->getTableInput()) . '.php'); + return str_replace(['App\\', '\\'], ['app/', '/'], $this->getControllerNamespace().'/'.$this->controller_name($this->getTableInput()).'.php'); } }
--- a/src/Generator/Controller/stubs/controller.stub Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Controller/stubs/controller.stub Wed Oct 18 21:04:11 2023 -0400 @@ -41,7 +41,7 @@ ${{ modelVariable }} = new {{ model }}(); //insert the values into the model - //{{ valuesForCreation }} + // {{ valuesForCreation }} ${{ modelVariable }}->save();
--- a/src/Generator/Generator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Generator.php Wed Oct 18 21:04:11 2023 -0400 @@ -2,15 +2,10 @@ namespace Wizzard\MagicForger\Generator; -use DB; - use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Illuminate\Support\Str; - -use Wizzard\MagicForger\Generator\BaseGenerator; #[AsCommand(name: 'mf')] class Generator extends BaseGenerator @@ -29,13 +24,11 @@ */ protected $description = 'Generates any (or all) of the available files.'; - /** * Execute the console command. */ public function handle() { - // First we need to ensure that the table exists, then we can if (!$this->tableExists($this->getTableInput())) { $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); @@ -43,8 +36,8 @@ return false; } - $this->setCurrentTable($this->getTableInput()); - dd($this->getCurrentTable()->getForeignKeys()); + /* $this->setCurrentTable($this->getTableInput()); */ + /* dd($this->getCurrentTable()->getForeignKeys()); */ if ($this->option('all')) { /* $this->input->setOption('factory', true); */ @@ -83,7 +76,6 @@ if ($this->option('route')) { $this->createRoute(); } - } /**
--- a/src/Generator/Model/ModelGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Model/ModelGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -4,8 +4,6 @@ use Symfony\Component\Console\Attribute\AsCommand; use Wizzard\MagicForger\Generator\BaseGenerator; -use Wizzard\MagicForger\Replacer; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:model')] class ModelGenerator extends BaseGenerator @@ -52,7 +50,8 @@ /** * Resolve the fully-qualified path to the stub. * - * @param string $stub + * @param string $stub + * * @return string */ protected function resolveStubPath($stub) @@ -74,6 +73,6 @@ */ protected function getPath($name = null) { - return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace() . '/' . $this->model_name($this->getTableInput()) . '.php'); + return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php'); } }
--- a/src/Generator/Requests/RequestGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Requests/RequestGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Wizzard\MagicForger\Generator\BaseGenerator; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:request')] class RequestGenerator extends BaseGenerator @@ -81,6 +80,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) { } + /** * Get the stub file for the generator. * @@ -99,5 +99,4 @@ { $this->call('mf:update_request', ['table' => $this->getTableInput()]); } - }
--- a/src/Generator/Requests/StoreRequestGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Requests/StoreRequestGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -4,8 +4,6 @@ use Symfony\Component\Console\Attribute\AsCommand; use Wizzard\MagicForger\Generator\BaseGenerator; -use Wizzard\MagicForger\Replacer; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:store_request')] class StoreRequestGenerator extends BaseGenerator @@ -52,7 +50,8 @@ /** * Resolve the fully-qualified path to the stub. * - * @param string $stub + * @param string $stub + * * @return string */ protected function resolveStubPath($stub) @@ -74,6 +73,6 @@ */ protected function getPath($name = null) { - return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRequestNamespace($this->getTableInput()) . '/' . $this->store_request_name($this->getTableInput()) . '.php'); + return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRequestNamespace($this->getTableInput()).'/'.$this->store_request_name($this->getTableInput()).'.php'); } }
--- a/src/Generator/Requests/UpdateRequestGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Requests/UpdateRequestGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -4,8 +4,6 @@ use Symfony\Component\Console\Attribute\AsCommand; use Wizzard\MagicForger\Generator\BaseGenerator; -use Wizzard\MagicForger\Replacer; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:update_request')] class UpdateRequestGenerator extends BaseGenerator @@ -52,7 +50,8 @@ /** * Resolve the fully-qualified path to the stub. * - * @param string $stub + * @param string $stub + * * @return string */ protected function resolveStubPath($stub) @@ -74,6 +73,6 @@ */ protected function getPath($name = null) { - return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRequestNamespace($this->getTableInput()) . '/' . $this->update_request_name($this->getTableInput()) . '.php'); + return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRequestNamespace($this->getTableInput()).'/'.$this->update_request_name($this->getTableInput()).'.php'); } }
--- a/src/Generator/Route/RouteGenerator.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Generator/Route/RouteGenerator.php Wed Oct 18 21:04:11 2023 -0400 @@ -4,8 +4,6 @@ use Symfony\Component\Console\Attribute\AsCommand; use Wizzard\MagicForger\Generator\BaseGenerator; -use Wizzard\MagicForger\Replacer; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:routes')] class RouteGenerator extends BaseGenerator @@ -52,7 +50,8 @@ /** * Resolve the fully-qualified path to the stub. * - * @param string $stub + * @param string $stub + * * @return string */ protected function resolveStubPath($stub) @@ -74,6 +73,6 @@ */ protected function getPath($name = null) { - return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRouteNamespace() . '/' . $this->routes_name($this->getTableInput()) . '.php'); + return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRouteNamespace().'/'.$this->routes_name($this->getTableInput()).'.php'); } }
--- a/src/MagicForgerServiceProvider.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/MagicForgerServiceProvider.php Wed Oct 18 21:04:11 2023 -0400 @@ -3,8 +3,8 @@ namespace Wizzard\MagicForger; use Illuminate\Support\ServiceProvider; +use Wizzard\MagicForger\Generator\Controller\ControllerGenerator; use Wizzard\MagicForger\Generator\Generator; -use Wizzard\MagicForger\Generator\Controller\ControllerGenerator; use Wizzard\MagicForger\Generator\Model\ModelGenerator; use Wizzard\MagicForger\Generator\Requests\RequestGenerator; use Wizzard\MagicForger\Generator\Requests\StoreRequestGenerator; @@ -29,5 +29,12 @@ RouteGenerator::class, ]); } + + $this->register_config_helper(); + } + + public function register_config_helper() + { + ConfigHelper::setup_config_path(base_path()); } }
--- a/src/Replacer/Replacer.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Replacer/Replacer.php Wed Oct 18 21:04:11 2023 -0400 @@ -12,8 +12,7 @@ * * @var string */ - protected $controller_prefix = ""; - + protected $controller_prefix = ''; /** * Prefix and Suffix for controller. @@ -21,28 +20,27 @@ * * @var string */ - protected $controller_suffix = "Controller"; - + protected $controller_suffix = 'Controller'; /** * Finds all places in a string that could be replaced. * Returns an array of all potential replacements as they * appear in the target. */ - public function get_all_inserts(string $target): array + public function get_all_keywords(string $target): array { - //find all the matches to our expected syntax + // find all the matches to our expected syntax $matches = []; preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); // sort the array and return unique values sort($matches[0]); + return array_values(array_unique($matches[0])); } - public function apply_replacements(string $target): string { - $inserts = $this->get_all_inserts($target); + $inserts = $this->get_all_keywords($target); $available_replacements = $this->get_available_replacements(); $target = str_replace( @@ -58,28 +56,28 @@ { $table_name = $this->getTableInput(); $replacements = [ - "{{ class }}" => $this->getClassName($table_name), - "{{ controllerName }}" => $this->controller_name($table_name), - "{{ model }}" => $this->model_name($table_name), - "{{ modelVariable }}" => $this->model_variable($table_name), - "{{ namespace }}" => $this->{'get' . $this->type . 'Namespace'}($table_name), - "{{ namespacedModel }}" => $this->getNamespacedModel($table_name), - "{{ requestUses }}" => $this->getRequestUses($table_name), - "{{ rootNamespace }}" => $this->getRootNamespace(), - "{{ storeRequest }}" => $this->store_request_name($table_name), - "{{ tableName }}" => $table_name, - "{{ updateRequest }}" => $this->update_request_name($table_name), + '{{ class }}' => $this->getClassName($table_name), + '{{ controllerName }}' => $this->controller_name($table_name), + '{{ model }}' => $this->model_name($table_name), + '{{ modelVariable }}' => $this->model_variable($table_name), + '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), + '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), + '{{ requestUses }}' => $this->getRequestUses($table_name), + '{{ rootNamespace }}' => $this->getRootNamespace(), + '{{ storeRequest }}' => $this->store_request_name($table_name), + '{{ tableName }}' => $table_name, + '{{ updateRequest }}' => $this->update_request_name($table_name), ]; return $replacements; } - //////////////////////////////////////////// + // ////////////////////////////////////////// // Internals and Classes // - //////////////////////////////////////////// + // ////////////////////////////////////////// /** - * Model names are generated in uppercase first Camel case + * Model names are generated in uppercase first Camel case. */ public function model_name(string $name): string { @@ -87,7 +85,7 @@ } /** - * Model variable is standardly just a singular version of the table name + * Model variable is standardly just a singular version of the table name. */ public function model_variable(string $name): string { @@ -96,29 +94,28 @@ /** * Controller names are generated in uppercase first Camel case - * and wrapped in the prefix and suffix + * and wrapped in the prefix and suffix. */ public function controller_name(string $name): string { - return $this->controller_prefix . - $this->model_name($name) . + return $this->controller_prefix. + $this->model_name($name). $this->controller_suffix; } public function store_request_name(string $name): string { - return 'Store' . $this->model_name($name) . 'Request'; + return 'Store'.$this->model_name($name).'Request'; } public function update_request_name(string $name): string { - return 'Update' . $this->model_name($name) . 'Request'; + return 'Update'.$this->model_name($name).'Request'; } - - //////////////////////////////////////////// + // ////////////////////////////////////////// // Namespaces // - //////////////////////////////////////////// + // ////////////////////////////////////////// public function getRootNamespace() { @@ -127,22 +124,22 @@ public function getModelNamespace(string $name = '') { - return $this->getRootNamespace() . 'Models'; + return $this->getRootNamespace().'Models'; } public function getNamespacedModel(string $name = '') { - return $this->getModelNamespace() . '\\' . $this->model_name($name); + return $this->getModelNamespace().'\\'.$this->model_name($name); } public function getControllerNamespace(string $name = '') { - return $this->getRootNamespace() . 'Http\\Controllers'; + return $this->getRootNamespace().'Http\\Controllers'; } public function getRequestNamespace(string $name) { - return $this->getRootNamespace() . 'Http\\Requests\\' . $this->model_name($name); + return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); } public function getStoreRequestNamespace(string $name) @@ -158,24 +155,22 @@ public function getRequestUses(string $name) { return implode("\n", [ - "use " . $this->getRequestNamespace($name) . '\\' . $this->store_request_name($name) . ';', - "use " . $this->getRequestNamespace($name) . '\\' . $this->update_request_name($name) . ';', + 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', + 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', ]); } - public function getRouteNamespace(string $name = '') { - return $this->getRootNamespace() . 'Http\\Controllers'; + return $this->getRootNamespace().'Http\\Controllers'; } - - //////////////////////////////////////////// + // ////////////////////////////////////////// // Language and Presentables // - //////////////////////////////////////////// + // ////////////////////////////////////////// /** - * Breaks up a string and makes it human readable + * Breaks up a string and makes it human readable. * * This function assumes that the inputted name is camel case */ @@ -185,7 +180,7 @@ } /** - * Breaks up a string and makes it human readable and lowecase + * Breaks up a string and makes it human readable and lowecase. * * This function assumes that the inputted name is camel case */
--- a/src/Replacer/TableReplacer.php Thu Oct 12 19:44:22 2023 -0400 +++ b/src/Replacer/TableReplacer.php Wed Oct 18 21:04:11 2023 -0400 @@ -2,16 +2,58 @@ namespace Wizzard\MagicForger\Replacer; -use Illuminate\Support\Str; - trait TableReplacer { + protected $columns; + protected function get_columns() { - return $this->getTables()->getColumns(); + if (is_null($this->columns)) { + $this->columns = $this->getCurrentTable()->getColumns(); + } + + return $this->columns; } protected function get_attributes() { } + + protected function getValuesForCreation() + { + $insert = ''; + foreach ($this->get_columns() as $column) { + $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; + } + + return $insert; + } + + public function apply_inserts(string $target): string + { + $inserts = $this->get_all_keywords($target); + $available_replacements = $this->get_available_inserts(); + + $target = str_replace( + array_keys($available_replacements), + $available_replacements, + $target + ); + + return $target; + } + + public function get_available_inserts() + { + $table_name = $this->getTableInput(); + $replacements = [ + '// {{ valuesForCreation }}' => self::getValuesForCreation(), + ]; + + foreach ($replacements as $key => &$replacement) { + $replacement = $replacement."\n".$key; + } + + return $replacements; + } }
