PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/application/libraries/Matchbox.php

https://github.com/ochinedu/FUEL-CMS
PHP | 329 lines | 136 code | 48 blank | 145 comment | 18 complexity | bb2ec7cec9d9d7f6a70190da3e0eac61 MD5 | raw file
  1. <?php
  2. /**
  3. * Matchbox class
  4. *
  5. * This file is part of Matchbox
  6. *
  7. * Matchbox is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Matchbox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Matchbox
  21. * @copyright 2007-2008 Zacharias Knudsen
  22. * @license http://www.gnu.org/licenses/gpl.html
  23. * @version $Id: Matchbox.php 205 2008-02-24 01:43:55Z zacharias@dynaknudsen.dk $
  24. */
  25. if (!defined('BASEPATH')) {
  26. exit('No direct script access allowed');
  27. }
  28. /**
  29. * Matchbox Version
  30. */
  31. define('MATCHBOX_VERSION', '0.9.3');
  32. /**
  33. * Provides modular functionality
  34. *
  35. * @package Matchbox
  36. * @copyright 2007-2008 Zacharias Knudsen
  37. * @license http://www.gnu.org/licenses/gpl.html
  38. */
  39. /**
  40. * FUEL Specific changes
  41. *
  42. * All code not encapsulated in <-- FUEL and FUEL--> was made by David McReynolds of Daylight
  43. *
  44. * @copyright 2008-2010 David McReynolds
  45. * @license http://www.gnu.org/licenses/gpl.html
  46. */
  47. class CI_Matchbox
  48. {
  49. /**
  50. * The files that should be excluded from the caller search
  51. *
  52. * @var array
  53. * @access private
  54. */
  55. var $_callers = array('Loader', 'Matchbox', 'MY_Config', 'MY_Language', 'Parser');
  56. /**
  57. * The directories that contain modules
  58. *
  59. * @var array
  60. * @access public
  61. */
  62. var $_directories = array('modules');
  63. /**
  64. * The directory in which the current module is located
  65. *
  66. * @var string
  67. * @access private
  68. */
  69. var $_directory = '';
  70. /**
  71. * The module in which the current controller is located
  72. *
  73. * @var string
  74. * @access private
  75. */
  76. var $_module = '';
  77. //<-- FUEL
  78. /**
  79. * A key value that you can specify as the module name to have it look in the application folder first
  80. *
  81. * @var string
  82. * @access private
  83. */
  84. var $app_folder_key = 'app';
  85. //FUEL -->
  86. /**
  87. * Fetches configuration file
  88. *
  89. * @return void
  90. * @access public
  91. */
  92. function CI_Matchbox()
  93. {
  94. @include(APPPATH . 'config/matchbox' . EXT);
  95. if (isset($config)) {
  96. $this->_callers = array_merge($this->_callers, (!is_array($config['callers']) ? array() : $config['callers']));
  97. $this->_directories = (!is_array($config['directories'])) ? $this->_directories : $config['directories'];
  98. } else {
  99. log_message('error', 'Matchbox Config File Not Found');
  100. }
  101. log_message('debug', 'Matchbox Class Initialized');
  102. }
  103. /**
  104. * Locates resources
  105. *
  106. * @param string
  107. * @param string
  108. * @param string
  109. * @param string
  110. * $param integer
  111. * @return mixed
  112. * @access public
  113. */
  114. function find($resource, $module = '', $search = 1)
  115. {
  116. log_message('debug', '---Matchbox---');
  117. log_message('debug', 'Finding: ' . $resource);
  118. $directories = array();
  119. //<-- FUEL ... added $this->app_folder_key check here -->
  120. if ($module !== '' && $module != $this->app_folder_key) {
  121. foreach ($this->directory_array() as $directory) {
  122. $directories[] = APPPATH . $directory . '/' . $module . '/';
  123. }
  124. } else {
  125. $caller = $this->detect_caller();
  126. //<-- FUEL
  127. // special module name to force the pull first form the application directory
  128. if ($module == $this->app_folder_key)
  129. {
  130. $directories[] = APPPATH;
  131. $filepath = $resource;
  132. }
  133. //FUEL -->
  134. foreach ($this->directory_array() as $directory) {
  135. $directories[] = APPPATH . $directory . '/' . $caller . '/';
  136. }
  137. if ($search == 3) {
  138. $directories[] = '';
  139. } else {
  140. $directories[] = APPPATH;
  141. if ($search == 2) {
  142. $directories[] = BASEPATH;
  143. }
  144. }
  145. }
  146. foreach ($directories as $directory) {
  147. $filepath = $directory . $resource;
  148. log_message('debug', 'Looking in: ' . $filepath);
  149. if (file_exists($filepath)) {
  150. log_message('debug', 'Found');
  151. log_message('debug', '--------------');
  152. return $filepath;
  153. }
  154. }
  155. log_message('debug', 'Not found');
  156. log_message('debug', '--------------');
  157. return false;
  158. }
  159. /**
  160. * Detects calling module
  161. *
  162. * @return string
  163. * @access public
  164. */
  165. function detect_caller()
  166. {
  167. $callers = array();
  168. $directories = array();
  169. $traces = debug_backtrace();
  170. foreach ($this->caller_array() as $caller) {
  171. $callers[] = $this->_swap_separators($caller, true);
  172. }
  173. $search = '/(?:' . implode('|', $callers) . ')' . EXT . '$/i';
  174. foreach ($traces as $trace) {
  175. $filepath = $this->_swap_separators($trace['file']);
  176. if (!preg_match($search, $filepath)) {
  177. break;
  178. }
  179. }
  180. foreach ($this->directory_array() as $directory) {
  181. $directories[] = $this->_swap_separators(realpath(APPPATH . $directory), true);
  182. }
  183. $search = '/^(?:' . implode('|', $directories) . ')\/(.+?)\//i';
  184. if (preg_match($search, $filepath, $matches)) {
  185. log_message('debug', 'Calling module: ' . $matches[1]);
  186. return $matches[1];
  187. }
  188. log_message('debug', 'No valid caller');
  189. return '';
  190. }
  191. /**
  192. * Returns the nth argument
  193. *
  194. * @access public
  195. */
  196. function argument($argument)
  197. {
  198. $traces = debug_backtrace();
  199. if (isset($traces[1]['args'][$argument])) {
  200. return $traces[1]['args'][$argument];
  201. }
  202. return '';
  203. }
  204. /**
  205. * Returns an array of callers
  206. *
  207. * @access public
  208. */
  209. function caller_array()
  210. {
  211. return $this->_callers;
  212. }
  213. /**
  214. * Returns an array of module directories
  215. *
  216. * @access public
  217. */
  218. function directory_array()
  219. {
  220. return $this->_directories;
  221. }
  222. /**
  223. * Sets the directory
  224. *
  225. * @return string
  226. * @access public
  227. */
  228. function set_directory($directory)
  229. {
  230. $this->_directory = $directory . '/';
  231. }
  232. /**
  233. * Fetches the current directory (if any)
  234. *
  235. * @return string
  236. * @access public
  237. */
  238. function fetch_directory()
  239. {
  240. return $this->_directory;
  241. }
  242. /**
  243. * Sets the module
  244. *
  245. * @return string
  246. * @access public
  247. */
  248. function set_module($module)
  249. {
  250. $this->_module = $module . '/';
  251. }
  252. /**
  253. * Fetches the current module (if any)
  254. *
  255. * @return string
  256. * @access public
  257. */
  258. function fetch_module()
  259. {
  260. return $this->_module;
  261. }
  262. /**
  263. * Swaps directory separators to Unix style for consistency
  264. *
  265. * @return string
  266. * @access private
  267. */
  268. function _swap_separators($path, $search = false)
  269. {
  270. $path = strtr($path, '\\', '/');
  271. if ($search) {
  272. $path = str_replace(array('/', '|'), array('\/', '\|'), $path);
  273. }
  274. return $path;
  275. }
  276. }
  277. ?>