App.php 24 KB

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