PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Http/UserAgent/Features/Adapter/WurflApi.php

https://bitbucket.org/hamidrezas/melobit
PHP | 103 lines | 55 code | 9 blank | 39 comment | 7 complexity | 56694943b38440a066f01a386917fa5d MD5 | raw file
Possible License(s): AGPL-1.0
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Http
 17 * @subpackage UserAgent
 18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 */
 21
 22/**
 23 * Zend_Http_UserAgent_Features_Adapter_Interface
 24 */
 25require_once 'Zend/Http/UserAgent/Features/Adapter.php';
 26
 27/**
 28 * Features adapter build with the official WURFL PHP API
 29 * See installation instruction here : http://wurfl.sourceforge.net/nphp/
 30 * Download : http://sourceforge.net/projects/wurfl/files/WURFL PHP/1.1/wurfl-php-1.1.tar.gz/download
 31 *
 32 * @package    Zend_Http
 33 * @subpackage UserAgent
 34 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 35 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 36 */
 37class Zend_Http_UserAgent_Features_Adapter_WurflApi
 38    implements Zend_Http_UserAgent_Features_Adapter
 39{
 40    const DEFAULT_API_VERSION = '1.1';
 41
 42    /**
 43     * Get features from request
 44     *
 45     * @param  array $request $_SERVER variable
 46     * @return array
 47     */
 48    public static function getFromRequest($request, array $config)
 49    {
 50        if (!isset($config['wurflapi'])) {
 51            require_once 'Zend/Http/UserAgent/Features/Exception.php';
 52            throw new Zend_Http_UserAgent_Features_Exception('"wurflapi" configuration is not defined');
 53        }
 54
 55        $config = $config['wurflapi'];
 56
 57        if (empty($config['wurfl_lib_dir'])) {
 58            require_once 'Zend/Http/UserAgent/Features/Exception.php';
 59            throw new Zend_Http_UserAgent_Features_Exception('The "wurfl_lib_dir" parameter is not defined');
 60        }
 61        if (empty($config['wurfl_config_file']) && empty($config['wurfl_config_array'])) {
 62            require_once 'Zend/Http/UserAgent/Features/Exception.php';
 63            throw new Zend_Http_UserAgent_Features_Exception('The "wurfl_config_file" parameter is not defined');
 64        }
 65
 66        if (empty($config['wurfl_api_version'])) {
 67            $config['wurfl_api_version'] = self::DEFAULT_API_VERSION;
 68        }
 69
 70        switch ($config['wurfl_api_version']) {
 71            case '1.0':
 72                // Zend_Http_UserAgent::$config['wurfl_config_file'] must be an XML file
 73                require_once ($config['wurfl_lib_dir'] . 'WURFLManagerProvider.php');
 74                $wurflManager = WURFL_WURFLManagerProvider::getWURFLManager(Zend_Http_UserAgent::$config['wurfl_config_file']);
 75                break;
 76            case '1.1':
 77                require_once ($config['wurfl_lib_dir'] . 'Application.php');
 78                if (!empty($config['wurfl_config_file'])) {
 79                    $wurflConfig = WURFL_Configuration_ConfigFactory::create($config['wurfl_config_file']);
 80                } elseif (!empty($config['wurfl_config_array'])) {
 81                    $c            = $config['wurfl_config_array'];
 82                    $wurflConfig  = new WURFL_Configuration_InMemoryConfig();
 83                    $wurflConfig->wurflFile($c['wurfl']['main-file'])
 84                                ->wurflPatch($c['wurfl']['patches'])
 85                                ->persistence($c['persistence']['provider'], $c['persistence']['dir']);
 86                }
 87
 88                $wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);
 89                $wurflManager = $wurflManagerFactory->create();
 90                break;
 91            default:
 92                require_once 'Zend/Http/UserAgent/Features/Exception.php';
 93                throw new Zend_Http_UserAgent_Features_Exception(sprintf(
 94                    'Unknown API version "%s"',
 95                    $config['wurfl_api_version']
 96                ));
 97        }
 98
 99        $device   = $wurflManager->getDeviceForHttpRequest(array_change_key_case($request, CASE_UPPER));
100        $features = $device->getAllCapabilities();
101        return $features;
102    }
103}