MyHelper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. App::uses('Helper', 'View');
  3. App::uses('Router', 'Routing');
  4. App::uses('UrlCacheManager', 'Tools.Routing');
  5. /**
  6. * Helper enhancements for CakePHP
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  10. */
  11. class MyHelper extends Helper {
  12. /**
  13. * Manually load helpers.
  14. *
  15. * Also makes sure callbacks are triggered.
  16. *
  17. * @param array $helpers (either strings, or [string => array(config...)])
  18. * @param bool $callbacks - trigger missed callbacks
  19. * @return void
  20. */
  21. public function loadHelpers($helpers = array(), $callbacks = false) {
  22. foreach ((array)$helpers as $helper => $config) {
  23. if (is_int($helper)) {
  24. $helper = $config;
  25. $config = array();
  26. }
  27. list($plugin, $helperName) = pluginSplit($helper, true);
  28. if (isset($this->{$helperName})) {
  29. continue;
  30. }
  31. App::uses($helperName . 'Helper', $plugin . 'View/Helper');
  32. $helperFullName = $helperName . 'Helper';
  33. $this->{$helperName} = new $helperFullName($this->_View, (array)$config);
  34. if ($callbacks) {
  35. if (method_exists($helper, 'beforeRender')) {
  36. $this->{$helperName}->beforeRender();
  37. }
  38. }
  39. }
  40. }
  41. /**
  42. * This function is responsible for setting up the Url cache before the application starts generating urls in views
  43. *
  44. * @return void
  45. */
  46. public function beforeRender($viewFile) {
  47. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.beforeRender')) {
  48. return;
  49. }
  50. // todo: maybe lazy load with HtmlHelper::url()?
  51. UrlCacheManager::init($this->_View);
  52. Configure::write('UrlCache.runtime.beforeRender', true);
  53. }
  54. /**
  55. * This method will store the current generated urls into a persistent cache for next use
  56. *
  57. * @return void
  58. */
  59. public function afterLayout($layoutFile) {
  60. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
  61. return;
  62. }
  63. UrlCacheManager::finalize();
  64. Configure::write('UrlCache.runtime.afterLayout', true);
  65. }
  66. /**
  67. * Intercepts the parent url function to first look if the cache was already generated for the same params
  68. *
  69. * @param mixed $url url to generate using cakephp array syntax
  70. * @param bool|array $full whether to generate a full url or not (http scheme). As array: full, escape.
  71. * @return string
  72. * @see Helper::url()
  73. */
  74. public function url($url = null, $full = false) {
  75. if (is_array($full)) {
  76. $escape = isset($full['ecape']) ? $full['escape'] : true;
  77. $full = isset($full['full']) ? $full['full'] : false;
  78. } else {
  79. $escape = true;
  80. }
  81. if (Configure::read('UrlCache.active')) {
  82. if ($cachedUrl = UrlCacheManager::get($url, $full)) {
  83. return $cachedUrl;
  84. }
  85. }
  86. if (!$escape) {
  87. $routerUrl = Router::url($url, $full);
  88. } else {
  89. $routerUrl = parent::url($url, $full);
  90. }
  91. if (Configure::read('UrlCache.active')) {
  92. UrlCacheManager::set($routerUrl);
  93. }
  94. return $routerUrl;
  95. }
  96. /**
  97. * Generate url for given asset file. Depending on options passed provides full url with domain name.
  98. * Also calls Helper::assetTimestamp() to add timestamp to local files.
  99. * Uses Configure::read('App.assetBaseUrl') for CDN setup.
  100. *
  101. * @param string|array Path string or url array
  102. * @param array $options Options array. Possible keys:
  103. * `fullBase` Return full url with domain name
  104. * `pathPrefix` Path prefix for relative URLs
  105. * `ext` Asset extension to append
  106. * `plugin` False value will prevent parsing path as a plugin
  107. * @return string Generated url
  108. */
  109. public function assetUrl($path, $options = array()) {
  110. if (!Configure::read('App.assetBaseUrl')) {
  111. return parent::assetUrl($path, $options);
  112. }
  113. if (is_array($path)) {
  114. return $this->url($path, !empty($options['fullBase']));
  115. }
  116. if (strpos($path, '://') !== false) {
  117. return $path;
  118. }
  119. if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
  120. list($plugin, $path) = $this->_View->pluginSplit($path, false);
  121. }
  122. if (!empty($options['pathPrefix']) && $path[0] !== '/') {
  123. $path = $options['pathPrefix'] . $path;
  124. }
  125. if (
  126. !empty($options['ext']) &&
  127. strpos($path, '?') === false &&
  128. substr($path, -strlen($options['ext']) + 1) !== '.' . $options['ext']
  129. ) {
  130. $path .= '.' . $options['ext'];
  131. }
  132. if (isset($plugin)) {
  133. $path = Inflector::underscore($plugin) . '/' . $path;
  134. }
  135. $path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
  136. $path = rtrim(Configure::read('App.assetBaseUrl'), '/') . '/' . ltrim($path, '/');
  137. return $path;
  138. }
  139. }