RouterResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @file
4
 * Contains \NotAFramework\App\Http\RouterResolver.
5
 */
6
7
namespace NotAFramework\App\Http;
8
9
use League\Container\Container;
10
use Phroute\Phroute\HandlerResolverInterface;
11
12
class RouterResolver implements HandlerResolverInterface
13
{
14
    /**
15
     * The dependency injection container.
16
     *
17
     * @var Container
18
     */
19
    private $container;
20
21
    /**
22
     * Create a new RouterResolver instance.
23
     *
24
     * @param Container $container
25
     *   Dependency injection container.
26
     */
27
    public function __construct(Container $container)
28
    {
29
        $this->container = $container;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function resolve($handler)
36
    {
37
        // Only attempt to resolve uninstantiated objects.
38
        if (is_array($handler) && is_string($handler[0])) {
39
            // Retrieve the route handler class from the dependency injection
40
            // container. This ensures that the instantiated handler class
41
            // receives all of the correct dependencies.
42
            $handler_object = $this->container->get($handler[0]);
43
44
            // Manually set the response and request dependencies.
45
            $handler_object->setRequest($this->container->get('Symfony\Component\HttpFoundation\Request'));
46
            $handler_object->setResponse($this->container->get('Symfony\Component\HttpFoundation\Response'));
47
48
            // Pass the object back.
49
            $handler[0] = $handler_object;
50
        }
51
52
        return $handler;
53
    }
54
}
55