pquery.url.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * pQuery plugin for parsing templates.
  4. *
  5. * @package pQuery
  6. */
  7. /**
  8. * @todo Documentation
  9. * @property string $content The template's content.
  10. */
  11. class pQueryUrl extends pQuery {
  12. static $accepts = array('string' => 'parse_url');
  13. /**
  14. * @see pQuery::$variable_alias
  15. * @var string|array
  16. */
  17. static $variable_alias = 'url';
  18. /**
  19. *
  20. *
  21. * @var string
  22. */
  23. static $handlers = array();
  24. /**
  25. * Remove slashes at the begin and end of the URL.
  26. *
  27. * @param string $url The URL to parse.
  28. */
  29. function parse_url($url) {
  30. return preg_replace('%(^/|/$)%', '', $url);
  31. }
  32. /**
  33. * Execute the handler of the first matching URL regex.
  34. *
  35. * @param string $path The path to add.
  36. * @param bool $relative Indicates whether the path is relative to the document root.
  37. */
  38. function handler() {
  39. foreach( self::$handlers as $pattern => $handler )
  40. if( preg_match($pattern, $this->url, $matches) )
  41. return call_user_func_array($handler, array_slice($matches, 1));
  42. //self::error('URL has no handler.', $this->url);
  43. }
  44. /**
  45. * Add a handler function to a URL match.
  46. *
  47. * @param string $pattern The URL pattern to match.
  48. * @param callback $handler The handler to execute when the pattern is matched.
  49. */
  50. static function add_handler($pattern, $handler) {
  51. is_callable($handler) || self::error('Handler "%s" is not callable.', $handler);
  52. self::$handlers["%$pattern%"] = $handler;
  53. }
  54. /**
  55. * Add a list of handler functions to regexes.
  56. *
  57. * @param array $handlers The list of handlers to add, with regexes as keys.
  58. */
  59. static function add_handlers($handlers) {
  60. foreach( $handlers as $pattern => $handler )
  61. self::add_handler($pattern, $handler);
  62. }
  63. }
  64. /**
  65. * Shortcut constructor for {@link pQueryUrl}.
  66. *
  67. * @param string $url
  68. * @returns pQueryUrl A new URL instance.
  69. */
  70. function _url($url) {
  71. return pQuery::create('url', $url);
  72. }
  73. /*
  74. * Add plugin to pQuery
  75. */
  76. __p::extend('pQueryUrl', 'url');
  77. ?>