App.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. /**
  3. * App class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs
  16. * @since CakePHP(tm) v 1.2.0.6001
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * App is responsible for path managment, class location and class loading.
  21. *
  22. * ### Adding paths
  23. *
  24. * You can add paths to the search indexes App uses to find classes using `App::build()`. Adding
  25. * additional controller paths for example would alter where CakePHP looks for controllers.
  26. * This allows you to split your application up across the filesystem.
  27. *
  28. * ### Inspecting loaded paths
  29. *
  30. * You can inspect the currently loaded paths using `App::path('controller')` for example to see loaded
  31. * controller paths.
  32. *
  33. * ### Locating plugins and themes
  34. *
  35. * Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will
  36. * give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the
  37. * `purple` theme.
  38. *
  39. * ### Inspecting known objects
  40. *
  41. * You can find out which objects App knows about using App::objects('controller') for example to find
  42. * which application controllers App knows about.
  43. *
  44. * @link http://book.cakephp.org/view/933/The-App-Class
  45. * @package cake.libs
  46. */
  47. class App {
  48. /**
  49. * List of object types and their properties
  50. *
  51. * @var array
  52. */
  53. public static $types = array(
  54. 'class' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  55. 'file' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  56. 'model' => array('suffix' => '.php', 'extends' => 'AppModel', 'core' => false),
  57. 'behavior' => array('suffix' => '.php', 'extends' => 'ModelBehavior', 'core' => true),
  58. 'controller' => array('suffix' => '_controller.php', 'extends' => 'AppController', 'core' => true),
  59. 'component' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  60. 'lib' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  61. 'view' => array('suffix' => '.php', 'extends' => null, 'core' => true),
  62. 'helper' => array('suffix' => '.php', 'extends' => 'AppHelper', 'core' => true),
  63. 'vendor' => array('suffix' => '', 'extends' => null, 'core' => true),
  64. 'shell' => array('suffix' => '.php', 'extends' => 'Shell', 'core' => true),
  65. 'plugin' => array('suffix' => '', 'extends' => null, 'core' => true)
  66. );
  67. /**
  68. * List of additional path(s) where model files reside.
  69. *
  70. * @var array
  71. */
  72. public static $models = array();
  73. /**
  74. * List of additional path(s) where behavior files reside.
  75. *
  76. * @var array
  77. */
  78. public static $behaviors = array();
  79. /**
  80. * List of additional path(s) where controller files reside.
  81. *
  82. * @var array
  83. */
  84. public static $controllers = array();
  85. /**
  86. * List of additional path(s) where component files reside.
  87. *
  88. * @var array
  89. */
  90. public static $components = array();
  91. /**
  92. * List of additional path(s) where datasource files reside.
  93. *
  94. * @var array
  95. */
  96. public static $datasources = array();
  97. /**
  98. * List of additional path(s) where libs files reside.
  99. *
  100. * @var array
  101. */
  102. public static $libs = array();
  103. /**
  104. * List of additional path(s) where view files reside.
  105. *
  106. * @var array
  107. */
  108. public static $views = array();
  109. /**
  110. * List of additional path(s) where helper files reside.
  111. *
  112. * @var array
  113. */
  114. public static $helpers = array();
  115. /**
  116. * List of additional path(s) where plugins reside.
  117. *
  118. * @var array
  119. */
  120. public static $plugins = array();
  121. /**
  122. * List of additional path(s) where vendor packages reside.
  123. *
  124. * @var array
  125. */
  126. public static $vendors = array();
  127. /**
  128. * List of additional path(s) where locale files reside.
  129. *
  130. * @var array
  131. */
  132. public static $locales = array();
  133. /**
  134. * List of additional path(s) where console shell files reside.
  135. *
  136. * @var array
  137. */
  138. public static $shells = array();
  139. /**
  140. * Paths to search for files.
  141. *
  142. * @var array
  143. */
  144. public static $search = array();
  145. /**
  146. * Whether or not to return the file that is loaded.
  147. *
  148. * @var boolean
  149. */
  150. public static $return = false;
  151. /**
  152. * Determines if $__maps and $__paths cache should be written.
  153. *
  154. * @var boolean
  155. */
  156. private static $__cache = false;
  157. /**
  158. * Holds key/value pairs of $type => file path.
  159. *
  160. * @var array
  161. */
  162. private static $__map = array();
  163. /**
  164. * Holds paths for deep searching of files.
  165. *
  166. * @var array
  167. */
  168. private static $__paths = array();
  169. /**
  170. * Holds loaded files.
  171. *
  172. * @var array
  173. */
  174. private static $__loaded = array();
  175. /**
  176. * Holds and key => value array of object types.
  177. *
  178. * @var array
  179. */
  180. private static $__objects = array();
  181. /**
  182. * Holds the location of each class
  183. *
  184. */
  185. private static $__classMap = array();
  186. /**
  187. * Holds the possible paths for each package name
  188. *
  189. */
  190. private static $__packages = array();
  191. /**
  192. *
  193. *
  194. */
  195. private static $__packageFormat = array();
  196. /**
  197. * Maps an old style cakephp class type to the corresponding package
  198. *
  199. */
  200. public static $legacy = array(
  201. 'models' => 'Model',
  202. 'behaviors' => 'Model/Behavior',
  203. 'datasources' => 'Model/Datasource',
  204. 'controllers' => 'Controller',
  205. 'components' => 'Model/Datasource',
  206. 'views' => 'View',
  207. 'helpers' => 'View/Helper',
  208. 'shells' => 'Console'
  209. );
  210. /**
  211. * Inicates whether the class cache should be stored again because of an addition to it
  212. *
  213. */
  214. private static $_cacheChange = false;
  215. /**
  216. * Inicates whether the object cache should be stored again because of an addition to it
  217. *
  218. */
  219. private static $_objectCacheChange = false;
  220. /**
  221. * Used to read information stored path
  222. *
  223. * Usage:
  224. *
  225. * `App::path('models'); will return all paths for models`
  226. *
  227. * @param string $type type of path
  228. * @return string array
  229. */
  230. public static function path($type, $plugin = null) {
  231. if (!empty(self::$legacy[$type])) {
  232. $type = self::$legacy[$type];
  233. }
  234. if (!empty($plugin)) {
  235. $path = array();
  236. $pluginPath = self::pluginPath($plugin);
  237. if (!empty(self::$__packageFormat[$type])) {
  238. foreach (self::$__packageFormat[$type] as $f) {
  239. $path[] = sprintf($f, $pluginPath);
  240. }
  241. }
  242. $path[] = $pluginPath . 'libs' . DS . $type . DS;
  243. return $path;
  244. }
  245. if (!isset(self::$__packages[$type])) {
  246. return array();
  247. }
  248. return self::$__packages[$type];
  249. }
  250. /**
  251. * Build path references. Merges the supplied $paths
  252. * with the base paths and the default core paths.
  253. *
  254. * @param array $paths paths defines in config/bootstrap.php
  255. * @param boolean $reset true will set paths, false merges paths [default] false
  256. * @return void
  257. */
  258. public static function build($paths = array(), $reset = false) {
  259. if (empty(self::$__packageFormat)) {
  260. self::$__packageFormat = array(
  261. 'Model' => array('%s' . 'models' . DS),
  262. 'Model/Behavior' => array('%s' . 'models' . DS . 'behaviors' . DS),
  263. 'Model/Datasource' => array('%s' . 'models' . DS . 'datasources' . DS),
  264. 'Model/Datasource/Database' => array('%s' . 'models' . DS . 'datasources' . DS . 'Database' . DS),
  265. 'Model/Datasource/Session' => array('%s' . 'models' . DS . 'datasources' . DS . 'Session' . DS),
  266. 'Controller' => array('%s' . 'controllers' . DS),
  267. 'Controller/Component' => array('%s' . 'controllers' . DS . 'components' . DS),
  268. 'View' => array('%s' . 'views' . DS),
  269. 'View/Helper' => array('%s' . 'views' . DS . 'helpers' . DS),
  270. 'Console' => array(
  271. '%s' . 'console' . DS . 'shells' . DS,
  272. '%s' . 'vendors' . DS . 'shells' . DS,
  273. VENDORS . 'shells' . DS
  274. ),
  275. 'libs' => array('%s' . 'libs' . DS),
  276. 'locales' => array('%s' . 'locale' . DS),
  277. 'vendors' => array('%s' . 'vendors' . DS, VENDORS),
  278. 'plugins' => array(APP . 'plugins' . DS)
  279. );
  280. self::$__packageFormat['Console/Command'] = self::$__packageFormat['Console'];
  281. }
  282. if ($reset == true) {
  283. foreach ($paths as $type => $new) {
  284. if (!empty(self::$legacy[$type])) {
  285. $type = self::$legacy[$type];
  286. }
  287. self::$__packages[$type] = (array)$new;
  288. }
  289. return $paths;
  290. }
  291. $defaults = array();
  292. foreach (self::$__packageFormat as $package => $format) {
  293. foreach ($format as $f) {
  294. $defaults[$package][] = sprintf($f, APP);
  295. }
  296. }
  297. $mergeExclude = array('libs', 'locales', 'vendors', 'plugins');
  298. $appLibs = empty($paths['libs']) ? $defaults['libs'] : $paths['libs'];
  299. foreach ($defaults as $type => $default) {
  300. if (empty(self::$__packages[$type]) || empty($paths)) {
  301. self::$__packages[$type] = $default;
  302. }
  303. if (!empty($paths[$type])) {
  304. $path = array_merge((array)$paths[$type], self::$__packages[$type]);
  305. } else {
  306. $path = self::$__packages[$type];
  307. }
  308. self::$__packages[$type] = array_values(array_unique($path));
  309. }
  310. }
  311. /**
  312. * Get the path that a plugin is on. Searches through the defined plugin paths.
  313. *
  314. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  315. * @return string full path to the plugin.
  316. */
  317. public static function pluginPath($plugin) {
  318. $pluginDir = Inflector::underscore($plugin);
  319. foreach (self::$__packages['plugins'] as $pluginPath) {
  320. if (is_dir($pluginPath . $pluginDir)) {
  321. return $pluginPath . $pluginDir . DS ;
  322. }
  323. }
  324. return self::$__packages['plugins'][0] . $pluginDir . DS;
  325. }
  326. /**
  327. * Find the path that a theme is on. Search through the defined theme paths.
  328. *
  329. * @param string $theme lower_cased theme name to find the path of.
  330. * @return string full path to the theme.
  331. */
  332. public static function themePath($theme) {
  333. $themeDir = 'themed' . DS . Inflector::underscore($theme);
  334. foreach (self::$__packages['View'] as $path) {
  335. if (is_dir($path . $themeDir)) {
  336. return $path . $themeDir . DS ;
  337. }
  338. }
  339. return self::$__packages['View'][0] . $themeDir . DS;
  340. }
  341. /**
  342. * Returns a key/value list of all paths where core libs are found.
  343. * Passing $type only returns the values for a given value of $key.
  344. *
  345. * @param string $type valid values are: 'cake' ,'plugins', 'vendors' and 'shells'
  346. * @return array numeric keyed array of core lib paths
  347. */
  348. public static function core($type = null) {
  349. static $paths = false;
  350. if (!$paths) {
  351. $paths = array();
  352. $root = dirname(dirname(LIBS)) . DS;
  353. $paths['cake'][] = LIBS;
  354. $paths['plugins'][] = $root . 'plugins' . DS;
  355. $paths['vendors'][] = $root . 'vendors' . DS;
  356. $paths['shells'][] = LIBS . 'Console' . DS . 'Command' . DS;
  357. // Provide BC path to vendors/shells
  358. $paths['shells'][] = $root . 'vendors' . DS . 'shells' . DS;
  359. }
  360. if ($type) {
  361. return isset($paths[$type]) ? $paths[$type] : array(LIBS . $type . DS);
  362. }
  363. return $paths;
  364. }
  365. /**
  366. * Returns an array of objects of the given type.
  367. *
  368. * Example usage:
  369. *
  370. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  371. *
  372. * You can also search only within a plugin's objects by using the plugin dot
  373. * syntax.
  374. *
  375. * `App::objects('MyPlugin.model');` returns `array('Post', 'Comment');`
  376. *
  377. * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  378. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
  379. * type will be used.
  380. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  381. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  382. */
  383. public static function objects($type, $path = null, $cache = true) {
  384. $objects = array();
  385. $extension = '/\.php$/';
  386. $directories = false;
  387. $name = $type;
  388. if (isset(self::$legacy[$type . 's'])) {
  389. $type = self::$legacy[$type . 's'];
  390. }
  391. if ($type === 'plugin') {
  392. $type = 'plugins';
  393. }
  394. if ($type == 'plugins') {
  395. $extension = '/.*/';
  396. $includeDirectories = true;
  397. }
  398. list($plugin, $type) = pluginSplit($type);
  399. if ($type === 'file' && !$path) {
  400. return false;
  401. } elseif ($type === 'file') {
  402. $extension = '/.*/';
  403. $name = $type . str_replace(DS, '', $path);
  404. }
  405. if (empty(self::$__objects) && $cache === true) {
  406. self::$__objects = Cache::read('object_map', '_cake_core_');
  407. }
  408. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  409. if ($cache !== true || !isset(self::$__objects[$cacheLocation][$name])) {
  410. $objects = array();
  411. if (empty($path)) {
  412. $path = self::path($type, $plugin);
  413. }
  414. $items = array();
  415. foreach ((array)$path as $dir) {
  416. if ($dir != APP && is_dir($dir)) {
  417. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  418. foreach ($files as $file) {
  419. if (!$file->isDot() && (!$file->isDir() || $includeDirectories)) {
  420. $objects[] = substr(basename($file), 0, -4);
  421. }
  422. }
  423. }
  424. }
  425. if ($type !== 'file') {
  426. foreach ($objects as $key => $value) {
  427. $objects[$key] = Inflector::camelize($value);
  428. }
  429. }
  430. if ($cache === true) {
  431. self::$__cache = true;
  432. }
  433. if ($plugin) {
  434. return $objects;
  435. }
  436. self::$__objects[$cacheLocation][$name] = $objects;
  437. self::$_objectCacheChange = true;
  438. }
  439. return self::$__objects[$cacheLocation][$name];
  440. }
  441. /**
  442. * Allows you to modify the object listings that App maintains inside of it
  443. * Useful for testing
  444. *
  445. * @param string $type Type of object listing you are changing
  446. * @param array $values The values $type should be set to.
  447. * @return void
  448. */
  449. public static function setObjects($type, $values) {
  450. self::$__objects[$type] = $values;
  451. }
  452. public static function uses($className, $location) {
  453. self::$__classMap[$className] = $location;
  454. }
  455. public static function load($className) {
  456. if (isset(self::$__classMap[$className])) {
  457. if ($file = self::__mapped($className)) {
  458. return include $file;
  459. }
  460. $parts = explode('.', self::$__classMap[$className], 2);
  461. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  462. $paths = self::path($package, $plugin);
  463. if (empty($plugin)) {
  464. $appLibs = empty(self::$__packages['libs']) ? APPLIBS : current(self::$__packages['libs']);
  465. $paths[] = $appLibs . self::$__classMap[$className] . DS;
  466. $paths[] = LIBS . self::$__classMap[$className] . DS;
  467. }
  468. foreach ($paths as $path) {
  469. $file = $path . $className . '.php';
  470. if (file_exists($file)) {
  471. self::__map($file, $className);
  472. return include $file;
  473. }
  474. }
  475. //To help apps migrate to 2.0 old style file names are allowed
  476. foreach ($paths as $path) {
  477. $underscored = Inflector::underscore($className);
  478. $tries = array($path . $underscored . '.php');
  479. $parts = explode('_', $underscored);
  480. if (count($parts) > 1) {
  481. array_pop($parts);
  482. $tries[] = $path . implode('_', $parts) . '.php';
  483. }
  484. foreach ($tries as $file) {
  485. if (file_exists($file)) {
  486. self::__map($file, $className);
  487. return include $file;
  488. }
  489. }
  490. }
  491. }
  492. return false;
  493. }
  494. /**
  495. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  496. * not construct any classes contained in the files. It will only find and require() the file.
  497. *
  498. * @link http://book.cakephp.org/view/934/Using-App-import
  499. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  500. * an single array to $type,
  501. * @param string $name Name of the Class or a unique name for the file
  502. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  503. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  504. * $ext allows setting the extension of the file name
  505. * based on Inflector::underscore($name) . ".$ext";
  506. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  507. * @param string $file full name of the file to search for including extension
  508. * @param boolean $return, return the loaded file, the file must have a return
  509. * statement in it to work: return $variable;
  510. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  511. */
  512. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  513. $plugin = $directory = null;
  514. if (is_array($type)) {
  515. extract($type, EXTR_OVERWRITE);
  516. }
  517. if (is_array($parent)) {
  518. extract($parent, EXTR_OVERWRITE);
  519. }
  520. if ($name === null && $file === null) {
  521. $name = $type;
  522. $type = 'Core';
  523. } elseif ($name === null) {
  524. $type = 'File';
  525. }
  526. if (is_array($name)) {
  527. foreach ($name as $class) {
  528. $tempType = $type;
  529. $plugin = null;
  530. if (strpos($class, '.') !== false) {
  531. $value = explode('.', $class);
  532. $count = count($value);
  533. if ($count > 2) {
  534. $tempType = $value[0];
  535. $plugin = $value[1] . '.';
  536. $class = $value[2];
  537. } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
  538. $tempType = $value[0];
  539. $class = $value[1];
  540. } else {
  541. $plugin = $value[0] . '.';
  542. $class = $value[1];
  543. }
  544. }
  545. if (!App::import($tempType, $plugin . $class, $parent)) {
  546. return false;
  547. }
  548. }
  549. return true;
  550. }
  551. if ($name != null && strpos($name, '.') !== false) {
  552. list($plugin, $name) = explode('.', $name);
  553. $plugin = Inflector::camelize($plugin);
  554. }
  555. self::$return = $return;
  556. if (isset($ext)) {
  557. $file = Inflector::underscore($name) . ".{$ext}";
  558. }
  559. $ext = self::__settings($type, $plugin, $parent);
  560. $className = $name;
  561. if (strpos($className, '/') !== false) {
  562. $className = substr($className, strrpos($className, '/') + 1);
  563. }
  564. if ($name != null && !class_exists($className . $ext['class'])) {
  565. if ($load = self::__mapped($name . $ext['class'], $type, $plugin)) {
  566. if (self::__load($load)) {
  567. if (self::$return) {
  568. return include($load);
  569. }
  570. return true;
  571. } else {
  572. self::__remove($name . $ext['class'], $type, $plugin);
  573. self::$__cache = true;
  574. }
  575. }
  576. if (!empty($search)) {
  577. self::$search = $search;
  578. } elseif ($plugin) {
  579. self::$search = self::__paths('plugin');
  580. } else {
  581. self::$search = self::__paths($type);
  582. }
  583. $find = $file;
  584. if ($find === null) {
  585. $find = Inflector::underscore($name . $ext['suffix']).'.php';
  586. if ($plugin) {
  587. $paths = self::$search;
  588. foreach ($paths as $key => $value) {
  589. self::$search[$key] = $value . $ext['path'];
  590. }
  591. }
  592. }
  593. if (strtolower($type) !== 'vendor' && empty($search) && self::__load($file)) {
  594. $directory = false;
  595. } else {
  596. $file = $find;
  597. $directory = self::__find($find, true);
  598. }
  599. if ($directory !== null) {
  600. self::$__cache = true;
  601. self::__map($directory . $file, $name . $ext['class'], $type, $plugin);
  602. if (self::$return) {
  603. return include($directory . $file);
  604. }
  605. return true;
  606. }
  607. return false;
  608. }
  609. return true;
  610. }
  611. /**
  612. * Initializes the cache for App, registers a shutdown function.
  613. *
  614. * @return void
  615. */
  616. public static function init() {
  617. self::$__map = (array)Cache::read('file_map', '_cake_core_');
  618. self::$__objects = (array)Cache::read('object_map', '_cake_core_');
  619. register_shutdown_function(array('App', 'shutdown'));
  620. }
  621. /**
  622. * Locates the $file in $__paths, searches recursively.
  623. *
  624. * @param string $file full file name
  625. * @param boolean $recursive search $__paths recursively
  626. * @return mixed boolean on fail, $file directory path on success
  627. */
  628. private static function __find($file, $recursive = true) {
  629. static $appPath = false;
  630. if (empty(self::$search)) {
  631. return null;
  632. } elseif (is_string(self::$search)) {
  633. self::$search = array(self::$search);
  634. }
  635. if (empty(self::$__paths)) {
  636. self::$__paths = Cache::read('dir_map', '_cake_core_');
  637. }
  638. foreach (self::$search as $path) {
  639. if ($appPath === false) {
  640. $appPath = rtrim(APP, DS);
  641. }
  642. $path = rtrim($path, DS);
  643. if ($path === $appPath) {
  644. $recursive = false;
  645. }
  646. if ($recursive === false) {
  647. if (self::__load($path . DS . $file)) {
  648. return $path . DS;
  649. }
  650. continue;
  651. }
  652. if (!isset(self::$__paths[$path])) {
  653. App::uses('Folder', 'Utility');
  654. $Folder = new Folder();
  655. $directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
  656. sort($directories);
  657. self::$__paths[$path] = $directories;
  658. }
  659. foreach (self::$__paths[$path] as $directory) {
  660. if (self::__load($directory . DS . $file)) {
  661. return $directory . DS;
  662. }
  663. }
  664. }
  665. return null;
  666. }
  667. /**
  668. * Attempts to load $file.
  669. *
  670. * @param string $file full path to file including file name
  671. * @return boolean
  672. * @access private
  673. */
  674. private static function __load($file) {
  675. if (empty($file)) {
  676. return false;
  677. }
  678. if (!self::$return && isset(self::$__loaded[$file])) {
  679. return true;
  680. }
  681. if (file_exists($file)) {
  682. if (!self::$return) {
  683. require($file);
  684. self::$__loaded[$file] = true;
  685. }
  686. return true;
  687. }
  688. return false;
  689. }
  690. /**
  691. * Maps the $name to the $file.
  692. *
  693. * @param string $file full path to file
  694. * @param string $name unique name for this map
  695. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  696. * @return void
  697. * @access private
  698. */
  699. private static function __map($file, $name, $plugin = null) {
  700. if ($plugin) {
  701. self::$__map['Plugin'][$plugin][$name] = $file;
  702. } else {
  703. self::$__map[$name] = $file;
  704. }
  705. self::$_cacheChange = true;
  706. }
  707. /**
  708. * Returns a file's complete path.
  709. *
  710. * @param string $name unique name
  711. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  712. * @return mixed, file path if found, false otherwise
  713. * @access private
  714. */
  715. private static function __mapped($name, $plugin = null) {
  716. if ($plugin) {
  717. if (isset(self::$__map['Plugin'][$plugin][$name])) {
  718. return self::$__map['Plugin'][$plugin][$name];
  719. }
  720. return false;
  721. }
  722. if (isset(self::$__map[$name])) {
  723. return self::$__map[$name];
  724. }
  725. return false;
  726. }
  727. /**
  728. * Loads parent classes based on $type.
  729. * Returns a prefix or suffix needed for loading files.
  730. *
  731. * @param string $type type of object
  732. * @param string $plugin camelized name of plugin
  733. * @param boolean $parent false will not attempt to load parent
  734. * @return array
  735. * @access private
  736. */
  737. private static function __settings($type, $plugin, $parent) {
  738. if (!$parent) {
  739. return array('class' => null, 'suffix' => null, 'path' => null);
  740. }
  741. if ($plugin) {
  742. $pluginPath = Inflector::underscore($plugin);
  743. }
  744. $path = null;
  745. $load = strtolower($type);
  746. switch ($load) {
  747. case 'model':
  748. App::uses('Model', 'Model');
  749. if (!class_exists('AppModel')) {
  750. App::import($type, 'AppModel', false);
  751. }
  752. if ($plugin) {
  753. if (!class_exists($plugin . 'AppModel')) {
  754. App::import($type, $plugin . '.' . $plugin . 'AppModel', false, array(), $pluginPath . DS . $pluginPath . '_app_model.php');
  755. }
  756. $path = $pluginPath . DS . 'models' . DS;
  757. }
  758. return array('class' => null, 'suffix' => null, 'path' => $path);
  759. break;
  760. case 'behavior':
  761. if ($plugin) {
  762. $path = $pluginPath . DS . 'models' . DS . 'behaviors' . DS;
  763. }
  764. return array('class' => $type, 'suffix' => null, 'path' => $path);
  765. break;
  766. case 'datasource':
  767. if ($plugin) {
  768. $path = $pluginPath . DS . 'models' . DS . 'datasources' . DS;
  769. }
  770. return array('class' => $type, 'suffix' => null, 'path' => $path);
  771. case 'controller':
  772. App::import($type, 'AppController', false);
  773. if ($plugin) {
  774. App::import($type, $plugin . '.' . $plugin . 'AppController', false, array(), $pluginPath . DS . $pluginPath . '_app_controller.php');
  775. $path = $pluginPath . DS . 'controllers' . DS;
  776. }
  777. return array('class' => $type, 'suffix' => $type, 'path' => $path);
  778. break;
  779. case 'component':
  780. App::import('Core', 'Component', false);
  781. if ($plugin) {
  782. $path = $pluginPath . DS . 'controllers' . DS . 'components' . DS;
  783. }
  784. return array('class' => $type, 'suffix' => null, 'path' => $path);
  785. break;
  786. case 'lib':
  787. if ($plugin) {
  788. $path = $pluginPath . DS . 'libs' . DS;
  789. }
  790. return array('class' => null, 'suffix' => null, 'path' => $path);
  791. break;
  792. case 'view':
  793. App::import('View', 'View', false);
  794. if ($plugin) {
  795. $path = $pluginPath . DS . 'views' . DS;
  796. }
  797. return array('class' => $type, 'suffix' => null, 'path' => $path);
  798. break;
  799. case 'helper':
  800. if (!class_exists('AppHelper')) {
  801. App::import($type, 'AppHelper', false);
  802. }
  803. if ($plugin) {
  804. $path = $pluginPath . DS . 'views' . DS . 'helpers' . DS;
  805. }
  806. return array('class' => $type, 'suffix' => null, 'path' => $path);
  807. break;
  808. case 'shell':
  809. if (!class_exists('Shell')) {
  810. App::import($type, 'Shell', false);
  811. }
  812. if (!class_exists('AppShell')) {
  813. App::import($type, 'AppShell', false);
  814. }
  815. if ($plugin) {
  816. $path = $pluginPath . DS . 'console' . DS . 'shells' . DS;
  817. }
  818. return array('class' => $type, 'suffix' => null, 'path' => $path);
  819. break;
  820. case 'vendor':
  821. if ($plugin) {
  822. $path = $pluginPath . DS . 'vendors' . DS;
  823. }
  824. return array('class' => null, 'suffix' => null, 'path' => $path);
  825. break;
  826. default:
  827. $type = $suffix = $path = null;
  828. break;
  829. }
  830. return array('class' => null, 'suffix' => null, 'path' => null);
  831. }
  832. /**
  833. * Returns default search paths.
  834. *
  835. * @param string $type type of object to be searched
  836. * @return array list of paths
  837. */
  838. private static function __paths($type) {
  839. $type = strtolower($type);
  840. $paths = array();
  841. if ($type === 'core') {
  842. return App::core('libs');
  843. }
  844. if (isset(self::${$type . 's'})) {
  845. return self::${$type . 's'};
  846. }
  847. return $paths;
  848. }
  849. /**
  850. * Removes file location from map if the file has been deleted.
  851. *
  852. * @param string $name name of object
  853. * @param string $type type of object
  854. * @param string $plugin camelized name of plugin
  855. * @return void
  856. */
  857. private static function __remove($name, $type, $plugin) {
  858. if ($plugin) {
  859. unset(self::$__map['Plugin'][$plugin][$type][$name]);
  860. } else {
  861. unset(self::$__map[$type][$name]);
  862. }
  863. }
  864. /**
  865. * Returns an array of filenames of PHP files in the given directory.
  866. *
  867. * @param string $path Path to scan for files
  868. * @param string $suffix if false, return only directories. if string, match and return files
  869. * @return array List of directories or files in directory
  870. */
  871. private static function __list($path, $suffix = false, $extension = false) {
  872. App::uses('Folder', 'Utility');
  873. $items = array();
  874. $Folder = new Folder($path);
  875. $contents = $Folder->read(false, true);
  876. if (is_array($contents)) {
  877. if (!$suffix) {
  878. return $contents[0];
  879. } else {
  880. foreach ($contents[1] as $item) {
  881. if (substr($item, - strlen($suffix)) === $suffix) {
  882. if ($extension) {
  883. $items[] = $item;
  884. } else {
  885. $items[] = substr($item, 0, strlen($item) - strlen($suffix));
  886. }
  887. }
  888. }
  889. }
  890. }
  891. return $items;
  892. }
  893. /**
  894. * Object destructor.
  895. *
  896. * Writes cache file if changes have been made to the $__map or $__paths
  897. *
  898. * @return void
  899. */
  900. public static function shutdown() {
  901. if (self::$__cache && self::$_cacheChange) {
  902. Cache::write('file_map', array_filter(self::$__map), '_cake_core_');
  903. }
  904. if (self::$__cache && self::$_objectCacheChange) {
  905. Cache::write('object_map', self::$__objects, '_cake_core_');
  906. }
  907. }
  908. }