Merge branch 'smarty' into admin

Conflicts:
	core/lib/Thelia/Config/Resources/config.xml
	core/lib/Thelia/Core/EventListener/ViewListener.php
	core/lib/Thelia/Core/Template/SmartyParser.php
	templates/smarty-sample/index.html
This commit is contained in:
franck
2013-06-19 11:20:41 +02:00
19 changed files with 9846 additions and 97 deletions

View File

@@ -50,6 +50,10 @@
</call>
</service>
<service id="smarty.plugin.assetic" class="Thelia\Core\Template\Smarty\Plugins\Assetic" >
<tag name="smarty.register_plugin"/>
</service>
<service id="http_kernel" class="Thelia\Core\TheliaHttpKernel">
<argument type="service" id="event_dispatcher" />
<argument type="service" id="service_container" />

View File

@@ -28,6 +28,7 @@ use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Scope;
use Thelia\Core\DependencyInjection\Compiler\RegisterListenersPass;
use Thelia\Core\DependencyInjection\Compiler\RegisterSmartyPluginPass;
/**
* First Bundle use in Thelia
@@ -56,5 +57,6 @@ class TheliaBundle extends Bundle
$container->addScope(new Scope('request'));
$container->addCompilerPass(new RegisterListenersPass());
$container->addCompilerPass(new RegisterSmartyPluginPass());
}
}

View File

@@ -0,0 +1,64 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 18/06/13
* Time: 21:55
* To change this template use File | Settings | File Templates.
*/
namespace Thelia\Core\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class RegisterSmartyPluginPass implements CompilerPassInterface {
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition("thelia.parser")) {
return;
}
$smarty = $container->getDefinition("thelia.parser");
foreach ($container->findTaggedServiceIds("smarty.register_plugin") as $id => $plugin) {
$smarty->addMethodCall("addPlugins", array(new Reference($id)));
/*$register_plugin = $container->get($id);
$reflectionObject = new \ReflectionObject($register_plugin);
$interface = "Thelia\Core\Template\Smarty\SmartyPluginInterface";
if (!$reflectionObject->implementsInterface($interface)) {
throw new \RuntimeException(sprintf("%s class must implement %s interface",$reflectionObject->getName(), $interface));
}
$plugins = $register_plugin->registerPlugins();
if(!is_array($plugins)) {
$plugins = array($plugins);
}
foreach($plugins as $plugin) {
$smarty->addMethodCall("registerPlugin", array(
$plugin->type,
$plugin->name,
array(
$plugin->class,
$plugin->method
)
));
}*/
}
}
}

View File

