PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Routing/Route/ApiRoute.php

https://github.com/kareypowell/croogo
PHP | 62 lines | 31 code | 6 blank | 25 comment | 8 complexity | 0110815a15c0b2a1d6c10158a67972ac MD5 | raw file
  1. <?php
  2. App::uses('CakeRoute', 'Routing/Route');
  3. /**
  4. * API Route class
  5. *
  6. * @package Croogo.Croogo.Routing.Route
  7. * @since 1.6
  8. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  9. * @link http://www.croogo.org
  10. */
  11. class ApiRoute extends CakeRoute {
  12. public function __construct($template, $defaults = array(), $options = array()) {
  13. $options = Hash::merge(array(
  14. 'api' => Configure::read('Croogo.Api.path'),
  15. 'prefix' => 'v[0-9.]+',
  16. ), $options);
  17. parent::__construct($template, $defaults, $options);
  18. }
  19. /**
  20. * Checks wether URL is an API route
  21. *
  22. * If the route is not an API route, we return false and let the next parser
  23. * to handle it.
  24. *
  25. * @param string $url The URL to attempt to parse.
  26. * @return mixed Boolean false on failure, otherwise an array or parameters
  27. * @see CakeRoute::parse()
  28. */
  29. public function parse($url) {
  30. $parsed = parent::parse($url);
  31. if (!isset($parsed['api']) || !isset($parsed['prefix'])) {
  32. return false;
  33. }
  34. $parsed['prefix'] = str_replace('.', '_', $parsed['prefix']);
  35. return $parsed;
  36. }
  37. /**
  38. * Checks if an URL array matches this route instance
  39. *
  40. * @param array $url An array of parameters to check matching with.
  41. * @return mixed Either a string URL for the parameters if they match or false.
  42. * @see CakeRoute::match()
  43. */
  44. public function match($url) {
  45. if (isset($url['prefix']) && isset($url['action'])) {
  46. $prefix = $url['prefix'];
  47. $url['prefix'] = str_replace('_', '.', $url['prefix']);
  48. $url['action'] = str_replace($prefix . '_', '', $url['action']);
  49. }
  50. $match = parent::match($url);
  51. if ($match && isset($url['action']) && $url['action'] == 'index') {
  52. $match = str_replace('/index', '', $match);
  53. }
  54. return $match;
  55. }
  56. }