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