Completed
Push — master ( fbfc99...da50d4 )
by Mr
02:29
created

App::containers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace DrMVC\Framework;
4
5
//use Psr\Container\ContainerInterface;
6
//use Psr\Container\NotFoundExceptionInterface;
7
8
use DrMVC\Router\RouterInterface;
9
use Zend\Diactoros\ServerRequestFactory;
10
use Zend\Diactoros\ServerRequest;
11
use Zend\Diactoros\Response;
12
13
use DrMVC\Config\ConfigInterface;
14
use DrMVC\Router;
15
16
/**
17
 * Class App
18
 * @package DrMVC\Framework
19
 * @method App options(string $pattern, callable $callable): App
20
 * @method App get(string $pattern, callable $callable): App
21
 * @method App head(string $pattern, callable $callable): App
22
 * @method App post(string $pattern, callable $callable): App
23
 * @method App put(string $pattern, callable $callable): App
24
 * @method App delete(string $pattern, callable $callable): App
25
 * @method App trace(string $pattern, callable $callable): App
26
 * @method App connect(string $pattern, callable $callable): App
27
 * @since 3.0
28
 */
29
class App implements AppInterface
30
{
31
    /**
32
     * @var ContainersInterface
33
     */
34
    private $_containers;
35
36
    /**
37
     * App constructor.
38
     * @param ConfigInterface $config
39
     */
40
    public function __construct(ConfigInterface $config)
41
    {
42
        $this
43
            ->initContainers()
44
            ->initConfig($config)
45
            ->initRouter();
46
    }
47
48
    /**
49
     * Initialize containers object
50
     *
51
     * @return  App
52
     */
53
    private function initContainers(): App
54
    {
55
        if (null === $this->_containers) {
56
            $this->_containers = new Containers();
57
        }
58
        return $this;
59
    }
60
61
    /**
62
     * Put config object into the containers class
63
     *
64
     * @param   ConfigInterface $config
65
     * @return  App
66
     */
67
    private function initConfig(ConfigInterface $config): App
68
    {
69
        $this->containers()->set('config', $config);
0 ignored issues
show
Bug introduced by
$config of type DrMVC\Config\ConfigInterface is incompatible with the type string expected by parameter $object of DrMVC\Framework\ContainersInterface::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $this->containers()->set('config', /** @scrutinizer ignore-type */ $config);
Loading history...
70
        return $this;
71
    }
72
73
    /**
74
     * Put route into the container of classes
75
     *
76
     * @return  App
77
     */
78
    private function initRouter(): App
79
    {
80
        $req = ServerRequestFactory::fromGlobals();
81
        $res = new Response();
82
        $router = new Router($req, $res);
83
84
        $this->containers()->set('router', $router);
0 ignored issues
show
Bug introduced by
$router of type DrMVC\Router is incompatible with the type string expected by parameter $object of DrMVC\Framework\ContainersInterface::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $this->containers()->set('router', /** @scrutinizer ignore-type */ $router);
Loading history...
85
        return $this;
86
    }
87
88
    /**
89
     * Get containers object
90
     *
91
     * @param   string|null $name
92
     * @return  mixed
93
     */
94
    public function containers(string $name = null)
95
    {
96
        return (null !== $name)
97
            ? $this->_containers->get($name)
98
            : $this->_containers;
99
    }
100
101
    public function __call(string $method, array $args): RouterInterface
102
    {
103
        $router = $this->containers('router');
104
        if (\in_array($method, Router::METHODS, false)) {
105
            $router->set([$method], $args);
106
        }
107
        return $router;
108
    }
109
110
    public function any(string $pattern, $callable): RouterInterface
111
    {
112
        $router = $this->containers('router');
113
        $router->any($pattern, $callable);
114
        return $router;
115
    }
116
117
    public function error($callable): RouterInterface
118
    {
119
        $router = $this->containers('router');
120
        $router->error($callable);
121
        return $router;
122
    }
123
124
    public function map(array $methods, string $pattern, $callable): RouterInterface
125
    {
126
        $router = $this->containers('router');
127
        $router->map($methods, $pattern, $callable);
128
        return $router;
129
    }
130
131
}
132