Commit 5feb690b authored by Tino Goratsch's avatar Tino Goratsch

reworked the Smarty plugin initialization

parent d0f13447
......@@ -6,6 +6,7 @@ use ACP3\Core\Enum\Environment;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\Modules;
use ACP3\Core\Logger as ACP3Logger;
use ACP3\Core\View\Renderer\Smarty\DependencyInjection\RegisterPluginsPass;
use Patchwork\Utf8;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\ContainerBuilder;
......@@ -131,7 +132,6 @@ class Application extends AbstractApplication
$this->systemSettings = $this->container->get('core.config')->getSettings('system');
$this->_setThemeConstants();
$this->container->get('core.user')->authenticate();
$this->container->get('core.view')->setRenderer('smarty');
/** @var \ACP3\Core\Http\Request $request */
$request = $this->container->get('core.request');
......@@ -214,6 +214,7 @@ class Application extends AbstractApplication
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->addCompilerPass(new RegisterListenersPass('core.eventDispatcher', 'core.eventListener', 'core.eventSubscriber'));
$containerBuilder->addCompilerPass(new RegisterPluginsPass());
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load(CLASSES_DIR . 'config/services.yml');
......
......@@ -75,10 +75,10 @@ class ThemeResolver
*/
public function getStaticAssetPath($modulePath, $designPath, $dir = '', $file = '')
{
if (strpos($modulePath, '.') === false && !preg_match('=/$=', $modulePath)) {
if (!empty($modulePath) && strpos($modulePath, '.') === false && !preg_match('=/$=', $modulePath)) {
$modulePath .= '/';
}
if (strpos($designPath, '.') === false && !preg_match('=/$=', $designPath)) {
if (!empty($designPath) && strpos($designPath, '.') === false && !preg_match('=/$=', $designPath)) {
$designPath .= '/';
}
if (!empty($dir) && !preg_match('=/$=', $dir)) {
......
......@@ -3,13 +3,13 @@
namespace ACP3\Core;
use ACP3\Core\View\Renderer\AbstractRenderer;
use Symfony\Component\DependencyInjection\ContainerAware;
use ACP3\Core\View\Renderer\Smarty;
/**
* Klasse für die Ausgabe der Seite
* @package ACP3\Core
*/
class View extends ContainerAware
class View
{
/**
* @var AbstractRenderer
......@@ -27,22 +27,13 @@ class View extends ContainerAware
}
/**
* Set the desired renderer with an optional config array
*
* @param string $renderer
* @param array $params
*
* @throws \Exception
* @param \ACP3\Core\View\Renderer\Smarty $smarty
* @param array $params
*/
public function setRenderer($renderer = 'smarty', array $params = [])
public function __construct(Smarty $smarty, array $params = [])
{
$serviceId = 'core.view.renderer.' . $renderer;
if ($this->container->has($serviceId) === true) {
$this->renderer = $this->container->get($serviceId);
$this->renderer->configure($params);
} else {
throw new \Exception('Renderer ' . $renderer . ' not found!');
}
$this->renderer = $smarty;
$this->renderer->configure($params);
}
/**
......@@ -99,4 +90,12 @@ class View extends ContainerAware
{
return $this->renderer->assign($name, $value);
}
/**
* @param \ACP3\Core\View\Renderer\Smarty\PluginInterface $plugin
*/
public function registerPlugin(Smarty\PluginInterface $plugin)
{
$plugin->register($this->renderer->renderer);
}
}
......@@ -34,8 +34,6 @@ class Smarty extends AbstractRenderer
$this->renderer->compile_check = $this->isDevOrInstall();
$this->renderer->compile_dir = CACHE_DIR . 'tpl_compiled/';
$this->renderer->cache_dir = CACHE_DIR . 'tpl_cached/';
$this->_registerPlugins();
}
/**
......@@ -83,29 +81,6 @@ class Smarty extends AbstractRenderer
return $this->renderer->templateExists($template);
}
/**
* Register all available Smarty plugins
*/
protected function _registerPlugins()
{
$services = $this->container->getServiceIds();
foreach ($services as $serviceName) {
if (strpos($serviceName, 'smarty.plugin.') === 0) {
/** @var AbstractPlugin $plugin */
$plugin = $this->container->get($serviceName);
$plugin->registerPlugin($this->renderer);
} elseif (strpos($serviceName, 'smarty.filter.') === 0) {
/** @var AbstractFilter $filter */
$filter = $this->container->get($serviceName);
$filter->registerFilter($this->renderer);
} elseif (strpos($serviceName, 'smarty.resource.') === 0) {
/** @var AbstractResource $resource */
$resource = $this->container->get($serviceName);
$resource->registerResource($this->renderer);
}
}
}
/**
* @return bool
*/
......
......@@ -8,12 +8,16 @@ namespace ACP3\Core\View\Renderer\Smarty;
abstract class AbstractPlugin implements PluginInterface
{
/**
* @param \Smarty $smarty
*
* @throws \SmartyException
* @inheritdoc
*/
public function registerPlugin(\Smarty $smarty)
public function register(\Smarty $smarty)
{
$smarty->registerPlugin($this->getPluginType(), $this->getPluginName(), [$this, 'process']);
if ($this->getExtensionType() === static::EXTENSION_TYPE_BLOCK ||
$this->getExtensionType() === static::EXTENSION_TYPE_FUNCTION ||
$this->getExtensionType() === static::EXTENSION_TYPE_MODIFIER) {
$smarty->registerPlugin($this->getExtensionType(), $this->getExtensionName(), [$this, 'process']);
} else if ($this->getExtensionType() === static::EXTENSION_TYPE_FILTER) {
$smarty->registerFilter($this->getExtensionName(), [$this, 'process']);
}
}
}
......@@ -2,6 +2,7 @@
namespace ACP3\Core\View\Renderer\Smarty\Blocks;
use ACP3\Core\View\Renderer\Smarty\AbstractPlugin;
use ACP3\Core\View\Renderer\Smarty\PluginInterface;
/**
* Class AbstractBlock
......@@ -12,9 +13,9 @@ abstract class AbstractBlock extends AbstractPlugin
/**
* @inheritdoc
*/
public function getPluginType()
public function getExtensionType()
{
return 'block';
return PluginInterface::EXTENSION_TYPE_BLOCK;
}
/**
......
......@@ -10,7 +10,7 @@ class Javascripts extends AbstractBlock
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'javascripts';
}
......
......@@ -10,7 +10,7 @@ class Stylesheets extends AbstractBlock
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'stylesheets';
}
......
services:
smarty.plugin.javascripts:
class: ACP3\Core\View\Renderer\Smarty\Blocks\Javascripts
tags:
- { name: core.view.extension }
smarty.plugin.stylesheets:
class: ACP3\Core\View\Renderer\Smarty\Blocks\Stylesheets
tags:
- { name: core.view.extension }
......@@ -3,6 +3,7 @@ namespace ACP3\Core\View\Renderer\Smarty\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Class RegisterPluginsPass
......@@ -20,30 +21,11 @@ class RegisterPluginsPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('core.view.renderer.smarty');
$definition = $container->findDefinition('core.view');
$plugins = $container->findTaggedServiceIds('core.view.extension');
$plugins = $container->findTaggedServiceIds('smarty.plugin');
foreach ($plugins as $plugin) {
$definition->addMethodCall()
}
$services = $this->container->getServiceIds();
foreach ($services as $serviceName) {
if (strpos($serviceName, 'smarty.plugin.') === 0) {
/** @var AbstractPlugin $plugin */
$plugin = $this->container->get($serviceName);
$plugin->registerPlugin($this->renderer);
} elseif (strpos($serviceName, 'smarty.filter.') === 0) {
/** @var AbstractFilter $filter */
$filter = $this->container->get($serviceName);
$filter->registerFilter($this->renderer);
} elseif (strpos($serviceName, 'smarty.resource.') === 0) {
/** @var AbstractResource $resource */
$resource = $this->container->get($serviceName);
$resource->registerResource($this->renderer);
}
foreach ($plugins as $serviceId => $tags) {
$definition->addMethodCall('registerPlugin', [new Reference($serviceId)]);
}
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\View\Renderer\Smarty\Filters;
use ACP3\Core\View\Renderer\Smarty\AbstractPlugin;
use ACP3\Core\View\Renderer\Smarty\PluginInterface;
/**
* Class AbstractFilter
* @package ACP3\Core\View\Renderer\Smarty\Filters
*/
abstract class AbstractFilter
abstract class AbstractFilter extends AbstractPlugin
{
/**
* @var string
* @inheritdoc
*/
protected $filterType = '';
/**
* @param \Smarty $smarty
*/
public function registerFilter(\Smarty $smarty)
public function getExtensionType()
{
$smarty->registerFilter($this->filterType, [$this, 'process']);
return PluginInterface::EXTENSION_TYPE_FILTER;
}
/**
......
......@@ -10,11 +10,6 @@ use ACP3\Core\Http\RequestInterface;
*/
class MoveToBottom extends AbstractFilter
{
/**
* @var string
*/
protected $filterType = 'output';
/**
* @var \ACP3\Core\Assets\AbstractMinifier
*/
......@@ -37,6 +32,14 @@ class MoveToBottom extends AbstractFilter
$this->request = $request;
}
/**
* @inheritdoc
*/
public function getExtensionName()
{
return 'output';
}
/**
* @param $tpl_output
* @param \Smarty_Internal_Template $smarty
......
......@@ -9,11 +9,6 @@ use ACP3\Core\Assets\MinifierInterface;
*/
class MoveToHead extends AbstractFilter
{
/**
* @var string
*/
protected $filterType = 'output';
/**
* @var \ACP3\Core\Assets\MinifierInterface
*/
......@@ -27,6 +22,14 @@ class MoveToHead extends AbstractFilter
$this->minifier = $minifier;
}
/**
* @inheritdoc
*/
public function getExtensionName()
{
return 'output';
}
/**
* @param $tpl_output
* @param \Smarty_Internal_Template $smarty
......
......@@ -9,11 +9,6 @@ use ACP3\Core;
*/
class PageCssClasses extends AbstractFilter
{
/**
* @var string
*/
protected $filterType = 'output';
/**
* @var \ACP3\Core\Assets\PageCssClasses
*/
......@@ -39,6 +34,14 @@ class PageCssClasses extends AbstractFilter
$this->request = $request;
}
/**
* @inheritdoc
*/
public function getExtensionName()
{
return 'output';
}
/**
* @param $tpl_output
* @param \Smarty_Internal_Template $smarty
......
......@@ -2,11 +2,17 @@ services:
smarty.filter.move_to_bottom:
class: ACP3\Core\View\Renderer\Smarty\Filters\MoveToBottom
arguments: ['@core.assets.minifier.javascript', '@core.request']
tags:
- { name: core.view.extension }
smarty.filter.move_to_head:
class: ACP3\Core\View\Renderer\Smarty\Filters\MoveToHead
arguments: ['@core.assets.minifier.css']
tags:
- { name: core.view.extension }
smarty.filter.page_css_classes:
class: ACP3\Core\View\Renderer\Smarty\Filters\PageCssClasses
arguments: ['@core.assets.pageCssClasses', '@core.request']
\ No newline at end of file
arguments: ['@core.assets.pageCssClasses', '@core.request']
tags:
- { name: core.view.extension }
\ No newline at end of file
......@@ -2,6 +2,7 @@
namespace ACP3\Core\View\Renderer\Smarty\Functions;
use ACP3\Core\View\Renderer\Smarty\AbstractPlugin;
use ACP3\Core\View\Renderer\Smarty\PluginInterface;
/**
* Class AbstractFunction
......@@ -12,9 +13,9 @@ abstract class AbstractFunction extends AbstractPlugin
/**
* @return string
*/
public function getPluginType()
public function getExtensionType()
{
return 'function';
return PluginInterface::EXTENSION_TYPE_FUNCTION;
}
/**
......
......@@ -25,7 +25,7 @@ class CheckAccess extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'check_access';
}
......
......@@ -25,7 +25,7 @@ class DateFormat extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'date_format';
}
......
......@@ -23,7 +23,7 @@ class DateRange extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'date_range';
}
......
......@@ -65,7 +65,7 @@ class Datepicker extends AbstractFunction
/**
* @return string
*/
public function getPluginName()
public function getExtensionName()
{
return 'datepicker';
}
......
......@@ -25,7 +25,7 @@ class HasPermission extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'has_permission';
}
......
......@@ -38,7 +38,7 @@ class IncludeJs extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'include_js';
}
......
......@@ -29,7 +29,7 @@ class JsLibraries extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'js_libraries';
}
......
......@@ -25,7 +25,7 @@ class Lang extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'lang';
}
......
......@@ -27,7 +27,7 @@ class LoadModule extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'load_module';
}
......
......@@ -25,7 +25,7 @@ class Mark extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'mark';
}
......
......@@ -47,7 +47,7 @@ class PageCssClasses extends AbstractFunction
/**
* @return string
*/
public function getPluginName()
public function getExtensionName()
{
return 'page_css_classes';
}
......
......@@ -18,7 +18,7 @@ class PageTitle extends SiteTitle
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'page_title';
}
......
......@@ -25,7 +25,7 @@ class RedirectMessage extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'redirect_message';
}
......
......@@ -18,7 +18,7 @@ class SiteAndPageTitle extends SiteTitle
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'site_and_page_title';
}
......
......@@ -37,7 +37,7 @@ class SiteTitle extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'site_title';
}
......
......@@ -25,7 +25,7 @@ class TemplatePath extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'template_path';
}
......
......@@ -25,7 +25,7 @@ class Uri extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'uri';
}
......
......@@ -26,7 +26,7 @@ class WYSIWYG extends AbstractFunction
/**
* @inheritdoc
*/
public function getPluginName()
public function getExtensionName()
{
return 'wysiwyg';
}
......
......@@ -2,71 +2,107 @@ services:
smarty.plugin.check_access:
class: ACP3\Core\View\Renderer\Smarty\Functions\CheckAccess
arguments: ['@core.helpers.view.checkAccess']
tags:
- { name: core.view.extension }
smarty.plugin.date_format:
class: ACP3\Core\View\Renderer\Smarty\Functions\DateFormat
arguments: ['@core.date']
tags:
- { name: core.view.extension }
smarty.plugin.date_picker:
class: ACP3\Core\View\Renderer\Smarty\Functions\Datepicker
arguments: [@core.view, @core.helpers.date]
tags:
- { name: core.view.extension }
smarty.plugin.date_ramge:
class: ACP3\Core\View\Renderer\Smarty\Functions\DateRange
arguments: [@core.helpers.formatter.dateRange]
tags:
- { name: core.view.extension }
smarty.plugin.redirect_message:
class: ACP3\Core\View\Renderer\Smarty\Functions\RedirectMessage
arguments: ['@core.helpers.redirect']
tags:
- { name: core.view.extension }
smarty.plugin.has_permission:
class: ACP3\Core\View\Renderer\Smarty\Functions\HasPermission
arguments: ['@core.acl']
tags:
- { name: core.view.extension }
smarty.plugin.include_js:
class: ACP3\Core\View\Renderer\Smarty\Functions\IncludeJs
arguments: ['@core.assets', '@core.assets.themeResolver']
tags:
- { name: core.view.extension }
smarty.plugin.js_libraries:
class: ACP3\Core\View\Renderer\Smarty\Functions\JsLibraries
arguments: ['@core.assets']
tags:
- { name: core.view.extension }
smarty.plugin.lang:
class: ACP3\Core\View\Renderer\Smarty\Functions\Lang
arguments: ['@core.lang']
tags:
- { name: core.view.extension }
smarty.plugin.load_module:
class: ACP3\Core\View\Renderer\Smarty\Functions\LoadModule
arguments: ['@service_container']
tags:
- { name: core.view.extension }
smarty.plugin.mark:
class: ACP3\Core\View\Renderer\Smarty\Functions\Mark
arguments: ['@core.lang']
tags:
- { name: core.view.extension }
smarty.plugin.page_css_classes:
class: ACP3\Core\View\Renderer\Smarty\Functions\PageCssClasses
arguments: ['@core.assets.pageCssClasses']
tags:
- { name: core.view.extension }
smarty.plugin.page_title:
class: ACP3\Core\View\Renderer\Smarty\Functions\PageTitle
arguments: ['@core.breadcrumb']
tags:
- { name: core.view.extension }
smarty.plugin.site_title:
class: ACP3\Core\View\Renderer\Smarty\Functions\SiteTitle
arguments: ['@core.breadcrumb']
tags:
- { name: core.view.extension }
smarty.plugin.site_and_page_title:
class: ACP3\Core\View\Renderer\Smarty\Functions\SiteAndPageTitle
arguments: ['@core.breadcrumb']
tags:
- { name: core.view.extension }
smarty.plugin.template_path:
class: ACP3\Core\View\Renderer\Smarty\Functions\TemplatePath
arguments: ['@core.assets.themeResolver']
tags:
- { name: core.view.extension }
smarty.plugin.uri:
class: ACP3\Core\View\Renderer\Smarty\Functions\Uri
arguments: ['@core.router']
tags:
- { name: core.view.extension }
smarty.plugin.wysiwyg:
class: ACP3\Core\View\Renderer\Smarty\Functions\WYSIWYG
arguments: ['@service_container']
tags:
- { name: core.view.extension }
......@@ -2,6 +2,7 @@
namespace ACP3\Core\View\Renderer\Smarty\Modifiers;
use ACP3\Core\View\Renderer\Smarty\AbstractPlugin;
use ACP3\Core\View\Renderer\Smarty\PluginInterface;
/**
* Class AbstractModifier
......@@ -12,9 +13,9 @@ abstract class AbstractModifier extends AbstractPlugin
/**
* @inheritdoc
*/
public function getPluginType()