Completed
Push — master ( 9a3a3c...24148b )
by Michael
14s
created

Kernel::loadConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0185
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer;
10
11
use Joomla\Application\AbstractApplication;
12
use Joomla\Database\Service\DatabaseProvider;
13
use Joomla\DI\Container;
14
use Joomla\DI\ContainerAwareInterface;
15
use Joomla\DI\ContainerAwareTrait;
16
use Joomla\Registry\Registry;
17
use Joomla\StatsServer\Providers\AnalyticsServiceProvider;
18
use Joomla\StatsServer\Providers\ConsoleServiceProvider;
19
use Joomla\StatsServer\Providers\DatabaseServiceProvider;
20
use Joomla\StatsServer\Providers\EventServiceProvider;
21
use Joomla\StatsServer\Providers\FlysystemServiceProvider;
22
use Joomla\StatsServer\Providers\GitHubServiceProvider;
23
use Joomla\StatsServer\Providers\MonologServiceProvider;
24
use Joomla\StatsServer\Providers\RepositoryServiceProvider;
25
use Joomla\StatsServer\Providers\WebApplicationServiceProvider;
26
use Monolog\ErrorHandler;
27
use Monolog\Logger;
28
29
/**
30
 * Application kernel
31
 */
32
abstract class Kernel implements ContainerAwareInterface
33
{
34
	use ContainerAwareTrait;
35
36
	/**
37
	 * Flag indicating this Kernel has been booted
38
	 *
39
	 * @var  boolean
40
	 */
41
	protected $booted = false;
42
43
	/**
44
	 * Boot the Kernel
45
	 *
46
	 * @return  void
47
	 */
48 11
	public function boot(): void
49
	{
50 11
		if ($this->booted)
51
		{
52
			return;
53
		}
54
55 11
		$this->setContainer($this->buildContainer());
56
57
		// Register deprecation logging via Monolog
58 11
		ErrorHandler::register($this->getContainer()->get(Logger::class), [E_DEPRECATED, E_USER_DEPRECATED], false, false);
59
60 11
		$this->booted = true;
61 11
	}
62
63
	/**
64
	 * Check if the Kernel is booted
65
	 *
66
	 * @return  void
67
	 */
68 2
	public function isBooted(): bool
69
	{
70 2
		return $this->booted;
71
	}
72
73
	/**
74
	 * Run the kernel
75
	 *
76
	 * @return  void
77
	 */
78 2
	public function run(): void
79
	{
80 2
		$this->boot();
81
82 2
		if (!$this->getContainer()->has(AbstractApplication::class))
83
		{
84 1
			throw new \RuntimeException('The application has not been registered with the container.');
85
		}
86
87 1
		$this->getContainer()->get(AbstractApplication::class)->execute();
88 1
	}
89
90
	/**
91
	 * Build the service container
92
	 *
93
	 * @return  void
94
	 */
95 11
	protected function buildContainer(): Container
96
	{
97 11
		$config = $this->loadConfiguration();
98
99 11
		$container = new Container;
100 11
		$container->share('config', $config);
101
102 11
		$container->registerServiceProvider(new AnalyticsServiceProvider)
103 11
			->registerServiceProvider(new ConsoleServiceProvider)
104 11
			->registerServiceProvider(new DatabaseProvider)
105 11
			->registerServiceProvider(new DatabaseServiceProvider)
106 11
			->registerServiceProvider(new EventServiceProvider)
107 11
			->registerServiceProvider(new FlysystemServiceProvider)
108 11
			->registerServiceProvider(new GitHubServiceProvider)
109 11
			->registerServiceProvider(new MonologServiceProvider)
110 11
			->registerServiceProvider(new RepositoryServiceProvider)
111 11
			->registerServiceProvider(new WebApplicationServiceProvider);
112
113 11
		return $container;
114
	}
115
116
	/**
117
	 * Load the application's configuration
118
	 *
119
	 * @return  Registry
120
	 */
121 11
	private function loadConfiguration(): Registry
122
	{
123 11
		$registry = new Registry;
124 11
		$registry->loadFile(APPROOT . '/etc/config.dist.json');
125
126 11
		if (file_exists(APPROOT . '/etc/config.json'))
127
		{
128
			$registry->loadFile(APPROOT . '/etc/config.json');
129
		}
130
131 11
		return $registry;
132
	}
133
}
134