Completed
Push — master ( da50d4...b36efb )
by Mr
02:12
created

App::container()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

70
        $this->containers()->set('config', /** @scrutinizer ignore-type */ $config);
Loading history...
71
        return $this;
72
    }
73
74
    /**
75
     * Put route into the container of classes
76
     *
77
     * @return  App
78
     */
79
    private function initRouter(): App
80
    {
81
        $req = ServerRequestFactory::fromGlobals();
82
        $res = new Response();
83
        $router = new Router($req, $res);
84
85
        $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

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