App.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. 'Console/Command' => array(
  276. '%s' . 'console' . DS . 'shells' . DS,
  277. '%s' . 'vendors' . DS . 'shells' . DS,
  278. VENDORS . 'shells' . DS
  279. ),
  280. 'Console/Command/Task' => array(
  281. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS,
  282. '%s' . 'vendors' . DS . 'shells' . DS . 'tasks' . DS,
  283. VENDORS . 'shells' . DS . 'tasks' . DS
  284. ),
  285. 'libs' => array('%s' . 'libs' . DS),
  286. 'locales' => array('%s' . 'locale' . DS),
  287. 'vendors' => array('%s' . 'vendors' . DS, VENDORS),
  288. 'plugins' => array(APP . 'plugins' . DS, CAKE_CORE_INCLUDE_PATH . DS . 'plugins' . DS)
  289. );
  290. }
  291. if ($reset == true) {
  292. foreach ($paths as $type => $new) {
  293. if (!empty(self::$legacy[$type])) {
  294. $type = self::$legacy[$type];
  295. }
  296. self::$__packages[$type] = (array)$new;
  297. }
  298. return $paths;
  299. }
  300. $defaults = array();
  301. foreach (self::$__packageFormat as $package => $format) {
  302. foreach ($format as $f) {
  303. $defaults[$package][] = sprintf($f, APP);
  304. }
  305. }
  306. $mergeExclude = array('libs', 'locales', 'vendors', 'plugins');
  307. $appLibs = empty($paths['libs']) ? $defaults['libs'] : $paths['libs'];
  308. foreach ($defaults as $type => $default) {
  309. if (empty(self::$__packages[$type]) || empty($paths)) {
  310. self::$__packages[$type] = $default;
  311. }
  312. if (!empty($paths[$type])) {
  313. $path = array_merge((array)$paths[$type], self::$__packages[$type]);
  314. } else {
  315. $path = self::$__packages[$type];
  316. }
  317. self::$__packages[$type] = array_values(array_unique($path));
  318. }
  319. }
  320. /**
  321. * Get the path that a plugin is on. Searches through the defined plugin paths.
  322. *
  323. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  324. * @return string full path to the plugin.
  325. */
  326. public static function pluginPath($plugin) {
  327. $pluginDir = Inflector::underscore($plugin);
  328. foreach (self::$__packages['plugins'] as $pluginPath) {
  329. if (is_dir($pluginPath . $pluginDir)) {
  330. return $pluginPath . $pluginDir . DS ;
  331. }
  332. }
  333. return self::$__packages['plugins'][0] . $pluginDir . DS;
  334. }
  335. /**
  336. * Find the path that a theme is on. Search through the defined theme paths.
  337. *
  338. * @param string $theme lower_cased theme name to find the path of.
  339. * @return string full path to the theme.
  340. */
  341. public static function themePath($theme) {
  342. $themeDir = 'themed' . DS . Inflector::underscore($theme);
  343. foreach (self::$__packages['View'] as $path) {
  344. if (is_dir($path . $themeDir)) {
  345. return $path . $themeDir . DS ;
  346. }
  347. }
  348. return self::$__packages['View'][0] . $themeDir . DS;
  349. }
  350. /**
  351. * Returns a key/value list of all paths where core libs are found.
  352. * Passing $type only returns the values for a given value of $key.
  353. *
  354. * @param string $type valid values are: 'cake' ,'plugins', 'vendors' and 'shells'
  355. * @return array numeric keyed array of core lib paths
  356. */
  357. public static function core($type = null) {
  358. static $paths = false;
  359. if (!$paths) {
  360. $paths = array();
  361. $root = dirname(dirname(LIBS)) . DS;
  362. $paths['cake'][] = LIBS;
  363. $paths['plugins'][] = $root . 'plugins' . DS;
  364. $paths['vendors'][] = $root . 'vendors' . DS;
  365. $paths['shells'][] = LIBS . 'Console' . DS . 'Command' . DS;
  366. // Provide BC path to vendors/shells
  367. $paths['shells'][] = $root . 'vendors' . DS . 'shells' . DS;
  368. }
  369. if ($type) {
  370. return isset($paths[$type]) ? $paths[$type] : array(LIBS . $type . DS);
  371. }
  372. return $paths;
  373. }
  374. /**
  375. * Returns an array of objects of the given type.
  376. *
  377. * Example usage:
  378. *
  379. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  380. *
  381. * You can also search only within a plugin's objects by using the plugin dot
  382. * syntax.
  383. *
  384. * `App::objects('MyPlugin.model');` returns `array('Post', 'Comment');`
  385. *
  386. * @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
  387. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen
  388. * type will be used.
  389. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  390. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  391. */
  392. public static function objects($type, $path = null, $cache = true) {
  393. $objects = array();
  394. $extension = '/\.php$/';
  395. $includeDirectories = false;
  396. $name = $type;
  397. if (isset(self::$legacy[$type . 's'])) {
  398. $type = self::$legacy[$type . 's'];
  399. }
  400. if ($type === 'plugin') {
  401. $type = 'plugins';
  402. }
  403. if ($type == 'plugins') {
  404. $extension = '/.*/';
  405. $includeDirectories = true;
  406. }
  407. list($plugin, $type) = pluginSplit($type);
  408. if ($type === 'file' && !$path) {
  409. return false;
  410. } elseif ($type === 'file') {
  411. $extension = '/\.php$/';
  412. $name = $type . str_replace(DS, '', $path);
  413. }
  414. if (empty(self::$__objects) && $cache === true) {
  415. self::$__objects = Cache::read('object_map', '_cake_core_');
  416. }
  417. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  418. if ($cache !== true || !isset(self::$__objects[$cacheLocation][$name])) {
  419. $objects = array();
  420. if (empty($path)) {
  421. $path = self::path($type, $plugin);
  422. }
  423. $items = array();
  424. foreach ((array)$path as $dir) {
  425. if ($dir != APP && is_dir($dir)) {
  426. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  427. foreach ($files as $file) {
  428. if (!$file->isDot()) {
  429. $isDir = $file->isDir() ;
  430. if ($isDir && $includeDirectories) {
  431. $objects[] = basename($file);
  432. } elseif (!$includeDirectories && !$isDir) {
  433. $objects[] = substr(basename($file), 0, -4);
  434. }
  435. }
  436. }
  437. }
  438. }
  439. if ($type !== 'file') {
  440. foreach ($objects as $key => $value) {
  441. $objects[$key] = Inflector::camelize($value);
  442. }
  443. }
  444. if ($cache === true) {
  445. self::$__cache = true;
  446. }
  447. if ($plugin) {
  448. return $objects;
  449. }
  450. self::$__objects[$cacheLocation][$name] = $objects;
  451. self::$_objectCacheChange = true;
  452. }
  453. return self::$__objects[$cacheLocation][$name];
  454. }
  455. /**
  456. * Allows you to modify the object listings that App maintains inside of it
  457. * Useful for testing
  458. *
  459. * @param string $type Type of object listing you are changing
  460. * @param array $values The values $type should be set to.
  461. * @return void
  462. */
  463. public static function setObjects($type, $values) {
  464. list($plugin, $type) = pluginSplit($type);
  465. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  466. self::$__objects[$cacheLocation][$type] = $values;
  467. }
  468. public static function uses($className, $location) {
  469. self::$__classMap[$className] = $location;
  470. }
  471. public static function load($className) {
  472. if (isset(self::$__classMap[$className])) {
  473. if ($file = self::__mapped($className)) {
  474. return include $file;
  475. }
  476. $parts = explode('.', self::$__classMap[$className], 2);
  477. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  478. $paths = self::path($package, $plugin);
  479. if (empty($plugin)) {
  480. $appLibs = empty(self::$__packages['libs']) ? APPLIBS : current(self::$__packages['libs']);
  481. $paths[] = $appLibs . self::$__classMap[$className] . DS;
  482. $paths[] = LIBS . self::$__classMap[$className] . DS;
  483. }
  484. foreach ($paths as $path) {
  485. $file = $path . $className . '.php';
  486. if (file_exists($file)) {
  487. self::__map($file, $className);
  488. return include $file;
  489. }
  490. }
  491. //To help apps migrate to 2.0 old style file names are allowed
  492. foreach ($paths as $path) {
  493. $underscored = Inflector::underscore($className);
  494. $tries = array($path . $underscored . '.php');
  495. $parts = explode('_', $underscored);
  496. if (count($parts) > 1) {
  497. array_pop($parts);
  498. $tries[] = $path . implode('_', $parts) . '.php';
  499. }
  500. foreach ($tries as $file) {
  501. if (file_exists($file)) {
  502. self::__map($file, $className);
  503. return include $file;
  504. }
  505. }
  506. }
  507. }
  508. return false;
  509. }
  510. /**
  511. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  512. * not construct any classes contained in the files. It will only find and require() the file.
  513. *
  514. * @link http://book.cakephp.org/view/934/Using-App-import
  515. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  516. * an single array to $type,
  517. * @param string $name Name of the Class or a unique name for the file
  518. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  519. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  520. * $ext allows setting the extension of the file name
  521. * based on Inflector::underscore($name) . ".$ext";
  522. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  523. * @param string $file full name of the file to search for including extension
  524. * @param boolean $return, return the loaded file, the file must have a return
  525. * statement in it to work: return $variable;
  526. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  527. */
  528. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  529. $plugin = $directory = null;
  530. if (is_array($type)) {
  531. extract($type, EXTR_OVERWRITE);
  532. }
  533. if (is_array($parent)) {
  534. extract($parent, EXTR_OVERWRITE);
  535. }
  536. if ($name === null && $file === null) {
  537. $name = $type;
  538. $type = 'Core';
  539. } elseif ($name === null) {
  540. $type = 'File';
  541. }
  542. if (is_array($name)) {
  543. foreach ($name as $class) {
  544. $tempType = $type;
  545. $plugin = null;
  546. if (strpos($class, '.') !== false) {
  547. $value = explode('.', $class);
  548. $count = count($value);
  549. if ($count > 2) {
  550. $tempType = $value[0];
  551. $plugin = $value[1] . '.';
  552. $class = $value[2];
  553. } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
  554. $tempType = $value[0];
  555. $class = $value[1];
  556. } else {
  557. $plugin = $value[0] . '.';
  558. $class = $value[1];
  559. }
  560. }
  561. if (!App::import($tempType, $plugin . $class, $parent)) {
  562. return false;
  563. }
  564. }
  565. return true;
  566. }
  567. if ($name != null && strpos($name, '.') !== false) {
  568. list($plugin, $name) = explode('.', $name);
  569. $plugin = Inflector::camelize($plugin);
  570. }
  571. self::$return = $return;
  572. if (isset($ext)) {
  573. $file = Inflector::underscore($name) . ".{$ext}";
  574. }
  575. $ext = self::__settings($type, $plugin, $parent);
  576. $className = $name;
  577. if (strpos($className, '/') !== false) {
  578. $className = substr($className, strrpos($className, '/') + 1);
  579. }
  580. if ($name != null && !class_exists($className . $ext['class'])) {
  581. if ($load = self::__mapped($name . $ext['class'], $type, $plugin)) {
  582. if (self::__load($load)) {
  583. if (self::$return) {
  584. return include($load);
  585. }
  586. return true;
  587. } else {
  588. self::__remove($name . $ext['class'], $type, $plugin);
  589. self::$__cache = true;
  590. }
  591. }
  592. if (!empty($search)) {
  593. self::$search = $search;
  594. } elseif ($plugin) {
  595. self::$search = self::__paths('plugin');
  596. } else {
  597. self::$search = self::__paths($type);
  598. }
  599. $find = $file;
  600. if ($find === null) {
  601. $find = Inflector::underscore($name . $ext['suffix']).'.php';
  602. if ($plugin) {
  603. $paths = self::$search;
  604. foreach ($paths as $key => $value) {
  605. self::$search[$key] = $value . $ext['path'];
  606. }
  607. }
  608. }
  609. if (strtolower($type) !== 'vendor' && empty($search) && self::__load($file)) {
  610. $directory = false;
  611. } else {
  612. $file = $find;
  613. $directory = self::__find($find, true);
  614. }
  615. if ($directory !== null) {
  616. self::$__cache = true;
  617. self::__map($directory . $file, $name . $ext['class'], $type, $plugin);
  618. if (self::$return) {
  619. return include($directory . $file);
  620. }
  621. return true;
  622. }
  623. return false;
  624. }
  625. return true;
  626. }
  627. /**
  628. * Initializes the cache for App, registers a shutdown function.
  629. *
  630. * @return void
  631. */
  632. public static function init() {
  633. self::$__map = (array)Cache::read('file_map', '_cake_core_');
  634. self::$__objects = (array)Cache::read('object_map', '_cake_core_');
  635. register_shutdown_function(array('App', 'shutdown'));
  636. }
  637. /**
  638. * Locates the $file in $__paths, searches recursively.
  639. *
  640. * @param string $file full file name
  641. * @param boolean $recursive search $__paths recursively
  642. * @return mixed boolean on fail, $file directory path on success
  643. */
  644. private static function __find($file, $recursive = true) {
  645. static $appPath = false;
  646. if (empty(self::$search)) {
  647. return null;
  648. } elseif (is_string(self::$search)) {
  649. self::$search = array(self::$search);
  650. }
  651. if (empty(self::$__paths)) {
  652. self::$__paths = Cache::read('dir_map', '_cake_core_');
  653. }
  654. foreach (self::$search as $path) {
  655. if ($appPath === false) {
  656. $appPath = rtrim(APP, DS);
  657. }
  658. $path = rtrim($path, DS);
  659. if ($path === $appPath) {
  660. $recursive = false;
  661. }
  662. if ($recursive === false) {
  663. if (self::__load($path . DS . $file)) {
  664. return $path . DS;
  665. }
  666. continue;
  667. }
  668. if (!isset(self::$__paths[$path])) {
  669. App::uses('Folder', 'Utility');
  670. $Folder = new Folder();
  671. $directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
  672. sort($directories);
  673. self::$__paths[$path] = $directories;
  674. }
  675. foreach (self::$__paths[$path] as $directory) {
  676. if (self::__load($directory . DS . $file)) {
  677. return $directory . DS;
  678. }
  679. }
  680. }
  681. return null;
  682. }
  683. /**
  684. * Attempts to load $file.
  685. *
  686. * @param string $file full path to file including file name
  687. * @return boolean
  688. * @access private
  689. */
  690. private static function __load($file) {
  691. if (empty($file)) {
  692. return false;
  693. }
  694. if (!self::$return && isset(self::$__loaded[$file])) {
  695. return true;
  696. }
  697. if (file_exists($file)) {
  698. if (!self::$return) {
  699. require($file);
  700. self::$__loaded[$file] = true;
  701. }
  702. return true;
  703. }
  704. return false;
  705. }
  706. /**
  707. * Maps the $name to the $file.
  708. *
  709. * @param string $file full path to file
  710. * @param string $name unique name for this map
  711. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  712. * @return void
  713. * @access private
  714. */
  715. private static function __map($file, $name, $plugin = null) {
  716. if ($plugin) {
  717. self::$__map['Plugin'][$plugin][$name] = $file;
  718. } else {
  719. self::$__map[$name] = $file;
  720. }
  721. self::$_cacheChange = true;
  722. }
  723. /**
  724. * Returns a file's complete path.
  725. *
  726. * @param string $name unique name
  727. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  728. * @return mixed, file path if found, false otherwise
  729. * @access private
  730. */
  731. private static function __mapped($name, $plugin = null) {
  732. if ($plugin) {
  733. if (isset(self::$__map['Plugin'][$plugin][$name])) {
  734. return self::$__map['Plugin'][$plugin][$name];
  735. }
  736. return false;
  737. }
  738. if (isset(self::$__map[$name])) {
  739. return self::$__map[$name];
  740. }
  741. return false;
  742. }
  743. /**
  744. * Loads parent classes based on $type.
  745. * Returns a prefix or suffix needed for loading files.
  746. *
  747. * @param string $type type of object
  748. * @param string $plugin camelized name of plugin
  749. * @param boolean $parent false will not attempt to load parent
  750. * @return array
  751. * @access private
  752. */
  753. private static function __settings($type, $plugin, $parent) {
  754. if (!$parent) {
  755. return array('class' => null, 'suffix' => null, 'path' => null);
  756. }
  757. if ($plugin) {
  758. $pluginPath = Inflector::underscore($plugin);
  759. }
  760. $path = null;
  761. $load = strtolower($type);
  762. switch ($load) {
  763. case 'model':
  764. App::uses('Model', 'Model');
  765. if (!class_exists('AppModel')) {
  766. App::import($type, 'AppModel', false);
  767. }
  768. if ($plugin) {
  769. if (!class_exists($plugin . 'AppModel')) {
  770. App::import($type, $plugin . '.' . $plugin . 'AppModel', false, array(), $pluginPath . DS . $pluginPath . '_app_model.php');
  771. }
  772. $path = $pluginPath . DS . 'models' . DS;
  773. }
  774. return array('class' => null, 'suffix' => null, 'path' => $path);
  775. break;
  776. case 'behavior':
  777. if ($plugin) {
  778. $path = $pluginPath . DS . 'models' . DS . 'behaviors' . DS;
  779. }
  780. return array('class' => $type, 'suffix' => null, 'path' => $path);
  781. break;
  782. case 'datasource':
  783. if ($plugin) {
  784. $path = $pluginPath . DS . 'models' . DS . 'datasources' . DS;
  785. }
  786. return array('class' => $type, 'suffix' => null, 'path' => $path);
  787. case 'controller':
  788. App::import($type, 'AppController', false);
  789. if ($plugin) {
  790. App::import($type, $plugin . '.' . $plugin . 'AppController', false, array(), $pluginPath . DS . $pluginPath . '_app_controller.php');
  791. $path = $pluginPath . DS . 'controllers' . DS;
  792. }
  793. return array('class' => $type, 'suffix' => $type, 'path' => $path);
  794. break;
  795. case 'component':
  796. App::import('Core', 'Component', false);
  797. if ($plugin) {
  798. $path = $pluginPath . DS . 'controllers' . DS . 'components' . DS;
  799. }
  800. return array('class' => $type, 'suffix' => null, 'path' => $path);
  801. break;
  802. case 'lib':
  803. if ($plugin) {
  804. $path = $pluginPath . DS . 'libs' . DS;
  805. }
  806. return array('class' => null, 'suffix' => null, 'path' => $path);
  807. break;
  808. case 'view':
  809. App::import('View', 'View', false);
  810. if ($plugin) {
  811. $path = $pluginPath . DS . 'views' . DS;
  812. }
  813. return array('class' => $type, 'suffix' => null, 'path' => $path);
  814. break;
  815. case 'helper':
  816. if (!class_exists('AppHelper')) {
  817. App::import($type, 'AppHelper', false);
  818. }
  819. if ($plugin) {
  820. $path = $pluginPath . DS . 'views' . DS . 'helpers' . DS;
  821. }
  822. return array('class' => $type, 'suffix' => null, 'path' => $path);
  823. break;
  824. case 'shell':
  825. if (!class_exists('Shell')) {
  826. App::import($type, 'Shell', false);
  827. }
  828. if (!class_exists('AppShell')) {
  829. App::import($type, 'AppShell', false);
  830. }
  831. if ($plugin) {
  832. $path = $pluginPath . DS . 'console' . DS . 'shells' . DS;
  833. }
  834. return array('class' => $type, 'suffix' => null, 'path' => $path);
  835. break;
  836. case 'vendor':
  837. if ($plugin) {
  838. $path = $pluginPath . DS . 'vendors' . DS;
  839. }
  840. return array('class' => null, 'suffix' => null, 'path' => $path);
  841. break;
  842. default:
  843. $type = $suffix = $path = null;
  844. break;
  845. }
  846. return array('class' => null, 'suffix' => null, 'path' => null);
  847. }
  848. /**
  849. * Returns default search paths.
  850. *
  851. * @param string $type type of object to be searched
  852. * @return array list of paths
  853. */
  854. private static function __paths($type) {
  855. $type = strtolower($type);
  856. $paths = array();
  857. if ($type === 'core') {
  858. return App::core('libs');
  859. }
  860. if (isset(self::${$type . 's'})) {
  861. return self::${$type . 's'};
  862. }
  863. return $paths;
  864. }
  865. /**
  866. * Removes file location from map if the file has been deleted.
  867. *
  868. * @param string $name name of object
  869. * @param string $type type of object
  870. * @param string $plugin camelized name of plugin
  871. * @return void
  872. */
  873. private static function __remove($name, $type, $plugin) {
  874. if ($plugin) {
  875. unset(self::$__map['Plugin'][$plugin][$type][$name]);
  876. } else {
  877. unset(self::$__map[$type][$name]);
  878. }
  879. }
  880. /**
  881. * Returns an array of filenames of PHP files in the given directory.
  882. *
  883. * @param string $path Path to scan for files
  884. * @param string $suffix if false, return only directories. if string, match and return files
  885. * @return array List of directories or files in directory
  886. */
  887. private static function __list($path, $suffix = false, $extension = false) {
  888. App::uses('Folder', 'Utility');
  889. $items = array();
  890. $Folder = new Folder($path);
  891. $contents = $Folder->read(false, true);
  892. if (is_array($contents)) {
  893. if (!$suffix) {
  894. return $contents[0];
  895. } else {
  896. foreach ($contents[1] as $item) {
  897. if (substr($item, - strlen($suffix)) === $suffix) {
  898. if ($extension) {
  899. $items[] = $item;
  900. } else {
  901. $items[] = substr($item, 0, strlen($item) - strlen($suffix));
  902. }
  903. }
  904. }
  905. }
  906. }
  907. return $items;
  908. }
  909. /**
  910. * Object destructor.
  911. *
  912. * Writes cache file if changes have been made to the $__map or $__paths
  913. *
  914. * @return void
  915. */
  916. public static function shutdown() {
  917. if (self::$__cache && self::$_cacheChange) {
  918. Cache::write('file_map', array_filter(self::$__map), '_cake_core_');
  919. }
  920. if (self::$__cache && self::$_objectCacheChange) {
  921. Cache::write('object_map', self::$__objects, '_cake_core_');
  922. }
  923. }
  924. }