1 <?php
2 /* --------------------------------------------------------------
3 HttpService.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('HttpServiceInterface');
13
14 /**
15 * Class HttpService
16 *
17 * @category System
18 * @package Http
19 * @implements HttpServiceInterface
20 */
21 class HttpService implements HttpServiceInterface
22 {
23 /**
24 * @var AbstractHttpContextFactory
25 */
26 protected $httpContextFactory;
27
28 /**
29 * @var HttpDispatcherInterface
30 */
31 protected $httpDispatcher;
32
33
34 /**
35 * Initializes the http service.
36 *
37 * @param AbstractHttpContextFactory $httpContextFactory Factory instance to create context objects.
38 * @param HttpDispatcherInterface $httpDispatcher Instance to dispatch the http response message.
39 */
40 public function __construct(AbstractHttpContextFactory $httpContextFactory, HttpDispatcherInterface $httpDispatcher)
41 {
42 $this->httpContextFactory = $httpContextFactory;
43 $this->httpDispatcher = $httpDispatcher;
44 }
45
46
47 /**
48 * Creates and returns a new instance of an http context object.
49 *
50 * @return HttpContextInterface
51 */
52 public function getHttpContext()
53 {
54 return $this->httpContextFactory->create();
55 }
56
57
58 /**
59 * Handles the current http request by the given context.
60 *
61 * @param HttpContextInterface $httpContext Context object which holds information about the current request.
62 */
63 public function handle(HttpContextInterface $httpContext)
64 {
65 try
66 {
67 $this->httpDispatcher->dispatch($httpContext);
68 }
69 catch(MissingControllerNameException $e)
70 {
71 // @todo Display error 404 "Page not found" to user.
72 // do nothing?
73 }
74 }
75 }