poc integrate smarty in DI

This commit is contained in:
Manuel Raynaud
2013-06-18 23:32:47 +02:00
parent 7351edeb46
commit dfafe36730
23 changed files with 9962 additions and 9 deletions

View File

@@ -23,7 +23,7 @@
namespace Thelia\Admin\Templating\Smarty;
use Thelia\Template\Assets\AsseticManager;
use Thelia\Core\Template\Assets\AsseticManager;
class AssetsManager extends AsseticManager {

View File

@@ -28,13 +28,17 @@
<service id="controller_resolver" class="Symfony\Component\HttpKernel\Controller\ControllerResolver"/>
<service id="parser" class="Thelia\Core\Template\SmartyParser">
<service id="smarty" class="Thelia\Core\Template\SmartyParser">
<argument type="service" id="service_container"/>
<call method="setLoopList">
<argument>%tpex.loop%</argument>
</call>
</service>
<service id="smarty.plugin.assetic" class="Thelia\Core\Template\Smarty\Plugins\AsseticManager" >
<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("smarty")) {
return;
}
$smarty = $container->getDefinition("smarty");
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,7 @@ class ViewListener implements EventSubscriberInterface
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$parser = $this->container->get('parser');
$parser = $this->container->get('smarty');
try {
$content = $parser->getContent();

View File

@@ -0,0 +1,57 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 18/06/13
* Time: 22:44
* To change this template use File | Settings | File Templates.
*/
namespace Thelia\Core\Template\Smarty\Plugins;
use Thelia\Core\Template\Smarty\RegisterSmartyPlugin;
use Thelia\Core\Template\Smarty\SmartyPluginInterface;
use Thelia\Admin\Templating\Smarty\AssetsManager;
class AsseticManager implements SmartyPluginInterface{
public $asset_manager;
public function __construct()
{
$web_root = THELIA_WEB_DIR;
$asset_dir_from_web_root = 'assets/admin/default'; // FIXME
$this->asset_manager = new AssetsManager($web_root, $asset_dir_from_web_root);
}
public function smartyBlockJavascripts($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
return $this->asset_manager->processSmartyPluginCall('js', $params, $content, $template, $repeat);
}
public function smartyBlockImages($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
return $this->asset_manager->processSmartyPluginCall(AssetsManager::ASSET_TYPE_AUTO, $params, $content, $template, $repeat);
}
public function smartyBlockStylesheets($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
return $this->asset_manager->processSmartyPluginCall('css', $params, $content, $template, $repeat);
}
/**
* @return mixed
*/
public function registerPlugins()
{
return array(
new RegisterSmartyPlugin('block', 'stylesheets', $this, 'smartyBlockStylesheets'),
new RegisterSmartyPlugin('block', 'javascripts', $this, 'smartyBlockJavascripts'),
new RegisterSmartyPlugin('block', 'images', $this, 'smartyBlockImages')
);
}
}

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

@@ -6,9 +6,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Core\Template\ParserInterface;
use \Smarty;
use Thelia\Core\Template\Loop\Category;
use Thelia\Core\Template\Smarty\SmartyPluginInterface;
class SmartyParser extends Smarty implements ParserInterface {
public $plugins;
/**
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
@@ -77,6 +80,33 @@ 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
)
);
}
}
}
private function extractParam($loop, $smartyParam)
{
$defaultItemsParams = array('required' => true);
@@ -242,6 +272,7 @@ class SmartyParser extends Smarty implements ParserInterface {
*/
public function getContent()
{
$this->registerPlugins();
return $this->fetch($this->getTemplateFilePath());
}