Passed
Branch master (f36b3c)
by Matthew
08:01
created

Fyuze   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 12
eloc 46
c 5
b 0
f 0
dl 0
loc 205
ccs 49
cts 49
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setupContainer() 0 17 1
A getContainer() 0 3 1
A registerServices() 0 12 2
A loadRoutes() 0 10 2
A __construct() 0 4 1
A errorHandling() 0 5 1
A getPath() 0 3 1
A configure() 0 8 1
A init() 0 8 1
A getConfigPath() 0 3 1
1
<?php
2
namespace Fyuze\Kernel;
3
4
use Fyuze\Config\Config;
5
use Fyuze\Error\ErrorHandler;
6
use Fyuze\Routing\Collection;
7
8
abstract class Fyuze
9
{
10
11
    /**
12
     * The framework version
13
     *
14
     * @var string
15
     */
16
    protected $version = '0.1.0';
17
18
    /**
19
     * has the application initalized?
20
     *
21
     * @var boolean
22
     */
23
    protected $initialized = false;
24
25
    /**
26
     * the IoC container
27
     *
28
     * @var Registry
29
     */
30
    protected $container;
31
32
    /**
33
     * Application path
34
     *
35
     * @var string
36
     */
37
    protected $path;
38
39
    /**
40
     * Configuration path
41
     *
42
     * @var
43
     */
44
    protected $configPath;
45
46
    /**
47
     * Config
48
     *
49
     * @var
50
     */
51
    protected $config;
52
53
    /**
54
     * Default Charset
55
     *
56
     * @var
57
     */
58
    protected $charset;
59
60
    /**
61
     * User Locale
62
     *
63
     * @var
64
     */
65
    protected $locale;
66
67
    /**
68
     * @var array
69
     */
70
    protected $services = [];
71
72
    /**
73
     * Initialize application
74
     *
75
     * @param string $path
76
     */
77 5
    public function __construct($path = '')
78
    {
79 5
        $this->path = $path;
80 5
        $this->init();
81
    }
82
83
    /**
84
     * @return Registry
85
     */
86 5
    public function getContainer()
87
    {
88 5
        return $this->container;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 5
    public function getPath()
95
    {
96 5
        return $this->path;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 5
    public function getConfigPath()
103
    {
104 5
        return $this->getPath() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'config';
105
    }
106
107
    /**
108
     * Initialize services
109
     *
110
     * return @void
111
     */
112 5
    protected function init()
113
    {
114 5
        $this->setupContainer();
115 5
        $this->configure();
116 5
        $this->errorHandling();
117 5
        $this->registerServices();
118
119 5
        $this->initialized = true;
120
    }
121
122
    /**
123
     * Registers container and some classes
124
     *
125
     * @return void
126
     */
127 5
    protected function setupContainer()
128
    {
129 5
        $container = Registry::init();
130 5
        $container->add('app', $this);
131 5
        $container->add('registry', $container);
132
133 5
        $container->add('config', function () {
134 5
            return new Config($this->getConfigPath(), 'prod');
135 5
        });
136
137 5
        $this->config = $container->make('config');
138
139 5
        $container->add('routes', function () {
140 5
            return new Collection();
141 5
        });
142
143 5
        $this->container = $container;
144
    }
145
146
    /**
147
     * Sets up basic application configurations
148
     *
149
     * @return void
150
     */
151 5
    protected function configure()
152
    {
153 5
        $config = $this->config->get('app');
154
155 5
        $this->charset = $config['charset'];
156 5
        mb_internal_encoding($this->charset);
157
158 5
        date_default_timezone_set($config['timezone']);
159
    }
160
161
    /**
162
     * Registers all defined services
163
     *
164
     * @return void
165
     */
166 5
    protected function registerServices()
167
    {
168 5
        $services = array_filter($this->config->get('app.services'), function ($service) {
169 5
            return class_exists($service);
170 5
        });
171
172 5
        foreach ($services as $service) {
173
            /** @var \Fyuze\Kernel\Service $obj */
174 5
            $obj = new $service($this->container);
175 5
            $obj->services();
176
177 5
            $this->services[] = $obj;
178
        }
179
    }
180
181
    /**
182
     * Registers error handler with container
183
     * And converts all errors into ErrorExceptions
184
     *
185
     * @return void
186
     */
187 5
    protected function errorHandling()
188
    {
189 5
        $handler = new ErrorHandler();
190
191 5
        $this->container->add('error', $handler);
192
    }
193
194
    /**
195
     * @return Collection
196
     */
197 5
    protected function loadRoutes()
198
    {
199 5
        $router = $this->container->make('routes');
200 5
        $routes = realpath($this->getPath() . '/app/routes.php');
201
202 5
        if (file_exists($routes)) {
203 5
            require $routes;
204
        }
205
206 5
        return $router;
207
    }
208
209
    /**
210
     * @return mixed
211
     */
212
    abstract public function boot();
213
}
214