DumpCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Command;
  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 Command
  24. {
  25. private $basePath;
  26. private $am;
  27. private $debug;
  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. ;
  36. }
  37. protected function initialize(InputInterface $input, OutputInterface $output)
  38. {
  39. parent::initialize($input, $output);
  40. $this->basePath = $input->getArgument('write_to') ?: $this->container->getParameter('assetic.write_to');
  41. $this->am = $this->container->get('assetic.asset_manager');
  42. $this->debug = $this->container->getParameter('assetic.debug');
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. if (!$input->getOption('watch')) {
  47. foreach ($this->am->getNames() as $name) {
  48. $this->dumpAsset($this->am->get($name), $output);
  49. }
  50. return;
  51. }
  52. if (!$this->debug) {
  53. throw new \RuntimeException('The --watch option is only available in debug mode.');
  54. }
  55. $this->watch($output);
  56. }
  57. /**
  58. * Watches a asset manager for changes.
  59. *
  60. * This method includes an infinite loop the continuously polls the asset
  61. * manager for changes.
  62. *
  63. * @param OutputInterface $output The command output
  64. */
  65. protected function watch(OutputInterface $output)
  66. {
  67. $refl = new \ReflectionClass('Assetic\\AssetManager');
  68. $prop = $refl->getProperty('assets');
  69. $prop->setAccessible(true);
  70. $cache = sys_get_temp_dir().'/assetic_watch_'.substr(sha1($this->basePath), 0, 7);
  71. if (file_exists($cache)) {
  72. $previously = unserialize(file_get_contents($cache));
  73. } else {
  74. $previously = array();
  75. }
  76. $error = '';
  77. while (true) {
  78. try {
  79. foreach ($this->am->getNames() as $name) {
  80. if ($asset = $this->checkAsset($name, $previously)) {
  81. $this->dumpAsset($asset, $output);
  82. }
  83. }
  84. // reset the asset manager
  85. $prop->setValue($this->am, array());
  86. $this->am->load();
  87. file_put_contents($cache, serialize($previously));
  88. $error = '';
  89. sleep(1);
  90. } catch (\Exception $e) {
  91. if ($error != $msg = $e->getMessage()) {
  92. $output->writeln('<error>[error]</error> '.$msg);
  93. $error = $msg;
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Checks if an asset should be dumped.
  100. *
  101. * @param string $name The asset name
  102. * @param array &$previously An array of previous visits
  103. *
  104. * @return AssetInterface|Boolean The asset if it should be dumped
  105. */
  106. protected function checkAsset($name, array &$previously)
  107. {
  108. $formula = $this->am->hasFormula($name) ? serialize($this->am->getFormula($name)) : null;
  109. $asset = $this->am->get($name);
  110. $mtime = $asset->getLastModified();
  111. if (isset($previously[$name])) {
  112. $changed = $previously[$name]['mtime'] != $mtime || $previously[$name]['formula'] != $formula;
  113. } else {
  114. $changed = true;
  115. }
  116. $previously[$name] = array('mtime' => $mtime, 'formula' => $formula);
  117. return $changed ? $asset : false;
  118. }
  119. /**
  120. * Writes an asset.
  121. *
  122. * @param AssetInterface $asset An asset
  123. * @param OutputInterface $output The command output
  124. *
  125. * @throws RuntimeException If there is a problem writing the asset
  126. */
  127. protected function dumpAsset(AssetInterface $asset, OutputInterface $output)
  128. {
  129. $target = rtrim($this->basePath, '/').'/'.str_replace('_controller/', '', $asset->getTargetUrl());
  130. if (!is_dir($dir = dirname($target))) {
  131. $output->writeln('<info>[dir+]</info> '.$dir);
  132. if (false === @mkdir($dir, 0777, true)) {
  133. throw new \RuntimeException('Unable to create directory '.$dir);
  134. }
  135. }
  136. $output->writeln('<info>[file+]</info> '.$target);
  137. if (false === @file_put_contents($target, $asset->dump())) {
  138. throw new \RuntimeException('Unable to write file '.$target);
  139. }
  140. }
  141. }