SprocketsFilter.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Filter;
  11. use Assetic\Asset\AssetInterface;
  12. use Assetic\Util\ProcessBuilder;
  13. /**
  14. * Runs assets through Sprockets.
  15. *
  16. * Requires Sprockets 1.0.x.
  17. *
  18. * @link http://getsprockets.org/
  19. * @link http://github.com/sstephenson/sprockets/tree/1.0.x
  20. *
  21. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  22. */
  23. class SprocketsFilter implements FilterInterface
  24. {
  25. private $sprocketsLib;
  26. private $rubyBin;
  27. private $includeDirs;
  28. private $assetRoot;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $sprocketsLib Path to the Sprockets lib/ directory
  33. * @param string $rubyBin Path to the ruby binary
  34. */
  35. public function __construct($sprocketsLib = null, $rubyBin = '/usr/bin/ruby')
  36. {
  37. $this->sprocketsLib = $sprocketsLib;
  38. $this->rubyBin = $rubyBin;
  39. $this->includeDirs = array();
  40. }
  41. public function addIncludeDir($directory)
  42. {
  43. $this->includeDirs[] = $directory;
  44. }
  45. public function setAssetRoot($assetRoot)
  46. {
  47. $this->assetRoot = $assetRoot;
  48. }
  49. /**
  50. * Hack around a bit, get the job done.
  51. */
  52. public function filterLoad(AssetInterface $asset)
  53. {
  54. static $format = <<<'EOF'
  55. #!/usr/bin/env ruby
  56. require %s
  57. %s
  58. options = { :load_path => [],
  59. :source_files => [%s],
  60. :expand_paths => false }
  61. %ssecretary = Sprockets::Secretary.new(options)
  62. secretary.install_assets if options[:asset_root]
  63. print secretary.concatenation
  64. EOF;
  65. $more = '';
  66. foreach ($this->includeDirs as $directory) {
  67. $more .= 'options[:load_path] << '.var_export($directory, true)."\n";
  68. }
  69. if (null !== $this->assetRoot) {
  70. $more .= 'options[:asset_root] = '.var_export($this->assetRoot, true)."\n";
  71. }
  72. if ($more) {
  73. $more .= "\n";
  74. }
  75. $tmpAsset = tempnam(sys_get_temp_dir(), 'assetic_sprockets');
  76. file_put_contents($tmpAsset, $asset->getContent());
  77. $input = tempnam(sys_get_temp_dir(), 'assetic_sprockets');
  78. file_put_contents($input, sprintf($format,
  79. $this->sprocketsLib
  80. ? sprintf('File.join(%s, \'sprockets\')', var_export($this->sprocketsLib, true))
  81. : '\'sprockets\'',
  82. $this->getHack($asset),
  83. var_export($tmpAsset, true),
  84. $more
  85. ));
  86. $pb = new ProcessBuilder(array(
  87. $this->rubyBin,
  88. $input,
  89. ));
  90. $proc = $pb->getProcess();
  91. $code = $proc->run();
  92. unlink($tmpAsset);
  93. unlink($input);
  94. if (0 < $code) {
  95. throw new \RuntimeException($proc->getErrorOutput());
  96. }
  97. $asset->setContent($proc->getOutput());
  98. }
  99. public function filterDump(AssetInterface $asset)
  100. {
  101. }
  102. private function getHack(AssetInterface $asset)
  103. {
  104. static $format = <<<'EOF'
  105. module Sprockets
  106. class Preprocessor
  107. protected
  108. def pathname_for_relative_require_from(source_line)
  109. Sprockets::Pathname.new(@environment, File.join(%s, location_from(source_line)))
  110. end
  111. end
  112. end
  113. EOF;
  114. $root = $asset->getSourceRoot();
  115. $path = $asset->getSourcePath();
  116. if ($root && $path) {
  117. return sprintf($format, var_export(dirname($root.'/'.$path), true));
  118. }
  119. }
  120. }