1 | <?php |
||
2 | /** |
||
3 | * Configuration file to create router as $di service. |
||
4 | */ |
||
5 | return [ |
||
6 | "services" => [ |
||
7 | "router" => [ |
||
8 | "shared" => true, |
||
9 | "callback" => function () { |
||
10 | $router = new \Anax\Route\Router(); |
||
11 | $router->setDI($this); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
12 | |||
13 | // Load the configuration files |
||
14 | $cfg = $this->get("configuration"); |
||
15 | $config = $cfg->load("router"); |
||
16 | |||
17 | // Set DEVELOPMENT/PRODUCTION mode, if defined |
||
18 | $mode = $config["config"]["mode"] ?? null; |
||
19 | if (isset($mode)) { |
||
20 | $router->setMode($mode); |
||
21 | } else if (defined("ANAX_PRODUCTION")) { |
||
22 | $router->setMode(\Anax\Route\Router::PRODUCTION); |
||
23 | } |
||
24 | |||
25 | // Add routes from configuration file |
||
26 | $file = null; |
||
0 ignored issues
–
show
|
|||
27 | try { |
||
28 | $file = $config["file"] ?? null; |
||
29 | $router->addRoutes($config["config"] ?? []); |
||
30 | foreach ($config["items"] ?? [] as $routes) { |
||
31 | $file = $routes["file"]; |
||
32 | $router->addRoutes($routes["config"]); |
||
33 | } |
||
34 | } catch (Exception $e) { |
||
35 | throw new Exception( |
||
36 | "Configuration file: '$file'. " |
||
37 | . $e->getMessage() |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | return $router; |
||
42 | } |
||
43 | ], |
||
44 | ], |
||
45 | ]; |
||
46 |