Test Failed
Push — master ( a28393...a400a4 )
by David
06:01 queued 03:02
created

lib/Dwoo/Adapters/ZendFramework/View.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Copyright (c) 2013-2016
5
 *
6
 * @category  Library
7
 * @package   Dwoo\Adapters\ZendFramework
8
 * @author    Jordi Boggiano <[email protected]>
9
 * @author    David Sanchez <[email protected]>
10
 * @copyright 2008-2013 Jordi Boggiano
11
 * @copyright 2013-2016 David Sanchez
12
 * @license   http://dwoo.org/LICENSE Modified BSD License
13
 * @version   1.3.0
14
 * @date      2016-09-19
15
 * @link      http://dwoo.org/
16
 */
17
class Dwoo_Adapters_ZendFramework_View extends Zend_View_Abstract
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
    /**
20
     * @var Core
21
     */
22
    protected $_engine = null;
23
24
    /**
25
     * @var Dwoo_Data
26
     */
27
    protected $_dataProvider = null;
28
29
    /**
30
     * @var Dwoo_Compiler
31
     */
32
    protected $_compiler = null;
33
34
    /**
35
     * Changing Filter's scope to play nicely.
36
     *
37
     * @var array
38
     */
39
    protected $_filter = array();
40
41
    /**
42
     * @var string
43
     */
44
    protected $_templateFileClass = 'Dwoo_Template_File';
45
46
    /**
47
     * @var array
48
     */
49
    protected $_templateFileSettings = array();
50
51
    /**
52
     * @var Dwoo_IPluginProxy
53
     */
54
    protected $_pluginProxy = null;
55
56
    /**
57
     * Constructor method.
58
     * See setOptions for $opt details.
59
     *
60
     * @see setOptions
61
     *
62
     * @param array|Zend_Config List of options or Zend_Config instance
63
     */
64
    public function __construct($opt = array())
65
    {
66
        if (is_array($opt)) {
67
            $this->setOptions($opt);
68
        } elseif ($opt instanceof Zend_Config) {
69
            $this->setConfig($opt);
70
        }
71
72
        $this->init();
73
    }
74
75
    /**
76
     * Set object state from options array
77
     *  - engine        = engine class name|engine object|array of options for engine
78
     *  - dataProvider  = data provider class name|data provider object|array of options for data provider
79
     *  - compiler      = compiler class name|compiler object|array of options for compiler
80
     *  - templateFile  =.
81
     *
82
     *  Array of options:
83
     *  - type class name or object for engine, dataProvider or compiler
84
     *  - any set* method (compileDir for setCompileDir ...)
85
     *
86
     * @param array $options
87
     *
88
     * @return Dwoo_Adapters_ZendFramework_View
89
     */
90
    public function setOptions(array $opt = array())
91
    {
92
        // Making sure that everything is loaded.
93
        $classes = array('engine', 'dataProvider', 'compiler');
94
95
        // Setting options to Dwoo objects...
96
        foreach ($opt as $type => $settings) {
97
            if (!method_exists($this, 'set'.$type)) {
98
                throw new Dwoo_Exception("Unknown type $type");
99
            }
100
101
            if (is_string($settings) || is_object($settings)) {
102
                call_user_func(array($this, 'set'.$type), $settings);
103
            } elseif (is_array($settings)) {
104
                // Set requested class
105
                if (array_key_exists('type', $settings)) {
106
                    call_user_func(array($this, 'set'.$type), $settings['type']);
107
                }
108
109
                if (in_array($type, $classes)) {
110
                    // Call get so that the class is initialized
111
                    $rel = call_user_func(array($this, 'get'.$type));
112
113
                    // Call set*() methods so that all the settings are set.
114 View Code Duplication
                    foreach ($settings as $method => $value) {
115
                        if (method_exists($rel, 'set'.$method)) {
116
                            call_user_func(array($rel, 'set'.$method), $value);
117
                        }
118
                    }
119
                } elseif ('templateFile' == $type) {
120
                    // Remember the settings for the templateFile
121
                    $this->_templateFileSettings = $settings;
122
                }
123
            }
124
        }
125
    }
126
127
    /**
128
     * Set object state from Zend_Config object.
129
     *
130
     * @param Zend_Config $config
131
     *
132
     * @return Dwoo_Adapters_ZendFramework_View
133
     */
134
    public function setConfig(Zend_Config $config)
135
    {
136
        return $this->setOptions($config->toArray());
137
    }
138
139
    /**
140
     * Called before template rendering.
141
     *
142
     * Binds plugin proxy to the Dwoo.
143
     *
144
     * @see Dwoo_Adapters_ZendFramework_View::getPluginProxy();
145
     * @see Dwoo_Core::setPluginProxy();
146
     */
147
    protected function preRender()
148
    {
149
        $this->getEngine()->setPluginProxy($this->getPluginProxy());
150
    }
151
152
    /**
153
     * Wraper for Dwoo_Data::__set()
154
     * allows to assign variables using the object syntax.
155
     *
156
     * @see Dwoo_Data::__set()
157
     *
158
     * @param string $name  the variable name
159
     * @param string $value the value to assign to it
160
     */
