Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 649 lines (544 sloc) 20.976 kB
d2b2a18 @fabpot added files
fabpot authored
1 <?php
2
87ad04f @willdurand Fixed LICENSE
willdurand authored
3 /**
4 * This file is part of the PropelBundle package.
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @license MIT License
9 */
10
5b5feec @willdurand Changed namespace to Propel\PropelBundle
willdurand authored
11 namespace Propel\PropelBundle\Command;
d2b2a18 @fabpot added files
fabpot authored
12
c9cfae5 @willdurand Fixed PhingCommand class to fit last Symfony2 changes. Fixed #17
willdurand authored
13 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
d2b2a18 @fabpot added files
fabpot authored
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
d12c092 @willdurand Use the new Filesystem component
willdurand authored
16 use Symfony\Component\Filesystem\Filesystem;
d2b2a18 @fabpot added files
fabpot authored
17 use Symfony\Component\Finder\Finder;
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
18 use Symfony\Component\HttpKernel\KernelInterface;
beab50c @havvg fix resulting package names to not include folder
havvg authored
19 use Symfony\Component\HttpKernel\Bundle\Bundle;
f35a683 @docteurklein use FileLocator to handle bundle's schema inheritance
docteurklein authored
20 use Symfony\Component\HttpKernel\Bundle\BundleInterface;
d2b2a18 @fabpot added files
fabpot authored
21
22 /**
4dcdc0f @willdurand [command] Naming + minor fixes on commands
willdurand authored
23 * Wrapper for Propel commands.
d2b2a18 @fabpot added files
fabpot authored
24 *
25 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
4d98e51 @willdurand Added getters
willdurand authored
26 * @author William DURAND <william.durand1@gmail.com>
d2b2a18 @fabpot added files
fabpot authored
27 */
29ca595 @willdurand Removed 'Propel' class prefixes
willdurand authored
28 abstract class AbstractCommand extends ContainerAwareCommand
d2b2a18 @fabpot added files
fabpot authored
29 {
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
30 /**
31 * Additional Phing args to add in specialized commands.
32 * @var array
33 */
d2b2a18 @fabpot added files
fabpot authored
34 protected $additionalPhingArgs = array();
ab50b5e @willdurand Fixed CS
willdurand authored
35
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
36 /**
37 * Temporary XML schemas used on command execution.
38 * @var array
39 */
d2b2a18 @fabpot added files
fabpot authored
40 protected $tempSchemas = array();
ab50b5e @willdurand Fixed CS
willdurand authored
41
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
42 /**
43 * @var string
44 */
ccb5f8c @willdurand Fixed naming
willdurand authored
45 protected $cacheDir = null;
ab50b5e @willdurand Fixed CS
willdurand authored
46
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
47 /**
48 * The Phing output.
49 * @string
50 */
ed1d9ad @willdurand Improved some commands
willdurand authored
51 protected $buffer = null;
d2b2a18 @fabpot added files
fabpot authored
52
bd64c55 @willdurand Refactored PhingCommand to check Propel database configuration before…
willdurand authored
53 /**
a30747e @willdurand Added a better way to use a bundle argument in commands
willdurand authored
54 * @var Symfony\Component\HttpKernel\Bundle\BundleInterface
55 */
56 protected $bundle = null;
57
58 /**
590787d @willdurand Removed command headers, fixed naming
willdurand authored
59 * @var Boolean
60 */
61 private $alreadyWroteConnection = false;
62
63 /**
a8bb6a2 @jaugustin add connection option for commands: build, sql:build, migration:gener…
jaugustin authored
64 *
65 * @var InputInterface
66 */
67 protected $input;
68
69 /**
ab50b5e @willdurand Fixed CS
willdurand authored
70 * Return the package prefix for a given bundle.
71 *
72 * @param Bundle $bundle
73 * @param string $baseDirectory The base directory to exclude from prefix.
74 *
75 * @return string
76 */
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
77 protected function getPackagePrefix(Bundle $bundle, $baseDirectory = '')
ab50b5e @willdurand Fixed CS
willdurand authored
78 {
79 $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
80 $length = count(explode('\\', $bundle->getNamespace())) * (-1);
81
a7ed277 @willdurand Fixed getPackagePrefix() method (fixes a bug on Windows platforms)
willdurand authored
82 $prefix = implode(DIRECTORY_SEPARATOR, array_slice($parts, 0, $length));
ab50b5e @willdurand Fixed CS
willdurand authored
83 $prefix = ltrim(str_replace($baseDirectory, '', $prefix), DIRECTORY_SEPARATOR);
84
85 if (!empty($prefix)) {
86 $prefix = str_replace(DIRECTORY_SEPARATOR, '.', $prefix).'.';
87 }
88
89 return $prefix;
90 }
91
92 /**
bd64c55 @willdurand Refactored PhingCommand to check Propel database configuration before…
willdurand authored
93 * {@inheritdoc}
94 */
95 protected function initialize(InputInterface $input, OutputInterface $output)
96 {
97 parent::initialize($input, $output);
98
a8bb6a2 @jaugustin add connection option for commands: build, sql:build, migration:gener…
jaugustin authored
99 $this->input = $input;
100
bd64c55 @willdurand Refactored PhingCommand to check Propel database configuration before…
willdurand authored
101 $this->checkConfiguration();
a30747e @willdurand Added a better way to use a bundle argument in commands
willdurand authored
102
103 if ($input->hasArgument('bundle') && '@' === substr($input->getArgument('bundle'), 0, 1)) {
104 $this->bundle = $this
105 ->getContainer()
106 ->get('kernel')
107 ->getBundle(substr($input->getArgument('bundle'), 1));
108 }
bd64c55 @willdurand Refactored PhingCommand to check Propel database configuration before…
willdurand authored
109 }
110
111 /**
112 * Call a Phing task.
113 *
6970913 @willdurand Fixed CS
willdurand authored
114 * @param string $taskName A Propel task name.
115 * @param array $properties An array of properties to pass to Phing.
bd64c55 @willdurand Refactored PhingCommand to check Propel database configuration before…
willdurand authored
116 */
d2b2a18 @fabpot added files
fabpot authored
117 protected function callPhing($taskName, $properties = array())
118 {
d7789af @willdurand Use array access for the PropelConfiguration object
willdurand authored
119 $kernel = $this->getApplication()->getKernel();
d2b2a18 @fabpot added files
fabpot authored
120
4a6088b @willdurand Added new command: 'data-sql' that generates SQL from dumped data
willdurand authored
121 if (isset($properties['propel.schema.dir'])) {
ccb5f8c @willdurand Fixed naming
willdurand authored
122 $this->cacheDir = $properties['propel.schema.dir'];
4a6088b @willdurand Added new command: 'data-sql' that generates SQL from dumped data
willdurand authored
123 } else {
b56f80a @havvg use configured cache directory
havvg authored
124 $this->cacheDir = $kernel->getCacheDir().'/propel';
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
125
126 $filesystem = new Filesystem();
ccb5f8c @willdurand Fixed naming
willdurand authored
127 $filesystem->remove($this->cacheDir);
128 $filesystem->mkdir($this->cacheDir);
4a6088b @willdurand Added new command: 'data-sql' that generates SQL from dumped data
willdurand authored
129 }
bcd3c9d @willdurand Minor change in PhingCommand
willdurand authored
130
ccb5f8c @willdurand Fixed naming
willdurand authored
131 $this->copySchemas($kernel, $this->cacheDir);
d2b2a18 @fabpot added files
fabpot authored
132
69c14b8 @willdurand Added a new configuration file: app/config/propel.ini which is a buil…
willdurand authored
133 // build.properties
ccb5f8c @willdurand Fixed naming
willdurand authored
134 $this->createBuildPropertiesFile($kernel, $this->cacheDir.'/build.properties');
d2b2a18 @fabpot added files
fabpot authored
135
77d338f @lucasaba Correct a typo in a comment in AbstractPropelCommand
lucasaba authored
136 // buildtime-conf.xml
ccb5f8c @willdurand Fixed naming
willdurand authored
137 $this->createBuildTimeFile($this->cacheDir.'/buildtime-conf.xml');
d2b2a18 @fabpot added files
fabpot authored
138
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
139 // Verbosity
140 $bufferPhingOutput = $this->getContainer()->getParameter('kernel.debug');
d2b2a18 @fabpot added files
fabpot authored
141
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
142 // Phing arguments
ccb5f8c @willdurand Fixed naming
willdurand authored
143 $args = $this->getPhingArguments($kernel, $this->cacheDir, $properties);
4a6088b @willdurand Added new command: 'data-sql' that generates SQL from dumped data
willdurand authored
144
d2b2a18 @fabpot added files
fabpot authored
145 // Add any arbitrary arguments last
146 foreach ($this->additionalPhingArgs as $arg) {
147 if (in_array($arg, array('verbose', 'debug'))) {
148 $bufferPhingOutput = false;
149 }
150
151 $args[] = '-'.$arg;
152 }
153
154 $args[] = $taskName;
155
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
156 // Enable output buffering
d2b2a18 @fabpot added files
fabpot authored
157 Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w')));
4c07e38 @willdurand Added a way to detect errors during Phing execution
willdurand authored
158 Phing::setErrorStream(new \OutputStream(fopen('php://output', 'w')));
d2b2a18 @fabpot added files
fabpot authored
159 Phing::startup();
160 Phing::setProperty('phing.home', getenv('PHING_HOME'));
161
4c07e38 @willdurand Added a way to detect errors during Phing execution
willdurand authored
162 ob_start();
163
164 $phing = new Phing();
165 $returnStatus = true; // optimistic way
d2b2a18 @fabpot added files
fabpot authored
166
4c07e38 @willdurand Added a way to detect errors during Phing execution
willdurand authored
167 try {
168 $phing->execute($args);
169 $phing->runBuild();
d2b2a18 @fabpot added files
fabpot authored
170
ed1d9ad @willdurand Improved some commands
willdurand authored
171 $this->buffer = ob_get_contents();
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
172
173 // Guess errors
cef9035 @willdurand Better Phing error detector
willdurand authored
174 if (strstr($this->buffer, 'failed. Aborting.') ||
85849bf @willdurand [command] Fixed error detection for insert-sql
willdurand authored
175 strstr($this->buffer, 'Failed to execute') ||
cef9035 @willdurand Better Phing error detector
willdurand authored
176 strstr($this->buffer, 'failed for the following reason:')) {
177 $returnStatus = false;
178 }
6970913 @willdurand Fixed CS
willdurand authored
179 } catch (\Exception $e) {
4c07e38 @willdurand Added a way to detect errors during Phing execution
willdurand authored
180 $returnStatus = false;
d2b2a18 @fabpot added files
fabpot authored
181 }
4a6088b @willdurand Added new command: 'data-sql' that generates SQL from dumped data
willdurand authored
182
096faad @willdurand Added shortcut to use dialog helper
willdurand authored
183 if ($bufferPhingOutput) {
184 ob_end_clean();
185 } else {
186 ob_end_flush();
187 }
4c07e38 @willdurand Added a way to detect errors during Phing execution
willdurand authored
188
189 return $returnStatus;
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
190 }
d2b2a18 @fabpot added files
fabpot authored
191
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
192 /**
6970913 @willdurand Fixed CS
willdurand authored
193 * @param KernelInterface $kernel The application kernel.
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
194 */
ccb5f8c @willdurand Fixed naming
willdurand authored
195 protected function copySchemas(KernelInterface $kernel, $cacheDir)
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
196 {
197 $filesystem = new Filesystem();
198
ccb5f8c @willdurand Fixed naming
willdurand authored
199 if (!is_dir($cacheDir)) {
200 $filesystem->mkdir($cacheDir);
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
201 }
202
beab50c @havvg fix resulting package names to not include folder
havvg authored
203 $base = ltrim(realpath($kernel->getRootDir().'/..'), DIRECTORY_SEPARATOR);
204
a30747e @willdurand Added a better way to use a bundle argument in commands
willdurand authored
205 $finalSchemas = $this->getFinalSchemas($kernel, $this->bundle);
f35a683 @docteurklein use FileLocator to handle bundle's schema inheritance
docteurklein authored
206 foreach ($finalSchemas as $schema) {
207 list($bundle, $finalSchema) = $schema;
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
208 $packagePrefix = $this->getPackagePrefix($bundle, $base);
f35a683 @docteurklein use FileLocator to handle bundle's schema inheritance
docteurklein authored
209
210 $tempSchema = $bundle->getName().'-'.$finalSchema->getBaseName();
211 $this->tempSchemas[$tempSchema] = array(
212 'bundle' => $bundle->getName(),
213 'basename' => $finalSchema->getBaseName(),
214 'path' => $finalSchema->getPathname(),
215 );
216
217 $file = $cacheDir.DIRECTORY_SEPARATOR.$tempSchema;
218 $filesystem->copy((string) $finalSchema, $file, true);
219
220 // the package needs to be set absolute
221 // besides, the automated namespace to package conversion has
222 // not taken place yet so it needs to be done manually
223 $database = simplexml_load_file($file);
224
225 if (isset($database['package'])) {
226 // Do not use the prefix!
227 // This is used to override the package resulting from namespace conversion.
228 $database['package'] = $database['package'];
229 } elseif (isset($database['namespace'])) {
230 $database['package'] = $packagePrefix . str_replace('\\', '.', $database['namespace']);
231 } else {
232 throw new \RuntimeException(
233 sprintf('%s : Please define a `package` attribute or a `namespace` attribute for schema `%s`',
234 $bundle->getName(), $finalSchema->getBaseName())
235 );
236 }
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
237
a8bb6a2 @jaugustin add connection option for commands: build, sql:build, migration:gener…
jaugustin authored
238 if ($this->input && $this->input->hasOption('connection') && $this->input->getOption('connection')
239 && $database['name'] != $this->input->getOption('connection')) {
240 //we skip this schema because the connection name doesn't match the input value
7ff2441 @jaugustin fix display message
jaugustin authored
241 unset($this->tempSchemas[$tempSchema]);
a8bb6a2 @jaugustin add connection option for commands: build, sql:build, migration:gener…
jaugustin authored
242 $filesystem->remove($file);
243 continue;
244 }
245
f35a683 @docteurklein use FileLocator to handle bundle's schema inheritance
docteurklein authored
246 foreach ($database->table as $table) {
247 if (isset($table['package'])) {
248 $table['package'] = $table['package'];
249 } elseif (isset($table['namespace'])) {
250 $table['package'] = $packagePrefix . str_replace('\\', '.', $table['namespace']);
251 } else {
252 $table['package'] = $database['package'];
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
253 }
254 }
f35a683 @docteurklein use FileLocator to handle bundle's schema inheritance
docteurklein authored
255
256 file_put_contents($file, $database->asXML());
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
257 }
258 }
259
beab50c @havvg fix resulting package names to not include folder
havvg authored
260 /**
4e45bab @havvg add InitAclCommand
havvg authored
261 * Return a list of final schema files that will be processed.
262 *
263 * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
264 *
265 * @return array
266 */
a30747e @willdurand Added a better way to use a bundle argument in commands
willdurand authored
267 protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null)
4e45bab @havvg add InitAclCommand
havvg authored
268 {
a30747e @willdurand Added a better way to use a bundle argument in commands
willdurand authored
269 if (null !== $bundle) {
270 return $this->getSchemasFromBundle($bundle);
271 }
272
4e45bab @havvg add InitAclCommand
havvg authored
273 $finalSchemas = array();
274 foreach ($kernel->getBundles() as $bundle) {
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
275 $finalSchemas = array_merge($finalSchemas, $this->getSchemasFromBundle($bundle));
276 }
4e45bab @havvg add InitAclCommand
havvg authored
277
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
278 return $finalSchemas;
279 }
4e45bab @havvg add InitAclCommand
havvg authored
280
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
281 /**
282 * @param \Symfony\Component\HttpKernel\Bundle\BundleInterface
283 */
284 protected function getSchemasFromBundle(BundleInterface $bundle)
285 {
286 $finalSchemas = array();
287
288 if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
289 $finder = new Finder();
290 $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
291
292 if (iterator_count($schemas)) {
293 foreach ($schemas as $schema) {
4e45bab @havvg add InitAclCommand
havvg authored
294 $logicalName = $this->transformToLogicalName($schema, $bundle);
295 $finalSchema = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
296
4219e70 @Adel-E RUN PHP-CS PSR-2
Adel-E authored
297 $finalSchemas[(string) $finalSchema] = array($bundle, $finalSchema);
4e45bab @havvg add InitAclCommand
havvg authored
298 }
299 }
300 }
301
302 return $finalSchemas;
303 }
304
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
305 /**
4219e70 @Adel-E RUN PHP-CS PSR-2
Adel-E authored
306 * @param \SplFileInfo $file
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
307 * @return string
308 */
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
309 protected function getRelativeFileName(\SplFileInfo $file)
310 {
311 return substr(str_replace(realpath($this->getContainer()->getParameter('kernel.root_dir') . '/../'), '', $file), 1);
312 }
313
4e45bab @havvg add InitAclCommand
havvg authored
314 /**
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
315 * Create a 'build.properties' file.
316 *
6970913 @willdurand Fixed CS
willdurand authored
317 * @param KernelInterface $kernel The application kernel.
318 * @param string $file Should be 'build.properties'.
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
319 */
320 protected function createBuildPropertiesFile(KernelInterface $kernel, $file)
321 {
322 $filesystem = new Filesystem();
323 $buildPropertiesFile = $kernel->getRootDir().'/config/propel.ini';
324
325 if (file_exists($buildPropertiesFile)) {
326 $filesystem->copy($buildPropertiesFile, $file);
327 } else {
328 $filesystem->touch($file);
329 }
330 }
331
332 /**
333 * Create an XML file which represents propel.configuration
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
334 *
6970913 @willdurand Fixed CS
willdurand authored
335 * @param string $file Should be 'buildtime-conf.xml'.
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
336 */
337 protected function createBuildTimeFile($file)
338 {
c9cfae5 @willdurand Fixed PhingCommand class to fit last Symfony2 changes. Fixed #17
willdurand authored
339 $container = $this->getContainer();
d2b2a18 @fabpot added files
fabpot authored
340
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
341 if (!$container->has('propel.configuration')) {
aa7eb61 @willdurand Fixed PhingCommand
willdurand authored
342 throw new \InvalidArgumentException('Could not find Propel configuration.');
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
343 }
d2b2a18 @fabpot added files
fabpot authored
344
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
345 $xml = strtr(<<<EOT
346 <?xml version="1.0"?>
347 <config>
348 <propel>
349 <datasources default="%default_connection%">
350
351 EOT
352 , array('%default_connection%' => $container->getParameter('propel.dbal.default_connection')));
353
d7789af @willdurand Use array access for the PropelConfiguration object
willdurand authored
354 $propelConfiguration = $container->get('propel.configuration');
355 foreach ($propelConfiguration['datasources'] as $name => $datasource) {
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
356 $xml .= strtr(<<<EOT
357 <datasource id="%name%">
358 <adapter>%adapter%</adapter>
359 <connection>
360 <dsn>%dsn%</dsn>
361 <user>%username%</user>
362 <password>%password%</password>
363 </connection>
364 </datasource>
365
366 EOT
367 , array(
368 '%name%' => $name,
369 '%adapter%' => $datasource['adapter'],
370 '%dsn%' => $datasource['connection']['dsn'],
371 '%username%' => $datasource['connection']['user'],
4e28a76 @willdurand Avoid error if blank password in datasource config
willdurand authored
372 '%password%' => isset($datasource['connection']['password']) ? $datasource['connection']['password'] : '',
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
373 ));
d2b2a18 @fabpot added files
fabpot authored
374 }
375
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
376 $xml .= <<<EOT
377 </datasources>
378 </propel>
379 </config>
380 EOT;
d2b2a18 @fabpot added files
fabpot authored
381
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
382 file_put_contents($file, $xml);
d2b2a18 @fabpot added files
fabpot authored
383 }
384
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
385 /**
386 * Returns an array of properties as key/value pairs from an input file.
aa7eb61 @willdurand Fixed PhingCommand
willdurand authored
387 *
4219e70 @Adel-E RUN PHP-CS PSR-2
Adel-E authored
388 * @param string $file A file properties.
389 * @return array An array of properties as key/value pairs.
a6dc926 @willdurand Cleaned PhingCommand class + added new method to create a 'buildtime-…
willdurand authored
390 */
d2b2a18 @fabpot added files
fabpot authored
391 protected function getProperties($file)
392 {
393 $properties = array();
394
395 if (false === $lines = @file($file)) {
4dcdc0f @willdurand [command] Naming + minor fixes on commands
willdurand authored
396 throw new \Exception(sprintf('Unable to parse contents of "%s".', $file));
d2b2a18 @fabpot added files
fabpot authored
397 }
398
399 foreach ($lines as $line) {
400 $line = trim($line);
401
4dcdc0f @willdurand [command] Naming + minor fixes on commands
willdurand authored
402 if ('' == $line || in_array($line[0], array('#', ';'))) {
d2b2a18 @fabpot added files
fabpot authored
403 continue;
404 }
405
f68972f @willdurand Fixed PropelGeneratorAwareCommand to use the propel.ini file
willdurand authored
406 $pos = strpos($line, '=');
407 $property = trim(substr($line, 0, $pos));
408 $value = trim(substr($line, $pos + 1));
409
410 if ("true" === $value) {
411 $value = true;
6970913 @willdurand Fixed CS
willdurand authored
412 } elseif ("false" === $value) {
f68972f @willdurand Fixed PropelGeneratorAwareCommand to use the propel.ini file
willdurand authored
413 $value = false;
414 }
415
416 $properties[$property] = $value;
d2b2a18 @fabpot added files
fabpot authored
417 }
418
419 return $properties;
420 }
db27ed0 @willdurand Added tmpDir as an attribute
willdurand authored
421
bcd3c9d @willdurand Minor change in PhingCommand
willdurand authored
422 /**
ccb5f8c @willdurand Fixed naming
willdurand authored
423 * Return the current Propel cache directory.
4219e70 @Adel-E RUN PHP-CS PSR-2
Adel-E authored
424 * @return string The current Propel cache directory.
bcd3c9d @willdurand Minor change in PhingCommand
willdurand authored
425 */
ab50b5e @willdurand Fixed CS
willdurand authored
426 protected function getCacheDir()
427 {
ccb5f8c @willdurand Fixed naming
willdurand authored
428 return $this->cacheDir;
db27ed0 @willdurand Added tmpDir as an attribute
willdurand authored
429 }
580f7ae @willdurand Refactored command outputs
willdurand authored
430
431 /**
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
432 * @return \Symfony\Component\Config\FileLocatorInterface
433 */
434 protected function getFileLocator()
435 {
436 return $this->getContainer()->get('file_locator');
437 }
438
439 /**
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
440 * Get connection by checking the input option named 'connection'.
7fbd755 @willdurand Changed getConnection return to get the name of the connection
willdurand authored
441 * Returns the default connection if no option specified or an exception
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
442 * if the specified connection doesn't exist.
443 *
6970913 @willdurand Fixed CS
willdurand authored
444 * @param InputInterface $input
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
445 * @param OutputInterface $output
446 * @throw \InvalidArgumentException If the connection does not exist.
7fbd755 @willdurand Changed getConnection return to get the name of the connection
willdurand authored
447 * @return array
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
448 */
ab50b5e @willdurand Fixed CS
willdurand authored
449 protected function getConnection(InputInterface $input, OutputInterface $output)
450 {
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
451 $propelConfiguration = $this->getContainer()->get('propel.configuration');
452 $name = $input->getOption('connection') ?: $this->getContainer()->getParameter('propel.dbal.default_connection');
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
453
454 if (isset($propelConfiguration['datasources'][$name])) {
455 $defaultConfig = $propelConfiguration['datasources'][$name];
456 } else {
457 throw new \InvalidArgumentException(sprintf('Connection named %s doesn\'t exist', $name));
458 }
459
590787d @willdurand Removed command headers, fixed naming
willdurand authored
460 if (false === $this->alreadyWroteConnection) {
461 $output->writeln(sprintf('Use connection named <comment>%s</comment> in <comment>%s</comment> environment.',
462 $name, $this->getApplication()->getKernel()->getEnvironment())
463 );
464 $this->alreadyWroteConnection = true;
465 }
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
466
27b0f3c @willdurand [Command] Prevent error if no password found
willdurand authored
467 // prevent errors
468 if (!isset($defaultConfig['connection']['password'])) {
469 $defaultConfig['connection']['password'] = null;
470 }
471
7fbd755 @willdurand Changed getConnection return to get the name of the connection
willdurand authored
472 return array($name, $defaultConfig);
f5b580e @willdurand Refactored commands, added a new method in PhingCommand to handle '--…
willdurand authored
473 }
474
475 /**
c7f5ed7 @willdurand Added a method to get the dbname in Command classes
willdurand authored
476 * Extract the database name from a given DSN
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
477 *
4219e70 @Adel-E RUN PHP-CS PSR-2
Adel-E authored
478 * @param string $dsn A DSN
479 * @return string The database name extracted from the given DSN
c7f5ed7 @willdurand Added a method to get the dbname in Command classes
willdurand authored
480 */
ab50b5e @willdurand Fixed CS
willdurand authored
481 protected function parseDbName($dsn)
482 {
c7f5ed7 @willdurand Added a method to get the dbname in Command classes
willdurand authored
483 preg_match('#dbname=([a-zA-Z0-9\_]+)#', $dsn, $matches);
ab50b5e @willdurand Fixed CS
willdurand authored
484
c0221a6 @willdurand Fixed issue when no dbname provided in the DSN
willdurand authored
485 if (isset($matches[1])) {
486 return $matches[1];
487 }
488
489 // e.g. SQLite
490 return null;
c7f5ed7 @willdurand Added a method to get the dbname in Command classes
willdurand authored
491 }
492
493 /**
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
494 * Check the PropelConfiguration object.
495 */
496 protected function checkConfiguration()
497 {
498 $parameters = $this->getContainer()->get('propel.configuration')->getParameters();
499
500 if (!isset($parameters['datasources']) || 0 === count($parameters['datasources'])) {
501 throw new \RuntimeException('Propel should be configured (no database configuration found).');
502 }
503 }
504
505 /**
506 * Write Propel output as summary based on a Regexp.
580f7ae @willdurand Refactored command outputs
willdurand authored
507 *
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
508 * @param OutputInterface $output The output object.
6970913 @willdurand Fixed CS
willdurand authored
509 * @param string $taskname A task name
580f7ae @willdurand Refactored command outputs
willdurand authored
510 */
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
511 protected function writeSummary(OutputInterface $output, $taskname)
512 {
580f7ae @willdurand Refactored command outputs
willdurand authored
513 foreach (explode("\n", $this->buffer) as $line) {
514 if (false !== strpos($line, '[' . $taskname . ']')) {
515 $arr = preg_split('#\[' . $taskname . '\] #', $line);
516 $info = $arr[1];
517
518 if ('"' === $info[0]) {
519 $info = sprintf('<info>%s</info>', $info);
520 }
521
522 $output->writeln($info);
523 }
524 }
525 }
bd64c55 @willdurand Refactored PhingCommand to check Propel database configuration before…
willdurand authored
526
527 /**
4f1cc4c @willdurand Fixed PhingCommand + added a writeSection() method
willdurand authored
528 * Comes from the SensioGeneratorBundle.
529 * @see https://github.com/sensio/SensioGeneratorBundle/blob/master/Command/Helper/DialogHelper.php#L52
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
530 *
6970913 @willdurand Fixed CS
willdurand authored
531 * @param OutputInterface $output The output.
532 * @param string $text A text message.
533 * @param string $style A style to apply on the section.
4f1cc4c @willdurand Fixed PhingCommand + added a writeSection() method
willdurand authored
534 */
3a0d86b @willdurand [command] Refactored the base command
willdurand authored
535 protected function writeSection(OutputInterface $output, $text, $style = 'bg=blue;fg=white')
4f1cc4c @willdurand Fixed PhingCommand + added a writeSection() method
willdurand authored
536 {
537 $output->writeln(array(
538 '',
539 $this->getHelperSet()->get('formatter')->formatBlock($text, $style, true),
540 '',
541 ));
542 }
096faad @willdurand Added shortcut to use dialog helper
willdurand authored
543
544 /**
af21516 @willdurand [command] Fixed console output
willdurand authored
545 * Renders an error message if a task has failed.
546 *
f18df26 @willdurand [command] Fixed output (for real)
willdurand authored
547 * @param OutputInterface $output The output.
6970913 @willdurand Fixed CS
willdurand authored
548 * @param string $taskName A task name.
549 * @param Boolean $more Whether to add a 'more details' message or not.
af21516 @willdurand [command] Fixed console output
willdurand authored
550 */
f18df26 @willdurand [command] Fixed output (for real)
willdurand authored
551 protected function writeTaskError($output, $taskName, $more = true)
af21516 @willdurand [command] Fixed console output
willdurand authored
552 {
553 $moreText = $more ? ' To get more details, run the command with the "--verbose" option.' : '';
554
555 return $this->writeSection($output, array(
556 '[Propel] Error',
557 '',
558 'An error has occured during the "' . $taskName . '" task process.' . $moreText
559 ), 'fg=white;bg=red');
560 }
f18df26 @willdurand [command] Fixed output (for real)
willdurand authored
561
ab50b5e @willdurand Fixed CS
willdurand authored
562 /**
f18df26 @willdurand [command] Fixed output (for real)
willdurand authored
563 * @param OutputInterface $output The output.
6970913 @willdurand Fixed CS
willdurand authored
564 * @param string $filename The filename.
ab50b5e @willdurand Fixed CS
willdurand authored
565 */
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
566 protected function writeNewFile(OutputInterface $output, $filename)
ab50b5e @willdurand Fixed CS
willdurand authored
567 {
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
568 $output->writeln('>> <info>File+</info> ' . $filename);
ab50b5e @willdurand Fixed CS
willdurand authored
569 }
4dcdc0f @willdurand [command] Naming + minor fixes on commands
willdurand authored
570
571 /**
6970913 @willdurand Fixed CS
willdurand authored
572 * @param OutputInterface $output The output.
573 * @param string $directory The directory.
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
574 */
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
575 protected function writeNewDirectory(OutputInterface $output, $directory)
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
576 {
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
577 $output->writeln('>> <info>Dir+</info> ' . $directory);
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
578 }
579
580 /**
4dcdc0f @willdurand [command] Naming + minor fixes on commands
willdurand authored
581 * Ask confirmation from the user.
582 *
583 * @param OutputInterface $output The output.
6970913 @willdurand Fixed CS
willdurand authored
584 * @param string $question A given question.
585 * @param string $default A default response.
4dcdc0f @willdurand [command] Naming + minor fixes on commands
willdurand authored
586 */
587 protected function askConfirmation(OutputInterface $output, $question, $default = null)
588 {
589 return $this->getHelperSet()->get('dialog')->askConfirmation($output, $question, $default);
590 }
ab50b5e @willdurand Fixed CS
willdurand authored
591
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
592 /**
4219e70 @Adel-E RUN PHP-CS PSR-2
Adel-E authored
593 * @param \SplFileInfo $schema
594 * @param BundleInterface $bundle
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
595 * @return string
0b85aba @willdurand [Command] Added a new command named: propel:form:generate
willdurand authored
596 */
dd13a0f @willdurand Added few unit tests, fixed CS
willdurand authored
597 protected function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
ab50b5e @willdurand Fixed CS
willdurand authored
598 {
a30747e @willdurand Added a better way to use a bundle argument in commands
willdurand authored
599 $schemaPath = str_replace(
600 $bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR,
601 '',
602 $schema->getRealPath()
603 );
ab50b5e @willdurand Fixed CS
willdurand authored
604
605 return sprintf('@%s/Resources/config/%s', $bundle->getName(), $schemaPath);
606 }
607
608 /**
609 * Compiles arguments/properties for the Phing process.
610 * @return array
611 */
612 private function getPhingArguments(KernelInterface $kernel, $workingDirectory, $properties)
613 {
614 $args = array();
615
616 // Default properties
617 $properties = array_merge(array(
618 'propel.database' => 'mysql',
619 'project.dir' => $workingDirectory,
620 'propel.output.dir' => $kernel->getRootDir().'/propel',
621 'propel.php.dir' => $kernel->getRootDir().'/..',
622 'propel.packageObjectModel' => true,
69cbd01 @willdurand Add more default values
willdurand authored
623 'propel.useDateTimeClass' => true,
624 'propel.dateTimeClass' => 'DateTime',
625 'propel.defaultTimeFormat' => '',
626 'propel.defaultDateFormat' => '',
8299a76 @willdurand Add new default parameter 'addClassLevelComment'
willdurand authored
627 'propel.addClassLevelComment' => false,
69cbd01 @willdurand Add more default values
willdurand authored
628 'propel.defaultTimeStampFormat' => '',
629 'propel.builder.pluralizer.class' => 'builder.util.StandardEnglishPluralizer',
ab50b5e @willdurand Fixed CS
willdurand authored
630 ), $properties);
631
632 // Adding user defined properties from the configuration
633 $properties = array_merge(
634 $properties,
635 $this->getContainer()->get('propel.build_properties')->getProperties()
636 );
637
638 foreach ($properties as $key => $value) {
639 $args[] = "-D$key=$value";
640 }
641
642 // Build file
643 $args[] = '-f';
644 $args[] = realpath($this->getContainer()->getParameter('propel.path').'/generator/build.xml');
645
646 return $args;
647 }
d2b2a18 @fabpot added files
fabpot authored
648 }
Something went wrong with that request. Please try again.