-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptHandler.php
193 lines (164 loc) · 4.83 KB
/
ScriptHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace Symfony\BundleGenerator;
use Symfony\Component\Filesystem\Filesystem;
use Composer\Script\Event;
use \Twig_Environment;
use \Twig_Loader_Filesystem;
/**
* Description of ScriptHandler
*
* @author Pierre-Gildas MILLON <[email protected]>
*/
class ScriptHandler
{
const PARAMETER_VENDOR = 'vendor';
const PARAMETER_BUNDLE = 'bundle';
/**
*
* @var Composer\Script\Event
*/
protected $event;
/**
*
* @var Twig_Environment
*/
protected $twig;
protected $parameters = [];
/**
*
* @param Composer\Script\Event $event
*/
public static function buildClasses(Event $event)
{
$handler = new ScriptHandler($event);
$handler->readParameters();
$handler->buildBundleClass();
$handler->buildExtensionClass();
$handler->buildConfigurationClass();
$handler->buildComposerFile();
$handler->buildTestAppKernelFile();
$handler->buildTestServicesFile();
}
/**
*
* @param Composer\Script\Event $event
*/
public function __construct(Event $event)
{
$this->event = $event;
$this->parameters = [
self::PARAMETER_VENDOR => new Parameter('Vendor name', 'Acme'),
self::PARAMETER_BUNDLE => new Parameter('Bundle name', 'MyBundle'),
];
$loader = new Twig_Loader_Filesystem(__DIR__ . '/Resources/templates');
$this->twig = new Twig_Environment($loader);
}
public function readParameters()
{
$io = $this->getEvent()->getIO();
foreach ($this->getParameters() as $param) {
$question = sprintf('<question>%s</question> (<comment>%s</comment>)', $param->getMessage(), $param->getDefaultValue());
$param->setValue($io->ask($question, $param->getDefaultValue()));
}
}
public function buildBundleClass()
{
$this->buildFile('Bundle.php.twig', $this->getBundleClassFile());
}
public function buildExtensionClass()
{
$this->buildFile('Extension.php.twig', $this->getExtensionClassFile());
}
public function buildConfigurationClass()
{
$this->buildFile('Configuration.php.twig', $this->getConfigurationClassFile());
}
public function buildComposerFile()
{
$this->buildFile('composer.json.twig', $this->getComposerFile());
}
public function buildTestAppKernelFile()
{
$this->buildFile('AppKernel.php.twig', $this->getTestAppKernelFile());
}
public function buildTestServicesFile()
{
$this->buildFile('services.xml.twig', $this->getTestServicesFile());
}
protected function buildFile($templateFile, $filename)
{
$fs = new Filesystem();
$content = $this->getTwig()->render($templateFile, ['parameters' => $this->getParameters()]);
$fs->mkdir(dirname($filename));
file_put_contents($filename, $content);
}
/**
*
* @return Composer\Script\Event
*/
public function getEvent()
{
return $this->event;
}
/**
*
* @return Parameter[]
*/
public function getParameters()
{
return $this->parameters;
}
public function getParameter($parameterName)
{
if (isset($this->parameters[$parameterName])) {
$parameter = $this->parameters[$parameterName];
/* @var $parameter Parameter */
return $parameter->getValue();
} else {
return null;
}
}
public function setParameters($parameters)
{
$this->parameters = $parameters;
}
/**
*
* @return Twig_Environment
*/
public function getTwig()
{
return $this->twig;
}
public function getRootDir()
{
return getcwd();
}
public function getBundleClassFile()
{
return $this->getRootDir() . '/' . $this->getParameter(self::PARAMETER_VENDOR) . $this->getParameter(self::PARAMETER_BUNDLE) . '.php';
}
public function getExtensionClassFile()
{
$filename = $this->getParameter(self::PARAMETER_VENDOR);
$filename .= str_replace('Bundle', '', $this->getParameter(self::PARAMETER_BUNDLE));
$filename .= 'Extension';
return $this->getRootDir() . '/DependencyInjection/' . $filename . '.php';
}
public function getConfigurationClassFile()
{
return $this->getRootDir() . '/DependencyInjection/Configuration.php';
}
public function getComposerFile()
{
return $this->getRootDir() . '/composer.json';
}
public function getTestAppKernelFile()
{
return $this->getRootDir() . '/Tests/Fixtures/app/AppKernel.php';
}
public function getTestServicesFile()
{
return $this->getRootDir() . '/Resources/config/services.xml';
}
}