MyHelper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 http://opensource.org/licenses/mit-license.php 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 = [], $callbacks = false) {
  22. foreach ((array)$helpers as $helper => $config) {
  23. if (is_int($helper)) {
  24. $helper = $config;
  25. $config = [];
  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. if (empty($this->request)) {
  51. return;
  52. }
  53. // todo: maybe lazy load with HtmlHelper::url()?
  54. UrlCacheManager::init($this->_View);
  55. Configure::write('UrlCache.runtime.beforeRender', true);
  56. }
  57. /**
  58. * This method will store the current generated urls into a persistent cache for next use
  59. *
  60. * @return void
  61. */
  62. public function afterLayout($layoutFile) {
  63. if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
  64. return;
  65. }
  66. if (empty($this->request)) {
  67. return;
  68. }
  69. UrlCacheManager::finalize();
  70. Configure::write('UrlCache.runtime.afterLayout', true);
  71. }
  72. /**
  73. * Intercepts the parent url function to first look if the cache was already generated for the same params
  74. *
  75. * @param mixed $url url to generate using cakephp array syntax
  76. * @param bool|array $full whether to generate a full url or not (http scheme). As array: full, escape.
  77. * @return string
  78. * @see Helper::url()
  79. */
  80. public function url($url = null, $full = false) {
  81. if (is_array($full)) {
  82. $escape = isset($full['ecape']) ? $full['escape'] : true;
  83. $full = isset($full['full']) ? $full['full'] : false;
  84. } else {
  85. $escape = true;
  86. }
  87. if (Configure::read('UrlCache.active')) {
  88. if ($cachedUrl = UrlCacheManager::get($url, $full)) {
  89. return $cachedUrl;
  90. }
  91. }
  92. if (!$escape) {
  93. $routerUrl = Router::url($url, $full);
  94. } else {
  95. $routerUrl = parent::url($url, $full);
  96. }
  97. if (Configure::read('UrlCache.active')) {
  98. UrlCacheManager::set($routerUrl);
  99. }
  100. return $routerUrl;
  101. }
  102. /**
  103. * Generate url for given asset file. Depending on options passed provides full url with domain name.
  104. * Also calls Helper::assetTimestamp() to add timestamp to local files.
  105. * Uses Configure::read('App.assetBaseUrl') for CDN setup.
  106. *
  107. * @param string|array Path string or url array
  108. * @param array $options Options array. Possible keys:
  109. * `fullBase` Return full url with domain name
  110. * `pathPrefix` Path prefix for relative URLs
  111. * `ext` Asset extension to append
  112. * `plugin` False value will prevent parsing path as a plugin
  113. * @return string Generated url
  114. */
  115. public function assetUrl($path, $options = []) {
  116. if (!Configure::read('App.assetBaseUrl')) {
  117. return parent::assetUrl($path, $options);
  118. }
  119. if (is_array($path)) {
  120. return $this->url($path, !empty($options['fullBase']));
  121. }
  122. if (strpos($path, '://') !== false) {
  123. return $path;
  124. }
  125. if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
  126. list($plugin, $path) = $this->_View->pluginSplit($path, false);
  127. }
  128. if (!empty($options['pathPrefix']) && $path[0] !== '/') {
  129. $path = $options['pathPrefix'] . $path;
  130. }
  131. if (
  132. !empty($options['ext']) &&
  133. strpos($path, '?') === false &&
  134. substr($path, -strlen($options['ext']) + 1) !== '.' . $options['ext']
  135. ) {
  136. $path .= '.' . $options['ext'];
  137. }
  138. if (isset($plugin)) {
  139. $path = Inflector::underscore($plugin) . '/' . $path;
  140. }
  141. $path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
  142. $path = rtrim(Configure::read('App.assetBaseUrl'), '/') . '/' . ltrim($path, '/');
  143. return $path;
  144. }
  145. }