*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Controller / Router / Route / Module.php
blobfa20b1d64be5c8492f0db45d0c209c171736970c
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Controller
17 * @subpackage Router
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @version $Id: Module.php 16541 2009-07-07 06:59:03Z bkarwin $
20 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_Controller_Router_Route_Abstract */
24 require_once 'Zend/Controller/Router/Route/Abstract.php';
26 /**
27 * Module Route
29 * Default route for module functionality
31 * @package Zend_Controller
32 * @subpackage Router
33 * @copyright Copyright (c) 2005-2009 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
37 class Zend_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Abstract
39 /**
40 * URI delimiter
42 const URI_DELIMITER = '/';
44 /**
45 * Default values for the route (ie. module, controller, action, params)
46 * @var array
48 protected $_defaults;
50 protected $_values = array();
51 protected $_moduleValid = false;
52 protected $_keysSet = false;
54 /**#@+
55 * Array keys to use for module, controller, and action. Should be taken out of request.
56 * @var string
58 protected $_moduleKey = 'module';
59 protected $_controllerKey = 'controller';
60 protected $_actionKey = 'action';
61 /**#@-*/
63 /**
64 * @var Zend_Controller_Dispatcher_Interface
66 protected $_dispatcher;
68 /**
69 * @var Zend_Controller_Request_Abstract
71 protected $_request;
73 public function getVersion() {
74 return 1;
77 /**
78 * Instantiates route based on passed Zend_Config structure
80 public static function getInstance(Zend_Config $config)
82 $frontController = Zend_Controller_Front::getInstance();
84 $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
85 $dispatcher = $frontController->getDispatcher();
86 $request = $frontController->getRequest();
88 return new self($defs, $dispatcher, $request);
91 /**
92 * Constructor
94 * @param array $defaults Defaults for map variables with keys as variable names
95 * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
96 * @param Zend_Controller_Request_Abstract $request Request object
98 public function __construct(array $defaults = array(),
99 Zend_Controller_Dispatcher_Interface $dispatcher = null,
100 Zend_Controller_Request_Abstract $request = null)
102 $this->_defaults = $defaults;
104 if (isset($request)) {
105 $this->_request = $request;
108 if (isset($dispatcher)) {
109 $this->_dispatcher = $dispatcher;
114 * Set request keys based on values in request object
116 * @return void
118 protected function _setRequestKeys()
120 if (null !== $this->_request) {
121 $this->_moduleKey = $this->_request->getModuleKey();
122 $this->_controllerKey = $this->_request->getControllerKey();
123 $this->_actionKey = $this->_request->getActionKey();
126 if (null !== $this->_dispatcher) {
127 $this->_defaults += array(
128 $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
129 $this->_actionKey => $this->_dispatcher->getDefaultAction(),
130 $this->_moduleKey => $this->_dispatcher->getDefaultModule()
134 $this->_keysSet = true;
138 * Matches a user submitted path. Assigns and returns an array of variables
139 * on a successful match.
141 * If a request object is registered, it uses its setModuleName(),
142 * setControllerName(), and setActionName() accessors to set those values.
143 * Always returns the values as an array.
145 * @param string $path Path used to match against this routing map
146 * @return array An array of assigned values or a false on a mismatch
148 public function match($path, $partial = false)
150 $this->_setRequestKeys();
152 $values = array();
153 $params = array();
155 if (!$partial) {
156 $path = trim($path, self::URI_DELIMITER);
157 } else {
158 $matchedPath = $path;
161 if ($path != '') {
162 $path = explode(self::URI_DELIMITER, $path);
164 if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
165 $values[$this->_moduleKey] = array_shift($path);
166 $this->_moduleValid = true;
169 if (count($path) && !empty($path[0])) {
170 $values[$this->_controllerKey] = array_shift($path);
173 if (count($path) && !empty($path[0])) {
174 $values[$this->_actionKey] = array_shift($path);
177 if ($numSegs = count($path)) {
178 for ($i = 0; $i < $numSegs; $i = $i + 2) {
179 $key = urldecode($path[$i]);
180 $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
181 $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
186 if ($partial) {
187 $this->setMatchedPath($matchedPath);
190 $this->_values = $values + $params;
192 return $this->_values + $this->_defaults;
196 * Assembles user submitted parameters forming a URL path defined by this route
198 * @param array $data An array of variable and value pairs used as parameters
199 * @param bool $reset Weither to reset the current params
200 * @return string Route path with user submitted parameters
202 public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
204 if (!$this->_keysSet) {
205 $this->_setRequestKeys();
208 $params = (!$reset) ? $this->_values : array();
210 foreach ($data as $key => $value) {
211 if ($value !== null) {
212 $params[$key] = $value;
213 } elseif (isset($params[$key])) {
214 unset($params[$key]);
218 $params += $this->_defaults;
220 $url = '';
222 if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
223 if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
224 $module = $params[$this->_moduleKey];
227 unset($params[$this->_moduleKey]);
229 $controller = $params[$this->_controllerKey];
230 unset($params[$this->_controllerKey]);
232 $action = $params[$this->_actionKey];
233 unset($params[$this->_actionKey]);
235 foreach ($params as $key => $value) {
236 if (is_array($value)) {
237 foreach ($value as $arrayValue) {
238 if ($encode) $arrayValue = urlencode($arrayValue);
239 $url .= '/' . $key;
240 $url .= '/' . $arrayValue;
242 } else {
243 if ($encode) $value = urlencode($value);
244 $url .= '/' . $key;
245 $url .= '/' . $value;
249 if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
250 if ($encode) $action = urlencode($action);
251 $url = '/' . $action . $url;
254 if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
255 if ($encode) $controller = urlencode($controller);
256 $url = '/' . $controller . $url;
259 if (isset($module)) {
260 if ($encode) $module = urlencode($module);
261 $url = '/' . $module . $url;
264 return ltrim($url, self::URI_DELIMITER);
268 * Return a single parameter of route's defaults
270 * @param string $name Array key of the parameter
271 * @return string Previously set default
273 public function getDefault($name) {
274 if (isset($this->_defaults[$name])) {
275 return $this->_defaults[$name];
280 * Return an array of defaults
282 * @return array Route defaults
284 public function getDefaults() {
285 return $this->_defaults;