1 <?php
2 /* --------------------------------------------------------------
3 HttpViewControllerFactory.inc.php 2015-03-12 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('HttpViewControllerFactoryInterface');
13
14 /**
15 * Class HttpViewControllerFactory
16 *
17 * @category System
18 * @package Http
19 * @subpackage Factories
20 * @implements HttpViewControllerFactoryInterface
21 */
22 class HttpViewControllerFactory implements HttpViewControllerFactoryInterface
23 {
24 /**
25 * @var HttpViewControllerRegistryInterface
26 */
27 protected $httpViewControllerRegistry;
28
29 /**
30 * @var HttpContextReaderInterface
31 */
32 protected $httpContextReader;
33
34 /**
35 * @var HttpResponseProcessorInterface
36 */
37 protected $httpResponseProcessor;
38
39
40 /**
41 * Initialize the http view controller factory.
42 *
43 * @param HttpViewControllerRegistryInterface $httpViewControllerRegistry Object which holds the registered
44 * controller class names.
45 * @param HttpContextReaderInterface $httpContextReader Object to read the http context.
46 * @param HttpResponseProcessorInterface $httpResponseProcessor Object to process the http response.
47 */
48 public function __construct(HttpViewControllerRegistryInterface $httpViewControllerRegistry,
49 HttpContextReaderInterface $httpContextReader,
50 HttpResponseProcessorInterface $httpResponseProcessor)
51 {
52 $this->httpViewControllerRegistry = $httpViewControllerRegistry;
53 $this->httpContextReader = $httpContextReader;
54 $this->httpResponseProcessor = $httpResponseProcessor;
55 }
56
57
58 /**
59 * Creates a new instance of a http view controller by the given controller name.
60 *
61 * @param string $controllerName Expected name of controller (without 'Controller'-Suffix)
62 *
63 * @throws LogicException If the controller is not registered in the http view controller registry or the
64 * controller does not implement the http view controller interface.
65 *
66 * @return HttpViewControllerInterface Created controller instance.
67 */
68 public function createController($controllerName)
69 {
70 $className = $this->_getControllerClassName($controllerName);
71 $contentView = $this->_createControllerContentView($controllerName);
72
73 $controller = MainFactory::create($className,
74 $this->httpContextReader,
75 $this->httpResponseProcessor,
76 $contentView);
77
78 return $controller;
79 }
80
81
82 /**
83 * Returns the class name of a http controller by the given controller name.
84 *
85 * @param string $controllerName Name of the http controller class.
86 *
87 * @throws LogicException When the found controller class does not implement the http view controller interface.
88 *
89 * @return string Class name of the http controller.
90 */
91 protected function _getControllerClassName($controllerName)
92 {
93 $className = $this->httpViewControllerRegistry->get($controllerName);
94
95 if(empty($className))
96 {
97 throw new LogicException('No controller class found for [' . htmlentities($controllerName) . ']');
98 }
99
100 if(in_array('HttpViewControllerInterface', class_implements($className)) == false)
101 {
102 throw new LogicException('HttpViewControllerInterface not implemented in called controller class ['
103 . htmlentities($controllerName)
104 . ']');
105 }
106
107 return $className;
108 }
109
110
111 /**
112 * Creates and returns the content view for a http view controller by the controller name.
113 *
114 * @param string $controllerName Name of the http controller class.
115 *
116 * @throws LogicException When the content view does not implement the content view interface.
117 *
118 * @return ContentViewInterface Content view instance for the http view controller.
119 */
120 protected function _createControllerContentView($controllerName)
121 {
122 $contentViewClassName = $controllerName . 'ContentView';
123
124 if(class_exists($contentViewClassName) == false)
125 {
126 $contentView = MainFactory::create('ContentView');
127 $contentView->set_flat_assigns(true);
128
129 return $contentView;
130 }
131
132 if(in_array('ContentViewInterface', class_implements($contentViewClassName)) == false)
133 {
134 throw new LogicException('ContentViewInterface not implemented in found ContentView class for called controller ['
135 . htmlentities($controllerName)
136 . ']');
137 }
138
139 return MainFactory::create($contentViewClassName);
140 }
141 }