fogproject  1.5.9
About: FOG is a Linux-based network computer cloning solution for Windows, Mac OSX and various Linux distributions that ties together a few open-source tools with a PHP-based web interface. FOG doesn’t use any boot disks, or CDs; everything is done via TFTP and PXE.
  Fossies Dox: fogproject-1.5.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

altorouter.class.php
Go to the documentation of this file.
1 <?php
15 //namespace AltoRouter;
28 {
34  protected $routes = array();
40  protected $namedRoutes = array();
47  protected $basePath = '';
53  protected $ignoreCase = false;
59  protected $matchTypes = array(
60  'i' => '[0-9]++',
61  'a' => '[0-9A-Za-z]++',
62  'h' => '[0-9A-Fa-f]++',
63  '*' => '.+?',
64  '**' => '.++',
65  '' => '[^/\.]++'
66  );
72  protected $defaultParams = array();
78  protected $transformers = array();
90  public function __construct(
91  $routes = array(),
92  $basePath = '',
93  $matchTypes = array(),
94  $defaultParams = array(),
95  $ignoreCase = false
96  ) {
97  $this->addRoutes($routes);
98  $this->setBasePath($basePath);
99  $this->addMatchTypes($matchTypes);
101  $this->setIgnoreCase($ignoreCase);
102  }
119  public function __call(
120  $name,
121  $arguments
122  ) {
123  $name = strtolower($name);
124  $validTypes = array(
125  'get' => 'GET',
126  'put' => 'PUT',
127  'head' => 'HEAD',
128  'post' => 'POST',
129  'patch' => 'PATCH',
130  'delete' => 'DELETE',
131  'options' => 'OPTIONS'
132  );
133  // If method type is invalid don't do anything.
134  if (!isset($validTypes[$name])) {
135  return;
136  }
137  // Prepend the type to our arguments to pass to the map.
138  array_unshift(
139  $arguments,
140  $validTypes[$name]
141  );
142  // Pass to the map method.
143  call_user_func_array(
144  array($this, 'map'),
145  $arguments
146  );
147  return $this;
148  }
156  public function getRoutes()
157  {
158  return $this->routes;
159  }
165  public function getNamedRoutes()
166  {
167  return $this->namedRoutes;
168  }
174  public function getBasePath()
175  {
176  return $this->basePath;
177  }
183  public function getDefaultParams()
184  {
185  return $this->getDefaultParams();
186  }
192  public function getIgnoreCase()
193  {
194  return $this->ignoreCase;
195  }
211  public function addRoutes($routes)
212  {
213  if (!is_array($routes)
214  && !$routes instanceof \Traversable
215  ) {
216  $msg
217  = 'Routes should be an array or an instance of Traversable';
218  if (!defined('HHVM_VERSION')) {
219  $msg = _('Routes should be an array or an instance of Traversable');
220  }
221  throw new \Exception($msg);
222  }
223  foreach ($routes as $route) {
224  call_user_func_array(array($this, 'map'), $route);
225  }
226  return $this;
227  }
236  public function setBasePath($basePath)
237  {
238  $this->basePath = $basePath;
239  return $this;
240  }
249  public function setIgnoreCase($ignoreCase)
250  {
251  $this->ignoreCase = (bool)$ignoreCase;
252  return $this;
253  }
261  public function addMatchTypes($matchTypes)
262  {
263  $this->matchTypes = array_merge(
264  $this->matchTypes,
266  );
267  return $this;
268  }
277  {
278  $this->defaultParams = array_merge(
279  $this->defaultParams,
281  );
282  return $this;
283  }
293  public function addTransformer($matchType, \AltoTransformer $transformer)
294  {
295  $this->transformers[$matchType] = $transformer;
296  return $this;
297  }
316  public function map(
317  $method,
318  $route,
319  $target,
320  $name = null
321  ) {
322  foreach (explode('|', $method) as $method) {
323  if (!isset($this->routes[$method])) {
324  $this->routes[$method] = array();
325  }
326  $this->routes[$method][] = array($route, $target, $name);
327  unset($method);
328  }
329  if ($name) {
330  if (isset($this->namedRoutes[$name])) {
331  $msg = sprintf(
332  "%s '%s'",
333  'Can not redeclare route',
334  $name
335  );
336  if (!defined('HHVM_VERSION')) {
337  $msg = sprintf(
338  "%s '%s'",
339  _('Can not redeclare route'),
340  $name
341  );
342  }
343  throw new \Exception($msg);
344  }
345  $this->namedRoutes[$name] = $route;
346  }
347  return $this;
348  }
361  public function generate(
362  $routeName,
363  array $params = array()
364  ) {
365  // Check if named route exists
366  if (!isset($this->namedRoutes[$routeName])) {
367  throw new \Exception(
368  "Route '{$routeName}' does not exist."
369  );
370  }
371  // Replace named parameters
372  $route = $this->namedRoutes[$routeName];
373  // prepend base path to route url again
374  $url = $this->basePath . $route;
375  // merge with default params.
376  $params = array_merge(
377  $this->defaultParams,
378  $params
379  );
380  $pattern = '`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`';
381  if (preg_match_all($pattern, $route, $matches, PREG_SET_ORDER)) {
382  foreach ($matches as $index => $match) {
383  list(
384  $block,
385  $pre,
386  $type,
387  $param,
388  $optional
389  ) = $match;
390  if ($pre) {
391  $block = substr($block, 1);
392  }
393  if (isset($this->transformers[$type])) {
394  $params[$param] = $this->transformers[$type]
395  ->toUrl($params[$param]);
396  }
397  if (isset($params[$param])) {
398  // Part is found, replace for param value.
399  $url = str_replace(
400  $block,
401  $params[$param],
402  $url
403  );
404  } elseif ($optional && $index !== 0) {
405  $url = str_replace(
406  $pre . $block,
407  '',
408  $url
409  );
410  } else {
411  $url = str_replace(
412  $block,
413  '',
414  $url
415  );
416  }
417  }
418  }
419  return $url;
420  }
431  public function match(
432  $requestUrl = null,
433  $requestMethod = null
434  ) {
435  $params = array();
436  $match = false;
437  // set Request Url if it isn't passed as parameter
438  if (null === $requestUrl) {
439  $requestUrl = $this->getRequestURI() ?: '/';
440  }
441  // strip base path from request url
442  $requestUrl = substr(
443  $requestUrl,
444  strlen($this->basePath)
445  );
446  // Strip query string (?a=b) from Request Url
447  if (false !== ($strpos = strpos($requestUrl, '?'))) {
448  $requestUrl = substr($requestUrl, 0, $strpos);
449  }
450  // set Request Method if it isn't passed as a parameter
451  if (null === $requestMethod) {
452  $requestMethod = $this->getRequestMethod() ?: 'GET';
453  }
454  if (empty($this->routes[$requestMethod])) {
455  return false;
456  }
457  foreach ($this->routes[$requestMethod] as $handler) {
458  list(
459  $route,
460  $target,
461  $name
462  ) = $handler;
463  unset($handler);
464  if ('*' === $route) {
465  // * wildcard (matches all)
466  $match = true;
467  } elseif (isset($route[0])
468  && $route[0] === '@'
469  ) {
470  // @ regex delimiter
471  $pattern = '`'
472  . substr($route, 1)
473  . '`u'
474  . ($this->ignoreCase ? 'i' : null);
475  $match = (1 === preg_match($pattern, $requestUrl, $params));
476  } elseif (false === ($position = strpos($route, '['))) {
477  // No params in url, do string comparison
478  $match = 0 === strcmp($requestUrl, $route);
479  } else {
480  // Compare longest non-param string with url
481  if (0 !== strncmp($requestUrl, $route, $position)) {
482  continue;
483  }
484  $regex = $this->_compileRoute($route);
485  $match = (1 === preg_match($regex['regex'], $requestUrl, $params));
486  }
487  if ($match) {
488  if ($params) {
489  $routeisarr = is_array($route);
490  foreach ($params as $key => $value) {
491  if (is_numeric($key)) {
492  unset($params[$key]);
493  continue;
494  }
495  if (!$routeisarr) {
496  continue;
497  }
498  $type = $route['types'][$key];
499  if (isset($this->transformers[$type])) {
500  $params[$key]
501  = $this->transformers[$type]->fromUrl($value);
502  }
503  unset($values);
504  }
515  $params['method'] = $requestMethod;
516  }
517  $result = $this->getMatchedResult(
518  $target,
519  $params,
520  $name
521  );
522  if ($result) {
523  return $result;
524  }
525  }
526  }
527  return false;
528  }
536  private function _compileRoute($route)
537  {
538  $pattern = '`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`';
539  $route = array(
540  'regex' => sprintf(
541  '`^%s$`u%s',
542  $route,
543  ($this->ignoreCase ? 'i' : '')
544  ),
545  'types' => array()
546  );
547  if (preg_match_all($pattern, $route['regex'], $matches, PREG_SET_ORDER)) {
549  foreach ($matches as $match) {
550  list(
551  $block,
552  $pre,
553  $type,
554  $param,
555  $optional,
556  ) = $match;
557  unset($match);
558  $optional = ('' !== $optional ? '?' : null);
559  $route['types'][$param] = $type;
560  if (isset($matchTypes[$type])) {
561  $type = $matchTypes[$type];
562  }
563  if ('.' === $pre) {
564  $pre = '\.';
565  }
566  // Older versions of PCRE require the 'P' in (?P<named>)
567  $pattern = '(?:'
568  . ('' !== $pre ? $pre.'+' : null)
569  . '('
570  . ('' !== $param ? "?P<$param>" : null)
571  . $type
572  . ')'
573  . $optional
574  . '(/+|))'
575  . $optional;
576  $route['regex'] = str_replace($block, $pattern, $route['regex']);
577  }
578  }
579  return $route;
580  }
586  protected function getRequestURI()
587  {
588  return filter_input(INPUT_SERVER, 'REQUEST_URI');
589  }
595  protected function getRequestMethod()
596  {
597  return filter_input(INPUT_SERVER, 'REQUEST_METHOD');
598  }
610  protected function getMatchedResult(
611  $target,
612  $params,
613  $name
614  ) {
615  return array(
616  'target' => $target,
617  'params' => $params,
618  'name' => $name
619  );
620  }
621 }
AltoRouter\getMatchedResult
getMatchedResult( $target, $params, $name)
Definition: altorouter.class.php:610
AltoRouter\getRoutes
getRoutes()
Definition: altorouter.class.php:156
AltoRouter\$basePath
$basePath
Definition: altorouter.class.php:47
AltoRouter\getIgnoreCase
getIgnoreCase()
Definition: altorouter.class.php:192
AltoRouter\$defaultParams
$defaultParams
Definition: altorouter.class.php:72
AltoRouter\__call
__call( $name, $arguments)
Definition: altorouter.class.php:119
AltoRouter\$ignoreCase
$ignoreCase
Definition: altorouter.class.php:53
AltoRouter\getDefaultParams
getDefaultParams()
Definition: altorouter.class.php:183
AltoRouter\getRequestURI
getRequestURI()
Definition: altorouter.class.php:586
AltoRouter\_compileRoute
_compileRoute($route)
Definition: altorouter.class.php:536
AltoRouter\__construct
__construct( $routes=array(), $basePath='', $matchTypes=array(), $defaultParams=array(), $ignoreCase=false)
Definition: altorouter.class.php:90
AltoRouter\match
match( $requestUrl=null, $requestMethod=null)
Definition: altorouter.class.php:431
AltoRouter\$transformers
$transformers
Definition: altorouter.class.php:78
AltoRouter\addTransformer
addTransformer($matchType, \AltoTransformer $transformer)
Definition: altorouter.class.php:293
AltoRouter
AltoRouter\getRequestMethod
getRequestMethod()
Definition: altorouter.class.php:595
AltoRouter\getBasePath
getBasePath()
Definition: altorouter.class.php:174
AltoRouter\addDefaultParams
addDefaultParams($defaultParams)
Definition: altorouter.class.php:276
AltoRouter\$routes
$routes
Definition: altorouter.class.php:34
AltoRouter\generate
generate( $routeName, array $params=array())
Definition: altorouter.class.php:361
AltoRouter\setIgnoreCase
setIgnoreCase($ignoreCase)
Definition: altorouter.class.php:249
AltoRouter\setBasePath
setBasePath($basePath)
Definition: altorouter.class.php:236
AltoRouter\getNamedRoutes
getNamedRoutes()
Definition: altorouter.class.php:165
AltoRouter\map
map( $method, $route, $target, $name=null)
Definition: altorouter.class.php:316
AltoRouter\addMatchTypes
addMatchTypes($matchTypes)
Definition: altorouter.class.php:261
$msg
if($Host->isValid()) $msg
Definition: hostnameloop.php:42
AltoTransformer
Definition: altotransformer.class.php:23
$url
$url
Definition: kernelvers.php:27
AltoRouter\addRoutes
addRoutes($routes)
Definition: altorouter.class.php:211
AltoRouter\$matchTypes
$matchTypes
Definition: altorouter.class.php:59
AltoRouter\$namedRoutes
$namedRoutes
Definition: altorouter.class.php:40