161
    public function __set($name, $value)
162
    {
163
        $this->getDataProvider()->__set($name, $value);
164
    }
165
166
    /**
167
     * Sraper for Dwoo_Data::__get() allows to read variables using the object
168
     * syntax.
169
     *
170
     * @see Dwoo_Data::__get()
171
     *
172
     * @param string $name the variable name
173
     *
174
     * @return mixed
175
     */
176
    public function __get($name)
177
    {
178
        return $this->getDataProvider()->__get($name);
179
    }
180
181
    /**
182
     * Wraper for Dwoo_Data::__isset()
183
     * supports calls to isset($dwooData->var).
184
     *
185
     * @see Dwoo_Data::__isset()
186
     *
187
     * @param string $name the variable name
188
     */
189
    public function __isset($name)
190
    {
191
        return $this->getDataProvider()->__isset($name);
192
    }
193
194
    /**
195
     * Wraper for Dwoo_Data::_unset()
196
     * supports unsetting variables using the object syntax.
197
     *
198
     * @see Dwoo_Data::__unset()
199
     *
200
     * @param string $name the variable name
201
     */
202
    public function __unset($name)
203
    {
204
        $this->getDataProvider()->__unset($name);
205
    }
206
207
    /**
208
     * Catches clone request and clones data provider.
209
     */
210
    public function __clone()
211
    {
212
        $this->setDataProvider(clone $this->getDataProvider());
213
    }
214
215
    /**
216
     * Returns plugin proxy interface.
217
     *
218
     * @return Dwoo_IPluginProxy
219
     */
220
    public function getPluginProxy()
221
    {
222
        if (!$this->_pluginProxy) {
223
            $this->_pluginProxy = new Dwoo_Adapters_ZendFramework_PluginProxy($this);
224
        }
225
226
        return $this->_pluginProxy;
227
    }
228
229
    /**
230
     * Sets plugin proxy.
231
     *
232
     * @param Dwoo_IPluginProxy
233
     *
234
     * @return Dwoo_Adapters_ZendFramework_View
235
     */
236
    public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy)
