use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Controller / Plugin / Layout.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Mvc\Controller\Plugin;
11
12 use Zend\Mvc\Exception;
13 use Zend\Mvc\InjectApplicationEventInterface;
14 use Zend\Mvc\MvcEvent;
15 use Zend\View\Model\ModelInterface as Model;
16
17 class Layout extends AbstractPlugin
18 {
19 /**
20 * @var MvcEvent
21 */
22 protected $event;
23
24 /**
25 * Set the layout template
26 *
27 * @param string $template
28 * @return Layout
29 */
30 public function setTemplate($template)
31 {
32 $viewModel = $this->getViewModel();
33 $viewModel->setTemplate((string) $template);
34 return $this;
35 }
36
37 /**
38 * Invoke as a functor
39 *
40 * If no arguments are given, grabs the "root" or "layout" view model.
41 * Otherwise, attempts to set the template for that view model.
42 *
43 * @param null|string $template
44 * @return Model|Layout
45 */
46 public function __invoke($template = null)
47 {
48 if (null === $template) {
49 return $this->getViewModel();
50 }
51 return $this->setTemplate($template);
52 }
53
54 /**
55 * Get the event
56 *
57 * @return MvcEvent
58 * @throws Exception\DomainException if unable to find event
59 */
60 protected function getEvent()
61 {
62 if ($this->event) {
63 return $this->event;
64 }
65
66 $controller = $this->getController();
67 if (!$controller instanceof InjectApplicationEventInterface) {
68 throw new Exception\DomainException('Layout plugin requires a controller that implements InjectApplicationEventInterface');
69 }
70
71 $event = $controller->getEvent();
72 if (!$event instanceof MvcEvent) {
73 $params = $event->getParams();
74 $event = new MvcEvent();
75 $event->setParams($params);
76 }
77 $this->event = $event;
78
79 return $this->event;
80 }
81
82 /**
83 * Retrieve the root view model from the event
84 *
85 * @return Model
86 * @throws Exception\DomainException
87 */
88 protected function getViewModel()
89 {
90 $event = $this->getEvent();
91 $viewModel = $event->getViewModel();
92 if (!$viewModel instanceof Model) {
93 throw new Exception\DomainException('Layout plugin requires that event view model is populated');
94 }
95 return $viewModel;
96 }
97 }