The asset manager is now a service in the container.

This commit is contained in:
Franck Allimant
2013-11-12 02:12:56 +01:00
parent d941134c84
commit 3ca3b4521b
5 changed files with 86 additions and 43 deletions

View File

@@ -4,23 +4,21 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
<services>
<!-- URL maganement -->
<!-- The assets manager -->
<service id="assetic.asset.manager" class="Thelia\Core\Template\Assets\AsseticAssetManager" >
<argument>%kernel.environment%</argument>
</service>
<!-- Smarty parser plugins -->
<service id="smarty.plugin.assetic" class="Thelia\Core\Template\Smarty\Plugins\Assetic" >
<service id="smarty.plugin.assets" class="Thelia\Core\Template\Smarty\Plugins\Assets" >
<tag name="thelia.parser.register_plugin"/>
<argument>%kernel.environment%</argument>
<argument type="service" id="assetic.asset.manager" />
</service>
<service id="smarty.plugin.theliasyntax" class="Thelia\Core\Template\Smarty\Plugins\TheliaSyntax" >

View File

@@ -0,0 +1,53 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Template\Assets;
interface AssetManagerInterface {
/**
* Prepare an asset directory.
*
* @param string $source_assets_directory the full path to the source asstes directory
* @param string $web_assets_directory_base the base directory of the web based asset directory
* @throws \RuntimeException if something goes wrong.
*/
public function prepareAssets($source_assets_directory, $web_assets_directory_base);
/**
* Generates assets from $asset_path in $output_path, using $filters.
*
* @param string $asset_path the full path to the asset file (or file collection, e.g. *.less)
*
* @param string $web_assets_directory_base the full disk path to the base assets output directory in the web space
* @param string $output_url the URL to the base assets output directory in the web space
*
* @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 array $filters a list of filters, as defined below (see switch($filter_name) ...)
*
* @param boolean $debug the debug mode, true or false
*
* @throws \InvalidArgumentException if an invalid filter name is found
* @return string The URL to the generated asset file.
*/
public function processAsset($asset_path, $web_assets_directory_base, $output_url, $asset_type, $filters, $debug);
}

View File

@@ -38,10 +38,17 @@ use Symfony\Component\Filesystem\Exception\IOException;
*
* @author Franck Allimant <franck@cqfdev.fr>
*/
class AsseticHelper
class AsseticAssetManager implements AssetManagerInterface
{
protected $developmentMode;
protected $source_file_extensions = array('less', 'js', 'coffee', 'html', 'tpl', 'htm', 'xml');
public function __construct($developmentMode)
{
$this->developmentMode = $developmentMode;
}
/**
* Create a stamp form the modification time of the content of the given directory and all of its subdirectories
*
@@ -175,7 +182,7 @@ class AsseticHelper
$fs = new Filesystem();
//FIXME: lock the stuff ?
// FIXME: locking or not locking ?
/*
$lock_file = "$web_assets_directory_base/assets-".md5($source_assets_directory)."-generation-lock.txt";
@@ -286,11 +293,10 @@ class AsseticHelper
* @param array $filters a list of filters, as defined below (see switch($filter_name) ...)
*
* @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
* @return string The URL to the generated asset file.
*/
public function asseticize($asset_path, $web_assets_directory_base, $output_url, $asset_type, $filters, $debug, $dev_mode = false)
public function processAsset($asset_path, $web_assets_directory_base, $output_url, $asset_type, $filters, $debug)
{
$asset_name = basename($asset_path);
$input_directory = realpath(dirname($asset_path));
@@ -330,7 +336,7 @@ class AsseticHelper
Tlog::getInstance()->addDebug("Asset destination name: ", $asset_destination_path);
// We generate an asset only if it does not exists, or if the asset processing is forced in development mode
if (! file_exists($asset_destination_path) || ($dev_mode && ConfigQuery::read('process_assets', true)) ) {
if (! file_exists($asset_destination_path) || ($this->developmentMode && ConfigQuery::read('process_assets', true)) ) {
$writer = new AssetWriter($output_directory);

View File

@@ -25,32 +25,30 @@ namespace Thelia\Core\Template\Smarty\Assets;
use Thelia\Core\Template\Assets\AsseticHelper;
use Thelia\Tools\URL;
use Thelia\Core\Template\Assets\AssetManagerInterface;
class SmartyAssetsManager
{
const ASSET_TYPE_AUTO = '';
private $assetic_manager;
private $assetsManager;
private $web_root;
private $path_relative_to_web_root;
private $developmentMode;
/**
* Creates a new SmartyAssetsManager instance
*
* @param AssetManagerInterface $assetsManager an asset manager instance
* @param string $web_root the disk path to the web root (with final /)
* @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, $developmentMode)
public function __construct(AssetManagerInterface $assetsManager, $web_root, $path_relative_to_web_root)
{
$this->web_root = $web_root;
$this->path_relative_to_web_root = $path_relative_to_web_root;
$this->developmentMode = $developmentMode;
$this->assetic_manager = new AsseticHelper();
$this->assetsManager = $assetsManager;
}
public function prepareAssets($assets_directory, \Smarty_Internal_Template $template) {
@@ -61,12 +59,10 @@ class SmartyAssetsManager
if ($asset_dir_absolute_path === false) throw new \Exception("Failed to get real path of '".$tpl_dir . DS . $assets_directory."'");
$modified = $this->assetic_manager->prepareAssets(
$this->assetsManager->prepareAssets(
$asset_dir_absolute_path,
$this->web_root . $this->path_relative_to_web_root
);
}
public function computeAssetUrl($assetType, $params, \Smarty_Internal_Template $template)
@@ -86,25 +82,14 @@ class SmartyAssetsManager
$asset_file = basename($file);
if ($asset_dir === false) throw new \Exception("Failed to get real path of '".$tpl_dir.'/'.dirname($file)."'");
/*
$url = $this->assetic_manager->asseticize(
$asset_dir.'/'.$asset_file,
$this->web_root . DS . $this->path_relative_to_web_root,
URL::getInstance()->absoluteUrl($this->path_relative_to_web_root, null, URL::PATH_TO_FILE),// PATH only
$assetType,
$filters,
$debug,
$this->developmentMode
);
*/
$url = $this->assetic_manager->asseticize(
$url = $this->assetsManager->processAsset(
$asset_dir . DS . $asset_file,
$this->web_root . $this->path_relative_to_web_root,
URL::getInstance()->absoluteUrl($this->path_relative_to_web_root, null, URL::PATH_TO_FILE /* path only */),
$assetType,
$filters,
$debug,
$this->developmentMode
$debug
);
return $url;

View File

@@ -27,16 +27,17 @@ use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\AbstractSmartyPlugin;
use Thelia\Core\Template\Smarty\Assets\SmartyAssetsManager;
use Thelia\Model\ConfigQuery;
use Thelia\Core\Template\Assets\AssetManagerInterface;
class Assetic extends AbstractSmartyPlugin
class Assets extends AbstractSmartyPlugin
{
public $assetManager;
public function __construct($developmentMode)
public function __construct(AssetManagerInterface $assetsManager)
{
$asset_dir_from_web_root = ConfigQuery::read('asset_dir_from_web_root', 'assets');
$this->assetManager = new SmartyAssetsManager(THELIA_WEB_DIR, $asset_dir_from_web_root, $developmentMode == 'dev');
$this->assetManager = new SmartyAssetsManager($assetsManager, THELIA_WEB_DIR, $asset_dir_from_web_root);
}
public function declareAssets($params, \Smarty_Internal_Template $template)