1 <?php
2 /* --------------------------------------------------------------
3 HttpDispatcher.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('HttpDispatcherInterface');
13
14 /**
15 * Class HttpDispatcher
16 *
17 * @category System
18 * @package Http
19 * @implements HttpDispatcherInterface
20 */
21 class HttpDispatcher implements HttpDispatcherInterface
22 {
23 /**
24 * @var HttpContextReaderInterface
25 */
26 protected $httpContextReader;
27
28 /**
29 * @var HttpViewControllerFactoryInterface
30 */
31 protected $httpViewControllerFactory;
32
33
34 /**
35 * Initializes the http dispatcher.
36 *
37 * @param HttpContextReaderInterface $httpContextReader
38 * @param HttpViewControllerFactoryInterface $httpViewControllerFactory
39 *
40 * @throws MissingControllerNameException If no controller is found.
41 */
42 public function __construct(HttpContextReaderInterface $httpContextReader,
43 HttpViewControllerFactoryInterface $httpViewControllerFactory)
44 {
45 $this->httpContextReader = $httpContextReader;
46 $this->httpViewControllerFactory = $httpViewControllerFactory;
47 }
48
49
50 /**
51 * Dispatches the current http request.
52 * If the http request is valid and can get handled by a controller class, the controllers ::proceed
53 * method is invoked by the dispatcher. Otherwise, the method will throw a missing controller name exception.
54 *
55 * @param HttpContextInterface $httpContext Object which holds information about the current http context.
56 *
57 * @throws MissingControllerNameException When the http request context is invalid.
58 */
59 public function dispatch(HttpContextInterface $httpContext)
60 {
61 $controllerName = $this->httpContextReader->getControllerName($httpContext);
62 if(empty($controllerName))
63 {
64 throw new MissingControllerNameException('No controller name found in given HttpContext');
65 }
66
67 $controller = $this->httpViewControllerFactory->createController($controllerName);
68 $controller->proceed($httpContext);
69 }
70 }
71