diff --git a/composer.json b/composer.json index 9f2909b..957a830 100644 --- a/composer.json +++ b/composer.json @@ -66,7 +66,7 @@ "latte/latte" : "~2.3", - "mobiledetect/mobiledetectlib" : "2.8.*" + "jenssegers/agent" : "~2.5" }, "require-dev": { diff --git a/src/IPub/MobileDetect/DI/MobileDetectExtension.php b/src/IPub/MobileDetect/DI/MobileDetectExtension.php index 84c5475..48a5ec5 100644 --- a/src/IPub/MobileDetect/DI/MobileDetectExtension.php +++ b/src/IPub/MobileDetect/DI/MobileDetectExtension.php @@ -74,7 +74,8 @@ final class MobileDetectExtension extends DI\CompilerExtension 'path' => '/', 'secure' => FALSE, 'httpOnly' => TRUE, - ] + ], + 'debugger' => '%debugMode%' ]; /** @@ -88,7 +89,7 @@ public function loadConfiguration() $configuration = $this->getConfig($this->defaults); // Install mobile detect service - $builder->addDefinition($this->prefix('mobileDetect')) + $mobileDetect = $builder->addDefinition($this->prefix('mobileDetect')) ->setClass(MobileDetect\MobileDetect::class); $builder->addDefinition($this->prefix('deviceView')) @@ -106,6 +107,13 @@ public function loadConfiguration() 'httpOnly' => $configuration['deviceViewCookie']['httpOnly'], ]); + if ($configuration['debugger'] && interface_exists('Tracy\IBarPanel')) { + $builder->addDefinition($this->prefix('panel')) + ->setClass('IPub\MobileDetect\Diagnostics\Panel'); + + $mobileDetect->addSetup('?->register(?)', [$this->prefix('@panel'), '@self']); + } + $builder->addDefinition($this->prefix('onRequestHandler')) ->setClass(Events\OnRequestHandler::class) ->addSetup('$redirectConf', [$configuration['redirect']]) diff --git a/src/IPub/MobileDetect/Diagnostics/Panel.php b/src/IPub/MobileDetect/Diagnostics/Panel.php new file mode 100644 index 0000000..191f1a0 --- /dev/null +++ b/src/IPub/MobileDetect/Diagnostics/Panel.php @@ -0,0 +1,84 @@ + + */ +final class Panel extends \Nette\Object implements \Tracy\IBarPanel +{ + /** @var \IPub\MobileDetect\MobileDetect + */ + private $mobileDetect; + + public function register(\IPub\MobileDetect\MobileDetect $mobileDetect) + { + $this->mobileDetect = $mobileDetect; + + \Tracy\Debugger::getBar()->addPanel($this, 'ipub.mobileDetect'); + + return $this; + } + + /** + * Renders HTML code for custom tab. + * + * @return string + */ + public function getTab() + { + return '' + . $this->mobileDetect->view() . ' / ' . $this->mobileDetect->platform() . ''; + } + + /** + * Renders HTML code for custom panel. + * + * @return string + */ + public function getPanel() + { + $h = 'htmlSpecialChars'; + + $panel = []; + + $panel[] = '

Original User agent header

'; + $panel[] = '

' . $h($this->mobileDetect->getUserAgent()) . '

'; + + $properties = ['view', 'platform', 'platformVersion', 'device', 'browser', 'browserVersion']; + + $panel[] = ''; + foreach ($properties as $property) + { + $panel[] = ''; + } + $panel[] = '
' . ucfirst($property) . '' . $h($this->mobileDetect->{$property}() ?: '') . '
'; + + return empty($panel) ? '' : + '

View: '.$this->mobileDetect->view() . ', OS: ' . $this->mobileDetect->platform().'

' . + '
' . implode($panel) . '
' . + ''; + } +} diff --git a/src/IPub/MobileDetect/MobileDetect.php b/src/IPub/MobileDetect/MobileDetect.php index a3bde97..4267d2e 100644 --- a/src/IPub/MobileDetect/MobileDetect.php +++ b/src/IPub/MobileDetect/MobileDetect.php @@ -31,7 +31,7 @@ * * @author Adam Kadlec */ -final class MobileDetect extends \Detection\MobileDetect +final class MobileDetect extends \Jenssegers\Agent\Agent { /** * @var Helpers\DeviceView @@ -62,16 +62,6 @@ public function __construct( $this->deviceView = $deviceView; } - /** - * Check if the device is mobile phone - * - * @return bool - */ - public function isPhone() : bool - { - return $this->isMobile() && !$this->isTablet(); - } - /** * Check if the device is not mobile phone * @@ -81,4 +71,19 @@ public function isNotPhone() : bool { return (($this->isMobile() && $this->isTablet()) || !$this->isMobile()); } + + public function view() + { + return $this->deviceView->getViewType(); + } + + public function browserVersion() + { + return $this->version($this->browser()); + } + + public function platformVersion() + { + return $this->version($this->platform()); + } }