Permalink
Browse files

Made device initialisation PHP 5.2 compatible.

  • Loading branch information...
1 parent f85b01b commit 214a26d223df05a4397bc61f34a99456e9181820 @psychoticmeow psychoticmeow committed Aug 29, 2011
View
@@ -1,17 +1,17 @@
<?php
-
+
class DeviceAndroid extends MobileDevice {
static protected $instance;
-
+
public function initialize() {
$this->{'name'} = 'Android';
$this->{'user-agent'} = 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; sdk Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
-
+
// Positive matching:
$this->allow(
'%android%i'
);
-
+
// Version matching:
$this->capture(
'software-version',
@@ -20,7 +20,7 @@ public function initialize() {
);
}
}
-
- DeviceAndroid::instance();
-
+
+ MobileDevice::instance('DeviceAndroid');
+
?>
@@ -1,19 +1,19 @@
<?php
-
+
class DeviceBlackBerry extends MobileDevice {
static protected $instance;
-
+
public function initialize() {
$this->{'name'} = 'BlackBerry';
$this->{'user-agent'} = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-GB) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+';
$this->{'user-agent'} = 'BlackBerry9500/5.0.0.484 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/-1';
$this->{'user-agent'} = 'BlackBerry9000/4.6.0.307 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/-1';
-
+
// Positive matching:
$this->allow(
'%blackberry%i'
);
-
+
// Version matching:
$this->capture(
'software-version',
@@ -22,7 +22,7 @@ public function initialize() {
);
}
}
-
- DeviceBlackBerry::instance();
-
+
+ MobileDevice::instance('DeviceBlackBerry');
+
?>
View
@@ -1,17 +1,17 @@
<?php
-
+
class DeviceIPad extends MobileDevice {
static protected $instance;
-
+
public function initialize() {
$this->{'name'} = 'iPad';
$this->{'user-agent'} = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7W367a Safari/531.21.10';
-
+
// Positive matching:
$this->allow(
'%ipad%i'
);
-
+
// Version matching:
$this->capture(
'software-version',
@@ -20,7 +20,7 @@ public function initialize() {
);
}
}
-
- DeviceIPad::instance();
-
+
+ MobileDevice::instance('DeviceIPad');
+
?>
View
@@ -1,18 +1,18 @@
<?php
-
+
class DeviceIPhone extends MobileDevice {
static protected $instance;
-
+
public function initialize() {
$this->{'name'} = 'iPhone';
$this->{'user-agent'} = 'Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7';
$this->{'user-agent'} = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7';
-
+
// Confirm device identity:
$this->allow(
'%iphone%i'
);
-
+
// Find software version:
$this->capture(
'software-version',
@@ -21,7 +21,7 @@ public function initialize() {
);
}
}
-
- DeviceIPhone::instance();
-
+
+ MobileDevice::instance('DeviceIPhone');
+
?>
View
@@ -1,18 +1,18 @@
<?php
-
+
class DevicePalm extends MobileDevice {
static protected $instance;
-
+
public function initialize() {
$this->{'name'} = 'Palm';
-
+
// Positive matching:
$this->allow(
'%palmsource%i', '%palmos%i', '%webos%i'
);
}
}
-
- DevicePalm::instance();
-
+
+ MobileDevice::instance('DevicePalm');
+
?>
@@ -1,124 +1,122 @@
<?php
-
+
class MobileDevice {
- static protected $instance;
-
- static public function instance() {
- if (!isset(static::$instance)) {
- static::$instance = new static();
- static::$instance->initialize();
-
- MobileDetector::registerDevice(static::$instance);
+ static public function instance($class) {
+ if (!isset($class::$instance)) {
+ $class::$instance = new $class();
+ $class::$instance->initialize();
+
+ MobileDetector::registerDevice($class::$instance);
}
-
- return static::$instance;
+
+ return $class::$instance;
}
-
+
protected $about;
protected $allows;
protected $captures;
protected $denies;
-
+
public function __construct() {
$this->about = array();
$this->allows = array();
$this->captures = array();
$this->denies = array();
-
+
$this->matches = (object)array(
'negative' => array(),
'positive' => array()
);
}
-
+
public function __isset($name) {
return isset($this->about[$name]);
}
-
+
public function __unset($name) {
unset($this->about[$name]);
}
-
+
public function __get($name) {
if ($name == 'handle') {
return Lang::createHandle($this->name);
}
-
+
else if (!isset($this->{$name})) {
return null;
}
-
+
return $this->about[$name];
}
-
+
public function __set($name, $value) {
$this->about[$name] = $value;
}
-
+
public function allow() {
$this->allows = array_merge($this->allows, func_get_args());
}
-
+
public function deny() {
$this->denies = array_merge($this->denies, func_get_args());
}
-
+
public function capture($name, $expression, $replacement) {
$this->captures[] = (object)array(
'name' => $name,
'expression' => $expression,
'replacement' => $replacement
);
}
-
+
public function detect($data, $result) {
- foreach ($this->denies as $match) {
+ foreach ($this->denies as $match) {
if (!preg_match($match, $data['HTTP_USER_AGENT'])) continue;
-
+
$result->fail();
-
+
return false;
}
-
- foreach ($this->allows as $match) {
+
+ foreach ($this->allows as $match) {
if (!preg_match($match, $data['HTTP_USER_AGENT'])) continue;
-
+
// Device matched:
$type = $this->{'handle'};
$result->pass();
$result->devices()->{$type}->detected = true;
-
+
foreach ($this->captures as $capture) {
if (!preg_match($capture->expression, $data['HTTP_USER_AGENT'], $matches)) continue;
-
+
if ($capture->replacement instanceof Closure) {
$value = preg_replace_callback(
$capture->expression, $capture->replacement, $matches[0]
);
}
-
+
else {
$value = preg_replace(
$capture->expression, $capture->replacement, $matches[0]
);
}
-
+
$result->devices()->{$type}->captures->{$capture->name} = $value;
}
-
+
return true;
}
-
+
return false;
}
-
+
public function simulate($data) {
if (isset($this->{'user-agent'})) {
$data['HTTP_USER_AGENT'] = $this->{'user-agent'};
}
-
+
return $data;
}
}
-
+
?>

0 comments on commit 214a26d

Please sign in to comment.