BuildCommand.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Symfony\Framework\PropelBundle\Command;
  3. use Symfony\Components\Console\Command\Command;
  4. use Symfony\Components\Console\Input\InputArgument;
  5. use Symfony\Components\Console\Input\InputOption;
  6. use Symfony\Components\Console\Input\InputInterface;
  7. use Symfony\Components\Console\Output\OutputInterface;
  8. use Symfony\Components\Console\Output\Output;
  9. use Symfony\Framework\WebBundle\Util\Filesystem;
  10. use Symfony\Components\Finder\Finder;
  11. /*
  12. * This file is part of the Symfony framework.
  13. *
  14. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  15. *
  16. * This source file is subject to the MIT license that is bundled
  17. * with this source code in the file LICENSE.
  18. */
  19. /**
  20. * BuildCommand.
  21. *
  22. * @package Symfony
  23. * @subpackage Framework_PropelBundle
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. */
  26. class BuildCommand extends Command
  27. {
  28. protected $additionalPhingArgs = array();
  29. /**
  30. * @see Command
  31. */
  32. protected function configure()
  33. {
  34. $this
  35. ->setDefinition(array(
  36. new InputOption('--classes', '', InputOption::PARAMETER_NONE, 'Build all classes'),
  37. ))
  38. ->setName('propel:build')
  39. ;
  40. }
  41. /**
  42. * @see Command
  43. *
  44. * @throws \InvalidArgumentException When the target directory does not exist
  45. */
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. $this->callPhing('om');
  49. }
  50. protected function callPhing($taskName, $properties = array())
  51. {
  52. $kernel = $this->application->getKernel();
  53. $tmpDir = sys_get_temp_dir().'/propel-gen';
  54. $filesystem = new Filesystem();
  55. $filesystem->remove($tmpDir);
  56. $filesystem->mkdirs($tmpDir);
  57. foreach ($kernel->getBundles() as $bundle) {
  58. if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
  59. $finder = new Finder();
  60. $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
  61. $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
  62. $prefix = implode('.', array_slice($parts, 1, -2));
  63. foreach ($schemas as $schema) {
  64. $filesystem->copy((string) $schema, $file = $tmpDir.DIRECTORY_SEPARATOR.md5($schema).'_'.$schema->getBaseName());
  65. $content = file_get_contents($file);
  66. $content = preg_replace_callback('/package\s*=\s*"(.*?)"/', function ($matches) use ($prefix) {
  67. return sprintf('package="%s"', $prefix.'.'.$matches[1]);
  68. }, $content);
  69. file_put_contents($file, $content);
  70. }
  71. }
  72. }
  73. $filesystem->touch($tmpDir.'/build.properties');
  74. $args = array();
  75. // $bufferPhingOutput = !$this->commandApplication->withTrace();
  76. $properties = array_merge(array(
  77. 'propel.database' => 'mysql',
  78. 'project.dir' => $tmpDir,
  79. 'propel.output.dir' => $kernel->getRootDir().'/propel',
  80. 'propel.php.dir' => '/',
  81. ), $properties);
  82. foreach ($properties as $key => $value) {
  83. $args[] = "-D$key=$value";
  84. }
  85. // Build file
  86. $args[] = '-f';
  87. $args[] = realpath($kernel->getContainer()->getParameter('propel.path').'/generator/build.xml');
  88. /*
  89. // Logger
  90. if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT))) {
  91. $args[] = '-logger';
  92. $args[] = 'phing.listener.AnsiColorLogger';
  93. }
  94. // Add our listener to detect errors
  95. $args[] = '-listener';
  96. $args[] = 'sfPhingListener';
  97. */
  98. // Add any arbitrary arguments last
  99. foreach ($this->additionalPhingArgs as $arg) {
  100. if (in_array($arg, array('verbose', 'debug'))) {
  101. $bufferPhingOutput = false;
  102. }
  103. $args[] = '-'.$arg;
  104. }
  105. $args[] = $taskName;
  106. // enable output buffering
  107. Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w')));
  108. Phing::startup();
  109. Phing::setProperty('phing.home', getenv('PHING_HOME'));
  110. // $this->logSection('propel', 'Running "'.$taskName.'" phing task');
  111. $bufferPhingOutput = false;
  112. if ($bufferPhingOutput) {
  113. ob_start();
  114. }
  115. $m = new Phing();
  116. $m->execute($args);
  117. $m->runBuild();
  118. if ($bufferPhingOutput) {
  119. ob_end_clean();
  120. }
  121. print $bufferPhingOutput;
  122. chdir($kernel->getRootDir());
  123. /*
  124. // any errors?
  125. $ret = true;
  126. if (sfPhingListener::hasErrors())
  127. {
  128. $messages = array('Some problems occurred when executing the task:');
  129. foreach (sfPhingListener::getExceptions() as $exception)
  130. {
  131. $messages[] = '';
  132. $messages[] = preg_replace('/^.*build\-propel\.xml/', 'build-propel.xml', $exception->getMessage());
  133. $messages[] = '';
  134. }
  135. if (count(sfPhingListener::getErrors()))
  136. {
  137. $messages[] = 'If the exception message is not clear enough, read the output of the task for';
  138. $messages[] = 'more information';
  139. }
  140. $this->logBlock($messages, 'ERROR_LARGE');
  141. $ret = false;
  142. }
  143. */
  144. $ret = true;
  145. return $ret;
  146. }
  147. protected function getPhingPropertiesForConnection($databaseManager, $connection)
  148. {
  149. $database = $databaseManager->getDatabase($connection);
  150. return array(
  151. 'propel.database' => $database->getParameter('phptype'),
  152. 'propel.database.driver' => $database->getParameter('phptype'),
  153. 'propel.database.url' => $database->getParameter('dsn'),
  154. 'propel.database.user' => $database->getParameter('username'),
  155. 'propel.database.password' => $database->getParameter('password'),
  156. 'propel.database.encoding' => $database->getParameter('encoding'),
  157. );
  158. }
  159. protected function getProperties($file)
  160. {
  161. $properties = array();
  162. if (false === $lines = @file($file)) {
  163. throw new sfCommandException('Unable to parse contents of the "sqldb.map" file.');
  164. }
  165. foreach ($lines as $line) {
  166. $line = trim($line);
  167. if ('' == $line) {
  168. continue;
  169. }
  170. if (in_array($line[0], array('#', ';'))) {
  171. continue;
  172. }
  173. $pos = strpos($line, '=');
  174. $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
  175. }
  176. return $properties;
  177. }
  178. }