App.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. <?php
  2. /**
  3. * App class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Core
  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 management, 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. * ### Packages
  29. *
  30. * CakePHP is organized around the idea of packages, each class belongs to a package or folder where other
  31. * classes reside. You can configure each package location in your application using `App::build('APackage/SubPackage', $paths)`
  32. * to inform the framework where should each class be loaded. Almost every class in the CakePHP framework can be swapped
  33. * by your own compatible implementation. If you wish to use you own class instead of the classes the framework provides,
  34. * just add the class to your libs folder mocking the directory location of where CakePHP expects to find it.
  35. *
  36. * For instance if you'd like to use your own HttpSocket class, put it under
  37. *
  38. * app/Network/Http/HttpSocket.php
  39. *
  40. * ### Inspecting loaded paths
  41. *
  42. * You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
  43. * controller paths.
  44. *
  45. * It is also possible to inspect paths for plugin classes, for instance, to see a plugin's helpers you would call
  46. * `App::path('View/Helper', 'MyPlugin')`
  47. *
  48. * ### Locating plugins and themes
  49. *
  50. * Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will
  51. * give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the
  52. * `purple` theme.
  53. *
  54. * ### Inspecting known objects
  55. *
  56. * You can find out which objects App knows about using App::objects('Controller') for example to find
  57. * which application controllers App knows about.
  58. *
  59. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
  60. * @package Cake.Core
  61. */
  62. class App {
  63. /**
  64. * Append paths
  65. *
  66. * @constant APPEND
  67. */
  68. const APPEND = 'append';
  69. /**
  70. * Prepend paths
  71. *
  72. * @constant PREPEND
  73. */
  74. const PREPEND = 'prepend';
  75. /**
  76. * Reset paths instead of merging
  77. *
  78. * @constant RESET
  79. */
  80. const RESET = true;
  81. /**
  82. * List of object types and their properties
  83. *
  84. * @var array
  85. */
  86. public static $types = array(
  87. 'class' => array('extends' => null, 'core' => true),
  88. 'file' => array('extends' => null, 'core' => true),
  89. 'model' => array('extends' => 'AppModel', 'core' => false),
  90. 'behavior' => array('suffix' => 'Behavior', 'extends' => 'Model/ModelBehavior', 'core' => true),
  91. 'controller' => array('suffix' => 'Controller', 'extends' => 'AppController', 'core' => true),
  92. 'component' => array('suffix' => 'Component', 'extends' => null, 'core' => true),
  93. 'lib' => array('extends' => null, 'core' => true),
  94. 'view' => array('suffix' => 'View', 'extends' => null, 'core' => true),
  95. 'helper' => array('suffix' => 'Helper', 'extends' => 'AppHelper', 'core' => true),
  96. 'vendor' => array('extends' => null, 'core' => true),
  97. 'shell' => array('suffix' => 'Shell', 'extends' => 'Shell', 'core' => true),
  98. 'plugin' => array('extends' => null, 'core' => true)
  99. );
  100. /**
  101. * Paths to search for files.
  102. *
  103. * @var array
  104. */
  105. public static $search = array();
  106. /**
  107. * Whether or not to return the file that is loaded.
  108. *
  109. * @var boolean
  110. */
  111. public static $return = false;
  112. /**
  113. * Holds key/value pairs of $type => file path.
  114. *
  115. * @var array
  116. */
  117. protected static $_map = array();
  118. /**
  119. * Holds and key => value array of object types.
  120. *
  121. * @var array
  122. */
  123. protected static $_objects = array();
  124. /**
  125. * Holds the location of each class
  126. *
  127. * @var array
  128. */
  129. protected static $_classMap = array();
  130. /**
  131. * Holds the possible paths for each package name
  132. *
  133. * @var array
  134. */
  135. protected static $_packages = array();
  136. /**
  137. * Holds the templates for each customizable package path in the application
  138. *
  139. * @var array
  140. */
  141. protected static $_packageFormat = array();
  142. /**
  143. * Maps an old style CakePHP class type to the corresponding package
  144. *
  145. * @var array
  146. */
  147. public static $legacy = array(
  148. 'models' => 'Model',
  149. 'behaviors' => 'Model/Behavior',
  150. 'datasources' => 'Model/Datasource',
  151. 'controllers' => 'Controller',
  152. 'components' => 'Controller/Component',
  153. 'views' => 'View',
  154. 'helpers' => 'View/Helper',
  155. 'shells' => 'Console/Command',
  156. 'libs' => 'Lib',
  157. 'vendors' => 'Vendor',
  158. 'plugins' => 'Plugin',
  159. );
  160. /**
  161. * Indicates whether the class cache should be stored again because of an addition to it
  162. *
  163. * @var boolean
  164. */
  165. protected static $_cacheChange = false;
  166. /**
  167. * Indicates whether the object cache should be stored again because of an addition to it
  168. *
  169. * @var boolean
  170. */
  171. protected static $_objectCacheChange = false;
  172. /**
  173. * Indicates the the Application is in the bootstrapping process. Used to better cache
  174. * loaded classes while the cache libraries have not been yet initialized
  175. *
  176. * @var boolean
  177. */
  178. public static $bootstrapping = false;
  179. /**
  180. * Used to read information stored path
  181. *
  182. * Usage:
  183. *
  184. * `App::path('Model'); will return all paths for models`
  185. *
  186. * `App::path('Model/Datasource', 'MyPlugin'); will return the path for datasources under the 'MyPlugin' plugin`
  187. *
  188. * @param string $type type of path
  189. * @param string $plugin name of plugin
  190. * @return string array
  191. */
  192. public static function path($type, $plugin = null) {
  193. if (!empty(self::$legacy[$type])) {
  194. $type = self::$legacy[$type];
  195. }
  196. if (!empty($plugin)) {
  197. $path = array();
  198. $pluginPath = self::pluginPath($plugin);
  199. $packageFormat= self::_packageFormat();
  200. if (!empty($packageFormat[$type])) {
  201. foreach ($packageFormat[$type] as $f) {
  202. $path[] = sprintf($f, $pluginPath);
  203. }
  204. }
  205. $path[] = $pluginPath . 'Lib' . DS . $type . DS;
  206. return $path;
  207. }
  208. if (!isset(self::$_packages[$type])) {
  209. return array();
  210. }
  211. return self::$_packages[$type];
  212. }
  213. /**
  214. * Get all the currently loaded paths from App. Useful for inspecting
  215. * or storing all paths App knows about. For a paths to a specific package
  216. * use App::path()
  217. *
  218. * @return array An array of packages and their associated paths.
  219. */
  220. public static function paths() {
  221. return self::$_packages;
  222. }
  223. /**
  224. * Sets up each package location on the file system. You can configure multiple search paths
  225. * for each package, those will be used to look for files one folder at a time in the specified order
  226. * All paths should be terminated with a Directory separator
  227. *
  228. * Usage:
  229. *
  230. * `App::build(array(Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
  231. *
  232. * `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
  233. *
  234. * `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers`
  235. *
  236. * If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
  237. *
  238. * @param array $paths associative array with package names as keys and a list of directories for new search paths
  239. * @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths, [default] App::PREPEND
  240. * @return void
  241. */
  242. public static function build($paths = array(), $mode = App::PREPEND) {
  243. //Provides Backwards compatibility for old-style package names
  244. $legacyPaths = array();
  245. foreach ($paths as $type => $path) {
  246. if (!empty(self::$legacy[$type])) {
  247. $type = self::$legacy[$type];
  248. }
  249. $legacyPaths[$type] = $path;
  250. }
  251. $paths = $legacyPaths;
  252. if ($mode === App::RESET) {
  253. foreach ($paths as $type => $new) {
  254. self::$_packages[$type] = (array)$new;
  255. self::objects($type, null, false);
  256. }
  257. return;
  258. }
  259. $packageFormat = self::_packageFormat();
  260. $defaults = array();
  261. foreach ($packageFormat as $package => $format) {
  262. foreach ($format as $f) {
  263. $defaults[$package][] = sprintf($f, APP);
  264. }
  265. }
  266. if (empty($paths)) {
  267. self::$_packages = $defaults;
  268. return;
  269. }
  270. foreach ($defaults as $type => $default) {
  271. if (!empty(self::$_packages[$type])) {
  272. $path = self::$_packages[$type];
  273. }
  274. if (!empty($paths[$type])) {
  275. $newPath = (array)$paths[$type];
  276. if ($mode === App::PREPEND) {
  277. $path = array_merge($newPath, $path);
  278. } else {
  279. $path = array_merge($path, $newPath);
  280. }
  281. $path = array_values(array_unique($path));
  282. }
  283. self::$_packages[$type] = $path;
  284. }
  285. }
  286. /**
  287. * Gets the path that a plugin is on. Searches through the defined plugin paths.
  288. *
  289. * Usage:
  290. *
  291. * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
  292. *
  293. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  294. * @return string full path to the plugin.
  295. */
  296. public static function pluginPath($plugin) {
  297. return CakePlugin::path($plugin);
  298. }
  299. /**
  300. * Finds the path that a theme is on. Searches through the defined theme paths.
  301. *
  302. * Usage:
  303. *
  304. * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
  305. *
  306. * @param string $theme theme name to find the path of.
  307. * @return string full path to the theme.
  308. */
  309. public static function themePath($theme) {
  310. $themeDir = 'Themed' . DS . Inflector::camelize($theme);
  311. foreach (self::$_packages['View'] as $path) {
  312. if (is_dir($path . $themeDir)) {
  313. return $path . $themeDir . DS ;
  314. }
  315. }
  316. return self::$_packages['View'][0] . $themeDir . DS;
  317. }
  318. /**
  319. * Returns the full path to a package inside the CakePHP core
  320. *
  321. * Usage:
  322. *
  323. * `App::core('Cache/Engine'); will return the full path to the cache engines package`
  324. *
  325. * @param string $type
  326. * @return string full path to package
  327. */
  328. public static function core($type) {
  329. return array(CAKE . str_replace('/', DS, $type) . DS);
  330. }
  331. /**
  332. * Returns an array of objects of the given type.
  333. *
  334. * Example usage:
  335. *
  336. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  337. *
  338. * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  339. *
  340. * You can also search only within a plugin's objects by using the plugin dot
  341. * syntax.
  342. *
  343. * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  344. *
  345. * When scanning directories, files and directories beginning with `.` will be excluded as these
  346. * are commonly used by version control systems.
  347. *
  348. * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  349. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  350. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  351. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  352. */
  353. public static function objects($type, $path = null, $cache = true) {
  354. $extension = '/\.php$/';
  355. $includeDirectories = false;
  356. $name = $type;
  357. if ($type === 'plugin') {
  358. $type = 'plugins';
  359. }
  360. if ($type == 'plugins') {
  361. $extension = '/.*/';
  362. $includeDirectories = true;
  363. }
  364. list($plugin, $type) = pluginSplit($type);
  365. if (isset(self::$legacy[$type . 's'])) {
  366. $type = self::$legacy[$type . 's'];
  367. }
  368. if ($type === 'file' && !$path) {
  369. return false;
  370. } elseif ($type === 'file') {
  371. $extension = '/\.php$/';
  372. $name = $type . str_replace(DS, '', $path);
  373. }
  374. if (empty(self::$_objects) && $cache === true) {
  375. self::$_objects = Cache::read('object_map', '_cake_core_');
  376. }
  377. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  378. if ($cache !== true || !isset(self::$_objects[$cacheLocation][$name])) {
  379. $objects = array();
  380. if (empty($path)) {
  381. $path = self::path($type, $plugin);
  382. if (empty($plugin)) {
  383. $path = array_merge($path, App::core($type));
  384. }
  385. }
  386. foreach ((array)$path as $dir) {
  387. if ($dir != APP && is_dir($dir)) {
  388. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  389. foreach ($files as $file) {
  390. $fileName = basename($file);
  391. if (!$file->isDot() && $fileName[0] !== '.') {
  392. $isDir = $file->isDir() ;
  393. if ($isDir && $includeDirectories) {
  394. $objects[] = $fileName;
  395. } elseif (!$includeDirectories && !$isDir) {
  396. $objects[] = substr($fileName, 0, -4);
  397. }
  398. }
  399. }
  400. }
  401. }
  402. if ($type !== 'file') {
  403. foreach ($objects as $key => $value) {
  404. $objects[$key] = Inflector::camelize($value);
  405. }
  406. }
  407. sort($objects);
  408. if ($plugin) {
  409. return $objects;
  410. }
  411. self::$_objects[$cacheLocation][$name] = $objects;
  412. if ($cache) {
  413. self::$_objectCacheChange = true;
  414. }
  415. }
  416. return self::$_objects[$cacheLocation][$name];
  417. }
  418. /**
  419. * Declares a package for a class. This package location will be used
  420. * by the automatic class loader if the class is tried to be used
  421. *
  422. * Usage:
  423. *
  424. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  425. *
  426. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  427. *
  428. * @param string $className the name of the class to configure package for
  429. * @param string $location the package name
  430. * @return void
  431. */
  432. public static function uses($className, $location) {
  433. self::$_classMap[$className] = $location;
  434. }
  435. /**
  436. * Method to handle the automatic class loading. It will look for each class' package
  437. * defined using App::uses() and with this information it will resolve the package name to a full path
  438. * to load the class from. File name for each class should follow the class name. For instance,
  439. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  440. *
  441. * @param string $className the name of the class to load
  442. * @return boolean
  443. */
  444. public static function load($className) {
  445. if (!isset(self::$_classMap[$className])) {
  446. return false;
  447. }
  448. if ($file = self::_mapped($className)) {
  449. return include $file;
  450. }
  451. $parts = explode('.', self::$_classMap[$className], 2);
  452. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  453. $paths = self::path($package, $plugin);
  454. if (empty($plugin)) {
  455. $appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
  456. $paths[] = $appLibs . $package . DS;
  457. $paths[] = CAKE . $package . DS;
  458. }
  459. foreach ($paths as $path) {
  460. $file = $path . $className . '.php';
  461. if (file_exists($file)) {
  462. self::_map($file, $className);
  463. return include $file;
  464. }
  465. }
  466. //To help apps migrate to 2.0 old style file names are allowed
  467. foreach ($paths as $path) {
  468. $underscored = Inflector::underscore($className);
  469. $tries = array($path . $underscored . '.php');
  470. $parts = explode('_', $underscored);
  471. if (count($parts) > 1) {
  472. array_pop($parts);
  473. $tries[] = $path . implode('_', $parts) . '.php';
  474. }
  475. foreach ($tries as $file) {
  476. if (file_exists($file)) {
  477. self::_map($file, $className);
  478. return include $file;
  479. }
  480. }
  481. }
  482. return false;
  483. }
  484. /**
  485. * Returns the package name where a class was defined to be located at
  486. *
  487. * @param string $className name of the class to obtain the package name from
  488. * @return string package name or null if not declared
  489. */
  490. public static function location($className) {
  491. if (!empty(self::$_classMap[$className])) {
  492. return self::$_classMap[$className];
  493. }
  494. return null;
  495. }
  496. /**
  497. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  498. * not construct any classes contained in the files. It will only find and require() the file.
  499. *
  500. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
  501. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  502. * an single array to $type,
  503. * @param string $name Name of the Class or a unique name for the file
  504. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  505. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  506. * $ext allows setting the extension of the file name
  507. * based on Inflector::underscore($name) . ".$ext";
  508. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  509. * @param string $file full name of the file to search for including extension
  510. * @param boolean $return, return the loaded file, the file must have a return
  511. * statement in it to work: return $variable;
  512. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  513. */
  514. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  515. $ext = null;
  516. if (is_array($type)) {
  517. extract($type, EXTR_OVERWRITE);
  518. }
  519. if (is_array($parent)) {
  520. extract($parent, EXTR_OVERWRITE);
  521. }
  522. if ($name == null && $file == null) {
  523. return false;
  524. }
  525. if (is_array($name)) {
  526. foreach ($name as $class) {
  527. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  528. return false;
  529. }
  530. }
  531. return true;
  532. }
  533. $originalType = strtolower($type);
  534. $specialPackage = in_array($originalType, array('file', 'vendor'));
  535. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  536. $type = self::$legacy[$originalType . 's'];
  537. }
  538. list($plugin, $name) = pluginSplit($name);
  539. if (!empty($plugin)) {
  540. if (!CakePlugin::loaded($plugin)) {
  541. return false;
  542. }
  543. }
  544. if (!$specialPackage) {
  545. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  546. }
  547. if ($originalType == 'file' && !empty($file)) {
  548. return self::_loadFile($name, $plugin, $search, $file, $return);
  549. }
  550. if ($originalType == 'vendor') {
  551. return self::_loadVendor($name, $plugin, $file, $ext);
  552. }
  553. return false;
  554. }
  555. /**
  556. * Helper function to include classes
  557. * This is a compatibility wrapper around using App::uses() and automatic class loading
  558. *
  559. * @param string $name unique name of the file for identifying it inside the application
  560. * @param string $plugin camel cased plugin name if any
  561. * @param string $type name of the packed where the class is located
  562. * @param string $originalType type name as supplied initially by the user
  563. * @param boolean $parent whether to load the class parent or not
  564. * @return boolean true indicating the successful load and existence of the class
  565. */
  566. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  567. if ($type == 'Console/Command' && $name == 'Shell') {
  568. $type = 'Console';
  569. } else if (isset(self::$types[$originalType]['suffix'])) {
  570. $suffix = self::$types[$originalType]['suffix'];
  571. $name .= ($suffix == $name) ? '' : $suffix;
  572. }
  573. if ($parent && isset(self::$types[$originalType]['extends'])) {
  574. $extends = self::$types[$originalType]['extends'];
  575. $extendType = $type;
  576. if (strpos($extends, '/') !== false) {
  577. $parts = explode('/', $extends);
  578. $extends = array_pop($parts);
  579. $extendType = implode('/', $parts);
  580. }
  581. App::uses($extends, $extendType);
  582. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  583. App::uses($plugin . $extends, $plugin . '.' .$type);
  584. }
  585. }
  586. if ($plugin) {
  587. $plugin .= '.';
  588. }
  589. $name = Inflector::camelize($name);
  590. App::uses($name, $plugin . $type);
  591. return class_exists($name);
  592. }
  593. /**
  594. * Helper function to include single files
  595. *
  596. * @param string $name unique name of the file for identifying it inside the application
  597. * @param string $plugin camel cased plugin name if any
  598. * @param array $search list of paths to search the file into
  599. * @param string $file filename if known, the $name param will be used otherwise
  600. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  601. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  602. */
  603. protected function _loadFile($name, $plugin, $search, $file, $return) {
  604. $mapped = self::_mapped($name, $plugin);
  605. if ($mapped) {
  606. $file = $mapped;
  607. } else if (!empty($search)) {
  608. foreach ($search as $path) {
  609. $found = false;
  610. if (file_exists($path . $file)) {
  611. $file = $path . $file;
  612. $found = true;
  613. break;
  614. }
  615. if (empty($found)) {
  616. $file = false;
  617. }
  618. }
  619. }
  620. if (!empty($file) && file_exists($file)) {
  621. self::_map($file, $name, $plugin);
  622. $returnValue = include $file;
  623. if ($return) {
  624. return $returnValue;
  625. }
  626. return (bool) $returnValue;
  627. }
  628. return false;
  629. }
  630. /**
  631. * Helper function to load files from vendors folders
  632. *
  633. * @param string $name unique name of the file for identifying it inside the application
  634. * @param string $plugin camel cased plugin name if any
  635. * @param string $file file name if known
  636. * @param string $ext file extension if known
  637. * @return boolean true if the file was loaded successfully, false otherwise
  638. */
  639. protected static function _loadVendor($name, $plugin, $file, $ext) {
  640. if ($mapped = self::_mapped($name, $plugin)) {
  641. return (bool) include_once($mapped);
  642. }
  643. $fileTries = array();
  644. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  645. if (empty($ext)) {
  646. $ext = 'php';
  647. }
  648. if (empty($file)) {
  649. $fileTries[] = $name . '.' . $ext;
  650. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  651. } else {
  652. $fileTries[] = $file;
  653. }
  654. foreach ($fileTries as $file) {
  655. foreach ($paths as $path) {
  656. if (file_exists($path . $file)) {
  657. self::_map($path . $file, $name, $plugin);
  658. return (bool) include($path . $file);
  659. }
  660. }
  661. }
  662. return false;
  663. }
  664. /**
  665. * Initializes the cache for App, registers a shutdown function.
  666. *
  667. * @return void
  668. */
  669. public static function init() {
  670. self::$_map += (array)Cache::read('file_map', '_cake_core_');
  671. self::$_objects += (array)Cache::read('object_map', '_cake_core_');
  672. register_shutdown_function(array('App', 'shutdown'));
  673. }
  674. /**
  675. * Maps the $name to the $file.
  676. *
  677. * @param string $file full path to file
  678. * @param string $name unique name for this map
  679. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  680. * @return void
  681. */
  682. protected static function _map($file, $name, $plugin = null) {
  683. if ($plugin) {
  684. self::$_map['Plugin'][$plugin][$name] = $file;
  685. } else {
  686. self::$_map[$name] = $file;
  687. }
  688. if (!self::$bootstrapping) {
  689. self::$_cacheChange = true;
  690. }
  691. }
  692. /**
  693. * Returns a file's complete path.
  694. *
  695. * @param string $name unique name
  696. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  697. * @return mixed file path if found, false otherwise
  698. */
  699. protected static function _mapped($name, $plugin = null) {
  700. if ($plugin) {
  701. if (isset(self::$_map['Plugin'][$plugin][$name])) {
  702. return self::$_map['Plugin'][$plugin][$name];
  703. }
  704. return false;
  705. }
  706. if (isset(self::$_map[$name])) {
  707. return self::$_map[$name];
  708. }
  709. return false;
  710. }
  711. protected static function _packageFormat() {
  712. if (empty(self::$_packageFormat)) {
  713. self::$_packageFormat = array(
  714. 'Model' => array(
  715. '%s' . 'Model' . DS,
  716. '%s' . 'models' . DS
  717. ),
  718. 'Model/Behavior' => array(
  719. '%s' . 'Model' . DS . 'Behavior' . DS,
  720. '%s' . 'models' . DS . 'behaviors' . DS
  721. ),
  722. 'Model/Datasource' => array(
  723. '%s' . 'Model' . DS . 'Datasource' . DS,
  724. '%s' . 'models' . DS . 'datasources' . DS
  725. ),
  726. 'Model/Datasource/Database' => array(
  727. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
  728. '%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
  729. ),
  730. 'Model/Datasource/Session' => array(
  731. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
  732. '%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
  733. ),
  734. 'Controller' => array(
  735. '%s' . 'Controller' . DS,
  736. '%s' . 'controllers' . DS
  737. ),
  738. 'Controller/Component' => array(
  739. '%s' . 'Controller' . DS . 'Component' . DS,
  740. '%s' . 'controllers' . DS . 'components' . DS
  741. ),
  742. 'Controller/Component/Auth' => array(
  743. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
  744. '%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
  745. ),
  746. 'View' => array(
  747. '%s' . 'View' . DS,
  748. '%s' . 'views' . DS
  749. ),
  750. 'View/Helper' => array(
  751. '%s' . 'View' . DS . 'Helper' . DS,
  752. '%s' . 'views' . DS . 'helpers' . DS
  753. ),
  754. 'Console' => array(
  755. '%s' . 'Console' . DS,
  756. '%s' . 'console' . DS
  757. ),
  758. 'Console/Command' => array(
  759. '%s' . 'Console' . DS . 'Command' . DS,
  760. '%s' . 'console' . DS . 'shells' . DS,
  761. ),
  762. 'Console/Command/Task' => array(
  763. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
  764. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
  765. ),
  766. 'Lib' => array(
  767. '%s' . 'Lib' . DS,
  768. '%s' . 'libs' . DS
  769. ),
  770. 'locales' => array(
  771. '%s' . 'Locale' . DS,
  772. '%s' . 'locale' . DS
  773. ),
  774. 'Vendor' => array(
  775. '%s' . 'Vendor' . DS, VENDORS
  776. ),
  777. 'Plugin' => array(
  778. APP . 'Plugin' . DS,
  779. APP . 'plugins' . DS,
  780. dirname(dirname(CAKE)) . DS . 'plugins' . DS
  781. )
  782. );
  783. }
  784. return self::$_packageFormat;
  785. }
  786. /**
  787. * Object destructor.
  788. *
  789. * Writes cache file if changes have been made to the $_map
  790. *
  791. * @return void
  792. */
  793. public static function shutdown() {
  794. if (self::$_cacheChange) {
  795. Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
  796. }
  797. if (self::$_objectCacheChange) {
  798. Cache::write('object_map', self::$_objects, '_cake_core_');
  799. }
  800. }
  801. }