source: branches/0.4/web/addons/job_monarch/dwoo/Dwoo/Adapters/Agavi/DwooRenderer.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 6.2 KB
Line 
1<?php
2
3/**
4 * Dwoo adapter for Agavi
5 *
6 * Install instructions :
7 *  - download dwoo from dwoo.org and unzip it somewhere in your agavi app
8 *  - add a renderer to your output_types.xml as such :
9 *     <renderer name="dwoo" class="DwooRenderer">
10 *        <parameter name="assigns">
11 *           <parameter name="routing">ro</parameter>
12 *           <parameter name="request">rq</parameter>
13 *           <parameter name="controller">ct</parameter>
14 *           <parameter name="user">us</parameter>
15 *           <parameter name="translation_manager">tm</parameter>
16 *           <parameter name="request_data">rd</parameter>
17 *        </parameter>
18 *        <parameter name="extract_vars">true</parameter>
19 *        <parameters name="plugin_dir">
20 *           <parameter>%core.lib_dir%/dwoo_plugins</parameter>
21 *           <parameter>%core.lib_dir%/other_dwoo_plugins</parameter>
22 *        </parameters>
23 *     </renderer>
24 *
25 *  - add dwoo's directory to your include path or include dwooAutoload.php yourself
26 *    either through agavi's autoload.xml (with name="Dwoo") or through your index.php
27 *
28 * Notes:
29 *  - you can copy the /Dwoo/Adapters/Agavi/dwoo_plugins directory to your agavi app's
30 *    lib directory, or change the plugin_dir parameter in the output_types.xml file.
31 *    these plugins are agavi-specific helpers that shortens the syntax to call common
32 *    agavi helpers (i18n, routing, ..)
33 *  - in the previous versions of this adapter, only a single value could be passed to
34 *    plugin_dir parameter; now you can pass several plugin directories (see example)
35 *
36 * This software is provided 'as-is', without any express or implied warranty.
37 * In no event will the authors be held liable for any damages arising from the
38 * use of this software.
39 *
40 * @author     Jordi Boggiano <j.boggiano@seld.be>
41 * @copyright  Copyright (c) 2008, Jordi Boggiano
42 * @license    http://dwoo.org/LICENSE   Modified BSD License
43 * @link       http://dwoo.org/
44 * @version    1.1.1
45 * @date       2010-04-06
46 * @package    Dwoo
47 */
48class DwooRenderer extends AgaviRenderer implements AgaviIReusableRenderer
49{
50        /**
51         * @constant   string The directory inside the cache dir where templates will
52         *                    be stored in compiled form.
53         */
54        const COMPILE_DIR = 'templates';
55
56        /**
57         * @constant   string The subdirectory inside the compile dir where templates
58         *                    will be stored in compiled form.
59         */
60        const COMPILE_SUBDIR = 'dwoo';
61
62        /**
63         * @constant   string The directory inside the cache dir where cached content
64         *                    will be stored.
65         */
66        const CACHE_DIR = 'dwoo';
67
68        /**
69         * @var        Dwoo Dwoo template engine.
70         */
71        protected $dwoo = null;
72
73        /**
74         * @var        string A string with the default template file extension,
75         *                    including the dot.
76         */
77        protected $defaultExtension = '.html';
78
79        /**
80         * stores the (optional) plugin directories to add to the Dwoo_Loader
81         */
82        protected $plugin_dir = null;
83
84        /**
85         * Pre-serialization callback.
86         *
87         * Excludes the Dwoo instance to prevent excessive serialization load.
88         */
89        public function __sleep()
90        {
91                $keys = parent::__sleep();
92                unset($keys[array_search('dwoo', $keys)]);
93                return $keys;
94        }
95
96        /**
97         * Initialize this Renderer.
98         *
99         * @param      AgaviContext The current application context.
100         * @param      array        An associative array of initialization parameters.
101         */
102        public function initialize(AgaviContext $context, array $parameters = array())
103        {
104                parent::initialize($context, $parameters);
105
106                $this->plugin_dir = $this->getParameter('plugin_dir', $this->plugin_dir);
107        }
108
109        /**
110         * provides a custom compiler to the dwoo renderer with optional settings
111         * you can set in the agavi output_types.xml config file
112         *
113         * @return Dwoo_Compiler
114         */
115        public function compilerFactory()
116        {
117                if (class_exists('Dwoo_Compiler', false) === false) {
118                        include DWOO_DIRECTORY . 'Dwoo/Compiler.php';
119                }
120                $compiler = Dwoo_Compiler::compilerFactory();
121                $compiler->setAutoEscape((bool) $this->getParameter('auto_escape', false));
122                return $compiler;
123        }
124
125        /**
126         * Grab a cleaned up dwoo instance.
127         *
128         * @return     Dwoo A Dwoo instance.
129         */
130        protected function getEngine()
131        {
132                if($this->dwoo) {
133                        return $this->dwoo;
134                }
135
136                // this triggers Agavi autoload
137                if(!class_exists('Dwoo')) {
138                        if (file_exists(dirname(__FILE__).'/../../../dwooAutoload.php')) {
139                                // file was dropped with the entire dwoo package
140                                require dirname(__FILE__).'/../../../dwooAutoload.php';
141                        } else {
142                                // assume the dwoo package is in the include path
143                                require 'dwooAutoload.php';
144                        }
145                }
146
147                $parentMode = fileperms(AgaviConfig::get('core.cache_dir'));
148
149                $compileDir = AgaviConfig::get('core.cache_dir') . DIRECTORY_SEPARATOR . self::COMPILE_DIR . DIRECTORY_SEPARATOR . self::COMPILE_SUBDIR;
150                AgaviToolkit::mkdir($compileDir, $parentMode, true);
151
152                $cacheDir = AgaviConfig::get('core.cache_dir') . DIRECTORY_SEPARATOR . self::CACHE_DIR;
153                AgaviToolkit::mkdir($cacheDir, $parentMode, true);
154
155                $this->dwoo = new Dwoo_Core($compileDir, $cacheDir);
156
157                if (!empty($this->plugin_dir)) {
158                        foreach ((array) $this->plugin_dir as $dir) {
159                                $this->dwoo->getLoader()->addDirectory($dir);
160                        }
161                }
162
163                $this->dwoo->setDefaultCompilerFactory('file', array($this, 'compilerFactory'));
164
165                return $this->dwoo;
166        }
167
168        /**
169         * Render the presentation and return the result.
170         *
171         * @param      AgaviTemplateLayer The template layer to render.
172         * @param      array              The template variables.
173         * @param      array              The slots.
174         * @param      array              Associative array of additional assigns.
175         *
176         * @return     string A rendered result.
177         */
178        public function render(AgaviTemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array())
179        {
180                $engine = $this->getEngine();
181
182                $data = array();
183                if($this->extractVars) {
184                        $data = $attributes;
185                } else {
186                        $data[$this->varName] = &$attributes;
187                }
188
189                $data[$this->slotsVarName] =& $slots;
190
191                foreach($this->assigns as $key => $getter) {
192                        $data[$key] = $this->getContext()->$getter();
193                }
194
195                foreach($moreAssigns as $key => &$value) {
196                        if(isset($this->moreAssignNames[$key])) {
197                                $key = $this->moreAssignNames[$key];
198                        }
199                        $data[$key] =& $value;
200                }
201
202                return $engine->get($layer->getResourceStreamIdentifier(), $data);
203        }
204}
Note: See TracBrowser for help on using the repository browser.