|
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\Foundation\ParameterHolder; |
|
13
|
|
|
use PommProject\ModelManager\Generator\EntityGenerator; |
|
14
|
|
|
use PommProject\ModelManager\Generator\ModelGenerator; |
|
15
|
|
|
use PommProject\ModelManager\Generator\StructureGenerator; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* GenerateForRelation |
|
23
|
|
|
* |
|
24
|
|
|
* Generate a Structure, a model and an entity class if they do not already |
|
25
|
|
|
* exist (unless --force is specified). |
|
26
|
|
|
* |
|
27
|
|
|
* @package Cli |
|
28
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
29
|
|
|
* @author Grégoire HUBERT |
|
30
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
31
|
|
|
* @see ModelGenerator |
|
32
|
|
|
*/ |
|
33
|
|
|
class GenerateForRelation extends RelationAwareCommand |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* configure |
|
37
|
|
|
* |
|
38
|
|
|
* @see Command |
|
39
|
|
|
*/ |
|
40
|
|
|
public function configure() |
|
41
|
|
|
{ |
|
42
|
|
|
$this |
|
43
|
|
|
->setName('pomm:generate:relation-all') |
|
44
|
|
|
->setDescription('Generate structure, model and entity file for a given relation.') |
|
45
|
|
|
; |
|
46
|
|
|
parent::configure(); |
|
47
|
|
|
$this |
|
48
|
|
|
->addOption( |
|
49
|
|
|
'force', |
|
50
|
|
|
null, |
|
51
|
|
|
InputOption::VALUE_NONE, |
|
52
|
|
|
'Force overwriting existing files.' |
|
53
|
|
|
) |
|
54
|
|
|
->addOption( |
|
55
|
|
|
'psr4', |
|
56
|
|
|
null, |
|
57
|
|
|
InputOption::VALUE_NONE, |
|
58
|
|
|
'Use PSR4 structure.' |
|
59
|
|
|
) |
|
60
|
|
|
->addOption( |
|
61
|
|
|
'path-pattern', |
|
62
|
|
|
null, |
|
63
|
|
|
InputOption::VALUE_REQUIRED, |
|
64
|
|
|
'Use a different directory pattern when generating classes.', |
|
65
|
|
|
'{session}/{schema}Schema' |
|
66
|
|
|
) |
|
67
|
|
|
; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* execute |
|
72
|
|
|
* |
|
73
|
|
|
* @see Command |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
76
|
|
|
{ |
|
77
|
|
|
parent::execute($input, $output); |
|
78
|
|
|
|
|
79
|
|
|
$session = $this->mustBeModelManagerSession($this->getSession()); |
|
80
|
|
|
|
|
81
|
|
|
$this->updateOutput( |
|
82
|
|
|
$output, |
|
83
|
|
|
(new StructureGenerator( |
|
84
|
|
|
$session, |
|
85
|
|
|
$this->schema, |
|
|
|
|
|
|
86
|
|
|
$this->relation, |
|
|
|
|
|
|
87
|
|
|
$this->getPathFile($input->getArgument('config-name'), $this->relation, null, 'AutoStructure', $input->getOption('psr4'), $input->getOption('path-pattern')), |
|
|
|
|
|
|
88
|
|
|
$this->getNamespace($input->getArgument('config-name'), 'AutoStructure', $input->getOption('path-pattern')) |
|
|
|
|
|
|
89
|
|
|
))->generate(new ParameterHolder(array_merge($input->getArguments(), $input->getOptions()))) |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$pathFile = $this->getPathFile($input->getArgument('config-name'), $this->relation, 'Model', '', $input->getOption('psr4'), $input->getOption('path-pattern')); |
|
|
|
|
|
|
93
|
|
View Code Duplication |
if (!file_exists($pathFile) || $input->getOption('force')) { |
|
|
|
|
|
|
94
|
|
|
$this->updateOutput( |
|
95
|
|
|
$output, |
|
96
|
|
|
(new ModelGenerator( |
|
97
|
|
|
$session, |
|
98
|
|
|
$this->schema, |
|
|
|
|
|
|
99
|
|
|
$this->relation, |
|
|
|
|
|
|
100
|
|
|
$pathFile, |
|
101
|
|
|
$this->getNamespace($input->getArgument('config-name'), null, $input->getOption('path-pattern')) |
|
|
|
|
|
|
102
|
|
|
))->generate(new ParameterHolder(array_merge($input->getArguments(), $input->getOptions()))) |
|
103
|
|
|
); |
|
104
|
|
|
} elseif ($output->isVerbose()) { |
|
105
|
|
|
$this->writelnSkipFile($output, $pathFile, 'model'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$pathFile = $this->getPathFile($input->getArgument('config-name'), $this->relation, '', '', $input->getOption('psr4'), $input->getOption('path-pattern')); |
|
|
|
|
|
|
109
|
|
View Code Duplication |
if (!file_exists($pathFile) || $input->getOption('force')) { |
|
|
|
|
|
|
110
|
|
|
$this->updateOutput( |
|
111
|
|
|
$output, |
|
112
|
|
|
(new EntityGenerator( |
|
113
|
|
|
$session, |
|
114
|
|
|
$this->schema, |
|
|
|
|
|
|
115
|
|
|
$this->relation, |
|
|
|
|
|
|
116
|
|
|
$pathFile, |
|
117
|
|
|
$this->getNamespace($input->getArgument('config-name'), null, $input->getOption('path-pattern')), |
|
|
|
|
|
|
118
|
|
|
$this->flexible_container |
|
119
|
|
|
))->generate(new ParameterHolder(array_merge($input->getArguments(), $input->getOptions()))) |
|
120
|
|
|
); |
|
121
|
|
|
} elseif ($output->isVerbose()) { |
|
122
|
|
|
$this->writelnSkipFile($output, $pathFile, 'entity'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return 0; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* writelnSkipFile |
|
130
|
|
|
* |
|
131
|
|
|
* Write an informative message |
|
132
|
|
|
* |
|
133
|
|
|
* @access private |
|
134
|
|
|
* @param OutputInterface $output |
|
135
|
|
|
* @param string $pathFile |
|
136
|
|
|
* @param null|string $file_type |
|
137
|
|
|
*/ |
|
138
|
|
|
private function writelnSkipFile(OutputInterface $output, $pathFile, $file_type = null) |
|
139
|
|
|
{ |
|
140
|
|
|
$file_type = $file_type === null ? '' : sprintf("%s ", $file_type); |
|
141
|
|
|
|
|
142
|
|
|
$output->writeln( |
|
143
|
|
|
sprintf( |
|
144
|
|
|
" <fg=red>✗</fg=red> <fg=blue>Preserving</fg=blue> existing %sfile <fg=yellow>'%s'</fg=yellow>.", |
|
145
|
|
|
$file_type, |
|
146
|
|
|
$pathFile |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.