@@ -67,7 +67,9 @@ class ViewListener implements EventSubscriberInterface
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$parser = $this->container->get('thelia.parser');
try {
$content = $parser->getContent();

View File

@@ -1,24 +1,24 @@
<?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/>. */
/* */
/* */
/* 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;
@@ -34,85 +34,92 @@ use Assetic\Cache\FilesystemCache;
class AsseticManager {
protected $options;
public function __construct($_options = array()) {
$this->options = $_options;
}
/**
* 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 unknown $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 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 unknown $filters a list of filters, as defined below (see switch($filter_name) ...)
* @param unknown $debug true / false
* @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 unknown $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 unknown $filters a list of filters, as defined below (see switch($filter_name) ...)
* @param unknown $debug true / false
* @throws \Exception
* @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) {
$asset_name = basename($asset_path);
$asset_dir = dirname($asset_path);
$asset_name = basename($asset_path);
$asset_dir = dirname($asset_path);
$am = new AssetManager();
$fm = new FilterManager();
$am = new AssetManager();
$fm = new FilterManager();
if (! empty($filters)) {
$filter_list = explode(',', $filters);
if (! empty($filters)) {
$filter_list = explode(',', $filters);
foreach($filter_list as $filter_name) {
foreach($filter_list as $filter_name) {
$filter_name = trim($filter_name);
$filter_name = trim($filter_name);
switch($filter_name) {
case 'less' :
$fm->set('less', new Filter\LessphpFilter());
break;
switch($filter_name) {
case 'less' :
$fm->set('less', new Filter\LessphpFilter());
break;
case 'sass' :
$fm->set('less', new Filter\Sass\SassFilter());
break;
case 'sass' :
$fm->set('less', new Filter\Sass\SassFilter());
break;
case 'cssembed' :
$fm->set('cssembed', new Filter\PhpCssEmbedFilter());
break;
case 'cssembed' :
$fm->set('cssembed', new Filter\PhpCssEmbedFilter());
break;
case 'cssrewrite':
$fm->set('cssrewrite', new Filter\CssRewriteFilter());
break;
case 'cssrewrite':
$fm->set('cssrewrite', new Filter\CssRewriteFilter());
break;
case 'cssimport':
$fm->set('cssimport', new Filter\CssImportFilter());
break;
case 'cssimport':
$fm->set('cssimport', new Filter\CssImportFilter());
break;
default :
throw new \Exception("Unsupported Assetic filter: '$filter_name'");
break;
}
}
}
else {
$filter_list = array();
}
default :
throw new \Exception("Unsupported Assetic filter: '$filter_name'");
break;
}
}
}
else {
$filter_list = array();
}
// Factory setup
$factory = new AssetFactory($asset_dir);
// Factory setup
$factory = new AssetFactory($asset_dir);
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
$factory->setDefaultOutput('*'.(! empty($asset_type) ? '.' : '').$asset_type);
$factory->setDefaultOutput('*'.(! empty($asset_type) ? '.' : '').$asset_type);
$factory->setDebug($debug);
$factory->setDebug($debug);
$factory->addWorker(new CacheBustingWorker());
$factory->addWorker(new CacheBustingWorker());
// Prepare the assets writer
$writer = new AssetWriter($output_path);
// Prepare the assets writer
$writer = new AssetWriter($output_path);
$asset = $factory->createAsset($asset_name, $filter_list);
$asset = $factory->createAsset($asset_name, $filter_list);
$cache = new AssetCache($asset, new FilesystemCache($output_path));
$cache = new AssetCache($asset, new FilesystemCache($output_path));
$writer->writeAsset($cache);
$writer->writeAsset($cache);
return rtrim($output_url, '/').'/'.$asset->getTargetPath();
return rtrim($output_url, '/').'/'.$asset->getTargetPath();
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 18/06/13
* Time: 22:41
* To change this template use File | Settings | File Templates.
*/
namespace Thelia\Core\Template\Smarty;
class RegisterSmartyPlugin {
public $type;
public $name;
public $class;
public $method;
public function __construct($type, $name, $class, $method)
{
$this->type = $type;
$this->name = $name;
$this->class = $class;
$this->method = $method;
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 18/06/13
* Time: 22:38
* To change this template use File | Settings | File Templates.
*/
namespace Thelia\Core\Template\Smarty;
interface SmartyPluginInterface {
/**
* @return mixed
*/
public function registerPlugins();
}

View File

@@ -8,8 +8,8 @@ use \Smarty;
use Thelia\Core\Template\ParserInterface;
use Thelia\Core\Template\Loop\Category;
use Thelia\Tpex\Element\Loop\BaseLoop;
use Thelia\Core\Template\Assets\SmartyAssetsManager;
use Thelia\Core\Template\Smarty\SmartyPluginInterface;
/**
*
@@ -17,6 +17,8 @@ use Thelia\Core\Template\Assets\SmartyAssetsManager;
*/
class SmartyParser extends Smarty implements ParserInterface {
public $plugins = array();
protected $container;
protected $template = "smarty-sample";
@@ -67,11 +69,6 @@ class SmartyParser extends Smarty implements ParserInterface {
// Register Thelia modules inclusion function 'thelia_module'
$this->registerPlugin('function', 'thelia_module', array($this, 'theliaModule'));
// Register asset management block plugins
$this->registerPlugin('block', 'stylesheets', array($this, 'theliaBlockStylesheets'));
$this->registerPlugin('block', 'javascripts', array($this, 'theliaBlockJavascripts'));
$this->registerPlugin('block', 'images' , array($this, 'theliaBlockImages'));
}
/**
@@ -205,22 +202,6 @@ class SmartyParser extends Smarty implements ParserInterface {
return "";
}
public function theliaBlockJavascripts($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
return $this->getAssetManager()->processSmartyPluginCall('js', $params, $content, $template, $repeat);
}
public function theliaBlockImages($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
return $this->getAssetManager()->processSmartyPluginCall(SmartyAssetsManager::ASSET_TYPE_AUTO, $params, $content, $template, $repeat);
}
public function theliaBlockStylesheets($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
return $this->getAssetManager()->processSmartyPluginCall('css', $params, $content, $template, $repeat);
}
/**
*
* This method must return a Symfony\Component\HttpFoudation\Response instance or the content of the response
@@ -228,6 +209,8 @@ class SmartyParser extends Smarty implements ParserInterface {
*/
public function getContent()
{
$this->registerPlugins();
return $this->fetch($this->getTemplateFilePath());
}
@@ -316,6 +299,34 @@ class SmartyParser extends Smarty implements ParserInterface {
}
}
public function addPlugins(SmartyPluginInterface $plugin)
{
$this->plugins[] = $plugin;
}
public function registerPlugins()
{
foreach ($this->plugins as $register_plugin) {
$plugins = $register_plugin->registerPlugins();
if(!is_array($plugins)) {
$plugins = array($plugins);
}
foreach ($plugins as $plugin) {
$this->registerPlugin(
$plugin->type,
$plugin->name,
array(
$plugin->class,
$plugin->method
)
);
}
}
}
/**
* Returns the value of a loop argument.
*
@@ -323,7 +334,7 @@ class SmartyParser extends Smarty implements ParserInterface {
* @param unknown $smartyParam
* @throws \InvalidArgumentException
*/
protected function getLoopArgument(BaseLoop $loop, $smartyParam)
protected function getLoopArgument($loop, $smartyParam)
{
$defaultItemsParams = array('required' => true);
@@ -419,4 +430,4 @@ class SmartyParser extends Smarty implements ParserInterface {
return $this->asset_manager;
}
}
}