1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MobileDetectExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
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
|
|
|
|
23
|
|
|
use Tracy; |
24
|
|
|
|
25
|
|
|
use IPub\MobileDetect; |
26
|
|
|
use IPub\MobileDetect\Diagnostics; |
27
|
|
|
use IPub\MobileDetect\Events; |
28
|
|
|
use IPub\MobileDetect\Helpers; |
29
|
|
|
use IPub\MobileDetect\Templating; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Mobile device detect extension container |
33
|
|
|
* |
34
|
|
|
* @package iPublikuj:MobileDetect! |
35
|
|
|
* @subpackage DI |
36
|
|
|
* |
37
|
|
|
* @author Adam Kadlec <[email protected]> |
38
|
|
|
*/ |
39
|
1 |
|
final class MobileDetectExtension extends DI\CompilerExtension |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $defaults = [ |
45
|
|
|
'redirect' => [ |
46
|
|
|
'mobile' => [ |
47
|
|
|
'isEnabled' => false, |
48
|
|
|
'host' => null, |
49
|
|
|
'statusCode' => 301, |
50
|
|
|
'action' => 'noRedirect', // redirect/noRedirect/redirectWithoutPath |
51
|
|
|
], |
52
|
|
|
'phone' => [ |
53
|
|
|
'isEnabled' => false, |
54
|
|
|
'host' => null, |
55
|
|
|
'statusCode' => 301, |
56
|
|
|
'action' => 'noRedirect', // redirect/noRedirect/redirectWithoutPath |
57
|
|
|
], |
58
|
|
|
'tablet' => [ |
59
|
|
|
'isEnabled' => false, |
60
|
|
|
'host' => null, |
61
|
|
|
'statusCode' => 301, |
62
|
|
|
'action' => 'noRedirect', // redirect/noRedirect/redirectWithoutPath |
63
|
|
|
], |
64
|
|
|
'detectPhoneAsMobile' => false, |
65
|
|
|
'detectTabletAsMobile' => false, |
66
|
|
|
], |
67
|
|
|
'switchDeviceView' => [ |
68
|
|
|
'saveRefererPath' => true |
69
|
|
|
], |
70
|
|
|
'switchParameterName' => 'device_view', |
71
|
|
|
'deviceViewCookie' => [ |
72
|
|
|
'name' => 'device_view', |
73
|
|
|
'domain' => null, |
74
|
|
|
'expireAfter' => '+1 month', |
75
|
|
|
'path' => '/', |
76
|
|
|
'secure' => false, |
77
|
|
|
'httpOnly' => true, |
78
|
|
|
], |
79
|
|
|
'debugger' => '%debugMode%' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function loadConfiguration() : void |
86
|
|
|
{ |
87
|
|
|
// Get container builder |
88
|
1 |
|
$builder = $this->getContainerBuilder(); |
89
|
|
|
|
90
|
|
|
// Set the default configuration |
91
|
1 |
|
$this->validateConfig($this->defaults); |
92
|
|
|
|
93
|
|
|
// Get extension configuration |
94
|
1 |
|
$configuration = $this->getConfig(); |
95
|
|
|
|
96
|
|
|
// Install mobile detect service |
97
|
1 |
|
$mobileDetect = $builder->addDefinition($this->prefix('mobileDetect')) |
98
|
1 |
|
->setType(MobileDetect\MobileDetect::class); |
99
|
|
|
|
100
|
1 |
|
$builder->addDefinition($this->prefix('deviceView')) |
101
|
1 |
|
->setType(Helpers\DeviceView::class) |
102
|
1 |
|
->setArguments(['setSwitchParameterName' => $configuration['switchParameterName']]); |
103
|
|
|
|
104
|
1 |
|
$builder->addDefinition($this->prefix('cookieSettings')) |
105
|
1 |
|
->setType(Helpers\CookieSettings::class) |
106
|
1 |
|
->setArguments([ |
107
|
1 |
|
'name' => $configuration['deviceViewCookie']['name'], |
108
|
1 |
|
'domain' => $configuration['deviceViewCookie']['domain'], |
109
|
1 |
|
'expireAfter' => $configuration['deviceViewCookie']['expireAfter'], |
110
|
1 |
|
'path' => $configuration['deviceViewCookie']['path'], |
111
|
1 |
|
'secure' => $configuration['deviceViewCookie']['secure'], |
112
|
1 |
|
'httpOnly' => $configuration['deviceViewCookie']['httpOnly'], |
113
|
|
|
]); |
114
|
|
|
|
115
|
1 |
|
if ($configuration['debugger'] && interface_exists(Tracy\IBarPanel::class)) { |
116
|
1 |
|
$builder->addDefinition($this->prefix('panel')) |
117
|
1 |
|
->setType(Diagnostics\Panel::class); |
118
|
|
|
|
119
|
1 |
|
$mobileDetect->addSetup('?->register(?)', [$this->prefix('@panel'), '@self']); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$builder->addDefinition($this->prefix('onRequestHandler')) |
123
|
1 |
|
->setType(Events\OnRequestHandler::class) |
124
|
1 |
|
->addSetup('$redirectConf', [$configuration['redirect']]) |
125
|
1 |
|
->addSetup('$isFullPath', [$configuration['switchDeviceView']['saveRefererPath']]); |
126
|
|
|
|
127
|
1 |
|
$builder->addDefinition($this->prefix('onResponseHandler')) |
128
|
1 |
|
->setType(Events\OnResponseHandler::class); |
129
|
|
|
|
130
|
|
|
// Register template helpers |
131
|
1 |
|
$builder->addDefinition($this->prefix('helpers')) |
132
|
1 |
|
->setType(Templating\Helpers::class) |
133
|
1 |
|
->setAutowired(FALSE); |
134
|
|
|
|
135
|
1 |
|
$application = $builder->getDefinition('application'); |
136
|
1 |
|
$application->addSetup('$service->onRequest[] = ?', ['@' . $this->prefix('onRequestHandler')]); |
137
|
1 |
|
$application->addSetup('$service->onResponse[] = ?', ['@' . $this->prefix('onResponseHandler')]); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function beforeCompile() : void |
144
|
|
|
{ |
145
|
1 |
|
parent::beforeCompile(); |
146
|
|
|
|
147
|
1 |
|
$builder = $this->getContainerBuilder(); |
148
|
|
|
|
149
|
|
|
// Install extension latte macros |
150
|
1 |
|
$latteFactory = $builder->getDefinition($builder->getByType(Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory'); |
151
|
|
|
|
152
|
1 |
|
$latteFactory->getResultDefinition() |
153
|
1 |
|
->addSetup('IPub\MobileDetect\Latte\Macros::install(?->getCompiler())', ['@self']) |
154
|
1 |
|
->addSetup('addFilter', ['isMobile', [$this->prefix('@helpers'), 'isMobile']]) |
155
|
1 |
|
->addSetup('addFilter', ['isPhone', [$this->prefix('@helpers'), 'isPhone']]) |
156
|
1 |
|
->addSetup('addFilter', ['isTablet', [$this->prefix('@helpers'), 'isTablet']]) |
157
|
1 |
|
->addSetup('addFilter', ['isDevice', [$this->prefix('@helpers'), 'isDevice']]) |
158
|
1 |
|
->addSetup('addFilter', ['isOs', [$this->prefix('@helpers'), 'isOs']]) |
159
|
1 |
|
->addSetup('addFilter', ['isFullView', [$this->prefix('@helpers'), 'isFullView']]) |
160
|
1 |
|
->addSetup('addFilter', ['isMobileView', [$this->prefix('@helpers'), 'isMobileView']]) |
161
|
1 |
|
->addSetup('addFilter', ['isPhoneView', [$this->prefix('@helpers'), 'isPhoneView']]) |
162
|
1 |
|
->addSetup('addFilter', ['isTabletView', [$this->prefix('@helpers'), 'isTabletView']]) |
163
|
1 |
|
->addSetup('addFilter', ['isNotMobileView', [$this->prefix('@helpers'), 'isNotMobileView']]) |
164
|
1 |
|
->addSetup('addFilter', ['getMobileDetectService', [$this->prefix('@helpers'), 'getMobileDetectService']]) |
165
|
1 |
|
->addSetup('addFilter', ['getDeviceViewService', [$this->prefix('@helpers'), 'getDeviceViewService']]); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Nette\Configurator $config |
170
|
|
|
* @param string $extensionName |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'mobileDetect') : void |
175
|
|
|
{ |
176
|
1 |
|
$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) : void { |
177
|
1 |
|
$compiler->addExtension($extensionName, new MobileDetectExtension()); |
178
|
1 |
|
}; |
179
|
1 |
|
} |
180
|
|
|
} |
181
|
|
|
|