1 | <?php |
||
25 | class Main extends Listener |
||
26 | { |
||
27 | const VERSION = '@package_version@'; |
||
28 | |||
29 | /** |
||
30 | * @todo review this. |
||
31 | * @var array |
||
32 | */ |
||
33 | public $config = array(); |
||
34 | |||
35 | /** |
||
36 | * @var Request |
||
37 | */ |
||
38 | public $request = null; |
||
39 | |||
40 | /** |
||
41 | * @var Route |
||
42 | */ |
||
43 | public $route = null; |
||
44 | |||
45 | /** |
||
46 | * @var Resources |
||
47 | */ |
||
48 | public $resources = null; |
||
49 | |||
50 | /** |
||
51 | * @var Entity |
||
52 | */ |
||
53 | public $entity = null; |
||
54 | |||
55 | /** |
||
56 | * @var Response |
||
57 | */ |
||
58 | public $response = null; |
||
59 | |||
60 | /** |
||
61 | * Constructor. |
||
62 | * |
||
63 | * @return void |
||
|
|||
64 | */ |
||
65 | public function __construct( |
||
66 | $config=null, Request $request=null, Response $response=null |
||
67 | ) { |
||
68 | |||
69 | // Set and intialise the config |
||
70 | $c = $config instanceof Config ? $config : Config::getInstance($config); |
||
71 | $this->config = $c->get(); |
||
72 | |||
73 | $this->initSet($this->config); |
||
74 | |||
75 | // Load all the plugins |
||
76 | // $this->loadPlugins($c->get('plugins')); |
||
77 | $this->loadPlugins($this->config['plugins']); |
||
78 | |||
79 | // Set the current request |
||
80 | $this->request = null === $request |
||
81 | ? new HttpRequest() |
||
82 | : $request; |
||
83 | |||
84 | if ($this->request instanceof HttpRequest) { |
||
85 | $this->request->setFormats($this->config['input_formats']); |
||
86 | } |
||
87 | |||
88 | // Initialise the response |
||
89 | $this->response = null === $response |
||
90 | ? new Response($this->request) |
||
91 | : $response; |
||
92 | |||
93 | $this->response->setFormats($this->config['routing']['formats']); |
||
94 | |||
95 | // Add all the resources from config. |
||
96 | $this->resources = new Resources(); |
||
97 | foreach ($c->getResources() as $key => $values) { |
||
98 | $this->resources->add( |
||
99 | $key, $values |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | // Set some generic services |
||
104 | Service::set('config', $this->config); |
||
105 | Service::set('response', $this->response); |
||
106 | Service::set('request', $this->request); |
||
107 | Service::set('server', $this); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Deals with PHP inits and error handlers. |
||
112 | * |
||
113 | * @param array $configs The config entries to initialise. |
||
114 | * @return void |
||
115 | * @codeCoverageIgnore |
||
116 | */ |
||
117 | private function initSet(array $configs) |
||
130 | |||
131 | /** |
||
132 | * Run the show... |
||
133 | * |
||
134 | * @throws \InvalidArgumentException 404 |
||
135 | * @codeCoverageIgnore |
||
136 | */ |
||
137 | public function run() |
||
218 | |||
219 | /** |
||
220 | * Gets the server version string. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getServerVersion(array $config) |
||
232 | |||
233 | /** |
||
234 | * Sets and initialise the routing processes. |
||
235 | * |
||
236 | * @param Request $request |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function setRouting( |
||
289 | |||
290 | /** |
||
291 | * Returns the route object. |
||
292 | * |
||
293 | * @return Router |
||
294 | */ |
||
295 | public function getRoute() |
||
299 | |||
300 | /** |
||
301 | * Returns the response object. |
||
302 | * |
||
303 | * @return Response |
||
304 | */ |
||
305 | public function getResponse() |
||
309 | |||
310 | /** |
||
311 | * Returns the output format from the request chain. |
||
312 | * |
||
313 | * @param array $opts Options are: |
||
314 | * - [default] => string e.g. 'json', |
||
315 | * - [allow_extension] => boolean, |
||
316 | * - [override] => false or $_REQUEST['format'], |
||
317 | * - [http_accept] => boolean. |
||
318 | * @param string|false $ext The contoller defined extension. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function negotiateFormat(array $opts, $ext=false) |
||
347 | |||
348 | } |
||
349 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.