Completed
Push — output_formatting ( c5a83b...d876bb )
by Emlyn
10:27
created

constructResponseFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
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 - 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('Fuel\Foundation\Request\Http', Http::forge(), false);
60 10
		$this->getContainer()->add('fuel.application.request', $this->constructRequest(), true);
61
62 10
		$this->getContainer()->add('Fuel\Foundation\Response\Cli', 'Fuel\Foundation\Response\Cli', false);
63 10
		$this->getContainer()->add('Fuel\Foundation\Response\Http', 'Fuel\Foundation\Response\Http', false);
64 10
		$this->getContainer()->add('fuel.application.response', $this->constructResponse(), true);
65
66 10
		$this->getContainer()->add('fuel.application.finder', 'Fuel\FileSystem\Finder', true);
67
68
		// Also create a config container for our services
69 10
		$this->getContainer()->add('fuel.config', new Container(null, $this->getContainer()->get('fuel.application.finder')), true);
70
71 10
		$this->getContainer()->add('fuel.application.component_manager', $this->constructComponentManager(), true);
72
73 10
		$this->getContainer()->add('fuel.application.router', $this->constructRouter(), true);
74
75
		// Add in the various formatters
76 10
		$this->container->add('Fuel\Foundation\Formatter\Noop', 'Fuel\Foundation\Formatter\Noop', true);
77 10
		$this->container->add('Fuel\Foundation\Formatter\HttpAcceptJson', 'Fuel\Foundation\Formatter\HttpAcceptJson', true);
78 10
		$this->container->add('Fuel\Foundation\ResponseFormatter', $this->constructResponseFormatter(), true);
79 10
	}
80
81
	/**
82
	 * @return Router
83
	 */
84 10
	protected function constructRouter() : Router
85
	{
86 10
		$router = new Router;
87 10
		$router->setType('string', Router::MATCH_ANY);
88 10
		$router->setType('num', Router::MATCH_NUM);
89 10
		$router->setType('int', Router::MATCH_NUM);
90
91 10
		return $router;
92
	}
93
94 10
	protected function constructComponentManager() : ComponentManager
95
	{
96 10
		return new ComponentManager($this->getContainer()->get('fuel.application.finder'));
97
	}
98
99
	/**
100
	 * @return RequestInterface
101
	 */
102 10
	protected function constructRequest() : RequestInterface
103
	{
104 10
		if ($this->isCli()) {
105 10
			return $this->getContainer()->get('Fuel\Foundation\Request\Cli');
106
		}
107
108
		return $this->getContainer()->get('Fuel\Foundation\Request\Http');
109
	}
110
111
	/**
112
	 * @return ResponseInterface
113
	 */
114 10
	protected function constructResponse() : ResponseInterface
115
	{
116 10
		if ($this->isCli()) {
117 10
			return $this->getContainer()->get('Fuel\Foundation\Response\Cli');
118
		}
119
120
		return $this->getContainer()->get('Fuel\Foundation\Response\Http');
121
	}
122
123 10
	protected function constructResponseFormatter() : ResponseFormatter
124
	{
125
		/** @var Container $config */
126 10
		$config = $this->getContainer()->get('fuel.config');
127 10
		$config->load('output_formatters', 'output_formatters');
128
129 10
		return new ResponseFormatter(
130 10
			$config->get('output_formatters', ['Fuel\Foundation\Formatter\Noop']),
131 10
			$this->getContainer()
132
		);
133
	}
134
135
	/**
136
	 * @return bool
137
	 */
138 10
	public function isCli() : bool
139
	{
140 10
		return php_sapi_name() === 'cli';
141
	}
142
}
143