From 85ec6fe9d980e81ae25169a28cf7fbf64852c0a1 Mon Sep 17 00:00:00 2001 From: cosmin Date: Fri, 25 May 2012 01:55:37 +0300 Subject: [PATCH] Updated module to be compatible with beta4 version of ZF2. --- LICENSE | 2 +- Module.php | 73 ++--- README.md | 68 +++- autoload/classmap.php | 26 +- config/module.config.php | 92 ++---- config/module.di.config.php | 301 ------------------ config/service.config.php | 7 + src/ZeTwig/View/Environment.php | 20 +- src/ZeTwig/View/Extension.php | 29 +- .../View/Service/ViewTwigRendererFactory.php | 73 +++++ .../View/Service/ViewTwigStrategyFactory.php | 37 +++ 11 files changed, 245 insertions(+), 483 deletions(-) delete mode 100644 config/module.di.config.php create mode 100644 config/service.config.php create mode 100644 src/ZeTwig/View/Service/ViewTwigRendererFactory.php create mode 100644 src/ZeTwig/View/Service/ViewTwigStrategyFactory.php diff --git a/LICENSE b/LICENSE index 286aa04..78b2af9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012 by the ZendExperts Team, see AUTHORS for more details. +Copyright (c) 2012 to ZendExperts Team, see AUTHORS for more details. Some rights reserved. diff --git a/Module.php b/Module.php index bb7975b..b8852b0 100644 --- a/Module.php +++ b/Module.php @@ -1,5 +1,4 @@ */ -class Module implements AutoloaderProvider +class Module implements AutoloaderProviderInterface { /** - * @var \Zend\Mvc\AppContext - */ - protected static $application; - - /** - * Module initialization - * @param \Zend\Module\Manager $moduleManager + * @var \Zend\ServiceManager\ServiceManager */ - public function init(Manager $moduleManager) - { - $events = StaticEventManager::getInstance(); - $events->attach('bootstrap', 'bootstrap', array($this, 'bootstrap'), 100); - } - - public function bootstrap($event) - { - // Register a "render" event, at high priority (so it executes prior - // to the view attempting to render) - $app = $event->getParam('application'); - static::$application = $app; - $app->events()->attach('render', array($this, 'registerTwigStrategy'), 100); - } + protected static $serviceManager; - public function registerTwigStrategy(Event $event) + public function onBootstrap(MvcEvent $event) { - $app = $event->getTarget(); - $locator = $app->getLocator(); - $view = $locator->get('Zend\View\View'); - $twigStrategy = $locator->get('ZeTwig\View\Strategy\TwigRendererStrategy'); - - $renderer = $twigStrategy->getRenderer(); - $basePath = $app->getRequest()->getBasePath(); - $renderer->plugin('basePath')->setBasePath($basePath); - $renderer->plugin('url')->setRouter($event->getRouter()); - $renderer->plugin('headTitle') - ->setSeparator(' - ') - ->setAutoEscape(false); - - // Attach strategy, which is a listener aggregate, at high priority - $view->events()->attach($twigStrategy, 100); + // Set the static service manager instance so we can use it everywhere in the module + $app = $event->getApplication(); + static::$serviceManager = $app->getServiceManager(); } /** @@ -80,32 +45,36 @@ public function getAutoloaderConfig() 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), - 'prefixes' => array( - 'Twig' => __DIR__ . '/vendor/Twig/lib/Twig' - ) ), ); } + /** + * Get Service Configuration + * @return array + */ + public function getServiceConfiguration(){ + return include __DIR__ . '/config/service.config.php'; + } + /** * Get Module Configuration * @return mixed */ public function getConfig() { - $definitions = include __DIR__ . '/config/module.di.config.php'; $config = include __DIR__ . '/config/module.config.php'; - $config = array_merge_recursive($definitions, $config); return $config; } /** + * Return the ServiceManager instance * @static - * @return \Zend\Mvc\AppContext + * @return \Zend\ServiceManager\ServiceManager */ - public static function getApplication() + public static function getServiceManager() { - return static::$application; + return static::$serviceManager; } } \ No newline at end of file diff --git a/README.md b/README.md index 2137418..bc7e142 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,82 @@ ZeTwig ==== -ZeTwig is a Twig / Zend Framework 2 module compatible with ViewModels from beta3. +ZeTwig is a Twig / Zend Framework 2 module compatible the beta4 version of the framework. It allows to render view templates using Twig instead of the default PHP templates. It also supports aliases for your template names, rendering a particular action from within the template files (follows the save naming conventions as Symfony) and triggering events on an object with different parameters. +Instalation +----------- + +ZeTwig can be installed using Composer by simply adding the following lines to your composer.json file: + + ```json + "require": { + "ZendExperts/ZeTwig": "1.0.*" + } + ``` + +Then run `php composer.phar update`. + +The module also defines a set of options that you can change from within the configuration files: + + ```php + 'zendexperts_zetwig' => array( + //you can change the extension of the loaded templates here + 'template_suffix' => 'twig', + 'extensions' => array( + //add any extensions you want to register with twig + ), + //set twig environment options + 'environment_options' => array( + 'cache' => BASE_PATH . '/data/cache/twig', + 'auto_reload' => true, + 'debug' => true + ), + ), + ``` + Documentation ------------- -Upgraded to the new beta3 version of ZF2 View Models. +2012.05.25: Upgraded to the beta4 version of ZF2 View Models. Any command from the original Twig library should work and also added support for Zend View helpers as functions and PHP functions as a fallback. -With this new update you should be able to use template names as aliases that can be -mapped to any twig file. - You can define an array for aliases within the configuration file for your modules and use those aliases throughout your code, instead of a specific file name. This way you can easily change the main layout of your pages from the configuration file and allow other modules to change them as well (this allows your code to be extensible and allows templates to have their own structure). -This latest version also contains two new constructs: +Apart from the functionality listed above the module adds two extension tags: -1. A tag for rendering a controller action, which follows the Symfony naming conventions - or the controller alias and can be used as : - +1. A tag for rendering a controller action, which follows the Symfony naming conventions + or the controller alias: + + ```html + {% render "Core:Index:index" %} + ``` + +The twig tag will call the action "index" from the "IndexController" located within the "Core" +module and based on the returned value it will either render a specific template or output the +returned value, following the same principles as with any other zf2 action. + +Optionally you can also specify different parameters to send to the processed action which can +later be retrieved from the matched route: + + ```html {% render "Core:Index:index" with {'param1':1} %} + ``` 1. A tag for triggering an event on the renderer that is similar to the above syntax: - + + ```html {% trigger "myRendererEvent" on myObject with {'param1':1} %} + ``` Both the target object and parameters are optional. The result of each listener is -converted to string and rendered intead of the definition. - -Also a new functionality allows the use of aliases within your template code or when -rendering a template. \ No newline at end of file +converted to string and rendered intead of the definition. \ No newline at end of file diff --git a/autoload/classmap.php b/autoload/classmap.php index f58e5a5..b934388 100644 --- a/autoload/classmap.php +++ b/autoload/classmap.php @@ -4,16 +4,18 @@ */ $prefix = dirname(__DIR__) . ""; return array( - 'ZeTwig\Module' => $prefix . '/Module.php', - 'ZeTwig\View\Environment' => $prefix . '/src/ZeTwig/View\Environment.php', - 'ZeTwig\View\Extension' => $prefix . '/src/ZeTwig/View\Extension.php', - 'ZeTwig\View\HelperFunction' => $prefix . '/src/ZeTwig/View\HelperFunction.php', - 'ZeTwig\View\Renderer' => $prefix . '/src/ZeTwig/View\Renderer.php', - 'ZeTwig\View\Resolver' => $prefix . '/src/ZeTwig/View\Resolver.php', - 'ZeTwig\View\Exception\TemplateException' => $prefix . '/src/ZeTwig/View\Exception\TemplateException.php', - 'ZeTwig\View\Extension\Render\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Render\RenderNode.php', - 'ZeTwig\View\Extension\Render\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Render\TokenParser.php', - 'ZeTwig\View\Extension\Trigger\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Trigger\RenderNode.php', - 'ZeTwig\View\Extension\Trigger\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Trigger\TokenParser.php', - 'ZeTwig\View\Strategy\TwigRendererStrategy' => $prefix . '/src/ZeTwig/View\Strategy\TwigRendererStrategy.php', + 'ZeTwig\Module' => $prefix . '/Module.php', + 'ZeTwig\View\Environment' => $prefix . '/src/ZeTwig/View\Environment.php', + 'ZeTwig\View\Extension' => $prefix . '/src/ZeTwig/View\Extension.php', + 'ZeTwig\View\HelperFunction' => $prefix . '/src/ZeTwig/View\HelperFunction.php', + 'ZeTwig\View\Renderer' => $prefix . '/src/ZeTwig/View\Renderer.php', + 'ZeTwig\View\Resolver' => $prefix . '/src/ZeTwig/View\Resolver.php', + 'ZeTwig\View\Exception\TemplateException' => $prefix . '/src/ZeTwig/View\Exception\TemplateException.php', + 'ZeTwig\View\Extension\Render\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Render\RenderNode.php', + 'ZeTwig\View\Extension\Render\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Render\TokenParser.php', + 'ZeTwig\View\Extension\Trigger\RenderNode' => $prefix . '/src/ZeTwig/View\Extension\Trigger\RenderNode.php', + 'ZeTwig\View\Extension\Trigger\TokenParser' => $prefix . '/src/ZeTwig/View\Extension\Trigger\TokenParser.php', + 'ZeTwig\View\Strategy\TwigRendererStrategy' => $prefix . '/src/ZeTwig/View\Strategy\TwigRendererStrategy.php', + 'ZeTwig\View\Service\ViewTwigRendererFactory' => $prefix . '/src/ZeTwig/View\Service\ViewTwigRendererFactory.php', + 'ZeTwig\View\Service\ViewTwigStrategyFactory' => $prefix . '/src/ZeTwig/View\Service\ViewTwigStrategyFactory.php', ); \ No newline at end of file diff --git a/config/module.config.php b/config/module.config.php index 56e29b3..cee26d3 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,77 +1,27 @@ array( - 'instance' => array( - 'alias'=>array( - 'view'=>'ZeTwig\View\Renderer' - ), - // Inject the plugin broker for controller plugins into - // the action controller for use by all controllers that - // extend it. -// 'Zend\Mvc\Controller\ActionController' => array( -// 'parameters' => array( -// 'broker' => 'Zend\Mvc\Controller\PluginBroker', -// ), -// ), -// 'Zend\Mvc\Controller\PluginBroker' => array( -// 'parameters' => array( -// 'loader' => 'Zend\Mvc\Controller\PluginLoader', -// ), -// ), -// 'Zend\View\Resolver\TemplateMapResolver' => array( -// 'parameters' => array( -// 'map' => array( -// ), -// ), -// ), - 'Zend\View\Resolver\TemplatePathStack' => array( - 'parameters' => array( - 'defaultSuffix'=>'twig', - ), - ), - 'ZeTwig\View\Resolver'=>array( - 'injections' => array( - 'Zend\View\Resolver\TemplateMapResolver', - 'Zend\View\Resolver\TemplatePathStack', - ), - ), - 'ZeTwig\View\Renderer' => array( - 'parameters' => array( - 'broker' => 'Zend\View\HelperBroker', - 'environment'=>'ZeTwig\View\Environment', - ), - ), - 'Zend\Mvc\View\DefaultRenderingStrategy' => array( - 'parameters' => array( - 'layoutTemplate' => 'layouts/layout', - ), - ), - 'Zend\Mvc\View\ExceptionStrategy' => array( - 'parameters' => array( - 'displayExceptions' => true, - 'exceptionTemplate' => 'error/index', - ), - ), - 'Zend\Mvc\View\RouteNotFoundStrategy' => array( - 'parameters' => array( - 'displayNotFoundReason' => true, - 'displayExceptions' => true, - 'notFoundTemplate' => 'error/404', - ), + 'view_manager' => array( + 'display_not_found_reason' => true, + 'display_exceptions' => true, + 'layout' => 'layout/layout', + 'doctype' => 'HTML5', + 'not_found_template' => 'error/404', + 'exception_template' => 'error/index', + 'strategies' => array( + 'ze-twig' => 'ViewTwigRendererStrategy', + ) + ), + + 'zendexperts_zetwig' => array( + 'template_suffix' => 'twig', + 'extensions' => array( + 'ZeTwig' => 'ZeTwig\View\Extension' ), - 'ZeTwig\View\Environment'=>array( - 'injections' => array( - 'ZeTwig'=>'ZeTwig\View\Extension' - ), - 'parameters' => array( - 'loader' => 'ZeTwig\View\Resolver', - 'options' => array( - 'cache' => BASE_PATH . '/data/cache/twig', - 'auto_reload' => true, - 'debug' => true - ), - ), + 'environment_options' => array( + 'cache' => BASE_PATH . '/data/cache/twig', + 'auto_reload' => true, + 'debug' => true ), - ), ), + ); diff --git a/config/module.di.config.php b/config/module.di.config.php deleted file mode 100644 index 714df0e..0000000 --- a/config/module.di.config.php +++ /dev/null @@ -1,301 +0,0 @@ - array( - 'definition' => array( - 'class' => array( - 'ZeTwig\\View\\Environment' => array( - '__construct' => array( - 'loader' => array( - 'type' => 'ZeTwig\\View\\Resolver', - 'required' => false, - ), - 'broker' => array( - 'type' => 'Zend\\View\\HelperBroker', - 'required' => false, - ), - 'options' => array( - 'type' => null, - 'required' => false, - ), - ), - 'setLocator' => array( - 'locator' => array( - 'type' => 'Zend\\Di\\Locator', - 'required' => true, - ), - ), - 'setEnvironmentOptions' => array( - 'environment_options' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setBroker' => array( - 'broker' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setBaseTemplateClass' => array( - 'class' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setCache' => array( - 'cache' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setLexer' => array( - 'lexer' => array( - 'type' => 'Twig_LexerInterface', - 'required' => true, - ), - ), - 'setParser' => array( - 'parser' => array( - 'type' => 'Twig_ParserInterface', - 'required' => true, - ), - ), - 'setCompiler' => array( - 'compiler' => array( - 'type' => 'Twig_CompilerInterface', - 'required' => true, - ), - ), - 'setLoader' => array( - 'loader' => array( - 'type' => 'Twig_LoaderInterface', - 'required' => true, - ), - ), - 'setCharset' => array( - 'charset' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setExtensions' => array( - 'extensions' => array( - 'type' => null, - 'required' => true, - ), - ), - 'addExtension' => array( - 'extension' => array('type' => 'Twig_ExtensionInterface', 'required' => true) - ) - ), - 'ZeTwig\\View\\Exception\\TemplateException' => array( - '__construct' => array( - 'message' => array( - 'type' => null, - 'required' => false, - ), - 'code' => array( - 'type' => null, - 'required' => false, - ), - 'previous' => array( - 'type' => null, - 'required' => false, - ), - ), - ), - 'ZeTwig\\View\\Extension\\Render\\RenderNode' => array( - '__construct' => array( - 'expr' => array( - 'type' => 'Twig_Node_Expression', - 'required' => true, - ), - 'attributes' => array( - 'type' => 'Twig_Node_Expression', - 'required' => true, - ), - 'options' => array( - 'type' => 'Twig_Node_Expression', - 'required' => true, - ), - 'lineno' => array( - 'type' => null, - 'required' => true, - ), - 'tag' => array( - 'type' => null, - 'required' => false, - ), - ), - 'setAttribute' => array( - 'name' => array( - 'type' => null, - 'required' => true, - ), - 'value' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setNode' => array( - 'name' => array( - 'type' => null, - 'required' => true, - ), - 'node' => array( - 'type' => null, - 'required' => false, - ), - ), - ), - 'ZeTwig\\View\\Extension\\Render\\TokenParser' => array( - 'setParser' => array( - 'parser' => array( - 'type' => 'Twig_Parser', - 'required' => true, - ), - ), - ), - 'ZeTwig\\View\\Extension\\Trigger\\RenderNode' => array( - '__construct' => array( - 'event' => array( - 'type' => 'Twig_Node_Expression', - 'required' => true, - ), - 'target' => array( - 'type' => 'Twig_Node_Expression', - 'required' => true, - ), - 'attributes' => array( - 'type' => 'Twig_Node_Expression', - 'required' => true, - ), - 'lineno' => array( - 'type' => null, - 'required' => true, - ), - 'tag' => array( - 'type' => null, - 'required' => false, - ), - ), - 'setAttribute' => array( - 'name' => array( - 'type' => null, - 'required' => true, - ), - 'value' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setNode' => array( - 'name' => array( - 'type' => null, - 'required' => true, - ), - 'node' => array( - 'type' => null, - 'required' => false, - ), - ), - ), - 'ZeTwig\\View\\Extension\\Trigger\\TokenParser' => array( - 'setParser' => array( - 'parser' => array( - 'type' => 'Twig_Parser', - 'required' => true, - ), - ), - ), - 'ZeTwig\\View\\Extension' => array( - 'setEventManager' => array( - 'events' => array( - 'type' => false, - 'required' => false, - ), - ), - ), - 'ZeTwig\\View\\HelperFunction' => array( - '__construct' => array( - 'name' => array( - 'type' => null, - 'required' => true, - ), - 'options' => array( - 'type' => null, - 'required' => false, - ), - ), - 'setArguments' => array( - 'arguments' => array( - 'type' => null, - 'required' => true, - ), - ), - ), - 'ZeTwig\\View\\Renderer' => array( - '__construct' => array( - 'environment' => array( - 'type' => 'ZeTwig\\View\\Environment', - 'required' => true, - ), - 'config' => array( - 'type' => null, - 'required' => false, - ), - ), - 'setResolver' => array( - 'resolver' => array( - 'type' => 'Zend\\View\\Resolver', - 'required' => true, - ), - ), - 'setEnvironmentOptions' => array( - 'options' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setBroker' => array( - 'broker' => array( - 'type' => null, - 'required' => true, - ), - ), - 'setFilterChain' => array( - 'filters' => array( - 'type' => 'Zend\\Filter\\FilterChain', - 'required' => true, - ), - ), - 'setCanRenderTrees' => array( - 'renderTrees' => array( - 'type' => null, - 'required' => true, - ), - ), - ), - 'ZeTwig\\View\\Resolver' => array( - 'setConfig' => array( - 'config' => array( - 'type' => null, - 'required' => true, - ), - ), - 'attach' => array( - 'resolver' => array('type' => 'Zend\View\Resolver\ResolverInterface', 'required' => true) - ) - ), - 'ZeTwig\\View\\Strategy\\TwigRendererStrategy' => array( - '__construct' => array( - 'renderer' => array( - 'type' => 'ZeTwig\\View\\Renderer', - 'required' => true, - ), - ), - ), - ), - ), - ), -); \ No newline at end of file diff --git a/config/service.config.php b/config/service.config.php new file mode 100644 index 0000000..ce3cb4d --- /dev/null +++ b/config/service.config.php @@ -0,0 +1,7 @@ + array( + 'ViewTwigRendererStrategy' =>'ZeTwig\View\Service\ViewTwigStrategyFactory', + 'ViewTwigRenderer' =>'ZeTwig\View\Service\ViewTwigRendererFactory', + ) +); diff --git a/src/ZeTwig/View/Environment.php b/src/ZeTwig/View/Environment.php index 55ff5e9..addaca9 100644 --- a/src/ZeTwig/View/Environment.php +++ b/src/ZeTwig/View/Environment.php @@ -11,8 +11,9 @@ use Zend\View\HelperBroker, Zend\Loader\Pluggable, - Zend\Loader\LocatorAware, Zend\Di\LocatorInterface, + Zend\ServiceManager\ServiceLocatorAwareInterface, + Zend\ServiceManager\ServiceLocatorInterface, Twig_Environment, Twig_Function_Function as TwigFunction, @@ -24,7 +25,7 @@ * @package ZeTwig * @author Cosmin Harangus */ -class Environment extends Twig_Environment implements Pluggable, LocatorAware +class Environment extends Twig_Environment implements Pluggable//, ServiceLocatorAwareInterface { /** * Zend View access broker for all the provided Zend Framework helpers @@ -73,16 +74,13 @@ public function __construct(Resolver $loader = null, HelperBroker $broker = null $this->setBroker( $broker ); } - public function setLocator(LocatorInterface $locator) + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { - $this->_locator = $locator; + $this->_locator = $serviceLocator; return $this; } - /** - * @return \Zend\Di\Di - */ - public function getLocator() + public function getServiceLocator() { return $this->_locator; } @@ -136,12 +134,12 @@ public function getFunction($name) /** * Get plugin broker instance * - * @return Zend\Loader\Broker + * @return \Zend\Loader\Broker */ public function getBroker() { if (null === $this->_broker){ - $this->_broker = $this->getLocator()->get('Zend\View\HelperBroker'); + $this->_broker = $this->getServiceLocator()->get('Zend\View\HelperBroker'); } return $this->_broker; } @@ -149,7 +147,7 @@ public function getBroker() /** * Set plugin broker instance * - * @param string|Broker $broker Plugin broker to load plugins + * @param string|\Zend\Loader\Broker $broker Plugin broker to load plugins * @return Zend\Loader\Pluggable */ public function setBroker($broker) diff --git a/src/ZeTwig/View/Extension.php b/src/ZeTwig/View/Extension.php index 6c0e276..011aef4 100644 --- a/src/ZeTwig/View/Extension.php +++ b/src/ZeTwig/View/Extension.php @@ -4,7 +4,7 @@ use Zend\Http\Response, Zend\Mvc\Controller\ActionController, Zend\View\Model\ModelInterface, - Zend\Loader\LocatorAware, + Zend\View\Model\ViewModel, Zend\Mvc\InjectApplicationEventInterface, Zend\EventManager\EventManager, Zend\EventManager\EventManagerInterface, @@ -107,7 +107,8 @@ public function triggerEvent($eventName, $target, $argv) */ public function renderAction($expr, $attributes, $options) { - $application = Module::getApplication(); + $serviceManager = Module::getServiceManager(); + $application = $serviceManager->get('Application'); //parse the name of the controller, action and template directory that should be used if (strpos($expr, '/')>0){ $params = explode('/',$expr); @@ -121,17 +122,12 @@ public function renderAction($expr, $attributes, $options) $actionName = $params[2]; $actionName = lcfirst($actionName); $actionName = strtolower(preg_replace('/([A-Z])/', '-$1', $actionName)); - $templateDir = lcfirst($moduleName).'-'.lcfirst($controllerName).'/'; + $templateDir = lcfirst($moduleName).'/'.lcfirst($controllerName).'/'; $controllerName = $moduleName.'\\Controller\\'.$controllerName.'Controller'; } //instantiate the controller based on the given name - $controller = $application->getLocator()->get($controllerName); - //inject the locator - if ($controller instanceof LocatorAware) { - $controller->setLocator($application->getLocator()); - } - + $controller = $serviceManager->get('ControllerLoader')->get($controllerName); //clone the MvcEvent and route and update them with the provided parameters $event = $application->getMvcEvent(); $routeMatch = clone $event->getRouteMatch(); @@ -162,7 +158,7 @@ public function renderAction($expr, $attributes, $options) //if the response is an instance of ViewModel then render that one if ($response instanceof ModelInterface){ $viewModel = $response; - }elseif (is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) { + }elseif ($response === null || is_array($response) || $response instanceof \ArrayAccess || $response instanceof \Traversable) { $viewModel = new ViewModel($response); $viewModel->setTemplate($templateDir . $actionName); }else{ @@ -171,18 +167,9 @@ public function renderAction($expr, $attributes, $options) $viewModel->terminate(); $viewModel->setOption('has_parent',true); - $view = $application->getLocator()->get('Zend\View\View'); + $view = $serviceManager->get('Zend\View\View'); $output = $view->render($viewModel); return $output; } -} - - - - - - - - - +} \ No newline at end of file diff --git a/src/ZeTwig/View/Service/ViewTwigRendererFactory.php b/src/ZeTwig/View/Service/ViewTwigRendererFactory.php new file mode 100644 index 0000000..3b31304 --- /dev/null +++ b/src/ZeTwig/View/Service/ViewTwigRendererFactory.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace ZeTwig\View\Service; + +use Zend\ServiceManager\FactoryInterface, + Zend\ServiceManager\ServiceLocatorInterface, + ZeTwig\View\Strategy\TwigRendererStrategy, + ZeTwig\View\Environment, + ZeTwig\View\Renderer; + +/** + * ZeTwig service renderer factory + * @package ZeTwig + * @author Cosmin Harangus + */ +class ViewTwigRendererFactory implements FactoryInterface +{ + + /** + * Create and return the twig view renderer + * + * @param ServiceLocatorInterface $serviceLocator + * @return TwigRendererStrategy + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $config = $serviceLocator->get('Configuration'); + $config = isset($config['zendexperts_zetwig']) && (is_array($config['zendexperts_zetwig']) || $config['zendexperts_zetwig'] instanceof ArrayAccess) + ? $config['zendexperts_zetwig'] + : array(); + + $viewLoader = $serviceLocator->get('view_manager')->getResolver(); + $loader = new \ZeTwig\View\Resolver(); + foreach($viewLoader->getIterator() as $resolver){ + if ($resolver instanceof \Zend\View\Resolver\TemplatePathStack){ + $resolver = clone $resolver; + $resolver->setDefaultSuffix($config['template_suffix']); + } + $loader->attach($resolver); + } + + $broker = $serviceLocator->get('view_manager')->getHelperBroker(); + $options = isset($config['environment_options']) ? $config['environment_options'] : array(); + $environment = new Environment($loader, $broker, $options); + if (isset($config['extensions'])){ + foreach($config['extensions'] as $extension){ + $extensionInstance = new $extension(); + if ($extensionInstance instanceof \Twig_ExtensionInterface){ + $environment->addExtension($extensionInstance); + } + } + } + + $twigRenderer = new Renderer($environment); + $this->defaultRendererSetup($twigRenderer); + return $twigRenderer; + } + + private function defaultRendererSetup($renderer) + { + $renderer->plugin('headTitle') + ->setSeparator(' - ') + ->setAutoEscape(false); + } + +} \ No newline at end of file diff --git a/src/ZeTwig/View/Service/ViewTwigStrategyFactory.php b/src/ZeTwig/View/Service/ViewTwigStrategyFactory.php new file mode 100644 index 0000000..74a683f --- /dev/null +++ b/src/ZeTwig/View/Service/ViewTwigStrategyFactory.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace ZeTwig\View\Service; + +use Zend\ServiceManager\FactoryInterface, + Zend\ServiceManager\ServiceLocatorInterface, + ZeTwig\View\Strategy\TwigRendererStrategy; + +/** + * ZeTwig service strategy factory + * @package ZeTwig + * @author Cosmin Harangus + */ +class ViewTwigStrategyFactory implements FactoryInterface +{ + + /** + * Create and return the twig view strategy + * + * @param ServiceLocatorInterface $serviceLocator + * @return TwigRendererStrategy + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $twigRenderer = $serviceLocator->get('ViewTwigRenderer'); + $twigStrategy = new TwigRendererStrategy($twigRenderer); + return $twigStrategy; + } + +} \ No newline at end of file