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> </call>
</service> </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"> <service id="http_kernel" class="Thelia\Core\TheliaHttpKernel">
<argument type="service" id="event_dispatcher" /> <argument type="service" id="event_dispatcher" />
<argument type="service" id="service_container" /> <argument type="service" id="service_container" />

View File

@@ -28,6 +28,7 @@ use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Scope; use Symfony\Component\DependencyInjection\Scope;
use Thelia\Core\DependencyInjection\Compiler\RegisterListenersPass; use Thelia\Core\DependencyInjection\Compiler\RegisterListenersPass;
use Thelia\Core\DependencyInjection\Compiler\RegisterSmartyPluginPass;
/** /**
* First Bundle use in Thelia * First Bundle use in Thelia
@@ -56,5 +57,6 @@ class TheliaBundle extends Bundle
$container->addScope(new Scope('request')); $container->addScope(new Scope('request'));
$container->addCompilerPass(new RegisterListenersPass()); $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) public function onKernelView(GetResponseForControllerResultEvent $event)
{ {
$parser = $this->container->get('thelia.parser'); $parser = $this->container->get('thelia.parser');
try { try {
$content = $parser->getContent(); $content = $parser->getContent();

View File

@@ -1,24 +1,24 @@
<?php <?php
/*************************************************************************************/ /*************************************************************************************/
/* */ /* */
/* Thelia */ /* Thelia */
/* */ /* */
/* Copyright (c) OpenStudio */ /* Copyright (c) OpenStudio */
/* email : info@thelia.net */ /* email : info@thelia.net */
/* web : http://www.thelia.net */ /* web : http://www.thelia.net */
/* */ /* */
/* This program is free software; you can redistribute it and/or modify */ /* 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 */ /* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */ /* the Free Software Foundation; either version 3 of the License */
/* */ /* */
/* This program is distributed in the hope that it will be useful, */ /* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */ /* GNU General Public License for more details. */
/* */ /* */
/* You should have received a copy of the GNU General Public License */ /* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */ /* */
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Core\Template\Assets; namespace Thelia\Core\Template\Assets;
@@ -34,85 +34,92 @@ use Assetic\Cache\FilesystemCache;
class AsseticManager { class AsseticManager {
protected $options;
public function __construct($_options = array()) {
$this->options = $_options;
}
/** /**
* 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 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 $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 $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 $filters a list of filters, as defined below (see switch($filter_name) ...)
* @param unknown $debug true / false * @param unknown $debug true / false
* @throws \Exception * @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) { public function asseticize($asset_path, $output_path, $output_url, $asset_type, $filters, $debug) {
$asset_name = basename($asset_path); $asset_name = basename($asset_path);
$asset_dir = dirname($asset_path); $asset_dir = dirname($asset_path);
$am = new AssetManager(); $am = new AssetManager();
$fm = new FilterManager(); $fm = new FilterManager();
if (! empty($filters)) { if (! empty($filters)) {
$filter_list = explode(',', $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) { switch($filter_name) {
case 'less' : case 'less' :
$fm->set('less', new Filter\LessphpFilter()); $fm->set('less', new Filter\LessphpFilter());
break; break;
case 'sass' : case 'sass' :
$fm->set('less', new Filter\Sass\SassFilter()); $fm->set('less', new Filter\Sass\SassFilter());
break; break;
case 'cssembed' : case 'cssembed' :
$fm->set('cssembed', new Filter\PhpCssEmbedFilter()); $fm->set('cssembed', new Filter\PhpCssEmbedFilter());
break; break;
case 'cssrewrite': case 'cssrewrite':
$fm->set('cssrewrite', new Filter\CssRewriteFilter()); $fm->set('cssrewrite', new Filter\CssRewriteFilter());
break; break;
case 'cssimport': case 'cssimport':
$fm->set('cssimport', new Filter\CssImportFilter()); $fm->set('cssimport', new Filter\CssImportFilter());
break; break;
default : default :
throw new \Exception("Unsupported Assetic filter: '$filter_name'"); throw new \Exception("Unsupported Assetic filter: '$filter_name'");
break; break;
} }
} }
} }
else { else {
$filter_list = array(); $filter_list = array();
} }
// Factory setup // Factory setup
$factory = new AssetFactory($asset_dir); $factory = new AssetFactory($asset_dir);
$factory->setAssetManager($am); $factory->setAssetManager($am);
$factory->setFilterManager($fm); $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 // Prepare the assets writer
$writer = new AssetWriter($output_path); $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\ParserInterface;
use Thelia\Core\Template\Loop\Category; 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 { class SmartyParser extends Smarty implements ParserInterface {
public $plugins = array();
protected $container; protected $container;
protected $template = "smarty-sample"; protected $template = "smarty-sample";
@@ -67,11 +69,6 @@ class SmartyParser extends Smarty implements ParserInterface {
// Register Thelia modules inclusion function 'thelia_module' // Register Thelia modules inclusion function 'thelia_module'
$this->registerPlugin('function', 'thelia_module', array($this, 'theliaModule')); $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 ""; 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 * 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() public function getContent()
{ {
$this->registerPlugins();
return $this->fetch($this->getTemplateFilePath()); 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. * Returns the value of a loop argument.
* *
@@ -323,7 +334,7 @@ class SmartyParser extends Smarty implements ParserInterface {
* @param unknown $smartyParam * @param unknown $smartyParam
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
protected function getLoopArgument(BaseLoop $loop, $smartyParam) protected function getLoopArgument($loop, $smartyParam)
{ {
$defaultItemsParams = array('required' => true); $defaultItemsParams = array('required' => true);
@@ -419,4 +430,4 @@ class SmartyParser extends Smarty implements ParserInterface {
return $this->asset_manager; return $this->asset_manager;
} }
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,13 @@
{* Include required JS files *}
{javascripts file='../assets/js/jquery.min.js'}
<link rel="stylesheet" href="{$asset_url}" target="screen">
{/javascripts}
{javascripts file='../assets/bootstrap/js/bootstrap.min.js'}
<link rel="stylesheet" href="{$asset_url}" target="screen">
{/javascripts}
{* TODO allow modules to include JS here *}
</body>
</html>

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="{$lang}">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{stylesheets file='../assets/css/*' filters='less,cssrewrite'}
<link rel="stylesheet" href="{$asset_url}">
{/stylesheets}
{stylesheets file='../assets/bootstrap/css/bootstrap.min.css' filters='cssrewrite'}
<link rel="stylesheet" href="{$asset_url}">
{/stylesheets}
{stylesheets file='../assets/bootstrap/css/bootstrap-responsive.min.css' filters='cssrewrite'}
<link rel="stylesheet" href="{$asset_url}">
{/stylesheets}
{* TODO allow modules to include CSS here *}
</head>
<body>