Completed
Push — master ( 3f3351...7c8768 )
by Michael
02:56
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\ConsoleServiceProvider;
18
use Joomla\StatsServer\Providers\DatabaseServiceProvider;
19
use Joomla\StatsServer\Providers\EventServiceProvider;
20
use Joomla\StatsServer\Providers\FlysystemServiceProvider;
21
use Joomla\StatsServer\Providers\GitHubServiceProvider;
22
use Joomla\StatsServer\Providers\MonologServiceProvider;
23
use Joomla\StatsServer\Providers\RepositoryServiceProvider;
24
use Joomla\StatsServer\Providers\WebApplicationServiceProvider;
25
use Monolog\ErrorHandler;
26
use Monolog\Logger;
27
28
/**
29
 * Application kernel
30
 */
31
abstract class Kernel implements ContainerAwareInterface
32
{
33
	use ContainerAwareTrait;
34
35
	/**
36
	 * Flag indicating this Kernel has been booted
37
	 *
38
	 * @var  boolean
39
	 */
40
	protected $booted = false;
41
42
	/**
43
	 * Boot the Kernel
44
	 *
45
	 * @return  void
46
	 */
47 12
	public function boot(): void
48
	{
49 12
		if ($this->booted)
50
		{
51
			return;
52
		}
53
54 12
		$this->setContainer($this->buildContainer());
55
56
		// Register deprecation logging via Monolog
57 12
		ErrorHandler::register($this->getContainer()->get(Logger::class), [E_DEPRECATED, E_USER_DEPRECATED], false, false);
58
59 12
		$this->booted = true;
60 12
	}
61
62
	/**
63
	 * Check if the Kernel is booted
64
	 *
65
	 * @return  void
66
	 */
67 2
	public function isBooted(): bool
68
	{
69 2
		return $this->booted;
70
	}
71
72
	/**
73
	 * Run the kernel
74
	 *
75
	 * @return  void
76
	 */
77 2
	public function run(): void
78
	{
79 2
		$this->boot();
80
81 2
		if (!$this->getContainer()->has(AbstractApplication::class))
82
		{
83 1
			throw new \RuntimeException('The application has not been registered with the container.');
84
		}
85
86 1
		$this->getContainer()->get(AbstractApplication::class)->execute();
87 1
	}
88
89
	/**
90
	 * Build the service container
91
	 *
92
	 * @return  void
93
	 */
94 12
	protected function buildContainer(): Container
95
	{
96 12
		$config = $this->loadConfiguration();
97
98 12
		$container = new Container;
99 12
		$container->share('config', $config);
100
101 12
		$container->registerServiceProvider(new ConsoleServiceProvider)
102 12
			->registerServiceProvider(new DatabaseProvider)
103 12
			->registerServiceProvider(new DatabaseServiceProvider)
104 12
			->registerServiceProvider(new EventServiceProvider)
105 12
			->registerServiceProvider(new FlysystemServiceProvider)
106 12
			->registerServiceProvider(new GitHubServiceProvider)
107 12
			->registerServiceProvider(new MonologServiceProvider)
108 12
			->registerServiceProvider(new RepositoryServiceProvider)
109 12
			->registerServiceProvider(new WebApplicationServiceProvider);
110
111 12
		return $container;
112
	}
113
114
	/**
115
	 * Load the application's configuration
116
	 *
117
	 * @return  Registry
118
	 */
119 12
	private function loadConfiguration(): Registry
120
	{
121 12
		$registry = new Registry;
122 12
		$registry->loadFile(APPROOT . '/etc/config.dist.json');
123
124 12
		if (file_exists(APPROOT . '/etc/config.json'))
125
		{
126
			$registry->loadFile(APPROOT . '/etc/config.json');
127
		}
128
129 12
		return $registry;
130
	}
131
}
132