PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/zf/library/Zend/Controller/Router/Route/Module.php

http://github.com/eryx/php-framework-benchmark
PHP | 284 lines | 150 code | 43 blank | 91 comment | 35 complexity | 856248dd771e4a6f94b8e5b61017d00c MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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_Controller
 17 * @subpackage Router
 18 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @version    $Id: Module.php 24182 2011-07-03 13:43:05Z adamlundrigan $
 20 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 21 */
 22
 23/** Zend_Controller_Router_Route_Abstract */
 24require_once 'Zend/Controller/Router/Route/Abstract.php';
 25
 26/**
 27 * Module Route
 28 *
 29 * Default route for module functionality
 30 *
 31 * @package    Zend_Controller
 32 * @subpackage Router
 33 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 35 * @see        http://manuals.rubyonrails.com/read/chapter/65
 36 */
 37class Zend_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Abstract
 38{
 39    /**
 40     * Default values for the route (ie. module, controller, action, params)
 41     * @var array
 42     */
 43    protected $_defaults;
 44
 45    protected $_values      = array();
 46    protected $_moduleValid = false;
 47    protected $_keysSet     = false;
 48
 49    /**#@+
 50     * Array keys to use for module, controller, and action. Should be taken out of request.
 51     * @var string
 52     */
 53    protected $_moduleKey     = 'module';
 54    protected $_controllerKey = 'controller';
 55    protected $_actionKey     = 'action';
 56    /**#@-*/
 57
 58    /**
 59     * @var Zend_Controller_Dispatcher_Interface
 60     */
 61    protected $_dispatcher;
 62
 63    /**
 64     * @var Zend_Controller_Request_Abstract
 65     */
 66    protected $_request;
 67
 68    public function getVersion() {
 69        return 1;
 70    }
 71
 72    /**
 73     * Instantiates route based on passed Zend_Config structure
 74     */
 75    public static function getInstance(Zend_Config $config)
 76    {
 77        $frontController = Zend_Controller_Front::getInstance();
 78
 79        $defs       = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
 80        $dispatcher = $frontController->getDispatcher();
 81        $request    = $frontController->getRequest();
 82
 83        return new self($defs, $dispatcher, $request);
 84    }
 85
 86    /**
 87     * Constructor
 88     *
 89     * @param array $defaults Defaults for map variables with keys as variable names
 90     * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
 91     * @param Zend_Controller_Request_Abstract $request Request object
 92     */
 93    public function __construct(array $defaults = array(),
 94                Zend_Controller_Dispatcher_Interface $dispatcher = null,
 95                Zend_Controller_Request_Abstract $request = null)
 96    {
 97        $this->_defaults = $defaults;
 98
 99        if (isset($request)) {
100            $this->_request = $request;
101        }
102
103        if (isset($dispatcher)) {
104            $this->_dispatcher = $dispatcher;
105        }
106    }
107
108    /**
109     * Set request keys based on values in request object
110     *
111     * @return void
112     */
113    protected function _setRequestKeys()
114    {
115        if (null !== $this->_request) {
116            $this->_moduleKey     = $this->_request->getModuleKey();
117            $this->_controllerKey = $this->_request->getControllerKey();
118            $this->_actionKey     = $this->_request->getActionKey();
119        }
120
121        if (null !== $this->_dispatcher) {
122            $this->_defaults += array(
123                $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
124                $this->_actionKey     => $this->_dispatcher->getDefaultAction(),
125                $this->_moduleKey     => $this->_dispatcher->getDefaultModule()
126            );
127        }
128
129        $this->_keysSet = true;
130    }
131
132    /**
133     * Matches a user submitted path. Assigns and returns an array of variables
134     * on a successful match.
135     *
136     * If a request object is registered, it uses its setModuleName(),
137     * setControllerName(), and setActionName() accessors to set those values.
138     * Always returns the values as an array.
139     *
140     * @param string $path Path used to match against this routing map
141     * @return array An array of assigned values or a false on a mismatch
142     */
143    public function match($path, $partial = false)
144    {
145        $this->_setRequestKeys();
146
147        $values = array();
148        $params = array();
149
150        if (!$partial) {
151            $path = trim($path, self::URI_DELIMITER);
152        } else {
153            $matchedPath = $path;
154        }
155
156        if ($path != '') {
157            $path = explode(self::URI_DELIMITER, $path);
158
159            if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
160                $values[$this->_moduleKey] = array_shift($path);
161                $this->_moduleValid = true;
162            }
163
164            if (count($path) && !empty($path[0])) {
165                $values[$this->_controllerKey] = array_shift($path);
166            }
167
168            if (count($path) && !empty($path[0])) {
169                $values[$this->_actionKey] = array_shift($path);
170            }
171
172            if ($numSegs = count($path)) {
173                for ($i = 0; $i < $numSegs; $i = $i + 2) {
174                    $key = urldecode($path[$i]);
175                    $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
176                    $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
177                }
178            }
179        }
180
181        if ($partial) {
182            $this->setMatchedPath($matchedPath);
183        }
184
185        $this->_values = $values + $params;
186
187        return $this->_values + $this->_defaults;
188    }
189
190    /**
191     * Assembles user submitted parameters forming a URL path defined by this route
192     *
193     * @param array $data An array of variable and value pairs used as parameters
194     * @param bool $reset Weither to reset the current params
195     * @return string Route path with user submitted parameters
196     */
197    public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
198    {
199        if (!$this->_keysSet) {
200            $this->_setRequestKeys();
201        }
202
203        $params = (!$reset) ? $this->_values : array();
204
205        foreach ($data as $key => $value) {
206            if ($value !== null) {
207                $params[$key] = $value;
208            } elseif (isset($params[$key])) {
209                unset($params[$key]);
210            }
211        }
212
213        $params += $this->_defaults;
214
215        $url = '';
216
217        if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
218            if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
219                $module = $params[$this->_moduleKey];
220            }
221        }
222        unset($params[$this->_moduleKey]);
223
224        $controller = $params[$this->_controllerKey];
225        unset($params[$this->_controllerKey]);
226
227        $action = $params[$this->_actionKey];
228        unset($params[$this->_actionKey]);
229
230        foreach ($params as $key => $value) {
231            $key = ($encode) ? urlencode($key) : $key;
232            if (is_array($value)) {
233                foreach ($value as $arrayValue) {
234                    $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
235                    $url .= self::URI_DELIMITER . $key;
236                    $url .= self::URI_DELIMITER . $arrayValue;
237                }
238            } else {
239                if ($encode) $value = urlencode($value);
240                $url .= self::URI_DELIMITER . $key;
241                $url .= self::URI_DELIMITER . $value;
242            }
243        }
244
245        if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
246            if ($encode) $action = urlencode($action);
247            $url = self::URI_DELIMITER . $action . $url;
248        }
249
250        if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
251            if ($encode) $controller = urlencode($controller);
252            $url = self::URI_DELIMITER . $controller . $url;
253        }
254
255        if (isset($module)) {
256            if ($encode) $module = urlencode($module);
257            $url = self::URI_DELIMITER . $module . $url;
258        }
259
260        return ltrim($url, self::URI_DELIMITER);
261    }
262
263    /**
264     * Return a single parameter of route's defaults
265     *
266     * @param string $name Array key of the parameter
267     * @return string Previously set default
268     */
269    public function getDefault($name) {
270        if (isset($this->_defaults[$name])) {
271            return $this->_defaults[$name];
272        }
273    }
274
275    /**
276     * Return an array of defaults
277     *
278     * @return array Route defaults
279     */
280    public function getDefaults() {
281        return $this->_defaults;
282    }
283
284}