Browse Source

Add MobileComponent.php

Mark Scherer 10 years ago
parent
commit
4b596cfeb6

+ 3 - 0
composer.json

@@ -17,6 +17,9 @@
 		"cakephp/cakephp": "~3.0",
 		"dereuromark/cakephp-shim": "~1.0"
 	},
+	"require-dev":{
+		"mobiledetect/mobiledetectlib": "2.*"
+	},
 	"autoload": {
 		"psr-4": {
 			"Tools\\": "src"

+ 217 - 0
src/Controller/Component/MobileComponent.php

@@ -0,0 +1,217 @@
+<?php
+namespace Tools\Controller\Component;
+
+use Cake\Controller\Controller;
+use Shim\Controller\Component\Component;
+use Cake\Core\Configure;
+use Cake\Event\Event;
+use Cake\Routing\Router;
+use Tools\Utility\Utility;
+
+/**
+ * A component to easily store mobile in session and serve mobile views to users.
+ * It allows good default values while not being restrictive as you can always
+ * overwrite the auto-detection manually to force desktop or mobile version.
+ *
+ * Uses object attributes as well as Configure to store the results for later use.
+ *
+ * Don't foget to set up your mobile detectors in your bootstrap.
+ *
+ * Uses Configure to cache lookups in request: User.isMobile and User.setMobile
+ * - isMobile is the auto-detection (true/false)
+ * - setMobile can be set by the user and overrides the default behavior/detection
+ *   (1=true/0=false or -1=null which will remove the override)
+ *
+ * The overwrite of a user is stored in the session: User.mobile.
+ * It overwrites the Configure value.
+ *
+ * It also pushes switch urls to the view.
+ *
+ * @author Mark Scherer
+ * @license http://opensource.org/licenses/mit-license.php MIT
+ */
+class MobileComponent extends Component {
+
+	public $Controller = null;
+
+	/**
+	 * Stores the result of the auto-detection.
+	 *
+	 * @var bool
+	 */
+	public $isMobile = null;
+
+	/**
+	 * Stores the final detection result including user preference.
+	 *
+	 * @var bool
+	 */
+	public $setMobile = null;
+
+	/**
+	 * Default values. Can also be set using Configure.
+	 *
+	 * @param array
+	 */
+	protected $_defaultConfig = [
+		'on' => 'beforeFilter', // initialize (prior to controller's beforeRender) or startup
+		'engine' => null, // CakePHP internal if null
+		'themed' => false, // If false uses subfolders instead of themes: /View/.../mobile/
+		'auto' => false, // auto set mobile views
+	];
+
+	/**
+	 * MobileComponent::initialize()
+	 *
+	 * @param Controller $Controller
+	 * @return void
+	 */
+	public function initialize(array $config) {
+		parent::initialize($config);
+
+		if ($this->_config['on'] !== 'initialize') {
+			return;
+		}
+		$this->_init();
+	}
+
+	/**
+	 * MobileComponent::startup()
+	 *
+	 * @param Controller $Controller
+	 * @return void
+	 */
+	public function beforeFilter(Event $event) {
+		if ($this->_config['on'] !== 'beforeFilter') {
+			return;
+		}
+		$this->_init();
+	}
+
+	/**
+	 * Main auto-detection logic including session based storage to avoid
+	 * multiple lookups.
+	 *
+	 * Uses "mobile" query string to overwrite the auto-detection.
+	 * -1 clears the fixation
+	 * 1 forces mobile
+	 * 0 forces no-mobile
+	 *
+	 * @return void
+	 */
+	protected function _init() {
+		$mobileOverwrite = $this->Controller->request->query('mobile');
+
+		if ($mobileOverwrite !== null) {
+			if ($mobileOverwrite === '-1') {
+				$noMobile = null;
+			} else {
+				$wantsMobile = (bool)$mobileOverwrite;
+			}
+			$this->request->session()->write('User.mobile', (int)$wantsMobile);
+		}
+		$this->isMobile();
+
+		if (!$this->_config['auto']) {
+			return;
+		}
+		$this->setMobile();
+	}
+
+	/**
+	 * Sets mobile views as `Mobile` theme.
+	 *
+	 * Only needs to be called if auto is set to false.
+	 * Then you probably want to call this from your AppController::beforeRender().
+	 *
+	 * @return void
+	 */
+	public function setMobile() {
+		if ($this->isMobile === null) {
+			$this->isMobile();
+		}
+		$forceMobile = $this->request->session()->read('User.mobile');
+
+		if ($forceMobile !== null && !$forceMobile) {
+			$this->setMobile = false;
+		} elseif ($forceMobile !== null && $forceMobile || $this->isMobile()) {
+			$this->setMobile = true;
+		} else {
+			$this->setMobile = false;
+		}
+
+		//$urlParams = Router::getParams(true);
+		$urlParams = [];
+		if (!isset($urlParams['pass'])) {
+			$urlParams['pass'] = [];
+		}
+		$urlParams = array_merge($urlParams, $urlParams['pass']);
+		unset($urlParams['pass']);
+		if (isset($urlParams['prefix'])) {
+			unset($urlParams['prefix']);
+		}
+
+		if ($this->setMobile) {
+			$urlParams['?']['mobile'] = 0;
+			$url = Router::url($urlParams);
+			$this->Controller->set('desktopUrl', $url);
+		} else {
+			$urlParams['?']['mobile'] = 1;
+			$url = Router::url($urlParams);
+			$this->Controller->set('mobileUrl', $url);
+		}
+
+		Configure::write('User.setMobile', (int)$this->setMobile);
+
+		if (!$this->setMobile) {
+			return;
+		}
+
+		$this->Controller->viewClass = 'Theme';
+		$this->Controller->theme = 'Mobile';
+	}
+
+	/**
+	 * Determines if we need to so serve mobile views based on session preference
+	 * and browser headers.
+	 *
+	 * @return bool Success
+	 */
+	public function isMobile() {
+		if ($this->isMobile !== null) {
+			return $this->isMobile;
+		}
+
+		$this->isMobile = Configure::read('User.isMobile');
+		if ($this->isMobile !== null) {
+			return $this->isMobile;
+		}
+		$this->isMobile = (bool)$this->detect();
+
+		Configure::write('User.isMobile', (int)$this->isMobile);
+		return $this->isMobile;
+	}
+
+	/**
+	 * Detects if the current request is from a mobile device.
+	 *
+	 * Note that the cake internal way might soon be deprecated:
+	 * https://github.com/cakephp/cakephp/issues/2546
+	 *
+	 * @return bool Success
+	 */
+	public function detect() {
+		// Deprecated - the vendor libs are far more accurate and up to date
+		if (!$this->_config['engine']) {
+			if (isset($this->Controller->RequestHandler)) {
+				return $this->Controller->RequestHandler->isMobile();
+			}
+			return $this->Controller->request->is('mobile');
+		}
+		if (is_callable($this->_config['engine'])) {
+			return call_user_func($this->_config['engine']);
+		}
+		throw new CakeException(sprintf('Engine %s not available', $this->_config['engine']));
+	}
+
+}

+ 194 - 0
tests/TestCase/Controller/Component/MobileComponentTest.php

@@ -0,0 +1,194 @@
+<?php
+
+namespace Tools\Test\TestCase\Controller\Component;
+
+use Cake\Controller\ComponentRegistry;
+use Shim\Controller\Component\Component;
+use Tools\Controller\Controller;
+use Cake\Core\Configure;
+use Cake\Network\Request;
+use Cake\Network\Session;
+use Cake\Routing\DispatcherFactory;
+use Tools\TestSuite\TestCase;
+use Cake\Event\Event;
+
+/**
+ * Test MobileComponent
+ */
+class MobileComponentTest extends TestCase {
+
+	public $fixtures = ['core.sessions'];
+
+	/**
+	 * SetUp method
+	 *
+	 * @return void
+	 */
+	public function setUp() {
+		parent::setUp();
+
+		Request::addDetector('mobile', function ($request) {
+		    $detector = new \Detection\MobileDetect();
+		    return $detector->isMobile();
+		});
+		Request::addDetector('tablet', function ($request) {
+		    $detector = new \Detection\MobileDetect();
+		    return $detector->isTablet();
+		});
+
+		$this->event = new Event('Controller.beforeFilter');
+		$this->Controller = new MobileComponentTestController(new Request());
+		//$this->Controller->constructClasses();
+
+		$this->Controller->request->session()->delete('User');
+		Configure::delete('User');
+	}
+
+	/**
+	 * Tear-down method. Resets environment state.
+	 *
+	 * @return void
+	 */
+	public function tearDown() {
+		parent::tearDown();
+
+		unset($this->Controller->Mobile);
+		unset($this->Controller);
+	}
+
+	public function testDetect() {
+		$is = $this->Controller->Mobile->detect();
+		$this->assertFalse($is);
+
+		$this->Controller->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
+		$is = $this->Controller->Mobile->detect();
+		$this->assertTrue($is);
+	}
+
+	public function testMobileNotMobile() {
+		$this->Controller->Mobile->config('on', 'initialize');
+		$this->Controller->Mobile->initialize([]);
+		$this->assertFalse($this->Controller->Mobile->isMobile);
+	}
+
+	public function testMobileForceActivated() {
+		$this->Controller->request->query['mobile'] = 1;
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$session = $this->Controller->request->session()->read('User');
+		$this->assertSame(['mobile' => 1], $session);
+
+		$this->Controller->Mobile->setMobile();
+		$this->assertTrue($this->Controller->Mobile->setMobile);
+
+		$configure = Configure::read('User');
+		$this->assertSame(['isMobile' => 0, 'setMobile' => 1], $configure);
+		$this->assertEquals(['desktopUrl' => '/?mobile=0'], $this->Controller->viewVars);
+	}
+
+	public function testMobileForceDeactivated() {
+		$this->Controller->request->query['mobile'] = 0;
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$session = $this->Controller->request->session()->read('User');
+		$this->assertSame(['mobile' => 0], $session);
+
+		$this->Controller->Mobile->setMobile();
+		$configure = Configure::read('User');
+		$this->assertSame(['isMobile' => 0, 'setMobile' => 0], $configure);
+		$this->assertEquals(['mobileUrl' => '/?mobile=1'], $this->Controller->viewVars);
+	}
+
+	public function testMobileFakeMobile() {
+		$_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+
+		$this->Controller->Mobile->setMobile();
+		$configure = Configure::read('User');
+		$this->assertSame(['isMobile' => 1, 'setMobile' => 1], $configure);
+	}
+
+	public function testMobileFakeMobileForceDeactivated() {
+		$this->Controller->request->query['mobile'] = 0;
+		$_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$session = $this->Controller->request->session()->read('User');
+		$this->assertSame(['mobile' => 0], $session);
+
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+
+		$this->Controller->Mobile->setMobile();
+		$this->assertFalse($this->Controller->Mobile->setMobile);
+
+		$configure = Configure::read('User');
+		$this->assertSame(['isMobile' => 1, 'setMobile' => 0], $configure);
+	}
+
+	public function testMobileFakeMobileAuto() {
+		$this->Controller->Mobile->config('auto', true);
+		$_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+
+		$configure = Configure::read('User');
+		$this->assertSame(['isMobile' => 1, 'setMobile' => 1], $configure);
+		$this->assertTrue($this->Controller->Mobile->setMobile);
+	}
+
+	public function testMobileVendorEngineCake() {
+		$this->Controller->Mobile->config('engine', '');
+		$_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$session = $this->Controller->request->session()->read('User');
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+	}
+
+	public function testMobileCustomMobileInvalid() {
+		$_SERVER['HTTP_USER_AGENT'] = 'Some Foo device';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$this->assertFalse($this->Controller->Mobile->isMobile);
+	}
+
+	public function testMobileCustomMobile() {
+		$_SERVER['HTTP_USER_AGENT'] = 'Some Android device';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+	}
+
+	public function testMobileCustomMobileTablet() {
+		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+	}
+
+	public function testMobileEngineClosure() {
+		$closure = function() {
+			return $_SERVER['HTTP_USER_AGENT'] === 'Foo';
+		};
+		$this->Controller->Mobile->config('engine', $closure);
+		$_SERVER['HTTP_USER_AGENT'] = 'Foo';
+
+		$this->Controller->Mobile->beforeFilter($this->event);
+		$this->assertTrue($this->Controller->Mobile->isMobile);
+	}
+
+}
+
+class MobileComponentTestController extends Controller {
+
+	/**
+	 * Components property
+	 *
+	 * @var array
+	 */
+	public $components = ['RequestHandler', 'Tools.Mobile'];
+
+}