|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Pomm's Cli package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace PommProject\Cli\Command; |
|
11
|
|
|
|
|
12
|
|
|
use PommProject\Cli\Exception\CliException; |
|
13
|
|
|
use PommProject\Foundation\Inflector; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* SchemaAwareCommand |
|
22
|
|
|
* |
|
23
|
|
|
* Base class for generator commands. |
|
24
|
|
|
* |
|
25
|
|
|
* @abstract |
|
26
|
|
|
* @package Cli |
|
27
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
28
|
|
|
* @author Grégoire HUBERT |
|
29
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
30
|
|
|
* @see PommAwareCommand |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract class SchemaAwareCommand extends SessionAwareCommand |
|
33
|
|
|
{ |
|
34
|
|
|
protected $schema; |
|
35
|
|
|
protected $prefix_dir; |
|
36
|
|
|
protected $prefix_ns; |
|
37
|
|
|
protected $pathFile; |
|
38
|
|
|
protected $namespace; |
|
39
|
|
|
protected $flexible_container; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* configure |
|
43
|
|
|
* |
|
44
|
|
|
* @see PommAwareCommand |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function configureRequiredArguments() |
|
47
|
|
|
{ |
|
48
|
|
|
parent::configureRequiredArguments() |
|
49
|
|
|
->addOption( |
|
50
|
|
|
'prefix-dir', |
|
51
|
|
|
'd', |
|
52
|
|
|
InputOption::VALUE_REQUIRED, |
|
53
|
|
|
'Indicate a directory prefix.', |
|
54
|
|
|
'.' |
|
55
|
|
|
) |
|
56
|
|
|
->addOption( |
|
57
|
|
|
'prefix-ns', |
|
58
|
|
|
'a', |
|
59
|
|
|
InputOption::VALUE_REQUIRED, |
|
60
|
|
|
'Indicate a namespace prefix.', |
|
61
|
|
|
'' |
|
62
|
|
|
) |
|
63
|
|
|
; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* configureOptionals |
|
70
|
|
|
* |
|
71
|
|
|
* @see PommAwareCommand |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function configureOptionals() |
|
74
|
|
|
{ |
|
75
|
|
|
parent::configureOptionals() |
|
76
|
|
|
->addArgument( |
|
77
|
|
|
'schema', |
|
78
|
|
|
InputArgument::OPTIONAL, |
|
79
|
|
|
'Schema of the relation.', |
|
80
|
|
|
'public' |
|
81
|
|
|
) |
|
82
|
|
|
->addOption( |
|
83
|
|
|
'flexible-container', |
|
84
|
|
|
null, |
|
85
|
|
|
InputOption::VALUE_REQUIRED, |
|
86
|
|
|
'Use an alternative flexible entity container', |
|
87
|
|
|
'PommProject\ModelManager\Model\FlexibleEntity' |
|
88
|
|
|
) |
|
89
|
|
|
; |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
/** |
|
94
|
|
|
* execute |
|
95
|
|
|
* |
|
96
|
|
|
* @see Command |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
99
|
|
|
{ |
|
100
|
|
|
parent::execute($input, $output); |
|
101
|
|
|
$this->schema = $input->getArgument('schema'); |
|
102
|
|
|
|
|
103
|
|
|
if (!$this->schema) { |
|
104
|
|
|
$this->schema = 'public'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->prefix_dir = $input->getOption('prefix-dir'); |
|
108
|
|
|
$this->prefix_ns = $input->getOption('prefix-ns'); |
|
109
|
|
|
$this->flexible_container = $input->getOption('flexible-container'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* getPathFile |
|
114
|
|
|
* |
|
115
|
|
|
* Create path file from parameters and namespace. |
|
116
|
|
|
* |
|
117
|
|
|
* @access protected |
|
118
|
|
|
* @param string $config_name |
|
119
|
|
|
* @param string $file_suffix |
|
120
|
|
|
* @param string $extra_dir |
|
121
|
|
|
* @param string $file_name |
|
122
|
|
|
* @param bool $format_psr4 |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getPathFile( |
|
126
|
|
|
$config_name, |
|
|
|
|
|
|
127
|
|
|
$file_name, |
|
128
|
|
|
$file_suffix = '', |
|
129
|
|
|
$extra_dir = '', |
|
130
|
|
|
$format_psr4 = null, |
|
131
|
|
|
$path_pattern = '{session}/{schema}Schema' |
|
132
|
|
|
) { |
|
133
|
|
|
|
|
134
|
|
|
$format_psr4 = $format_psr4 === null ? false : (bool) $format_psr4; |
|
135
|
|
|
$prefix_ns = ""; |
|
136
|
|
|
|
|
137
|
|
|
if (!$format_psr4) { |
|
138
|
|
|
$prefix_ns = str_replace('\\', '/', trim($this->prefix_ns, '\\')); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$elements = |
|
142
|
|
|
[ |
|
143
|
|
|
rtrim($this->prefix_dir, '/'), |
|
144
|
|
|
$prefix_ns, |
|
145
|
|
|
$this->expandPath($path_pattern), |
|
146
|
|
|
$extra_dir, |
|
147
|
|
|
sprintf("%s%s.php", Inflector::studlyCaps($file_name), $file_suffix) |
|
148
|
|
|
]; |
|
149
|
|
|
|
|
150
|
|
|
return join('/', array_filter($elements, function ($val) { |
|
151
|
|
|
return $val != null; |
|
152
|
|
|
})); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* expandPath |
|
157
|
|
|
* |
|
158
|
|
|
* Expand path pattern with the context values |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $pattern |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function expandPath($pattern) |
|
164
|
|
|
{ |
|
165
|
|
|
return trim( |
|
166
|
|
|
strtr( |
|
167
|
|
|
$pattern, |
|
168
|
|
|
[ |
|
169
|
|
|
'{session}' => Inflector::studlyCaps($this->config_name), |
|
170
|
|
|
'{schema}' => Inflector::studlyCaps($this->schema), |
|
171
|
|
|
] |
|
172
|
|
|
), |
|
173
|
|
|
'/' |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* getNamespace |
|
179
|
|
|
* |
|
180
|
|
|
* Create namespace from parameters. |
|
181
|
|
|
* |
|
182
|
|
|
* @access protected |
|
183
|
|
|
* @param string $config_name |
|
184
|
|
|
* @param string $extra_ns |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function getNamespace($config_name, $extra_ns = '') |
|
188
|
|
|
{ |
|
189
|
|
|
$elements = |
|
190
|
|
|
[ |
|
191
|
|
|
$this->prefix_ns, |
|
192
|
|
|
Inflector::studlyCaps($config_name), |
|
193
|
|
|
Inflector::studlyCaps(sprintf("%s_schema", $this->schema)), |
|
194
|
|
|
$extra_ns |
|
195
|
|
|
]; |
|
196
|
|
|
|
|
197
|
|
|
return join('\\', array_filter($elements, function ($val) { |
|
198
|
|
|
return $val != null; |
|
199
|
|
|
})); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* fetchSchemaOid |
|
204
|
|
|
* |
|
205
|
|
|
* Get the schema Oid from database. |
|
206
|
|
|
* |
|
207
|
|
|
* @access protected |
|
208
|
|
|
* @return int $oid |
|
209
|
|
|
* @throws CliException |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function fetchSchemaOid() |
|
212
|
|
|
{ |
|
213
|
|
|
$schema_oid = $this |
|
214
|
|
|
->getSession() |
|
215
|
|
|
->getInspector() |
|
216
|
|
|
->getSchemaOid($this->schema) |
|
217
|
|
|
; |
|
218
|
|
|
|
|
219
|
|
|
if ($schema_oid === null) { |
|
220
|
|
|
throw new CliException( |
|
221
|
|
|
sprintf( |
|
222
|
|
|
"Could not find schema '%s'.", |
|
223
|
|
|
$this->schema |
|
224
|
|
|
) |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $schema_oid; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.