Mercurial > packages > magicforger
changeset 29:010ace248d14 codex
Added support for filters, not fully there with relations or anything like that but it's a start
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Mon, 09 Jun 2025 20:51:04 -0400 |
| parents | f88d2d5dee30 |
| children | 21439512ba69 |
| files | src/Generator/Controller/stubs/controller.stub src/Generator/Model/ModelGenerator.php src/Generator/Model/stubs/model.stub src/Generator/Requests/FilterRequestGenerator.php src/Generator/Requests/RequestGenerator.php src/MagicForgerServiceProvider.php src/Replacer/Replacer.php |
| diffstat | 7 files changed, 130 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Generator/Controller/stubs/controller.stub Mon Jun 09 19:51:31 2025 -0400 +++ b/src/Generator/Controller/stubs/controller.stub Mon Jun 09 20:51:04 2025 -0400 @@ -11,12 +11,14 @@ /** * Display a listing of the resource. * + * @param {{ filterRequest }} $request * @return \Illuminate\View\View */ - public function index() + public function index({{ filterRequest }} $request) { + $validated = $request->validated(); $data = []; - $data['items'] = {{ model }}::get_data(); + $data['items'] = {{ model }}::get_data($validated); $data = array_merge($data, {{ model }}::load_index()); return view('{{ tableName }}.index', $data);
--- a/src/Generator/Model/ModelGenerator.php Mon Jun 09 19:51:31 2025 -0400 +++ b/src/Generator/Model/ModelGenerator.php Mon Jun 09 20:51:04 2025 -0400 @@ -89,7 +89,7 @@ protected function gatherRelations() { $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); - return renderRelations($relations); + return $relations; } @@ -99,9 +99,22 @@ if (in_array($column['name'], $this->columns_to_ignore)) { continue; } - $insert .= sprintf("'%s',", $column['name'])."\n"; + $snippet = $this->getSnippet('filter'); + $tableName = $this->getCurrentTable(); + $value = 'value'; // TODO: this should be determined based on column type + $columnName = $column['name']; + $columnDisplay = Str::headline($columnName); + + // Replace placeholders with actual values + $string = str_replace( + ['{{value}}', '{{columnDisplay}}', '{{tableName}}', '{{columnName}}'], + [$value, $columnDisplay, $tableName, $columnName], + $snippet + ); + $insert .= sprintf("%s", $string); } + return $insert; } protected function renderRelations($relations) {
--- a/src/Generator/Model/stubs/model.stub Mon Jun 09 19:51:31 2025 -0400 +++ b/src/Generator/Model/stubs/model.stub Mon Jun 09 20:51:04 2025 -0400 @@ -2,11 +2,9 @@ namespace {{ namespace }}; -use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; -class {{ class }} extends Model +class {{ class }} extends BaseModel { //use HasFactory; use SoftDeletes; @@ -35,7 +33,7 @@ # {{ defaultRelationsInsertPoint }} ]; - protecte static $filters = [ + protected static $filters = [ # {{ defaultFiltersInsertPoint }} ]; @@ -67,22 +65,6 @@ return $this; } - //MARK FOR MODEL - protected static function load_auxilary_data() { - $data = []; - - $instance = new static(); - - foreach($instance->default_relations as $relation) { - $related_model = $instance->$relation()->getRelated(); - $related_table = $related_model->getTable(); - $data[$related_table] = $related_model->all()->pluck('name','id')->toArray(); - } - - return $data; - } - - /** * Retrieve a query builder instance with default relations loaded. *
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Generator/Requests/FilterRequestGenerator.php Mon Jun 09 20:51:04 2025 -0400 @@ -0,0 +1,77 @@ +<?php + +namespace Wizard\MagicForger\Generator\Requests; + +use Symfony\Component\Console\Attribute\AsCommand; +use Wizard\MagicForger\Generator\BaseGenerator; + +#[AsCommand(name: 'mf:filter_request')] +class FilterRequestGenerator extends BaseGenerator +{ + /** + * The name and signature of the console command. + * + * @var string + */ + protected $name = 'mf:filter_request'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Generates the FilterRequest File for a table.'; + + /** + * The type of class being generated. + * + * @var string + */ + protected $type = 'FilterRequest'; + + /** + * Execute the console command. + */ + public function handle() + { + parent::handle(); + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub() + { + return $this->resolveStubPath('/stubs/request.stub'); + } + + /** + * Resolve the fully-qualified path to the stub. + * + * @param string $stub + * @return string + */ + protected function resolveStubPath($stub) + { + return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__.$stub; + } + + protected function getClassName($name) + { + return $this->filter_request_name($name); + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getPath($name = null) + { + return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRequestNamespace($this->getTableInput()).'/'.$this->filter_request_name($this->getTableInput()).'.php'); + } +}
--- a/src/Generator/Requests/RequestGenerator.php Mon Jun 09 19:51:31 2025 -0400 +++ b/src/Generator/Requests/RequestGenerator.php Mon Jun 09 20:51:04 2025 -0400 @@ -45,10 +45,15 @@ } if ($this->option('all')) { + $this->input->setOption('filter_request', true); $this->input->setOption('store_request', true); $this->input->setOption('update_request', true); } + if ($this->option('filter_request')) { + $this->createFilterRequest(); + } + if ($this->option('store_request')) { $this->createStoreRequest(); } @@ -65,6 +70,7 @@ { return array_merge(parent::getOptions(), [ ['all', 'a', InputOption::VALUE_NONE, 'Generate all request classes for the table.'], + ['filter_request', 'f', InputOption::VALUE_NONE, 'Generate filter request class for the table.'], ['store_request', 's', InputOption::VALUE_NONE, 'Generate store request class for the table.'], ['update_request', 'u', InputOption::VALUE_NONE, 'Generate update request class for the table.'], ]); @@ -82,6 +88,11 @@ */ protected function getStub() {} + protected function createFilterRequest() + { + $this->call('mf:filter_request', ['table' => $this->getTableInput()]); + } + protected function createStoreRequest() { $this->call('mf:store_request', ['table' => $this->getTableInput()]);
--- a/src/MagicForgerServiceProvider.php Mon Jun 09 19:51:31 2025 -0400 +++ b/src/MagicForgerServiceProvider.php Mon Jun 09 20:51:04 2025 -0400 @@ -7,6 +7,7 @@ use Wizard\MagicForger\Generator\Generator; use Wizard\MagicForger\Generator\Model\ModelGenerator; use Wizard\MagicForger\Generator\Requests\RequestGenerator; +use Wizard\MagicForger\Generator\Requests\FilterRequestGenerator; use Wizard\MagicForger\Generator\Requests\StoreRequestGenerator; use Wizard\MagicForger\Generator\Requests\UpdateRequestGenerator; use Wizard\MagicForger\Generator\Route\RouteGenerator; @@ -28,6 +29,7 @@ ControllerGenerator::class, ModelGenerator::class, RequestGenerator::class, + FilterRequestGenerator::class, StoreRequestGenerator::class, UpdateRequestGenerator::class, RouteGenerator::class,
--- a/src/Replacer/Replacer.php Mon Jun 09 19:51:31 2025 -0400 +++ b/src/Replacer/Replacer.php Mon Jun 09 20:51:04 2025 -0400 @@ -65,8 +65,9 @@ '{{ requestUses }}' => $this->getRequestUses($table_name), '{{ rootNamespace }}' => $this->getRootNamespace(), '{{ storeRequest }}' => $this->store_request_name($table_name), + '{{ filterRequest }}' => $this->filter_request_name($table_name), + '{{ updateRequest }}' => $this->update_request_name($table_name), '{{ tableName }}' => $table_name, - '{{ updateRequest }}' => $this->update_request_name($table_name), ]; } @@ -107,6 +108,14 @@ } /** + * Generate the filter request name. + */ + public function filter_request_name(string $name): string + { + return 'Filter'.$this->model_name($name).'Request'; + } + + /** * Generate the update request name. */ public function update_request_name(string $name): string @@ -211,6 +220,14 @@ } /** + * Get the filter request namespace. + */ + public function getFilterRequestNamespace(string $name): string + { + return $this->getRequestNamespace($name); + } + + /** * Get the update request namespace. */ public function getUpdateRequestNamespace(string $name): string @@ -258,6 +275,7 @@ { return implode("\n", [ 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', + 'use '.$this->getRequestNamespace($name).'\\'.$this->filter_request_name($name).';', 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', ]); }
