Completed
Push — master ( f44b26...73adc3 )
by Adam
02:18
created

MobileDetectExtension::loadConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
ccs 29
cts 29
cp 1
rs 8.8571
cc 1
eloc 29
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * MobileDetectExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:MobileDetect!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           21.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\MobileDetect\DI;
18
19
use Nette;
20
use Nette\Bridges;
21
use Nette\DI;
22
use Nette\PhpGenerator as Code;
23
24
use IPub;
25
use IPub\MobileDetect;
26
use IPub\MobileDetect\Events;
27
use IPub\MobileDetect\Helpers;
28
use IPub\MobileDetect\Templating;
29
30 1
final class MobileDetectExtension extends DI\CompilerExtension
31
{
32
	/**
33
	 * @var array
34
	 */
35
	private $defaults = [
36
		'redirect'         => [
37
			'mobile'               => [
38
				'isEnabled'  => FALSE,
39
				'host'       => NULL,
40
				'statusCode' => 301,
41
				'action'     => 'noRedirect',    // redirect/noRedirect/redirectWithoutPath
42
			],
43
			'phone'                => [
44
				'isEnabled'  => FALSE,
45
				'host'       => NULL,
46
				'statusCode' => 301,
47
				'action'     => 'noRedirect',    // redirect/noRedirect/redirectWithoutPath
48
			],
49
			'tablet'               => [
50
				'isEnabled'  => FALSE,
51
				'host'       => NULL,
52
				'statusCode' => 301,
53
				'action'     => 'noRedirect',    // redirect/noRedirect/redirectWithoutPath
54
			],
55
			'detectPhoneAsMobile'  => FALSE,
56
			'detectTabletAsMobile' => FALSE,
57
		],
58
		'switchDeviceView' => [
59
			'saveRefererPath' => TRUE
60
		],
61
		'switchParameterName' => 'device_view',
62
		'deviceViewCookie' => [
63
			'name'        => 'device_view',
64
			'domain'      => NULL,
65
			'expireAfter' => '+1 month',
66
			'path'        => '/',
67
			'secure'      => FALSE,
68
			'httpOnly'    => TRUE,
69
		]
70
	];
71
72
	/**
73
	 * @return void
74
	 */
75
	public function loadConfiguration()
76
	{
77
		// Get container builder
78 1
		$builder = $this->getContainerBuilder();
79
		// Get extension configuration
80 1
		$configuration = $this->getConfig($this->defaults);
81
82
		// Install mobile detect service
83 1
		$builder->addDefinition($this->prefix('mobileDetect'))
84 1
			->setClass(MobileDetect\MobileDetect::class);
85
86 1
		$builder->addDefinition($this->prefix('deviceView'))
87 1
			->setClass(Helpers\DeviceView::class)
88 1
			->setArguments(['setSwitchParameterName' => $configuration['switchParameterName']]);
89
90 1
		$builder->addDefinition($this->prefix('cookieSettings'))
91 1
			->setClass(Helpers\CookieSettings::class)
92 1
			->setArguments([
93 1
				'name'        => $configuration['deviceViewCookie']['name'],
94 1
				'domain'      => $configuration['deviceViewCookie']['domain'],
95 1
				'expireAfter' => $configuration['deviceViewCookie']['expireAfter'],
96 1
				'path'        => $configuration['deviceViewCookie']['path'],
97 1
				'secure'      => $configuration['deviceViewCookie']['secure'],
98 1
				'httpOnly'    => $configuration['deviceViewCookie']['httpOnly'],
99
			]);
100
101 1
		$builder->addDefinition($this->prefix('onRequestHandler'))
102 1
			->setClass(Events\OnRequestHandler::class)
103 1
			->addSetup('$redirectConf', [$configuration['redirect']])
104 1
			->addSetup('$isFullPath', [$configuration['switchDeviceView']['saveRefererPath']]);
105
106 1
		$builder->addDefinition($this->prefix('onResponseHandler'))
107 1
			->setClass(Events\OnResponseHandler::class);
108
109
		// Register template helpers
110 1
		$builder->addDefinition($this->prefix('helpers'))
111 1
			->setClass(Templating\Helpers::class)
112 1
			->setAutowired(FALSE);
113
114 1
		$application = $builder->getDefinition('application');
115 1
		$application->addSetup('$service->onRequest[] = ?', ['@' . $this->prefix('onRequestHandler')]);
116 1
		$application->addSetup('$service->onResponse[] = ?', ['@' . $this->prefix('onResponseHandler')]);
117 1
	}
118
119
	/**
120
	 * {@inheritdoc}
121
	 */
122
	public function beforeCompile()
123
	{
124 1
		parent::beforeCompile();
125
126 1
		$builder = $this->getContainerBuilder();
127
128
		// Install extension latte macros
129 1
		$latteFactory = $builder->getDefinition($builder->getByType(Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory');
130
131
		$latteFactory
132 1
			->addSetup('IPub\MobileDetect\Latte\Macros::install(?->getCompiler())', ['@self'])
133 1
			->addSetup('addFilter', ['isMobile', [$this->prefix('@helpers'), 'isMobile']])
134 1
			->addSetup('addFilter', ['isPhone', [$this->prefix('@helpers'), 'isPhone']])
135 1
			->addSetup('addFilter', ['isTablet', [$this->prefix('@helpers'), 'isTablet']])
136 1
			->addSetup('addFilter', ['isDevice', [$this->prefix('@helpers'), 'isDevice']])
137 1
			->addSetup('addFilter', ['isOs', [$this->prefix('@helpers'), 'isOs']])
138 1
			->addSetup('addFilter', ['isFullView', [$this->prefix('@helpers'), 'isFullView']])
139 1
			->addSetup('addFilter', ['isMobileView', [$this->prefix('@helpers'), 'isMobileView']])
140 1
			->addSetup('addFilter', ['isPhoneView', [$this->prefix('@helpers'), 'isPhoneView']])
141 1
			->addSetup('addFilter', ['isTabletView', [$this->prefix('@helpers'), 'isTabletView']])
142 1
			->addSetup('addFilter', ['isNotMobileView', [$this->prefix('@helpers'), 'isNotMobileView']])
143 1
			->addSetup('addFilter', ['getMobileDetectService', [$this->prefix('@helpers'), 'getMobileDetectService']])
144 1
			->addSetup('addFilter', ['getDeviceViewService', [$this->prefix('@helpers'), 'getDeviceViewService']]);
145 1
	}
146
147
	/**
148
	 * @param Nette\Configurator $config
149
	 * @param string $extensionName
150
	 *
151
	 * @return void
152
	 */
153
	public static function register(Nette\Configurator $config, string $extensionName = 'mobileDetect')
154
	{
155 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
156 1
			$compiler->addExtension($extensionName, new MobileDetectExtension());
157 1
		};
158 1
	}
159
}
160