App.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. }
  383. foreach ((array)$path as $dir) {
  384. if ($dir != APP && is_dir($dir)) {
  385. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  386. foreach ($files as $file) {
  387. $fileName = basename($file);
  388. if (!$file->isDot() && $fileName[0] !== '.') {
  389. $isDir = $file->isDir() ;
  390. if ($isDir && $includeDirectories) {
  391. $objects[] = $fileName;
  392. } elseif (!$includeDirectories && !$isDir) {
  393. $objects[] = substr($fileName, 0, -4);
  394. }
  395. }
  396. }
  397. }
  398. }
  399. if ($type !== 'file') {
  400. foreach ($objects as $key => $value) {
  401. $objects[$key] = Inflector::camelize($value);
  402. }
  403. }
  404. sort($objects);
  405. if ($plugin) {
  406. return $objects;
  407. }
  408. self::$_objects[$cacheLocation][$name] = $objects;
  409. if ($cache) {
  410. self::$_objectCacheChange = true;
  411. }
  412. }
  413. return self::$_objects[$cacheLocation][$name];
  414. }
  415. /**
  416. * Declares a package for a class. This package location will be used
  417. * by the automatic class loader if the class is tried to be used
  418. *
  419. * Usage:
  420. *
  421. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  422. *
  423. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  424. *
  425. * @param string $className the name of the class to configure package for
  426. * @param string $location the package name
  427. * @return void
  428. */
  429. public static function uses($className, $location) {
  430. self::$_classMap[$className] = $location;
  431. }
  432. /**
  433. * Method to handle the automatic class loading. It will look for each class' package
  434. * defined using App::uses() and with this information it will resolve the package name to a full path
  435. * to load the class from. File name for each class should follow the class name. For instance,
  436. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  437. *
  438. * @param string $className the name of the class to load
  439. * @return boolean
  440. */
  441. public static function load($className) {
  442. if (!isset(self::$_classMap[$className])) {
  443. return false;
  444. }
  445. if ($file = self::_mapped($className)) {
  446. return include $file;
  447. }
  448. $parts = explode('.', self::$_classMap[$className], 2);
  449. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  450. $paths = self::path($package, $plugin);
  451. if (empty($plugin)) {
  452. $appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
  453. $paths[] = $appLibs . $package . DS;
  454. $paths[] = CAKE . $package . DS;
  455. }
  456. foreach ($paths as $path) {
  457. $file = $path . $className . '.php';
  458. if (file_exists($file)) {
  459. self::_map($file, $className);
  460. return include $file;
  461. }
  462. }
  463. //To help apps migrate to 2.0 old style file names are allowed
  464. foreach ($paths as $path) {
  465. $underscored = Inflector::underscore($className);
  466. $tries = array($path . $underscored . '.php');
  467. $parts = explode('_', $underscored);
  468. if (count($parts) > 1) {
  469. array_pop($parts);
  470. $tries[] = $path . implode('_', $parts) . '.php';
  471. }
  472. foreach ($tries as $file) {
  473. if (file_exists($file)) {
  474. self::_map($file, $className);
  475. return include $file;
  476. }
  477. }
  478. }
  479. return false;
  480. }
  481. /**
  482. * Returns the package name where a class was defined to be located at
  483. *
  484. * @param string $className name of the class to obtain the package name from
  485. * @return string package name or null if not declared
  486. */
  487. public static function location($className) {
  488. if (!empty(self::$_classMap[$className])) {
  489. return self::$_classMap[$className];
  490. }
  491. return null;
  492. }
  493. /**
  494. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  495. * not construct any classes contained in the files. It will only find and require() the file.
  496. *
  497. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
  498. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  499. * an single array to $type,
  500. * @param string $name Name of the Class or a unique name for the file
  501. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  502. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  503. * $ext allows setting the extension of the file name
  504. * based on Inflector::underscore($name) . ".$ext";
  505. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  506. * @param string $file full name of the file to search for including extension
  507. * @param boolean $return, return the loaded file, the file must have a return
  508. * statement in it to work: return $variable;
  509. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  510. */
  511. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  512. $ext = null;
  513. if (is_array($type)) {
  514. extract($type, EXTR_OVERWRITE);
  515. }
  516. if (is_array($parent)) {
  517. extract($parent, EXTR_OVERWRITE);
  518. }
  519. if ($name == null && $file == null) {
  520. return false;
  521. }
  522. if (is_array($name)) {
  523. foreach ($name as $class) {
  524. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  525. return false;
  526. }
  527. }
  528. return true;
  529. }
  530. $originalType = strtolower($type);
  531. $specialPackage = in_array($originalType, array('file', 'vendor'));
  532. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  533. $type = self::$legacy[$originalType . 's'];
  534. }
  535. list($plugin, $name) = pluginSplit($name);
  536. if (!empty($plugin)) {
  537. if (!CakePlugin::loaded($plugin)) {
  538. return false;
  539. }
  540. }
  541. if (!$specialPackage) {
  542. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  543. }
  544. if ($originalType == 'file' && !empty($file)) {
  545. return self::_loadFile($name, $plugin, $search, $file, $return);
  546. }
  547. if ($originalType == 'vendor') {
  548. return self::_loadVendor($name, $plugin, $file, $ext);
  549. }
  550. return false;
  551. }
  552. /**
  553. * Helper function to include classes
  554. * This is a compatibility wrapper around using App::uses() and automatic class loading
  555. *
  556. * @param string $name unique name of the file for identifying it inside the application
  557. * @param string $plugin camel cased plugin name if any
  558. * @param string $type name of the packed where the class is located
  559. * @param string $originalType type name as supplied initially by the user
  560. * @param boolean $parent whether to load the class parent or not
  561. * @return boolean true indicating the successful load and existence of the class
  562. */
  563. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  564. if ($type == 'Console/Command' && $name == 'Shell') {
  565. $type = 'Console';
  566. } else if (isset(self::$types[$originalType]['suffix'])) {
  567. $suffix = self::$types[$originalType]['suffix'];
  568. $name .= ($suffix == $name) ? '' : $suffix;
  569. }
  570. if ($parent && isset(self::$types[$originalType]['extends'])) {
  571. $extends = self::$types[$originalType]['extends'];
  572. $extendType = $type;
  573. if (strpos($extends, '/') !== false) {
  574. $parts = explode('/', $extends);
  575. $extends = array_pop($parts);
  576. $extendType = implode('/', $parts);
  577. }
  578. App::uses($extends, $extendType);
  579. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  580. App::uses($plugin . $extends, $plugin . '.' .$type);
  581. }
  582. }
  583. if ($plugin) {
  584. $plugin .= '.';
  585. }
  586. $name = Inflector::camelize($name);
  587. App::uses($name, $plugin . $type);
  588. return class_exists($name);
  589. }
  590. /**
  591. * Helper function to include single files
  592. *
  593. * @param string $name unique name of the file for identifying it inside the application
  594. * @param string $plugin camel cased plugin name if any
  595. * @param array $search list of paths to search the file into
  596. * @param string $file filename if known, the $name param will be used otherwise
  597. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  598. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  599. */
  600. protected function _loadFile($name, $plugin, $search, $file, $return) {
  601. $mapped = self::_mapped($name, $plugin);
  602. if ($mapped) {
  603. $file = $mapped;
  604. } else if (!empty($search)) {
  605. foreach ($search as $path) {
  606. $found = false;
  607. if (file_exists($path . $file)) {
  608. $file = $path . $file;
  609. $found = true;
  610. break;
  611. }
  612. if (empty($found)) {
  613. $file = false;
  614. }
  615. }
  616. }
  617. if (!empty($file) && file_exists($file)) {
  618. self::_map($file, $name, $plugin);
  619. $returnValue = include $file;
  620. if ($return) {
  621. return $returnValue;
  622. }
  623. return (bool) $returnValue;
  624. }
  625. return false;
  626. }
  627. /**
  628. * Helper function to load files from vendors folders
  629. *
  630. * @param string $name unique name of the file for identifying it inside the application
  631. * @param string $plugin camel cased plugin name if any
  632. * @param string $file file name if known
  633. * @param string $ext file extension if known
  634. * @return boolean true if the file was loaded successfully, false otherwise
  635. */
  636. protected static function _loadVendor($name, $plugin, $file, $ext) {
  637. if ($mapped = self::_mapped($name, $plugin)) {
  638. return (bool) include_once($mapped);
  639. }
  640. $fileTries = array();
  641. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  642. if (empty($ext)) {
  643. $ext = 'php';
  644. }
  645. if (empty($file)) {
  646. $fileTries[] = $name . '.' . $ext;
  647. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  648. } else {
  649. $fileTries[] = $file;
  650. }
  651. foreach ($fileTries as $file) {
  652. foreach ($paths as $path) {
  653. if (file_exists($path . $file)) {
  654. self::_map($path . $file, $name, $plugin);
  655. return (bool) include($path . $file);
  656. }
  657. }
  658. }
  659. return false;
  660. }
  661. /**
  662. * Initializes the cache for App, registers a shutdown function.
  663. *
  664. * @return void
  665. */
  666. public static function init() {
  667. self::$_map += (array)Cache::read('file_map', '_cake_core_');
  668. self::$_objects += (array)Cache::read('object_map', '_cake_core_');
  669. register_shutdown_function(array('App', 'shutdown'));
  670. }
  671. /**
  672. * Maps the $name to the $file.
  673. *
  674. * @param string $file full path to file
  675. * @param string $name unique name for this map
  676. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  677. * @return void
  678. */
  679. protected static function _map($file, $name, $plugin = null) {
  680. if ($plugin) {
  681. self::$_map['Plugin'][$plugin][$name] = $file;
  682. } else {
  683. self::$_map[$name] = $file;
  684. }
  685. if (!self::$bootstrapping) {
  686. self::$_cacheChange = true;
  687. }
  688. }
  689. /**
  690. * Returns a file's complete path.
  691. *
  692. * @param string $name unique name
  693. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  694. * @return mixed file path if found, false otherwise
  695. */
  696. protected static function _mapped($name, $plugin = null) {
  697. if ($plugin) {
  698. if (isset(self::$_map['Plugin'][$plugin][$name])) {
  699. return self::$_map['Plugin'][$plugin][$name];
  700. }
  701. return false;
  702. }
  703. if (isset(self::$_map[$name])) {
  704. return self::$_map[$name];
  705. }
  706. return false;
  707. }
  708. protected static function _packageFormat() {
  709. if (empty(self::$_packageFormat)) {
  710. self::$_packageFormat = array(
  711. 'Model' => array(
  712. '%s' . 'Model' . DS,
  713. '%s' . 'models' . DS
  714. ),
  715. 'Model/Behavior' => array(
  716. '%s' . 'Model' . DS . 'Behavior' . DS,
  717. '%s' . 'models' . DS . 'behaviors' . DS
  718. ),
  719. 'Model/Datasource' => array(
  720. '%s' . 'Model' . DS . 'Datasource' . DS,
  721. '%s' . 'models' . DS . 'datasources' . DS
  722. ),
  723. 'Model/Datasource/Database' => array(
  724. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
  725. '%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
  726. ),
  727. 'Model/Datasource/Session' => array(
  728. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
  729. '%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
  730. ),
  731. 'Controller' => array(
  732. '%s' . 'Controller' . DS,
  733. '%s' . 'controllers' . DS
  734. ),
  735. 'Controller/Component' => array(
  736. '%s' . 'Controller' . DS . 'Component' . DS,
  737. '%s' . 'controllers' . DS . 'components' . DS
  738. ),
  739. 'Controller/Component/Auth' => array(
  740. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
  741. '%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
  742. ),
  743. 'View' => array(
  744. '%s' . 'View' . DS,
  745. '%s' . 'views' . DS
  746. ),
  747. 'View/Helper' => array(
  748. '%s' . 'View' . DS . 'Helper' . DS,
  749. '%s' . 'views' . DS . 'helpers' . DS
  750. ),
  751. 'Console' => array(
  752. '%s' . 'Console' . DS,
  753. '%s' . 'console' . DS
  754. ),
  755. 'Console/Command' => array(
  756. '%s' . 'Console' . DS . 'Command' . DS,
  757. '%s' . 'console' . DS . 'shells' . DS,
  758. ),
  759. 'Console/Command/Task' => array(
  760. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
  761. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
  762. ),
  763. 'Lib' => array(
  764. '%s' . 'Lib' . DS,
  765. '%s' . 'libs' . DS
  766. ),
  767. 'locales' => array(
  768. '%s' . 'Locale' . DS,
  769. '%s' . 'locale' . DS
  770. ),
  771. 'Vendor' => array(
  772. '%s' . 'Vendor' . DS, VENDORS
  773. ),
  774. 'Plugin' => array(
  775. APP . 'Plugin' . DS,
  776. APP . 'plugins' . DS,
  777. dirname(dirname(CAKE)) . DS . 'plugins' . DS
  778. )
  779. );
  780. }
  781. return self::$_packageFormat;
  782. }
  783. /**
  784. * Object destructor.
  785. *
  786. * Writes cache file if changes have been made to the $_map
  787. *
  788. * @return void
  789. */
  790. public static function shutdown() {
  791. if (self::$_cacheChange) {
  792. Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
  793. }
  794. if (self::$_objectCacheChange) {
  795. Cache::write('object_map', self::$_objects, '_cake_core_');
  796. }
  797. }
  798. }