- For Laravel Applications
- Place the PHP script in
app/Console/Commands/directory - Run
php artisan make:filter {FilterName}where{FilterName}is the name of the filter you intend to create.
- Place the PHP script in
Created
June 23, 2018 16:35
-
-
Save mykeels/6bf11904832a496cbcf69cc8398d5c20 to your computer and use it in GitHub Desktop.
Artisan Command to generate Filter Class, based on https://medium.com/@mykeels/writing-clean-composable-eloquent-filters-edd242c82cc8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Console\Commands; | |
| use Illuminate\Console\Command; | |
| class MakeFilterCommand extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'make:filter { name : The name of the Filter }'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'Creates a filter class in app\\Filters'; | |
| /** | |
| * Create a new command instance. | |
| * | |
| * @return void | |
| */ | |
| public function __construct() | |
| { | |
| parent::__construct(); | |
| } | |
| /** | |
| * Execute the console command. | |
| * | |
| * @return mixed | |
| */ | |
| public function handle() | |
| { | |
| $name = $this->getFilterName(); | |
| file_put_contents('./app/Filters/' . $name . 'Filters.php', $this->getFileContents($name)); | |
| } | |
| private function getFilterName() { | |
| $name = $this->argument('name'); | |
| if (!$name) { | |
| throw new \Exception('Invalid Filter Name'); | |
| } | |
| return ucwords(preg_replace('/filter(s)?/i', '', $name)); | |
| } | |
| private function getFileContents($name) { | |
| return preg_replace('/\n( ){2}/i', "\n", "<?php | |
| namespace App\Filters; | |
| use App\User; | |
| use App\Models\\$name; | |
| use Illuminate\Http\Request; | |
| use Carbon\Carbon; | |
| class ${name}Filters extends BaseFilters | |
| { | |
| protected \$request; | |
| public function __construct(Request \$request) | |
| { | |
| \$this->request = \$request; | |
| parent::__construct(\$request); | |
| } | |
| }"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment