1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\QueryFilters\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Artisan command to generate query filters. |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class MakeQueryFiltersCommand extends GeneratorCommand |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The console command name. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name = 'make:query-filters'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Create a new query filters class'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The type of class being generated. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $type = 'Query filters'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the stub file for the generator. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
9 |
|
*/ |
42
|
|
|
protected function getStub() |
43
|
9 |
|
{ |
44
|
|
|
return __DIR__ . '/../../../stubs/query_filters.stub'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the default namespace for the class. |
49
|
|
|
* |
50
|
|
|
* @param string $rootNamespace |
51
|
|
|
* @return string |
52
|
9 |
|
*/ |
53
|
|
|
protected function getDefaultNamespace($rootNamespace) |
54
|
|
|
{ |
55
|
9 |
|
// get query filters path from configuration |
56
|
|
|
$path = Config::get('query_filters.path'); |
57
|
9 |
|
// ensure the path always starts with "app/" |
58
|
|
|
$path = Str::start(ltrim($path, '/'), 'app/'); |
59
|
9 |
|
// remove "app/" from the beginning of the path |
60
|
|
|
$path = preg_replace('#^app\/#', '', $path); |
61
|
9 |
|
// convert the path into namespace |
62
|
|
|
$namespace = implode('\\', array_map('ucfirst', explode('/', $path))); |
63
|
9 |
|
// prepend the root namespace |
64
|
|
|
return $rootNamespace . '\\' . $namespace; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Build the class with the given name. |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* @return string |
72
|
9 |
|
*/ |
73
|
|
|
protected function buildClass($name) |
74
|
9 |
|
{ |
75
|
|
|
$stub = parent::buildClass($name); |
76
|
9 |
|
|
77
|
|
|
return $this->replaceFilters($stub); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Replace filters for the given stub |
82
|
|
|
* |
83
|
|
|
* @param string $stub |
84
|
|
|
* @return string |
85
|
9 |
|
*/ |
86
|
|
|
protected function replaceFilters($stub) |
87
|
9 |
|
{ |
88
|
|
|
parse_str($this->argument('filters'), $rawFilters); |
|
|
|
|
89
|
9 |
|
|
90
|
6 |
|
if (empty($rawFilters)) { |
91
|
|
|
return str_replace('DummyFilters', PHP_EOL . ' //' . PHP_EOL, $stub); |
92
|
|
|
} |
93
|
3 |
|
|
94
|
3 |
|
$filters = ''; |
95
|
|
|
$filterStub = file_get_contents(__DIR__ . '/../../../stubs/filter.stub'); |
96
|
3 |
|
|
97
|
3 |
|
foreach ($rawFilters as $queryParameter => $parameterName) { |
98
|
3 |
|
$filterName = Str::camel($queryParameter); |
99
|
3 |
|
$parameterVariable = $parameterName === '' ? '' : '$' . $parameterName; |
100
|
3 |
|
$parameterDoc = $parameterName === '' ? '' : '@param mixed dummyParameter' . PHP_EOL . ' * '; |
101
|
3 |
|
$search = ['dummyQueryParameter', 'dummyParameterDoc', 'dummyFilter', 'dummyParameter']; |
102
|
3 |
|
$replace = [$queryParameter, $parameterDoc, $filterName, $parameterVariable]; |
103
|
1 |
|
$filters .= str_replace($search, $replace, $filterStub); |
104
|
|
|
} |
105
|
3 |
|
|
106
|
|
|
return str_replace('DummyFilters', $filters, $stub); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the console command arguments. |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
9 |
|
*/ |
114
|
|
|
protected function getArguments() |
115
|
|
|
{ |
116
|
9 |
|
return [ |
117
|
9 |
|
['name', InputArgument::REQUIRED, 'The name of the class'], |
118
|
3 |
|
['filters', InputArgument::OPTIONAL, "The name of the filters e.g. 'won_oscar&acting=bool&acted-in=year'"], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|