/libraries/rokcommon/RokCommon/Composite/Context.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 327 lines · 177 code · 34 blank · 116 comment · 32 complexity · d8d90d7148047c142ef78de2fde994fa MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: Context.php 57540 2012-10-14 18:27:59Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. defined('ROKCOMMON') or die;
  9. /**
  10. *
  11. */
  12. class RokCommon_Composite_Context
  13. {
  14. /**
  15. * @var string
  16. */
  17. protected $_context = '';
  18. /**
  19. * @var string
  20. */
  21. protected $_paths;
  22. /**
  23. * @param $context
  24. * @param array $paths
  25. *
  26. * @return RokCommon_Composite_Context
  27. *
  28. */
  29. public function __construct($context, array &$paths = array())
  30. {
  31. $this->_context = $context;
  32. $this->_paths = $paths;
  33. }
  34. /**
  35. * Perform an include with passed variables set for the passed in found file on the paths of the
  36. * context in the context path
  37. *
  38. * @param $file
  39. * @param $vars
  40. *
  41. * @param bool $hierarchical
  42. *
  43. * @return string|bool
  44. */
  45. public function load($file, $vars, $hierarchical = true)
  46. {
  47. $_internal_loading_final_file = false;
  48. if (!$hierarchical) {
  49. $_internal_loading_final_file = self::_findFile($file, $this->_context, $this->_paths);
  50. } else {
  51. $found_paths = self::_findSet($file, $this->_context, $this->_paths);
  52. if (!empty($found_paths)) {
  53. $_internal_loading_final_file = $found_paths[0];
  54. }
  55. }
  56. if ($_internal_loading_final_file === false) return false;
  57. if (!file_exists($_internal_loading_final_file)) return false;
  58. extract($vars, EXTR_REFS | EXTR_SKIP);
  59. ob_start();
  60. include($_internal_loading_final_file);
  61. $output = ob_get_clean();
  62. return $output;
  63. }
  64. /**
  65. * Perform an include with passed variables set for the passed in found file on the paths of the
  66. * context in the context path
  67. *
  68. * @param $file
  69. * @param $vars
  70. * @param bool $hierarchical
  71. * @param bool $startbuild
  72. *
  73. * @return string|bool
  74. */
  75. public function build($file, $vars, $hierarchical = true, $startbuild = false)
  76. {
  77. $_internal_loading_final_file = false;
  78. if (!$hierarchical) {
  79. $_internal_loading_final_file = self::_findFile($file, $this->_context, $this->_paths);
  80. } else {
  81. $found_paths = self::_findSet($file, $this->_context, $this->_paths);
  82. if (!empty($found_paths)) {
  83. $_internal_loading_final_file = $found_paths[0];
  84. }
  85. }
  86. if ($_internal_loading_final_file === false) return false;
  87. if (!file_exists($_internal_loading_final_file)) return false;
  88. extract($vars, EXTR_REFS | EXTR_SKIP);
  89. if (!$startbuild){
  90. // look at backtrace to see where called from
  91. $backtrace = debug_backtrace();
  92. $filecontents = file($backtrace[0]['file']);
  93. $line = $backtrace[0]['line']-1;
  94. preg_replace('/build/','ignore',$filecontents[$line]);
  95. }
  96. ob_start();
  97. include($_internal_loading_final_file);
  98. $output = ob_get_clean();
  99. return $output;
  100. }
  101. /**
  102. * Perform an include with passed variables set for the passed in found file on the paths of the
  103. * context in the context path
  104. *
  105. * @param $file
  106. * @param $vars
  107. *
  108. * @return string|bool
  109. */
  110. public function loadAll($file, $vars)
  111. {
  112. $found_paths = self::_findSet($file, $this->_context, $this->_paths);
  113. if (!empty($found_paths)) {
  114. $_internal_loading_final_file = $found_paths[0];
  115. }
  116. if ($found_paths === false && !empty($found_paths)) return false;
  117. extract($vars, EXTR_REFS | EXTR_SKIP);
  118. ob_start();
  119. foreach ($found_paths as $found_path) {
  120. if (!file_exists($_internal_loading_final_file)) continue;
  121. include($found_path);
  122. }
  123. $output = ob_get_clean();
  124. return $output;
  125. }
  126. /**
  127. * Get the path of the highest priority package file with the context in the context paths;
  128. *
  129. * @param $file
  130. *
  131. * @return bool|string
  132. */
  133. public function get($file)
  134. {
  135. return self::_findFile($file, $this->_context, $this->_paths);
  136. }
  137. /**
  138. * @param string $file
  139. * @param bool $hierarchical
  140. *
  141. * @return string
  142. */
  143. public function getUrl($file, $hierarchical = true)
  144. {
  145. $file_path = false;
  146. if (!$hierarchical) {
  147. $file_path = self::_findFile($file, $this->_context, $this->_paths);
  148. } else {
  149. $found_paths = self::_findSet($file, $this->_context, $this->_paths);
  150. if (!empty($found_paths)) {
  151. $file_path = $found_paths[0];
  152. }
  153. }
  154. if ($file_path == false) return '';
  155. $container = RokCommon_Service::getContainer();
  156. /** @var $platforminfo RokCommon_IPlatformInfo */
  157. $platforminfo = $container->platforminfo;
  158. return $platforminfo->getUrlForPath($file_path);
  159. }
  160. /**
  161. * Get the hierarchical set of the highest priority files with the filename along the context path
  162. *
  163. * @param $file
  164. *
  165. * @return array
  166. */
  167. public function getSet($file)
  168. {
  169. return self::_findSet($file, $this->_context, $this->_paths);
  170. }
  171. /**
  172. * @param $file
  173. * @param $context
  174. * @param $basepaths
  175. *
  176. * @return bool|string
  177. */
  178. protected static function _findFile($file, $context, $basepaths)
  179. {
  180. $hunt_path = str_replace('.', DS, $context);
  181. foreach ($basepaths as $priority => $paths) {
  182. foreach ($paths as $path) {
  183. $find_path = $path;
  184. $find_path .= (!empty($hunt_path)) ? DS . $hunt_path : '';
  185. $find_path .= DS . $file;
  186. if (file_exists($find_path) && is_file($find_path)) {
  187. return $find_path;
  188. }
  189. }
  190. }
  191. return false;
  192. }
  193. /**
  194. * @param $file
  195. *
  196. * @return array
  197. */
  198. public function getAll($file)
  199. {
  200. return self::_findAllFiles($file, $this->_context, $this->_paths);
  201. }
  202. /**
  203. * @param $file
  204. *
  205. * @return array
  206. */
  207. public function getAllSubFiles($file)
  208. {
  209. return self::_findSubFiles($file, $this->_context, $this->_paths);
  210. }
  211. /**
  212. * @param $file
  213. * @param $context
  214. * @param $basepaths
  215. *
  216. * @return array
  217. */
  218. protected static function _findAllFiles($file, $context, $basepaths)
  219. {
  220. $ret = array();
  221. $hunt_path = str_replace('.', DS, $context);
  222. foreach ($basepaths as $priority => $paths) {
  223. foreach ($paths as $path) {
  224. $find_path = $path;
  225. $find_path .= (!empty($hunt_path)) ? DS . $hunt_path : '';
  226. $find_path .= DS . $file;
  227. if (file_exists($find_path) && is_file($find_path)) {
  228. $ret[$priority][] = $find_path;
  229. }
  230. }
  231. }
  232. return $ret;
  233. }
  234. /**
  235. * @static
  236. *
  237. * @param $file
  238. * @param $context
  239. * @param $basepaths
  240. *
  241. * @return array
  242. */
  243. protected static function _findSubFiles($file, $context, $basepaths)
  244. {
  245. $ret = array();
  246. $hunt_path = str_replace('.', DS, $context);
  247. foreach ($basepaths as $priority => $paths) {
  248. foreach ($paths as $path) {
  249. $find_path = $path;
  250. $find_path .= (!empty($hunt_path)) ? DS . $hunt_path : '';
  251. if (defined("RecursiveDirectoryIterator::FOLLOW_SYMLINKS")) {
  252. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($find_path, RecursiveDirectoryIterator::FOLLOW_SYMLINKS), RecursiveIteratorIterator::LEAVES_ONLY);
  253. } else {
  254. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($find_path), RecursiveIteratorIterator::LEAVES_ONLY);
  255. }
  256. foreach ($iterator as $path) {
  257. /** @var $path FilesystemIterator */
  258. if ($path->getFilename() == $file) {
  259. $ret[$priority][] = $path->__toString();
  260. }
  261. }
  262. }
  263. }
  264. return $ret;
  265. }
  266. /**
  267. * @param $file
  268. * @param $context
  269. * @param $basepaths
  270. *
  271. * @return array
  272. */
  273. protected static function _findSet($file, $context, $basepaths)
  274. {
  275. $ret = array();
  276. $context_parts = explode('.', $context);
  277. if (!empty($context)) {
  278. while (count($context_parts)) {
  279. $context_path = implode('.', $context_parts);
  280. $filepath = self::_findFile($file, $context_path, $basepaths);
  281. if ($filepath !== false) {
  282. $ret[] = $filepath;
  283. }
  284. array_pop($context_parts);
  285. }
  286. }
  287. $filepath = self::_findFile($file, '', $basepaths);
  288. if ($filepath !== false) {
  289. $ret[] = $filepath;
  290. }
  291. return $ret;
  292. }
  293. }