Router.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Memcache_UI\Core {
  3. class Router {
  4. function __construct(Context $context) {
  5. $this->context = $context;
  6. }
  7. function getInfo() {
  8. $ret = array(
  9. '^server/(\w+)/flush/(\w+)$' => array(
  10. 'page class' => 'ServerFlush',
  11. 'page arguments' => array('$1', '$2'),
  12. 'title callback' => 'title_server',
  13. 'title arguments' => 'page arguments',
  14. ),
  15. '^server/(\w+)/flush$' => array(
  16. 'page class' => 'ServerFlush',
  17. 'page arguments' => array('$1'),
  18. 'title callback' => 'title_server',
  19. 'title arguments' => 'page arguments',
  20. ),
  21. '^server/(\w+)/slab/(\d+)$' => array(
  22. 'page class' => 'SlabView',
  23. 'page arguments' => array('$1', '$2'),
  24. 'title callback' => 'title_slab',
  25. 'title arguments' => 'page arguments',
  26. ),
  27. '^server/(\w+)/key/(.+)/delete/(\w+)$' => array(
  28. 'page class' => 'VariableDeleteConfirm',
  29. 'page arguments' => array('$1', '$2', '$3'),
  30. 'title callback' => 'title_variable',
  31. 'title arguments' => 'page arguments',
  32. ),
  33. '^server/(\w+)/key/(.+)/delete$' => array(
  34. 'page class' => 'VariableDeleteForm',
  35. 'page arguments' => array('$1', '$2'),
  36. 'title callback' => 'title_variable',
  37. 'title arguments' => 'page arguments',
  38. ),
  39. '^server/(\w+)/key/(.+)/dump$' => array(
  40. 'page class' => 'VariableViewPhp',
  41. 'page arguments' => array('$1', '$2'),
  42. 'title callback' => 'title_variable',
  43. 'title arguments' => 'page arguments',
  44. ),
  45. '^server/(\w+)/key/(.+)/php$' => array(
  46. 'page class' => 'VariableViewPhp',
  47. 'page arguments' => array('$1', '$2'),
  48. 'title callback' => 'title_variable',
  49. 'title arguments' => 'page arguments',
  50. ),
  51. '^server/(\w+)/key/(.+)$' => array(
  52. 'page class' => 'VariableViewText',
  53. 'page arguments' => array('$1', '$2'),
  54. 'title callback' => 'title_variable',
  55. 'title arguments' => 'page arguments',
  56. ),
  57. '^slabs$' => array(
  58. 'page class' => 'SlabOverview',
  59. 'title' => 'Slabs per server',
  60. ),
  61. '' => array( // Catch-all regex
  62. 'page class' => 'Main',
  63. 'title' => 'Overview',
  64. ),
  65. );
  66. return $ret;
  67. }
  68. function getRoute() {
  69. $found = FALSE;
  70. $path = $this->context->getPath();
  71. $matches = array();
  72. $infoDefaults = array(
  73. 'page arguments' => array(),
  74. );
  75. foreach ($this->getInfo() as $regex => $info) {
  76. $regex = "@$regex@";
  77. $count = preg_match($regex, $path, $matches);
  78. if ($count) {
  79. $found = TRUE;
  80. break;
  81. }
  82. }
  83. if ($found) {
  84. $this->context->setMessage("Menu hit on $regex", LOG_DEBUG);
  85. $info = array_merge($infoDefaults, $info);
  86. $this->context->setMessage("Info: ". print_r($info, TRUE), LOG_DEBUG);
  87. $info['page class'] = 'Memcache_UI\Page\\' . $info['page class'];
  88. if (!empty($info['page arguments'])) {
  89. $regexes = array_fill(0, count($info['page arguments']), $regex);
  90. $paths = array_fill(0, count($info['page arguments']), $path);
  91. $info['page arguments'] = preg_replace($regexes, $info['page arguments'], $paths);
  92. }
  93. if (isset($info['title arguments']) && $info['title arguments'] == 'page arguments') {
  94. $info['title arguments'] = &$info['page arguments'];
  95. }
  96. $this->context->setMessage("Info resolved: ". print_r($info, TRUE), LOG_DEBUG);
  97. }
  98. else {
  99. $info = NULL;
  100. }
  101. return $info;
  102. }
  103. }
  104. }