DumpCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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.wallsmith@symfony-project.com>
  22. */
  23. class DumpCommand extends Command
  24. {
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('assetic:dump')
  29. ->setDescription('Dumps all assets to the filesystem')
  30. ->addArgument('base_dir', InputArgument::OPTIONAL, 'The base directory')
  31. ->addOption('watch', null, InputOption::VALUE_NONE, 'Check for changes every second')
  32. ;
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. if (!$baseDir = $input->getArgument('base_dir')) {
  37. $baseDir = $this->container->getParameter('assetic.document_root');
  38. }
  39. $am = $this->container->get('assetic.asset_manager');
  40. if ($input->getOption('watch')) {
  41. return $this->watch($am, $baseDir, $output, $this->container->getParameter('kernel.debug'));
  42. }
  43. foreach ($am->getNames() as $name) {
  44. $this->dumpAsset($am->get($name), $baseDir, $output);
  45. }
  46. }
  47. /**
  48. * Watches a asset manager for changes.
  49. *
  50. * This method includes an infinite loop the continuously polls the asset
  51. * manager for changes.
  52. *
  53. * @param LazyAssetManager $am The asset manager
  54. * @param string $baseDir The base directory to write to
  55. * @param OutputInterface $output The command output
  56. * @param Boolean $debug Debug mode
  57. */
  58. protected function watch(LazyAssetManager $am, $baseDir, OutputInterface $output, $debug = false)
  59. {
  60. $previously = array();
  61. while (true) {
  62. // reload formulae when in debug
  63. if ($debug) {
  64. $am->load();
  65. }
  66. foreach ($am->getNames() as $name) {
  67. if ($asset = $this->checkAsset($am, $name, $previously)) {
  68. $this->dumpAsset($asset, $baseDir, $output);
  69. }
  70. }
  71. sleep(1);
  72. }
  73. }
  74. /**
  75. * Checks if an asset should be dumped.
  76. *
  77. * @param LazyAssetManager $am The asset manager
  78. * @param string $name The asset name
  79. * @param array $previously An array of previous visits
  80. *
  81. * @return AssetInterface|Boolean The asset if it should be dumped
  82. */
  83. protected function checkAsset(LazyAssetManager $am, $name, array &$previously)
  84. {
  85. $formula = serialize($am->getFormula($name));
  86. $asset = $am->get($name);
  87. $mtime = $asset->getLastModified();
  88. if (isset($previously[$name])) {
  89. $changed = $previously[$name]['mtime'] != $mtime || $previously[$name]['formula'] != $formula;
  90. } else {
  91. $changed = true;
  92. }
  93. $previously[$name] = array('mtime' => $mtime, 'formula' => $formula);
  94. return $changed ? $asset : false;
  95. }
  96. /**
  97. * Writes an asset.
  98. *
  99. * @param AssetInterface $asset An asset
  100. * @param string $baseDir The base directory to write to
  101. * @param OutputInterface $output The command output
  102. *
  103. * @throws RuntimeException If there is a problem writing the asset
  104. */
  105. protected function dumpAsset(AssetInterface $asset, $baseDir, OutputInterface $output)
  106. {
  107. $target = rtrim($baseDir, '/') . '/' . $asset->getTargetUrl();
  108. if (!is_dir($dir = dirname($target))) {
  109. $output->writeln('<info>[dir+]</info> '.$dir);
  110. if (false === @mkdir($dir)) {
  111. throw new \RuntimeException('Unable to create directory '.$dir);
  112. }
  113. }
  114. $output->writeln('<info>[file+]</info> '.$target);
  115. if (false === @file_put_contents($target, $asset->dump())) {
  116. throw new \RuntimeException('Unable to write file '.$target);
  117. }
  118. }
  119. }