1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc; |
13
|
|
|
|
14
|
|
|
use Phalcon\Di\DiInterface; |
15
|
|
|
use Phalcon\Http\ResponseInterface; |
16
|
|
|
use Zemit\Dispatcher\AbstractDispatcher; |
17
|
|
|
use Zemit\Mvc\Dispatcher as MvcDispatcher; |
18
|
|
|
use Zemit\Cli\Dispatcher as CliDispatcher; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Simple HMVC - allow requests with namespaces and modules |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
class Application extends \Phalcon\Mvc\Application |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* HMVC Application |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
15 |
|
public function __construct(DiInterface $di) |
31
|
|
|
{ |
32
|
|
|
// Registering app itself as a service |
33
|
15 |
|
$di->setShared('application', $this); |
34
|
15 |
|
parent::__construct($di); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* HMVC request |
39
|
|
|
* You can request call any module/namespace |
40
|
|
|
*/ |
41
|
|
|
public function request(array $location = []): string |
42
|
|
|
{ |
43
|
|
|
// Get a unique dispatcher |
44
|
|
|
$dispatcher = clone $this->getDI()->get('dispatcher'); |
45
|
|
|
assert($dispatcher instanceof AbstractDispatcher); |
46
|
|
|
|
47
|
|
|
// Route dispatcher |
48
|
|
|
$dispatcher->setDefaultNamespace($location['namespace'] ?? $dispatcher->getNamespaceName()); |
49
|
|
|
$dispatcher->setNamespaceName($location['namespace'] ?? $dispatcher->getNamespaceName()); |
50
|
|
|
$dispatcher->setModuleName($location['module'] ?? $dispatcher->getModuleName()); |
51
|
|
|
if ($dispatcher instanceof MvcDispatcher) { |
|
|
|
|
52
|
|
|
$dispatcher->setControllerName($location['controller'] ?? 'index'); |
53
|
|
|
} |
54
|
|
|
elseif ($dispatcher instanceof CliDispatcher) { |
|
|
|
|
55
|
|
|
$dispatcher->setTaskName($location['task'] ?? 'index'); |
56
|
|
|
} |
57
|
|
|
$dispatcher->setActionName($location['action'] ?? 'index'); |
58
|
|
|
$dispatcher->setParams($location['params'] ?? []); |
59
|
|
|
$dispatcher->dispatch(); |
60
|
|
|
|
61
|
|
|
// Get and return value |
62
|
|
|
$response = $dispatcher->getReturnedValue(); |
63
|
|
|
if ($response instanceof ResponseInterface) { |
64
|
|
|
return $response->getContent(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $response; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|