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