Completed
Pull Request — master (#35)
by Emlyn
01:55
created

constructResponseFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @package    Fuel\Foundation
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2017 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\Exception\Formatter;
17
use Fuel\Foundation\Request\Http;
18
use Fuel\Foundation\Request\RequestInterface;
19
use Fuel\Foundation\Response\ResponseInterface;
20
use Fuel\Routing\Router;
21
use League\Container\ServiceProvider\AbstractServiceProvider;
22
use Symfony\Component\DomCrawler\Form;
23
24
class ApplicationServicesProvider extends AbstractServiceProvider
25
{
26
27
	protected $provides = [
28
		'fuel.application.event',
29
30
		'fuel.application.request',
31
		'Fuel\Foundation\Request\Cli',
32
		'Fuel\Foundation\Request\Http',
33
34
		'fuel.application.response',
35
		'Fuel\Foundation\Response\Cli',
36
		'Fuel\Foundation\Response\Http',
37
38
		'fuel.application.finder',
39
40
		'fuel.config',
41
42
		'fuel.application.component_manager',
43
44
		'fuel.application.router',
45
46
		'Fuel\Foundation\Formatter\Noop',
47
		'Fuel\Foundation\Formatter\HttpAcceptJson',
48
		'Fuel\Foundation\ResponseFormatter',
49
	];
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54 10
	public function register()
55
	{
56 10
		$this->getContainer()->add('fuel.application.event', 'League\Event\Emitter', true);
57
58 10
		$this->getContainer()->add('Fuel\Foundation\Request\Cli', 'Fuel\Foundation\Request\Cli', false);
59 10
		$this->getContainer()->add(
60 10
			'Fuel\Foundation\Request\Http',
61
			function() {
62
				return Http::forge();
63 10
			},
64 10
			false
65
		);
66
67 10
		$this->getContainer()->add(
68 10
			'fuel.application.request',
69
			function() {
70 1
				return $this->constructRequest();
71 10
			},
72 10
			true
73
		);
74
75 10
		$this->getContainer()->add('Fuel\Foundation\Response\Cli', 'Fuel\Foundation\Response\Cli', false);
76 10
		$this->getContainer()->add('Fuel\Foundation\Response\Http', 'Fuel\Foundation\Response\Http', false);
77 10
		$this->getContainer()->add(
78 10
			'fuel.application.response',
79
			function() {
80 5
				return $this->constructResponse();
81 10
				},
82 10
			true
83
		);
84
85 10
		$this->getContainer()->add('fuel.application.finder', 'Fuel\FileSystem\Finder', true);
86
87
		// Also create a config container for our services
88 10
		$this->getContainer()->add(
89 10
			'fuel.config',
90
			function() {
91 10
				return new Container(null, $this->getContainer()->get('fuel.application.finder'));
92 10
			},
93 10
			true
94
		);
95
96 10
		$this->getContainer()->add(
97 10
			'fuel.application.component_manager',
98
			function () {
99 10
				return $this->constructComponentManager();
100 10
			},
101 10
			true
102
		);
103
104 10
		$this->getContainer()->add(
105 10
			'fuel.application.router',
106
			function() {
107 10
				return $this->constructRouter();
108 10
			},
109 10
			true
110
		);
111
112
		// Add in the various formatters
113 10
		$this->getContainer()->add('Fuel\Foundation\Formatter\Noop', 'Fuel\Foundation\Formatter\Noop', true);
114 10
		$this->getContainer()->add('Fuel\Foundation\Formatter\HttpAcceptJson', 'Fuel\Foundation\Formatter\HttpAcceptJson', true);
115 10
		$this->getContainer()->add(
116 10
			'Fuel\Foundation\ResponseFormatter',
117 10
			function() {
118
				return $this->constructResponseFormatter();
119 10
			},
120 10
			true
121
		);
122 10
	}
123
124
	/**
125
	 * @return Router
126
	 */
127 10
	protected function constructRouter() : Router
128
	{
129 10
		$router = new Router;
130 10
		$router->setType('string', Router::MATCH_ANY);
131 10
		$router->setType('num', Router::MATCH_NUM);
132 10
		$router->setType('int', Router::MATCH_NUM);
133
134 10
		return $router;
135
	}
136
137 10
	protected function constructComponentManager() : ComponentManager
138
	{
139 10
		return new ComponentManager($this->getContainer()->get('fuel.application.finder'));
140
	}
141
142
	/**
143
	 * @return RequestInterface
144
	 */
145 1
	protected function constructRequest() : RequestInterface
146
	{
147 1
		if ($this->isCli()) {
148 1
			return $this->getContainer()->get('Fuel\Foundation\Request\Cli');
149
		}
150
151
		return $this->getContainer()->get('Fuel\Foundation\Request\Http');
152
	}
153
154
	/**
155
	 * @return ResponseInterface
156
	 */
157 5
	protected function constructResponse() : ResponseInterface
158
	{
159 5
		if ($this->isCli()) {
160 5
			return $this->getContainer()->get('Fuel\Foundation\Response\Cli');
161
		}
162
163
		return $this->getContainer()->get('Fuel\Foundation\Response\Http');
164
	}
165
166
	protected function constructResponseFormatter() : ResponseFormatter
167
	{
168
		/** @var Container $config */
169
		$config = $this->getContainer()->get('fuel.config');
170
		$config->load('output_formatters', 'output_formatters');
171
172
		return new ResponseFormatter(
173
			$config->get('output_formatters', ['Fuel\Foundation\Formatter\Noop']),
174
			$this->getContainer()
175
		);
176
	}
177
178
	/**
179
	 * @return bool
180
	 */
181 6
	public function isCli() : bool
182
	{
183 6
		return php_sapi_name() === 'cli';
184
	}
185
}
186