PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/MiniMVC/MiniMVC/Helper/Static.php

https://github.com/tquensen/MiniMVC
PHP | 149 lines | 138 code | 11 blank | 0 comment | 31 complexity | 6fc600fca3b9fcc0c2aec9790c386faa MD5 | raw file
  1. <?php
  2. class Helper_Static extends MiniMVC_Helper
  3. {
  4. public function get($file, $module = null, $app = null)
  5. {
  6. if ($module === null)
  7. {
  8. $module = $this->module;
  9. }
  10. if (strpos($file, '?')) {
  11. list($file, $queryStr) = explode('?', $file, 2);
  12. } else {
  13. $queryStr = false;
  14. }
  15. $app = ($app) ? $app : $this->registry->settings->get('currentApp');
  16. $theme = $this->registry->layout->getTheme();
  17. $filekey = $app.'_'.$theme.'_'.$module.'_'.str_replace('/', '__', $file);
  18. if ($cache = $this->registry->cache->get('staticCached/'.$filekey))
  19. {
  20. return $queryStr ? $cache . '?' . $queryStr : $cache;
  21. }
  22. $prefixHash = $this->registry->settings->get('view/static/prefixHash', false);
  23. if ($baseurl = $this->registry->settings->get('apps/'.$app.'/baseurlStatic')) {
  24. if (is_array($baseurl)) {
  25. $baseurl = array_values($baseurl);
  26. $baseurl = $baseurl[hexdec(substr(md5($file), 0, 6)) % count($baseurl)];
  27. }
  28. }
  29. else {
  30. $baseurl = $this->registry->settings->get('apps/'.$app.'/baseurl', '');
  31. }
  32. if ($theme && $module !== null && file_exists(APPPATH.$app.'/web/'.$theme.'/'.$module.'/'.$file))
  33. {
  34. if ($prefixHash) {
  35. $parts = explode('.', $file);
  36. $ext = array_pop($parts);
  37. $file = implode('.', $parts) . '_' . md5(filemtime(APPPATH.$app.'/web/'.$theme.'/'.$module.'/'.$file)) . '_.' . $ext;
  38. }
  39. $url = $baseurl.'app/'.$app.'/'.$theme.'/'.$module.'/'.$file;
  40. }
  41. elseif ($theme && $module !== null && file_exists(WEBPATH.$theme.'/'.$module.'/'.$file))
  42. {
  43. if ($prefixHash) {
  44. $parts = explode('.', $file);
  45. $ext = array_pop($parts);
  46. $file = implode('.', $parts) . '_' . md5(filemtime(WEBPATH.$theme.'/'.$module.'/'.$file)) . '_.' . $ext;
  47. }
  48. $url = $baseurl.$theme.'/'.$module.'/'.$file;
  49. }
  50. elseif ($theme && $module !== null && file_exists(THEMEPATH.$theme.'/web/'.$module.'/'.$file))
  51. {
  52. if ($prefixHash) {
  53. $parts = explode('.', $file);
  54. $ext = array_pop($parts);
  55. $file = implode('.', $parts) . '_' . md5(filemtime(THEMEPATH.$theme.'/web/'.$module.'/'.$file)) . '_.' . $ext;
  56. }
  57. $url = $baseurl.'theme/'.$theme.'/'.$module.'/'.$file;
  58. }
  59. elseif ($module !== null && file_exists(APPPATH.$app.'/web/'.$module.'/'.$file))
  60. {
  61. if ($prefixHash) {
  62. $parts = explode('.', $file);
  63. $ext = array_pop($parts);
  64. $file = implode('.', $parts) . '_' . md5(filemtime(APPPATH.$app.'/web/'.$module.'/'.$file)) . '_.' . $ext;
  65. }
  66. $url = $baseurl.'app/'.$app.'/'.$module.'/'.$file;
  67. }
  68. elseif ($module !== null && file_exists(WEBPATH.$module.'/'.$file))
  69. {
  70. if ($prefixHash) {
  71. $parts = explode('.', $file);
  72. $ext = array_pop($parts);
  73. $file = implode('.', $parts) . '_' . md5(filemtime(WEBPATH.$module.'/'.$file)) . '_.' . $ext;
  74. }
  75. $url = $baseurl.$module.'/'.$file;
  76. }
  77. elseif ($module !== null && file_exists(MODULEPATH.$module.'/web/'.$file))
  78. {
  79. if ($prefixHash) {
  80. $parts = explode('.', $file);
  81. $ext = array_pop($parts);
  82. $file = implode('.', $parts) . '_' . md5(filemtime(MODULEPATH.$module.'/web/'.$file)) . '_.' . $ext;
  83. }
  84. $url = $baseurl.'module/'.$module.'/'.$file;
  85. }
  86. elseif ($theme && file_exists(APPPATH.$app.'/web/'.$theme.'/'.$file))
  87. {
  88. if ($prefixHash) {
  89. $parts = explode('.', $file);
  90. $ext = array_pop($parts);
  91. $file = implode('.', $parts) . '_' . md5(filemtime(APPPATH.$app.'/web/'.$theme.'/'.$file)) . '_.' . $ext;
  92. }
  93. $url = $baseurl.'app/'.$app.'/'.$theme.'/'.$file;
  94. }
  95. elseif ($theme && file_exists(WEBPATH.$theme.'/'.$file))
  96. {
  97. if ($prefixHash) {
  98. $parts = explode('.', $file);
  99. $ext = array_pop($parts);
  100. $file = implode('.', $parts) . '_' . md5(filemtime(WEBPATH.$theme.'/'.$file)) . '_.' . $ext;
  101. }
  102. $url = $baseurl.$theme.'/'.$file;
  103. }
  104. elseif ($theme && file_exists(THEMEPATH.$theme.'/web/'.$file))
  105. {
  106. if ($prefixHash) {
  107. $parts = explode('.', $file);
  108. $ext = array_pop($parts);
  109. $file = implode('.', $parts) . '_' . md5(filemtime(THEMEPATH.$theme.'/web/'.$file)) . '_.' . $ext;
  110. }
  111. $url = $baseurl.'theme/'.$theme.'/'.$file;
  112. }
  113. elseif (file_exists(APPPATH.$app.'/web/'.$file))
  114. {
  115. if ($prefixHash) {
  116. $parts = explode('.', $file);
  117. $ext = array_pop($parts);
  118. $file = implode('.', $parts) . '_' . md5(filemtime(APPPATH.$app.'/web/'.$file)) . '_.' . $ext;
  119. }
  120. $url = $baseurl.'app/'.$app.'/'.$file;
  121. }
  122. elseif (file_exists(WEBPATH.$file))
  123. {
  124. if ($prefixHash) {
  125. $parts = explode('.', $file);
  126. $ext = array_pop($parts);
  127. $file = implode('.', $parts) . '_' . md5(filemtime(WEBPATH.$file)) . '_.' . $ext;
  128. }
  129. $url = $baseurl.$file;
  130. }
  131. else
  132. {
  133. $url = $baseurl.$file;
  134. }
  135. $this->registry->cache->set('staticCached/'.$filekey, $url);
  136. return $queryStr ? $url . '?' . $queryStr : $url;
  137. }
  138. }