Skip to content

Instantly share code, notes, and snippets.

@benlumley
Created May 5, 2012 09:57
Show Gist options
  • Save benlumley/2601266 to your computer and use it in GitHub Desktop.
Save benlumley/2601266 to your computer and use it in GitHub Desktop.
Assetic worker to write asset urls back to the standard sf /bundles urls when in debug mode in order to speed things up
<?php
namespace You\YourBundle\Assetic\Worker;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\Worker\WorkerInterface;
/**
* Maps assetic urls back to the standard sf /bundle urls when debug / dev mode is on, as finding it's too damned slow to run through the controller, and 'assetic:dump --watch' takes too long to notice changes.
* It will likely mean certain assetic stuff doesn't work/has issues though - if you use bits of assetic that I'm not using (I use it to combine and minify css and js primarily)
*
* Example:
*
* You have in your template something like:
*
* {% javascripts
* '@SonatajQueryBundle/Resources/public/*.js'
* %}
* <script type="text/javascript" src="{{ asset_url }}"></script>
* {% endjavascripts %}
*
* Your paths in the above can use this syntax if you prefer: 'bundles/somebundle/yoURFILE.js'.
*
* This, in dev mode, results in a url running through a controller, like:
*
* /app_dev.php/js/somethingorother.js
*
* For me at least, this becomes painfully slow to develop with, especially if you have a lot of js/css files.
*
* This file will change these urls back to the path symfony's assets:install command uses - removing the need to run them through the controller, and speeding things up a bit.
*
* IE - Something like
*
* /bundles/yourbundle/js/file.js
*
* Usage:
*
* - Put this file somewhere, and fix the namespace
* - Add the following service config somewhere (app/config/config.yml maybe):
*
* assetic.bundles_url_worker:
* class: You\YourBundle\Assetic\Worker\BundlesUrlWorker
* arguments: [ %assetic.debug% ]
* tags:
* - { name: assetic.factory_worker }
*
* - Turn assetic's use_controller setting off - usually in your app's config.yml or config_dev.yml. (You need to remove the _assetic route then as well - it's probably in your app's routing_dev.yml)
* - Run 'app/console assets:install --symlink web' (You don't need to symlink if you don't want to - but saves re-running this too often)
*
* @author Ben Lumley - @benlumley
*/
class BundlesUrlWorker implements WorkerInterface
{
protected $enabled = false;
protected $stub = '/bundles/';
public function __construct($enabled) {
$this->enabled = $enabled;
}
public function process(AssetInterface $asset)
{
if ($this->enabled) {
$path = $this->parsePath($asset->getSourceRoot() . DIRECTORY_SEPARATOR . $asset->getSourcePath());
if ($path) {
$asset->setTargetPath($this->stub . $path);
}
}
return $asset;
}
protected function parsePath($path) {
$divider = DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'bundles' . DIRECTORY_SEPARATOR;
if ($pos = strpos($path, '/web/bundles/')) {
return substr($path, $pos + strlen($divider));
}
$divider = 'Bundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'public';
if ($pos = strpos($path, $divider)) {
$start = substr($path, 0, $pos);
$end = substr($path, $pos + strlen($divider));
$start = explode(DIRECTORY_SEPARATOR, $start);
$path = strtolower(array_pop($start));
$path = strtolower(array_pop($start)) . $path;
return $path . $end;
}
return false;
}
}
@benlumley
Copy link
Author

Updated to support bundle inheritance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment