Added a 'development mode' to assetic smarty plugin

This commit is contained in:
franck
2013-09-06 09:56:39 +02:00
parent c8a5734a9e
commit 9655ce3c72
5 changed files with 22 additions and 18 deletions

View File

@@ -129,6 +129,7 @@
<service id="smarty.plugin.assetic" class="Thelia\Core\Template\Smarty\Plugins\Assetic" > <service id="smarty.plugin.assetic" class="Thelia\Core\Template\Smarty\Plugins\Assetic" >
<tag name="thelia.parser.register_plugin"/> <tag name="thelia.parser.register_plugin"/>
<argument>%kernel.environment%</argument>
</service> </service>
<service id="smarty.plugin.theliasyntax" class="Thelia\Core\Template\Smarty\Plugins\TheliaSyntax" > <service id="smarty.plugin.theliasyntax" class="Thelia\Core\Template\Smarty\Plugins\TheliaSyntax" >

View File

@@ -43,15 +43,16 @@ class AsseticHelper
* Generates assets from $asset_path in $output_path, using $filters. * Generates assets from $asset_path in $output_path, using $filters.
* *
* @param string $asset_path the full path to the asset file (or file collection) * @param string $asset_path the full path to the asset file (or file collection)
* @param unknown $output_path the full disk path to the output directory (shoud be visible to web server) * @param string $output_path the full disk path to the output directory (shoud be visible to web server)
* @param unknown $output_url the URL to the generated asset directory * @param string $output_url the URL to the generated asset directory
* @param unknown $asset_type the asset type: css, js, ... The generated files will have this extension. Pass an empty string to use the asset source extension. * @param string $asset_type the asset type: css, js, ... The generated files will have this extension. Pass an empty string to use the asset source extension.
* @param unknown $filters a list of filters, as defined below (see switch($filter_name) ...) * @param array $filters a list of filters, as defined below (see switch($filter_name) ...)
* @param unknown $debug true / false * @param boolean $debug true / false
* @param boolean $dev_mode true / false. If true, assets are not cached and always compiled.
* @throws \InvalidArgumentException if an invalid filter name is found * @throws \InvalidArgumentException if an invalid filter name is found
* @return string The URL to the generated asset file. * @return string The URL to the generated asset file.
*/ */
public function asseticize($asset_path, $output_path, $output_url, $asset_type, $filters, $debug) public function asseticize($asset_path, $output_path, $output_url, $asset_type, $filters, $debug, $dev_mode = false)
{ {
$asset_name = basename($asset_path); $asset_name = basename($asset_path);
$asset_dir = dirname($asset_path); $asset_dir = dirname($asset_path);
@@ -112,6 +113,7 @@ class AsseticHelper
$asset = $factory->createAsset($asset_name); $asset = $factory->createAsset($asset_name);
$asset_target_path = $asset->getTargetPath(); $asset_target_path = $asset->getTargetPath();
$target_file = sprintf("%s/%s", $output_path, $asset_target_path); $target_file = sprintf("%s/%s", $output_path, $asset_target_path);
// As it seems that assetic cannot handle a real file cache, let's do the job ourselves. // As it seems that assetic cannot handle a real file cache, let's do the job ourselves.
@@ -124,7 +126,7 @@ class AsseticHelper
// //
// before generating 3bc974a-ad3ef47.css, delete 3bc974a-* files. // before generating 3bc974a-ad3ef47.css, delete 3bc974a-* files.
// //
if (! file_exists($target_file)) { if ($dev_mode == true || ! file_exists($target_file)) {
// Delete previous version of the file // Delete previous version of the file
list($commonPart, $dummy) = explode('-', $asset_target_path); list($commonPart, $dummy) = explode('-', $asset_target_path);
@@ -143,8 +145,6 @@ class AsseticHelper
} }
} }
//$cache = new AssetCache($asset, new FilesystemCache($output_path));
$writer = new AssetWriter($output_path); $writer = new AssetWriter($output_path);
$writer->writeAsset($asset); $writer->writeAsset($asset);

View File

@@ -34,18 +34,22 @@ class SmartyAssetsManager
private $web_root; private $web_root;
private $path_relative_to_web_root; private $path_relative_to_web_root;
private $developmentMode;
/** /**
* Creates a new SmartyAssetsManager instance * Creates a new SmartyAssetsManager instance
* *
* @param string $web_root the disk path to the web root * @param string $web_root the disk path to the web root
* @param string $path_relative_to_web_root the path (relative to web root) where the assets will be generated * @param string $path_relative_to_web_root the path (relative to web root) where the assets will be generated
* @param boolean $developmentMode true / false. If true, assets are not cached, and always generated.
*/ */
public function __construct($web_root, $path_relative_to_web_root) public function __construct($web_root, $path_relative_to_web_root, $developmentMode)
{ {
$this->web_root = $web_root; $this->web_root = $web_root;
$this->path_relative_to_web_root = $path_relative_to_web_root; $this->path_relative_to_web_root = $path_relative_to_web_root;
$this->developmentMode = $developmentMode;
$this->assetic_manager = new AsseticHelper(); $this->assetic_manager = new AsseticHelper();
} }
@@ -73,7 +77,8 @@ class SmartyAssetsManager
URL::getInstance()->absoluteUrl($this->path_relative_to_web_root, null, URL::PATH_TO_FILE /* path only */), URL::getInstance()->absoluteUrl($this->path_relative_to_web_root, null, URL::PATH_TO_FILE /* path only */),
$assetType, $assetType,
$filters, $filters,
$debug $debug,
$this->developmentMode
); );
return $url; return $url;

View File

@@ -32,13 +32,13 @@ class Assetic extends AbstractSmartyPlugin
{ {
public $assetManager; public $assetManager;
public function __construct() public function __construct($developmentMode)
{ {
$web_root = THELIA_WEB_DIR; $web_root = THELIA_WEB_DIR;
$asset_dir_from_web_root = ConfigQuery::read('asset_dir_from_web_root', 'assets'); $asset_dir_from_web_root = ConfigQuery::read('asset_dir_from_web_root', 'assets');
$this->assetManager = new SmartyAssetsManager($web_root, $asset_dir_from_web_root); $this->assetManager = new SmartyAssetsManager($web_root, $asset_dir_from_web_root, $developmentMode == 'dev');
} }
public function blockJavascripts($params, $content, \Smarty_Internal_Template $template, &$repeat) public function blockJavascripts($params, $content, \Smarty_Internal_Template $template, &$repeat)

View File

@@ -3,6 +3,4 @@
/* Thelia Admin */ /* Thelia Admin */
@import "thelia/thelia.less"; @import "thelia/thelia.less";
// @import "thelia/responsive.less"; // @import "thelia/responsive.less";
//mmm