237
    {
238
        $this->_pluginProxy = $pluginProxy;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Sets template engine.
245
     *
246
     * @param string|Dwoo Object or name of the class
247
     */
248
    public function setEngine($engine)
249
    {
250
        // if param given as an object
251
        if ($engine instanceof Dwoo_Core) {
252
            $this->_engine = $engine;
253
        } elseif (is_subclass_of($engine, 'Dwoo') || 'Dwoo' === $engine) {
254
            $this->_engine = new $engine();
255
        } else {
256
            throw new Dwoo_Exception('Custom engine must be a subclass of Dwoo');
257
        }
258
    }
259
260
    /**
261
     * Return the Dwoo template engine object.
262
     *
263
     * @return Dwoo
264
     */
265
    public function getEngine()
266
    {
267
        if (null === $this->_engine) {
268
            $this->_engine = new Dwoo_Adapters_ZendFramework_Dwoo();
269
        }
270
271
        return $this->_engine;
272
    }
273
274
    /**
275
     * Sets Dwoo data object.
276
     *
277
     * @param string|Dwoo_Data Object or name of the class
278
     */
279
    public function setDataProvider($data)
280
    {
281
        if ($data instanceof Dwoo_IDataProvider) {
282
            $this->_dataProvider = $data;
283
        } elseif (is_subclass_of($data, 'Dwoo_Data') || 'Dwoo_Data' == $data) {
284
            $this->_dataProvider = new $data();
285
        } else {
286
            throw new Dwoo_Exception('Custom data provider must be a subclass of Dwoo_Data or instance of Dwoo_IDataProvider');
287
        }
288
    }
289
290
    /**
291
     * Return the Dwoo data object.
292
     *
293
     * @return Dwoo_Data
294
     */
295
    public function getDataProvider()
296
    {
297
        if (null === $this->_dataProvider) {
298
            $this->_dataProvider = new Dwoo_Data();
299
300
            // Satisfy Zend_View_Abstract wishes to access this unexisting property
301
            // by setting it to empty array (see Zend_View_Abstract::_filter)
302
            $this->_dataProvider->_filter = array();
303
        }
304
305
        return $this->_dataProvider;
306
    }
307
308
    /**
309
     * Sets Dwoo compiler.
310
     *
311
     * @param string|Dwoo_Compiler Object or name of the class
312
     */
313
    public function setCompiler($compiler)
314
    {
315
316
        // if param given as an object
317
        if ($compiler instanceof Dwoo_ICompiler) {
318
            $this->_compiler = $compiler;
319
        }
320
        // if param given as a string
321
        elseif (is_subclass_of($compiler, 'Dwoo_Compiler') || 'Dwoo_Compiler' == $compiler) {
322
            $this->_compiler = new $compiler();
323
        } else {
324
            throw new Dwoo_Exception('Custom compiler must be a subclass of Dwoo_Compiler or instance of Dwoo_ICompiler');
325
        }
326
    }
327
328
    /**
329
     * Return the Dwoo compiler object.
330
     *
331
     * @return Dwoo_Compiler
332
     */
333
    public function getCompiler()
334
    {
335
        if (null === $this->_compiler) {
336
            $this->_compiler = Dwoo_Compiler::compilerFactory();
337
        }
338
339
        return $this->_compiler;
340
    }
341
342
    /**
343
     * Initializes Dwoo_ITemplate type of class and sets properties from _templateFileSettings.
344
     *
345
     * @param string Dwoo_ITemplate $template
346
     *
347
     * @return Dwoo_ITemplate
348
     */
349
    public function getTemplateFile($template)
350
    {
351
        $templateFileClass = $this->_templateFileClass;
352
353
        $dwooTemplateFile = new $templateFileClass($template);
354
355
        if (!($dwooTemplateFile instanceof Dwoo_ITemplate)) {
356
            throw new Dwoo_Exception('Custom templateFile class must be a subclass of Dwoo_ITemplate');
357
        }
358
359 View Code Duplication
        foreach ($this->_templateFileSettings as $method => $value) {
360
            if (method_exists($dwooTemplateFile, 'set'.$method)) {
361
                call_user_func(array($dwooTemplateFile, 'set'.$method), $value);
362
            }
363
        }
364
365
        return $dwooTemplateFile;
366
    }
367
368
    /**
369
     * Dwoo_ITemplate type of class.
370
     *
371
     * @param string Name of the class
372
     */
373
    public function setTemplateFile($tempateFileClass)
374
    {
375
        $this->_templateFileClass = $tempateFileClass;
376
    }
377
378
    /**
379
     * Passes data to Dwoo_Data object.
380
     *
381
     * @see Dwoo_Data::assign()
382
     *
383
     * @param array|string $name
384
     * @param mixed        $val
385
     *
386
     * @return Dwoo_Adapters_ZendFramework_View
387
     */
388
    public function assign($name, $val = null)
389
    {
390
        $this->getDataProvider()->assign($name, $val);
391
392
        return $this;
393
    }
394
395
    /**
396
     * Return list of all assigned variables.
397
     *
398
     * @return array
399
     */
400
    public function getVars()
401
    {
402
        return $this->getDataProvider()->getData();
403
    }
404
405
    /**
406
     * Clear all assigned variables.
407
     *
408
     * Clears all variables assigned to Zend_View either via {@link assign()} or
409
     * property overloading ({@link __get()}/{@link __set()}).
410
     *
411
     * @return Dwoo_Adapters_ZendFramework_View
412
     */
413
    public function clearVars()
414
    {
415
        $this->getDataProvider()->clear();
416
417
        return $this;
418
    }
419
420
    /**
421
     * Wraper for parent's render method so preRender method
422
     * can be called (that will bind the plugin proxy to the
423
     * engine.
424
     *
425
     * @see Zend_View_Abstract::render
426
     *
427
     * @return string The script output
428
     */
429
    public function render($name)
430
    {
431
        $this->preRender();
432
433
        return parent::render($name);
434
    }
435
436
    /**
437
     * Processes a view script and outputs it. Output is then
438
     * passed through filters.
439
     *
440
     * @param string $name The script script name to process
441
     *
442
     * @return string The script output
443
     */
444
    public function _run()
445
    {
446
        echo $this->_engine->get(
447
            $this->getTemplateFile(func_get_arg(0)),
448
            $this->getDataProvider(),
449
            $this->getCompiler()
450
        );
451
    }
452
453
    /**
454
     * Add plugin path.
455
     *
456
     * @param string $dir Directory
457
     *
458
     * @return Dwoo_Adapters_ZendFramework_View
459
     */
460
    public function addPluginDir($dir)
461
    {
462
        $this->getEngine()->getLoader()->addDirectory($dir);
463
464
        return $this;
465
    }
466
467
    /**
468
     * Set compile path.
469
     *
470
     * @param string $dir Directory
471
     *
472
     * @return Dwoo_Adapters_ZendFramework_View
473
     */
474
    public function setCompileDir($dir)
475
    {
476
        $this->getEngine()->setCompileDir($dir);
477
478
        return $this;
479
    }
480
481
    /**
482
     * Set cache path.
483
     *
484
     * @param string $dir Directory
485
     *
486
     * @return Dwoo_Adapters_ZendFramework_View
487
     */
488
    public function setCacheDir($dir)
489
    {
490
        $this->getEngine()->setCacheDir($dir);
491
492
        return $this;
493
    }
494
495
    /**
496
     * Set cache lifetime.
497
     *
498
     * @param string $seconds Lifetime in seconds
499
     *
500
     * @return Dwoo_Adapters_ZendFramework_View
501
     */
502
    public function setCacheLifetime($seconds)
503
    {
504
        $this->getEngine()->setCacheTime($seconds);
505
506
        return $this;
507
    }
508
509
    /**
510
     * Set charset.
511
     *
512
     * @param string $charset
513
     *
514
     * @return Dwoo_Adapters_ZendFramework_View
515
     */
516
    public function setCharset($charset)
517
    {
518
        $this->_engine->setCharset($charset);
519
520
        return $this;
521
    }
522
}
523