|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* generate:resource command |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\GeneratorBundle\Command; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\GeneratorBundle\Generator\ResourceGenerator; |
|
9
|
|
|
use Graviton\GeneratorBundle\Definition\Loader\LoaderInterface; |
|
10
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand; |
|
11
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\Validators; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* generator command |
|
19
|
|
|
* |
|
20
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
22
|
|
|
* @link http://swisscom.ch |
|
23
|
|
|
*/ |
|
24
|
|
|
class GenerateResourceCommand extends GeneratorCommand |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Kernel |
|
28
|
|
|
*/ |
|
29
|
|
|
private $kernel; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ResourceGenerator |
|
32
|
|
|
*/ |
|
33
|
|
|
private $resourceGenerator; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var LoaderInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $definitionLoader; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var InputInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $input; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Kernel $kernel kernel |
|
45
|
|
|
* @param ResourceGenerator $resourceGenerator generator to use for resource generation |
|
46
|
|
|
* @param LoaderInterface $definitionLoader JSON definition loaded |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function __construct(Kernel $kernel, ResourceGenerator $resourceGenerator, LoaderInterface $definitionLoader) |
|
49
|
|
|
{ |
|
50
|
4 |
|
$this->kernel = $kernel; |
|
51
|
4 |
|
$this->resourceGenerator = $resourceGenerator; |
|
52
|
4 |
|
$this->definitionLoader = $definitionLoader; |
|
53
|
4 |
|
parent::__construct(); |
|
54
|
4 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
4 |
|
protected function configure() |
|
62
|
|
|
{ |
|
63
|
|
|
$this |
|
64
|
4 |
|
->addOption( |
|
65
|
4 |
|
'entity', |
|
66
|
4 |
|
'', |
|
67
|
4 |
|
InputOption::VALUE_REQUIRED, |
|
68
|
4 |
|
'The entity name' |
|
69
|
|
|
) |
|
70
|
4 |
|
->addOption( |
|
71
|
4 |
|
'json', |
|
72
|
4 |
|
'', |
|
73
|
4 |
|
InputOption::VALUE_REQUIRED, |
|
74
|
4 |
|
'The JSON Payload of the service' |
|
75
|
|
|
) |
|
76
|
4 |
|
->addOption( |
|
77
|
4 |
|
'no-controller', |
|
78
|
4 |
|
'', |
|
79
|
4 |
|
InputOption::VALUE_OPTIONAL, |
|
80
|
4 |
|
'Pass if no controller should be generated' |
|
81
|
|
|
) |
|
82
|
4 |
|
->setName('graviton:generate:resource') |
|
83
|
4 |
|
->setDescription('Generates a graviton rest resource'); |
|
84
|
4 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritDoc} |
|
88
|
|
|
* |
|
89
|
|
|
* @param InputInterface $input input |
|
90
|
|
|
* @param OutputInterface $output output |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
95
|
|
|
{ |
|
96
|
|
|
// put input here for later use.. |
|
97
|
|
|
$this->input = $input; |
|
98
|
|
|
|
|
99
|
|
|
$entity = Validators::validateEntityName($input->getOption('entity')); |
|
100
|
|
|
list($bundle, $entity) = $this->parseShortcutNotation($entity); |
|
101
|
|
|
|
|
102
|
|
|
$bundle = $this->kernel->getBundle($bundle); |
|
103
|
|
|
|
|
104
|
|
|
/** @var DoctrineEntityGenerator $generator */ |
|
105
|
|
|
$generator = $this->getGenerator(); |
|
106
|
|
|
$generatorResult = $generator->generate($bundle, $entity, 'xml', []); |
|
107
|
|
|
|
|
108
|
|
|
$output->writeln( |
|
109
|
|
|
sprintf( |
|
110
|
|
|
'> Generating entity class <info>%s</info>: <comment>OK!</comment>', |
|
111
|
|
|
$generatorResult->getEntityPath() |
|
112
|
|
|
) |
|
113
|
|
|
); |
|
114
|
|
|
$output->writeln( |
|
115
|
|
|
sprintf( |
|
116
|
|
|
'> Generating repository class <info>%s</info>: <comment>OK!</comment>', |
|
117
|
|
|
$generatorResult->getRepositoryPath() |
|
118
|
|
|
) |
|
119
|
|
|
); |
|
120
|
|
|
if ($generatorResult->getMappingPath()) { |
|
121
|
|
|
$output->writeln( |
|
122
|
|
|
sprintf( |
|
123
|
|
|
'> Generating mapping file <info>%s</info>: <comment>OK!</comment>', |
|
124
|
|
|
$generatorResult->getMappingPath() |
|
125
|
|
|
) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritDoc} |
|
132
|
|
|
* |
|
133
|
|
|
* @return ResourceGenerator |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function createGenerator() |
|
136
|
|
|
{ |
|
137
|
|
|
// do we have a json path passed? |
|
138
|
|
|
if ($this->input->getOption('json') !== null) { |
|
139
|
|
|
$definitions = $this->definitionLoader->load($this->input->getOption('json')); |
|
140
|
|
|
if (count($definitions) > 0) { |
|
141
|
|
|
$this->resourceGenerator->setJson($definitions[0]); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->resourceGenerator->setGenerateController( |
|
146
|
|
|
$this->input->getOption('no-controller') != 'true' |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
return $this->resourceGenerator; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* parses bundle shortcut notation |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $shortcut shortcut |
|
156
|
|
|
* |
|
157
|
|
|
* @return array parsed name |
|
|
|
|
|
|
158
|
|
|
*/ |
|
159
|
|
|
protected function parseShortcutNotation($shortcut) |
|
160
|
|
|
{ |
|
161
|
|
|
$entity = str_replace('/', '\\', $shortcut); |
|
162
|
|
|
|
|
163
|
|
|
if (false === $pos = strpos($entity, ':')) { |
|
164
|
|
|
throw new \InvalidArgumentException( |
|
165
|
|
|
sprintf( |
|
166
|
|
|
'The entity name must contain a : ("%s" given, expecting '. |
|
167
|
|
|
'something like AcmeBlogBundle:Blog/Post)', |
|
168
|
|
|
$entity |
|
169
|
|
|
) |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return array(substr($entity, 0, $pos), substr($entity, $pos + 1)); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.