UrlCacheManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * This class will statically hold in memory url's indexed by a custom hash
  4. *
  5. * @licence MIT
  6. * @modified Mark Scherer
  7. * - now easier to integrate
  8. * - optimization for `pageFiles` (still stores urls with only controller/action keys in global file)
  9. * - can handle legacy `prefix` urls
  10. *
  11. * 2012-02-13 ms
  12. */
  13. class UrlCacheManager {
  14. /**
  15. * Holds all generated urls so far by the application indexed by a custom hash
  16. *
  17. */
  18. public static $cache = array();
  19. /**
  20. * Holds all generated urls so far by the application indexed by a custom hash
  21. *
  22. */
  23. public static $cachePage = array();
  24. /**
  25. * Holds all generated urls so far by the application indexed by a custom hash
  26. *
  27. */
  28. public static $extras = array();
  29. /**
  30. * type for the current set (triggered by last get)
  31. */
  32. public static $type = 'cache';
  33. /**
  34. * key for current get/set
  35. */
  36. public static $key = null;
  37. /**
  38. * cache key for pageFiles
  39. */
  40. public static $cacheKey = 'url_map';
  41. /**
  42. * cache key for pageFiles
  43. */
  44. public static $cachePageKey = null;
  45. /**
  46. * params that will always be present and will determine the global cache if pageFiles is used
  47. */
  48. public static $paramFields = array('controller', 'plugin', 'action', 'prefix');
  49. /**
  50. * should be called in beforeRender()
  51. *
  52. */
  53. public static function init(View $View) {
  54. $params = $View->request->params;
  55. if (Configure::read('UrlCache.pageFiles')) {
  56. $cachePageKey = '_misc';
  57. if (is_object($View)) {
  58. $path = $View->request->here;
  59. if ($path == '/') {
  60. $path = 'uc_homepage';
  61. } else {
  62. $path = strtolower(Inflector::slug($path));
  63. }
  64. if (empty($path)) {
  65. $path = 'uc_error';
  66. }
  67. $cachePageKey = '_' . $path;
  68. }
  69. self::$cachePageKey = self::$cacheKey . $cachePageKey;
  70. self::$cachePage = Cache::read(self::$cachePageKey, '_cake_core_');
  71. }
  72. self::$cache = Cache::read(self::$cacheKey, '_cake_core_');
  73. # still old "prefix true/false" syntax?
  74. if (Configure::read('UrlCache.verbosePrefixes')) {
  75. unset(self::$paramFields[3]);
  76. self::$paramFields = array_merge(self::$paramFields, (array)Configure::read('Routing.prefixes'));
  77. }
  78. self::$extras = array_intersect_key($params, array_combine(self::$paramFields, self::$paramFields));
  79. $defaults = array();
  80. foreach (self::$paramFields as $field) {
  81. $defaults[$field] = '';
  82. }
  83. self::$extras = array_merge($defaults, self::$extras);
  84. }
  85. /**
  86. * should be called in afterLayout()
  87. *
  88. */
  89. public static function finalize() {
  90. Cache::write(self::$cacheKey, self::$cache, '_cake_core_');
  91. if (Configure::read('UrlCache.pageFiles') && !empty(self::$cachePage)) {
  92. Cache::write(self::$cachePageKey, self::$cachePage, '_cake_core_');
  93. }
  94. }
  95. /**
  96. * Returns the stored url if it was already generated, false otherwise
  97. *
  98. * @param string $key
  99. * @return mixed
  100. */
  101. public static function get($url, $full) {
  102. $keyUrl = $url;
  103. if (is_array($keyUrl)) {
  104. $keyUrl += self::$extras;
  105. # prevent different hashs on different orders
  106. ksort($keyUrl, SORT_STRING);
  107. # prevent different hashs on different types (int/string/bool)
  108. foreach ($keyUrl as $key => $val) {
  109. $keyUrl[$key] = (String) $val;
  110. }
  111. }
  112. self::$key = md5(serialize($keyUrl) . $full);
  113. if (Configure::read('UrlCache.pageFiles')) {
  114. self::$type = 'cachePage';
  115. if (is_array($keyUrl)) {
  116. $res = array_diff_key($keyUrl, self::$extras);
  117. if (empty($res)) {
  118. self::$type = 'cache';
  119. }
  120. }
  121. if (self::$type === 'cachePage') {
  122. return isset(self::$cachePage[self::$key]) ? self::$cachePage[self::$key] : false;
  123. }
  124. }
  125. return isset(self::$cache[self::$key]) ? self::$cache[self::$key] : false;
  126. }
  127. /**
  128. * Stores a ney key in memory cache
  129. *
  130. * @param string $key
  131. * @param mixed data to be stored
  132. * @return void
  133. */
  134. public static function set($data) {
  135. if (Configure::read('UrlCache.pageFiles') && self::$type === 'cachePage') {
  136. self::$cachePage[self::$key] = $data;
  137. } else {
  138. self::$cache[self::$key] = $data;
  139. }
  140. }
  141. }