Completed
Push — master ( d34e65...8470a4 )
by Eric
8s
created

ContainerProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 7
c 3
b 0
f 2
lcom 0
cbo 5
dl 0
loc 86
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C hydrate() 0 80 7
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Jarvis\Skill\DependencyInjection;
6
7
use Jarvis\Jarvis;
8
use Jarvis\Skill\Core\CallbackResolver;
9
use Jarvis\Skill\EventBroadcaster\BroadcasterInterface;
10
use Jarvis\Skill\EventBroadcaster\ControllerEvent;
11
use Jarvis\Skill\EventBroadcaster\ExceptionEvent;
12
use Jarvis\Skill\Routing\Router;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\Session\Session;
16
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
17
18
/**
19
 * This is Jarvis internal container provider. It will inject every core
20
 * parameters and services into Jarvis.
21
 *
22
 * @author Eric Chau <[email protected]>
23
 */
24
final class ContainerProvider implements ContainerProviderInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function hydrate(Jarvis $app)
30
    {
31
        // settings process
32
        // ================
33
34
        $app['debug'] = $app['settings']['debug'] ?? Jarvis::DEFAULT_DEBUG;
35
36
        $extra = $app['settings']['extra'] ?? [];
37
        foreach ((array) $extra as $key => $data) {
38
            $app["{$key}.settings"] = $data;
39
        }
40
41
        unset($app['settings']);
42
43
        // services declarations
44
        // =====================
45
46
        $app['app'] = function () use ($app): Jarvis {
47
            return $app;
48
        };
49
50
        $app['request'] = function (Jarvis $app): Request {
51
            $request = Request::createFromGlobals();
52
53
            $session = $request->getSession();
54
            if (null === $session) {
55
                $settings = $app['session.settings'] ?? [];
56
                $storageClassname = $settings['session.storage.classname'] ?? NativeSessionStorage::class;
57
                unset($settings['session.storage.classname']);
58
                $session = new Session(new $storageClassname($settings));
59
            }
60
61
            $request->setSession($session);
62
63
            return $request;
64
        };
65
66
        $app['session'] = function (Jarvis $app): Session {
67
            return $app['request']->getSession();
68
        };
69
70
        $app['router'] = function (): Router {
71
            return new Router();
72
        };
73
74
        $app['callbackResolver'] = function (Jarvis $app): CallbackResolver {
75
            return new CallbackResolver($app);
76
        };
77
78
        $app->lock(['debug', 'request', 'session', 'router', 'callbackResolver']);
79
80
        // events receivers
81
        // ================
82
83
        $app->on(BroadcasterInterface::EXCEPTION_EVENT, function (ExceptionEvent $event): void {
84
            $response = new Response($event->exception()->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
85
            $event->setResponse($response);
86
        }, BroadcasterInterface::RECEIVER_LOW_PRIORITY);
87
88
        $app->on(BroadcasterInterface::CONTROLLER_EVENT, function (ControllerEvent $event) use ($app): void {
89
            $finalArgs = [];
90
            $rawArgs = $event->arguments();
91
            $refMethod = new \ReflectionMethod($event->callback(), '__invoke');
92
            foreach ($refMethod->getParameters() as $refParam) {
93
                if (null !== $refClass = $refParam->getClass()) {
94
                    if (isset($app[$refClass->getName()])) {
95
                        $finalArgs[$refParam->getPosition()] = $app[$refClass->getName()];
96
97
                        continue;
98
                    }
99
                }
100
101
                if (in_array($refParam->name, array_keys($rawArgs))) {
102
                    $finalArgs[$refParam->getPosition()] = $rawArgs[$refParam->name];
103
                }
104
            }
105
106
            $event->setArguments($finalArgs);
107
        }, BroadcasterInterface::RECEIVER_LOW_PRIORITY);
108
    }
109
}
110