1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enzyme\Axiom\Console\Commands; |
4
|
|
|
|
5
|
|
|
use ICanBoogie\Inflector; |
6
|
|
|
use Enzyme\Axiom\Console\Config; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Enzyme\Axiom\Console\Stubs\Manager as StubManager; |
13
|
|
|
|
14
|
|
|
abstract class BaseCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Reference to the stub manager. |
18
|
|
|
* |
19
|
|
|
* @var \Enzyme\Axiom\Console\Stubs\Manager |
20
|
|
|
*/ |
21
|
|
|
protected $stub_manager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The namespace this generated class falls under. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $namespace; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The location this generated class will be saved at. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $location; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The global configuration manager. |
39
|
|
|
* |
40
|
|
|
* @var \Enzyme\Axiom\Console\Config |
41
|
|
|
*/ |
42
|
|
|
protected $config; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Instantiate a new command. |
46
|
|
|
* |
47
|
|
|
* @param \Enzyme\Axiom\Console\Stubs\Manager $stub_manager |
48
|
|
|
* @param \Enzyme\Axiom\Console\Config $config |
49
|
|
|
*/ |
50
|
|
|
public function __construct(StubManager $stub_manager, Config $config) |
51
|
|
|
{ |
52
|
|
|
parent::__construct(); |
53
|
|
|
|
54
|
|
|
$this->stub_manager = $stub_manager; |
55
|
|
|
$this->config = $config; |
56
|
|
|
$this->location = null; |
57
|
|
|
$this->namespace = null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The generator type this class is handling, eg: "factory". |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
abstract protected function getGeneratorType(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
|
|
protected function configure() |
71
|
|
|
{ |
72
|
|
|
$generator_type = $this->getGeneratorType(); |
73
|
|
|
|
74
|
|
|
$this |
75
|
|
|
->setName("make:{$generator_type}") |
76
|
|
|
->setDescription("Make a new {$generator_type}.") |
77
|
|
|
->addArgument( |
78
|
|
|
'name', |
79
|
|
|
InputArgument::REQUIRED, |
80
|
|
|
"The name prefix on the generated {$generator_type}, eg: \"user\"" |
81
|
|
|
) |
82
|
|
|
->addOption( |
83
|
|
|
'location', |
84
|
|
|
null, |
85
|
|
|
InputOption::VALUE_REQUIRED, |
86
|
|
|
'The location for the generated file.' |
87
|
|
|
) |
88
|
|
|
->addOption( |
89
|
|
|
'namespace', |
90
|
|
|
null, |
91
|
|
|
InputOption::VALUE_REQUIRED, |
92
|
|
|
'The namespace to fall under.' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
* |
99
|
|
|
* @throws Exception If there is no namespace or location defined for the |
100
|
|
|
* class type(s) being generated. |
101
|
|
|
*/ |
102
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
103
|
|
|
{ |
104
|
|
|
$inflector = Inflector::get(Inflector::DEFAULT_LOCALE); |
105
|
|
|
$type_plural = $inflector->pluralize($this->getGeneratorType()); |
106
|
|
|
|
107
|
|
|
$this->namespace = $input->getOption('namespace') !== null |
108
|
|
|
? $input->getOption('namespace') |
109
|
|
|
: $this->config->get("{$type_plural}.namespace"); |
110
|
|
|
|
111
|
|
|
$this->location = $input->getOption('location') !== null |
112
|
|
|
? $input->getOption('location') |
113
|
|
|
: $this->config->get("{$type_plural}.location"); |
114
|
|
|
|
115
|
|
|
if (null === $this->namespace) { |
116
|
|
|
throw new Exception( |
117
|
|
|
"There is no namespace defined for {$type_plural}" |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (null === $this->location) { |
122
|
|
|
throw new Exception( |
123
|
|
|
"There is no location defined for {$type_plural}" |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Execute a generation command - make a single usuable Axiom class. If |
130
|
|
|
* $dont_affix_type is true, do not add the class type to the end of the |
131
|
|
|
* name, this is useful for models which generally are just named as they |
132
|
|
|
* are and don't have "Model" affixed, eg: "User", not "UserModel". |
133
|
|
|
* |
134
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
135
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
136
|
|
|
* @param bool $dont_affix_type |
137
|
|
|
*/ |
138
|
|
|
protected function executeGeneration( |
139
|
|
|
InputInterface $input, |
140
|
|
|
OutputInterface $output, |
141
|
|
|
$dont_affix_type = false |
142
|
|
|
) { |
143
|
|
|
$uc_name = ucfirst($input->getArgument('name')); |
144
|
|
|
$uc_type = ucfirst($this->getGeneratorType()); |
145
|
|
|
|
146
|
|
|
$contents = $this->stub_manager->get($this->getGeneratorType()); |
147
|
|
|
$contents = $this->stub_manager->hydrate($contents, [ |
148
|
|
|
'namespace' => $this->namespace, |
149
|
|
|
'name' => $uc_name, |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
$out_file = $dont_affix_type === true |
153
|
|
|
? "{$this->location}/{$uc_name}.php" |
154
|
|
|
: "{$this->location}/{$uc_name}{$uc_type}.php"; |
155
|
|
|
|
156
|
|
|
$this->stub_manager->writeOut($contents, $out_file); |
157
|
|
|
|
158
|
|
|
$this->printResults( |
159
|
|
|
$output, |
160
|
|
|
$uc_name, |
161
|
|
|
$dont_affix_type |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Print the results of the generate operation to the console window. |
167
|
|
|
* |
168
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
169
|
|
|
* @param string $name |
170
|
|
|
* @param bool $dont_affix_type |
171
|
|
|
*/ |
172
|
|
|
protected function printResults( |
173
|
|
|
OutputInterface $output, |
174
|
|
|
$name, |
175
|
|
|
$dont_affix_type = false |
176
|
|
|
) { |
177
|
|
|
$uc_type = ucfirst($this->getGeneratorType()); |
178
|
|
|
$output->writeln("<info>{$uc_type} created for [{$name}]</info>"); |
179
|
|
|
|
180
|
|
|
$class = $dont_affix_type === true |
181
|
|
|
? $name |
182
|
|
|
: $name . $uc_type; |
183
|
|
|
|
184
|
|
|
if (true === $output->isVerbose()) { |
185
|
|
|
$output->writeln("<comment> Namespace -> {$this->namespace}</comment>"); |
186
|
|
|
$output->writeln("<comment> Class -> {$class}</comment>"); |
187
|
|
|
$output->writeln("<comment> Location -> {$this->location}</comment>"); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|