DumpCommand.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\Command;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Factory\LazyAssetManager;
  13. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * Dumps assets to the filesystem.
  20. *
  21. * @author Kris Wallsmith <kris@symfony.com>
  22. */
  23. class DumpCommand extends ContainerAwareCommand
  24. {
  25. private $basePath;
  26. private $verbose;
  27. private $am;
  28. protected function configure()
  29. {
  30. $this
  31. ->setName('assetic:dump')
  32. ->setDescription('Dumps all assets to the filesystem')
  33. ->addArgument('write_to', InputArgument::OPTIONAL, 'Override the configured asset root')
  34. ->addOption('watch', null, InputOption::VALUE_NONE, 'Check for changes every second, debug mode only')
  35. ->addOption('force', null, InputOption::VALUE_NONE, 'Force an initial generation of all assets (used with --watch)')
  36. ->addOption('period', null, InputOption::VALUE_REQUIRED, 'Set the polling period in seconds (used with --watch)', 1)
  37. ;
  38. }
  39. protected function initialize(InputInterface $input, OutputInterface $output)
  40. {
  41. parent::initialize($input, $output);
  42. $this->basePath = $input->getArgument('write_to') ?: $this->getContainer()->getParameter('assetic.write_to');
  43. $this->verbose = $input->getOption('verbose');
  44. $this->am = $this->getContainer()->get('assetic.asset_manager');
  45. }
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. $output->writeln(sprintf('Dumping all <comment>%s</comment> assets.', $input->getOption('env')));
  49. $output->writeln(sprintf('Debug mode is <comment>%s</comment>.', $input->getOption('no-debug') ? 'off' : 'on'));
  50. $output->writeln('');
  51. if (!$input->getOption('watch')) {
  52. foreach ($this->am->getNames() as $name) {
  53. $this->dumpAsset($name, $output);
  54. }
  55. return;
  56. }
  57. if (!$this->am->isDebug()) {
  58. throw new \RuntimeException('The --watch option is only available in debug mode.');
  59. }
  60. $this->watch($input, $output);
  61. }
  62. /**
  63. * Watches a asset manager for changes.
  64. *
  65. * This method includes an infinite loop the continuously polls the asset
  66. * manager for changes.
  67. *
  68. * @param InputInterface $input The command input
  69. * @param OutputInterface $output The command output
  70. */
  71. private function watch(InputInterface $input, OutputInterface $output)
  72. {
  73. $refl = new \ReflectionClass('Assetic\\AssetManager');
  74. $prop = $refl->getProperty('assets');
  75. $prop->setAccessible(true);
  76. $cache = sys_get_temp_dir().'/assetic_watch_'.substr(sha1($this->basePath), 0, 7);
  77. if ($input->getOption('force') || !file_exists($cache)) {
  78. $previously = array();
  79. } else {
  80. $previously = unserialize(file_get_contents($cache));
  81. }
  82. $error = '';
  83. while (true) {
  84. try {
  85. foreach ($this->am->getNames() as $name) {
  86. if ($this->checkAsset($name, $previously)) {
  87. $this->dumpAsset($name, $output);
  88. }
  89. }
  90. // reset the asset manager
  91. $prop->setValue($this->am, array());
  92. $this->am->load();
  93. file_put_contents($cache, serialize($previously));
  94. $error = '';
  95. sleep($input->getOption('period'));
  96. } catch (\Exception $e) {
  97. if ($error != $msg = $e->getMessage()) {
  98. $output->writeln('<error>[error]</error> '.$msg);
  99. $error = $msg;
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Checks if an asset should be dumped.
  106. *
  107. * @param string $name The asset name
  108. * @param array &$previously An array of previous visits
  109. *
  110. * @return Boolean Whether the asset should be dumped
  111. */
  112. private function checkAsset($name, array &$previously)
  113. {
  114. $formula = $this->am->hasFormula($name) ? serialize($this->am->getFormula($name)) : null;
  115. $asset = $this->am->get($name);
  116. $mtime = $asset->getLastModified();
  117. if (isset($previously[$name])) {
  118. $changed = $previously[$name]['mtime'] != $mtime || $previously[$name]['formula'] != $formula;
  119. } else {
  120. $changed = true;
  121. }
  122. $previously[$name] = array('mtime' => $mtime, 'formula' => $formula);
  123. return $changed;
  124. }
  125. /**
  126. * Writes an asset.
  127. *
  128. * If the application or asset is in debug mode, each leaf asset will be
  129. * dumped as well.
  130. *
  131. * @param string $name An asset name
  132. * @param OutputInterface $output The command output
  133. */
  134. private function dumpAsset($name, OutputInterface $output)
  135. {
  136. $asset = $this->am->get($name);
  137. $formula = $this->am->getFormula($name);
  138. // start by dumping the main asset
  139. $this->doDump($asset, $output);
  140. // dump each leaf if debug
  141. if (isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug()) {
  142. foreach ($asset as $leaf) {
  143. $this->doDump($leaf, $output);
  144. }
  145. }
  146. }
  147. /**
  148. * Performs the asset dump.
  149. *
  150. * @param AssetInterface $asset An asset
  151. * @param OutputInterface $output The command output
  152. *
  153. * @throws RuntimeException If there is a problem writing the asset
  154. */
  155. private function doDump(AssetInterface $asset, OutputInterface $output)
  156. {
  157. $target = rtrim($this->basePath, '/').'/'.str_replace('_controller/', '', $asset->getTargetPath());
  158. if (!is_dir($dir = dirname($target))) {
  159. $output->writeln('<info>[dir+]</info> '.$dir);
  160. if (false === @mkdir($dir, 0777, true)) {
  161. throw new \RuntimeException('Unable to create directory '.$dir);
  162. }
  163. }
  164. $output->writeln('<info>[file+]</info> '.$target);
  165. if ($this->verbose) {
  166. if ($asset instanceof \Traversable) {
  167. foreach ($asset as $leaf) {
  168. $root = $leaf->getSourceRoot();
  169. $path = $leaf->getSourcePath();
  170. $output->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
  171. }
  172. } else {
  173. $root = $asset->getSourceRoot();
  174. $path = $asset->getSourcePath();
  175. $output->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
  176. }
  177. }
  178. if (false === @file_put_contents($target, $asset->dump())) {
  179. throw new \RuntimeException('Unable to write file '.$target);
  180. }
  181. }
  182. }