Completed
Push — master ( 27c6b5...777874 )
by Emlyn
05:09
created

ApplicationServicesProvider::constructRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @package    Fuel\Foundation
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2016 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fuel\Foundation;
14
15
use Fuel\Config\Container;
16
use Fuel\Foundation\Request\Http;
17
use Fuel\Foundation\Request\RequestInterface;
18
use Fuel\Foundation\Response\ResponseInterface;
19
use Fuel\Routing\Router;
20
use League\Container\ServiceProvider\AbstractServiceProvider;
21
22
class ApplicationServicesProvider extends AbstractServiceProvider
23
{
24
25
	protected $provides = [
26
		'fuel.application.event',
27
28
		'fuel.application.request',
29
		'Fuel\Foundation\Request\Cli',
30
		'Fuel\Foundation\Request\Http',
31
32
		'fuel.application.response',
33
		'Fuel\Foundation\Response\Cli',
34
		'Fuel\Foundation\Response\Http',
35
36
		'fuel.application.finder',
37
38
		'fuel.config',
39
40
		'fuel.application.component_manager',
41
42
		'fuel.application.router',
43
	];
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48 9
	public function register()
49
	{
50 9
		$this->getContainer()->add('fuel.application.event', 'League\Event\Emitter', true);
51
52 9
		$this->getContainer()->add('Fuel\Foundation\Request\Cli', 'Fuel\Foundation\Request\Cli', false);
53 9
		$this->getContainer()->add('Fuel\Foundation\Request\Http', Http::forge(), false);
54 9
		$this->getContainer()->add('fuel.application.request', $this->constructRequest(), true);
55
56 9
		$this->getContainer()->add('Fuel\Foundation\Response\Cli', 'Fuel\Foundation\Response\Cli', false);
57 9
		$this->getContainer()->add('Fuel\Foundation\Response\Http', 'Fuel\Foundation\Response\Http', false);
58 9
		$this->getContainer()->add('fuel.application.response', $this->constructResponse(), true);
59
60 9
		$this->getContainer()->add('fuel.application.finder', 'Fuel\FileSystem\Finder', true);
61
62
		// Also create a config container for our services
63 9
		$this->getContainer()->add('fuel.config', new Container(null, $this->getContainer()->get('fuel.application.finder')), true);
64
65 9
		$this->getContainer()->add('fuel.application.component_manager', $this->constructComponentManager(), true);
66
67 9
		$this->getContainer()->add('fuel.application.router', $this->constructRouter(), true);
68 9
	}
69
70
	/**
71
	 * @return Router
72
	 */
73 9
	protected function constructRouter() : Router
74
	{
75 9
		$router = new Router;
76 9
		$router->setType('string', Router::MATCH_ANY);
77 9
		$router->setType('num', Router::MATCH_NUM);
78 9
		$router->setType('int', Router::MATCH_NUM);
79
80 9
		return $router;
81
	}
82
83 9
	protected function constructComponentManager() : ComponentManager
84
	{
85 9
		return new ComponentManager($this->getContainer()->get('fuel.application.finder'));
86
	}
87
88
	/**
89
	 * @return RequestInterface
90
	 */
91 9
	protected function constructRequest() : RequestInterface
92
	{
93 9
		if ($this->isCli()) {
94 9
			return $this->getContainer()->get('Fuel\Foundation\Request\Cli');
95
		}
96
97
		return $this->getContainer()->get('Fuel\Foundation\Request\Http');
98
	}
99
100
	/**
101
	 * @return ResponseInterface
102
	 */
103 9
	protected function constructResponse() : ResponseInterface
104
	{
105 9
		if ($this->isCli()) {
106 9
			return $this->getContainer()->get('Fuel\Foundation\Response\Cli');
107
		}
108
109
		return $this->getContainer()->get('Fuel\Foundation\Response\Http');
110
	}
111
112
	/**
113
	 * @return bool
114
	 */
115 9
	public function isCli() : bool
116
	{
117 9
		return php_sapi_name() === 'cli';
118
	}
119
}
120