-
Notifications
You must be signed in to change notification settings - Fork 1
/
Layout.php
798 lines (718 loc) · 18.5 KB
/
Layout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Layout
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Layout.php 25263 2013-02-18 11:48:02Z frosch $
*/
/**
* Provide Layout support for MVC applications
*
* @category Zend
* @package Zend_Layout
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Layout
{
/**
* Placeholder container for layout variables
* @var Zend_View_Helper_Placeholder_Container
*/
protected $_container;
/**
* Key used to store content from 'default' named response segment
* @var string
*/
protected $_contentKey = 'content';
/**
* Are layouts enabled?
* @var bool
*/
protected $_enabled = true;
/**
* Helper class
* @var string
*/
protected $_helperClass = 'Zend_Layout_Controller_Action_Helper_Layout';
/**
* Inflector used to resolve layout script
* @var Zend_Filter_Inflector
*/
protected $_inflector;
/**
* Flag: is inflector enabled?
* @var bool
*/
protected $_inflectorEnabled = true;
/**
* Inflector target
* @var string
*/
protected $_inflectorTarget = ':script.:suffix';
/**
* Layout view
* @var string
*/
protected $_layout = 'layout';
/**
* Layout view script path
* @var string
*/
protected $_viewScriptPath = null;
protected $_viewBasePath = null;
protected $_viewBasePrefix = 'Layout_View';
/**
* Flag: is MVC integration enabled?
* @var bool
*/
protected $_mvcEnabled = true;
/**
* Instance registered with MVC, if any
* @var Zend_Layout
*/
protected static $_mvcInstance;
/**
* Flag: is MVC successful action only flag set?
* @var bool
*/
protected $_mvcSuccessfulActionOnly = true;
/**
* Plugin class
* @var string
*/
protected $_pluginClass = 'Zend_Layout_Controller_Plugin_Layout';
/**
* @var Zend_View_Interface
*/
protected $_view;
/**
* View script suffix for layout script
* @var string
*/
protected $_viewSuffix = 'phtml';
/**
* Constructor
*
* Accepts either:
* - A string path to layouts
* - An array of options
* - A Zend_Config object with options
*
* Layout script path, either as argument or as key in options, is
* required.
*
* If mvcEnabled flag is false from options, simply sets layout script path.
* Otherwise, also instantiates and registers action helper and controller
* plugin.
*
* @param string|array|Zend_Config $options
* @return void
*/
public function __construct($options = null, $initMvc = false)
{
if (null !== $options) {
if (is_string($options)) {
$this->setLayoutPath($options);
} elseif (is_array($options)) {
$this->setOptions($options);
} elseif ($options instanceof Zend_Config) {
$this->setConfig($options);
} else {
require_once 'Zend/Layout/Exception.php';
throw new Zend_Layout_Exception('Invalid option provided to constructor');
}
}
$this->_initVarContainer();
if ($initMvc) {
$this->_setMvcEnabled(true);
$this->_initMvc();
} else {
$this->_setMvcEnabled(false);
}
}
/**
* Static method for initialization with MVC support
*
* @param string|array|Zend_Config $options
* @return Zend_Layout
*/
public static function startMvc($options = null)
{
if (null === self::$_mvcInstance) {
self::$_mvcInstance = new self($options, true);
} else {
if (is_string($options)) {
self::$_mvcInstance->setLayoutPath($options);
} elseif (is_array($options) || $options instanceof Zend_Config) {
self::$_mvcInstance->setOptions($options);
}
}
return self::$_mvcInstance;
}
/**
* Retrieve MVC instance of Zend_Layout object
*
* @return Zend_Layout|null
*/
public static function getMvcInstance()
{
return self::$_mvcInstance;
}
/**
* Reset MVC instance
*
* Unregisters plugins and helpers, and destroys MVC layout instance.
*
* @return void
*/
public static function resetMvcInstance()
{
if (null !== self::$_mvcInstance) {
$layout = self::$_mvcInstance;
$pluginClass = $layout->getPluginClass();
$front = Zend_Controller_Front::getInstance();
if ($front->hasPlugin($pluginClass)) {
$front->unregisterPlugin($pluginClass);
}
if (Zend_Controller_Action_HelperBroker::hasHelper('layout')) {
Zend_Controller_Action_HelperBroker::removeHelper('layout');
}
unset($layout);
self::$_mvcInstance = null;
}
}
/**
* Set options en masse
*
* @param array|Zend_Config $options
* @return void
*/
public function setOptions($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (!is_array($options)) {
require_once 'Zend/Layout/Exception.php';
throw new Zend_Layout_Exception('setOptions() expects either an array or a Zend_Config object');
}
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (method_exists($this, $method)) {
$this->$method($value);
}
}
}
/**
* Initialize MVC integration
*
* @return void
*/
protected function _initMvc()
{
$this->_initPlugin();
$this->_initHelper();
}
/**
* Initialize front controller plugin
*
* @return void
*/
protected function _initPlugin()
{
$pluginClass = $this->getPluginClass();
require_once 'Zend/Controller/Front.php';
$front = Zend_Controller_Front::getInstance();
if (!$front->hasPlugin($pluginClass)) {
if (!class_exists($pluginClass)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($pluginClass);
}
$front->registerPlugin(
// register to run last | BUT before the ErrorHandler (if its available)
new $pluginClass($this),
99
);
}
}
/**
* Initialize action helper
*
* @return void
*/
protected function _initHelper()
{
$helperClass = $this->getHelperClass();
require_once 'Zend/Controller/Action/HelperBroker.php';
if (!Zend_Controller_Action_HelperBroker::hasHelper('layout')) {
if (!class_exists($helperClass)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($helperClass);
}
Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-90, new $helperClass($this));
}
}
/**
* Set options from a config object
*
* @param Zend_Config $config
* @return Zend_Layout
*/
public function setConfig(Zend_Config $config)
{
$this->setOptions($config->toArray());
return $this;
}
/**
* Initialize placeholder container for layout vars
*
* @return Zend_View_Helper_Placeholder_Container
*/
protected function _initVarContainer()
{
if (null === $this->_container) {
require_once 'Zend/View/Helper/Placeholder/Registry.php';
$this->_container = Zend_View_Helper_Placeholder_Registry::getRegistry()->getContainer(__CLASS__);
}
return $this->_container;
}
/**
* Set layout script to use
*
* Note: enables layout by default, can be disabled
*
* @param string $name
* @param boolean $enabled
* @return Zend_Layout
*/
public function setLayout($name, $enabled = true)
{
$this->_layout = (string) $name;
if ($enabled) {
$this->enableLayout();
}
return $this;
}
/**
* Get current layout script
*
* @return string
*/
public function getLayout()
{
return $this->_layout;
}
/**
* Disable layout
*
* @return Zend_Layout
*/
public function disableLayout()
{
$this->_enabled = false;
return $this;
}
/**
* Enable layout
*
* @return Zend_Layout
*/
public function enableLayout()
{
$this->_enabled = true;
return $this;
}
/**
* Is layout enabled?
*
* @return bool
*/
public function isEnabled()
{
return $this->_enabled;
}
public function setViewBasePath($path, $prefix = 'Layout_View')
{
$this->_viewBasePath = $path;
$this->_viewBasePrefix = $prefix;
return $this;
}
public function getViewBasePath()
{
return $this->_viewBasePath;
}
public function setViewScriptPath($path)
{
$this->_viewScriptPath = $path;
return $this;
}
public function getViewScriptPath()
{
return $this->_viewScriptPath;
}
/**
* Set layout script path
*
* @param string $path
* @return Zend_Layout
*/
public function setLayoutPath($path)
{
return $this->setViewScriptPath($path);
}
/**
* Get current layout script path
*
* @return string
*/
public function getLayoutPath()
{
return $this->getViewScriptPath();
}
/**
* Set content key
*
* Key in namespace container denoting default content
*
* @param string $contentKey
* @return Zend_Layout
*/
public function setContentKey($contentKey)
{
$this->_contentKey = (string) $contentKey;
return $this;
}
/**
* Retrieve content key
*
* @return string
*/
public function getContentKey()
{
return $this->_contentKey;
}
/**
* Set MVC enabled flag
*
* @param bool $mvcEnabled
* @return Zend_Layout
*/
protected function _setMvcEnabled($mvcEnabled)
{
$this->_mvcEnabled = ($mvcEnabled) ? true : false;
return $this;
}
/**
* Retrieve MVC enabled flag
*
* @return bool
*/
public function getMvcEnabled()
{
return $this->_mvcEnabled;
}
/**
* Set MVC Successful Action Only flag
*
* @param bool $successfulActionOnly
* @return Zend_Layout
*/
public function setMvcSuccessfulActionOnly($successfulActionOnly)
{
$this->_mvcSuccessfulActionOnly = ($successfulActionOnly) ? true : false;
return $this;
}
/**
* Get MVC Successful Action Only Flag
*
* @return bool
*/
public function getMvcSuccessfulActionOnly()
{
return $this->_mvcSuccessfulActionOnly;
}
/**
* Set view object
*
* @param Zend_View_Interface $view
* @return Zend_Layout
*/
public function setView(Zend_View_Interface $view)
{
$this->_view = $view;
return $this;
}
/**
* Retrieve helper class
*
* @return string
*/
public function getHelperClass()
{
return $this->_helperClass;
}
/**
* Set helper class
*
* @param string $helperClass
* @return Zend_Layout
*/
public function setHelperClass($helperClass)
{
$this->_helperClass = (string) $helperClass;
return $this;
}
/**
* Retrieve plugin class
*
* @return string
*/
public function getPluginClass()
{
return $this->_pluginClass;
}
/**
* Set plugin class
*
* @param string $pluginClass
* @return Zend_Layout
*/
public function setPluginClass($pluginClass)
{
$this->_pluginClass = (string) $pluginClass;
return $this;
}
/**
* Get current view object
*
* If no view object currently set, retrieves it from the ViewRenderer.
*
* @todo Set inflector from view renderer at same time
* @return Zend_View_Interface
*/
public function getView()
{
if (null === $this->_view) {
require_once 'Zend/Controller/Action/HelperBroker.php';
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
$viewRenderer->initView();
}
$this->setView($viewRenderer->view);
}
return $this->_view;
}
/**
* Set layout view script suffix
*
* @param string $viewSuffix
* @return Zend_Layout
*/
public function setViewSuffix($viewSuffix)
{
$this->_viewSuffix = (string) $viewSuffix;
return $this;
}
/**
* Retrieve layout view script suffix
*
* @return string
*/
public function getViewSuffix()
{
return $this->_viewSuffix;
}
/**
* Retrieve inflector target
*
* @return string
*/
public function getInflectorTarget()
{
return $this->_inflectorTarget;
}
/**
* Set inflector target
*
* @param string $inflectorTarget
* @return Zend_Layout
*/
public function setInflectorTarget($inflectorTarget)
{
$this->_inflectorTarget = (string) $inflectorTarget;
return $this;
}
/**
* Set inflector to use when resolving layout names
*
* @param Zend_Filter_Inflector $inflector
* @return Zend_Layout
*/
public function setInflector(Zend_Filter_Inflector $inflector)
{
$this->_inflector = $inflector;
return $this;
}
/**
* Retrieve inflector
*
* @return Zend_Filter_Inflector
*/
public function getInflector()
{
if (null === $this->_inflector) {
require_once 'Zend/Filter/Inflector.php';
$inflector = new Zend_Filter_Inflector();
$inflector->setTargetReference($this->_inflectorTarget)
->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower')))
->setStaticRuleReference('suffix', $this->_viewSuffix);
$this->setInflector($inflector);
}
return $this->_inflector;
}
/**
* Enable inflector
*
* @return Zend_Layout
*/
public function enableInflector()
{
$this->_inflectorEnabled = true;
return $this;
}
/**
* Disable inflector
*
* @return Zend_Layout
*/
public function disableInflector()
{
$this->_inflectorEnabled = false;
return $this;
}
/**
* Return status of inflector enabled flag
*
* @return bool
*/
public function inflectorEnabled()
{
return $this->_inflectorEnabled;
}
/**
* Set layout variable
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
$this->_container[$key] = $value;
}
/**
* Get layout variable
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if (isset($this->_container[$key])) {
return $this->_container[$key];
}
return null;
}
/**
* Is a layout variable set?
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
return (isset($this->_container[$key]));
}
/**
* Unset a layout variable?
*
* @param string $key
* @return void
*/
public function __unset($key)
{
if (isset($this->_container[$key])) {
unset($this->_container[$key]);
}
}
/**
* Assign one or more layout variables
*
* @param mixed $spec Assoc array or string key; if assoc array, sets each
* key as a layout variable
* @param mixed $value Value if $spec is a key
* @return Zend_Layout
* @throws Zend_Layout_Exception if non-array/string value passed to $spec
*/
public function assign($spec, $value = null)
{
if (is_array($spec)) {
$orig = $this->_container->getArrayCopy();
$merged = array_merge($orig, $spec);
$this->_container->exchangeArray($merged);
return $this;
}
if (is_string($spec)) {
$this->_container[$spec] = $value;
return $this;
}
require_once 'Zend/Layout/Exception.php';
throw new Zend_Layout_Exception('Invalid values passed to assign()');
}
/**
* Render layout
*
* Sets internal script path as last path on script path stack, assigns
* layout variables to view, determines layout name using inflector, and
* renders layout view script.
*
* $name will be passed to the inflector as the key 'script'.
*
* @param mixed $name
* @return mixed
*/
public function render($name = null)
{
if (null === $name) {
$name = $this->getLayout();
}
if ($this->inflectorEnabled() && (null !== ($inflector = $this->getInflector())))
{
$name = $this->_inflector->filter(array('script' => $name));
}
$view = $this->getView();
if (null !== ($path = $this->getViewScriptPath())) {
if (method_exists($view, 'addScriptPath')) {
$view->addScriptPath($path);
} else {
$view->setScriptPath($path);
}
} elseif (null !== ($path = $this->getViewBasePath())) {
$view->addBasePath($path, $this->_viewBasePrefix);
}
return $view->render($name);
}
}