BuildCommand.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. foreach ($this->application->getKernel()->getBundles() as $bundle) {
  50. $map = array();
  51. if (is_dir($dir = $bundle->getPath().'/Model/map')) {
  52. $finder = new Finder();
  53. $files = $finder->files()->name('*TableMap.php')->followLinks()->in($dir);
  54. foreach ($files as $file) {
  55. $class = substr($file->getBasename(), 0, -12);
  56. $map = array_merge($map, array(
  57. $class => 'Model/'.$class.'.php',
  58. $class.'Peer' => 'Model/'.$class.'Peer.php',
  59. $class.'Query' => 'Model/'.$class.'Query.php',
  60. 'Base'.$class => 'Model/om/Base'.$class.'.php',
  61. 'Base'.$class.'Peer' => 'Model/om/Base'.$class.'Peer.php',
  62. 'Base'.$class.'Query' => 'Model/om/Base'.$class.'Query.php',
  63. $class.'TableMap' => 'Model/map/'.$class.'TableMap.php',
  64. ));
  65. }
  66. }
  67. if (!is_dir($bundle->getPath().'/Resources/config'))
  68. {
  69. mkdir($bundle->getPath().'/Resources/config', null, true);
  70. }
  71. if ($map)
  72. {
  73. file_put_contents($bundle->getPath().'/Resources/config/classmap.php', '<?php return '.var_export($map, true).';');
  74. }
  75. }
  76. unlink($this->application->getKernel()->getCacheDir().'/propel_autoload.php');
  77. }
  78. protected function callPhing($taskName, $properties = array())
  79. {
  80. $kernel = $this->application->getKernel();
  81. $tmpDir = sys_get_temp_dir().'/propel-gen';
  82. $filesystem = new Filesystem();
  83. $filesystem->remove($tmpDir);
  84. $filesystem->mkdirs($tmpDir);
  85. foreach ($kernel->getBundles() as $bundle) {
  86. if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
  87. $finder = new Finder();
  88. $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
  89. $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath()));
  90. $prefix = implode('.', array_slice($parts, 1, -2));
  91. foreach ($schemas as $schema) {
  92. $filesystem->copy((string) $schema, $file = $tmpDir.DIRECTORY_SEPARATOR.md5($schema).'_'.$schema->getBaseName());
  93. $content = file_get_contents($file);
  94. $content = preg_replace_callback('/package\s*=\s*"(.*?)"/', function ($matches) use ($prefix) {
  95. return sprintf('package="%s"', $prefix.'.'.$matches[1]);
  96. }, $content);
  97. file_put_contents($file, $content);
  98. }
  99. }
  100. }
  101. $filesystem->touch($tmpDir.'/build.properties');
  102. $args = array();
  103. // $bufferPhingOutput = !$this->commandApplication->withTrace();
  104. $properties = array_merge(array(
  105. 'propel.database' => 'mysql',
  106. 'project.dir' => $tmpDir,
  107. 'propel.output.dir' => $kernel->getRootDir().'/propel',
  108. 'propel.php.dir' => '/',
  109. ), $properties);
  110. foreach ($properties as $key => $value) {
  111. $args[] = "-D$key=$value";
  112. }
  113. // Build file
  114. $args[] = '-f';
  115. $args[] = realpath($kernel->getContainer()->getParameter('propel.generator.path').DIRECTORY_SEPARATOR.'build.xml');
  116. /*
  117. // Logger
  118. if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT))) {
  119. $args[] = '-logger';
  120. $args[] = 'phing.listener.AnsiColorLogger';
  121. }
  122. // Add our listener to detect errors
  123. $args[] = '-listener';
  124. $args[] = 'sfPhingListener';
  125. */
  126. // Add any arbitrary arguments last
  127. foreach ($this->additionalPhingArgs as $arg) {
  128. if (in_array($arg, array('verbose', 'debug'))) {
  129. $bufferPhingOutput = false;
  130. }
  131. $args[] = '-'.$arg;
  132. }
  133. $args[] = $taskName;
  134. // enable output buffering
  135. Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w')));
  136. Phing::startup();
  137. Phing::setProperty('phing.home', getenv('PHING_HOME'));
  138. // $this->logSection('propel', 'Running "'.$taskName.'" phing task');
  139. $bufferPhingOutput = false;
  140. if ($bufferPhingOutput) {
  141. ob_start();
  142. }
  143. $m = new Phing();
  144. $m->execute($args);
  145. $m->runBuild();
  146. if ($bufferPhingOutput) {
  147. ob_end_clean();
  148. }
  149. print $bufferPhingOutput;
  150. chdir($kernel->getRootDir());
  151. /*
  152. // any errors?
  153. $ret = true;
  154. if (sfPhingListener::hasErrors())
  155. {
  156. $messages = array('Some problems occurred when executing the task:');
  157. foreach (sfPhingListener::getExceptions() as $exception)
  158. {
  159. $messages[] = '';
  160. $messages[] = preg_replace('/^.*build\-propel\.xml/', 'build-propel.xml', $exception->getMessage());
  161. $messages[] = '';
  162. }
  163. if (count(sfPhingListener::getErrors()))
  164. {
  165. $messages[] = 'If the exception message is not clear enough, read the output of the task for';
  166. $messages[] = 'more information';
  167. }
  168. $this->logBlock($messages, 'ERROR_LARGE');
  169. $ret = false;
  170. }
  171. */
  172. $ret = true;
  173. return $ret;
  174. }
  175. protected function getPhingPropertiesForConnection($databaseManager, $connection)
  176. {
  177. $database = $databaseManager->getDatabase($connection);
  178. return array(
  179. 'propel.database' => $database->getParameter('phptype'),
  180. 'propel.database.driver' => $database->getParameter('phptype'),
  181. 'propel.database.url' => $database->getParameter('dsn'),
  182. 'propel.database.user' => $database->getParameter('username'),
  183. 'propel.database.password' => $database->getParameter('password'),
  184. 'propel.database.encoding' => $database->getParameter('encoding'),
  185. );
  186. }
  187. protected function getProperties($file)
  188. {
  189. $properties = array();
  190. if (false === $lines = @file($file)) {
  191. throw new sfCommandException('Unable to parse contents of the "sqldb.map" file.');
  192. }
  193. foreach ($lines as $line) {
  194. $line = trim($line);
  195. if ('' == $line) {
  196. continue;
  197. }
  198. if (in_array($line[0], array('#', ';'))) {
  199. continue;
  200. }
  201. $pos = strpos($line, '=');
  202. $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
  203. }
  204. return $properties;
  205. }
  206. }