App.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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/view/933/The-App-Class
  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. if (!empty(self::$_packageFormat[$type])) {
  200. foreach (self::$_packageFormat[$type] as $f) {
  201. $path[] = sprintf($f, $pluginPath);
  202. }
  203. }
  204. $path[] = $pluginPath . 'Lib' . DS . $type . DS;
  205. return $path;
  206. }
  207. if (!isset(self::$_packages[$type])) {
  208. return array();
  209. }
  210. return self::$_packages[$type];
  211. }
  212. /**
  213. * Get all the currently loaded paths from App. Useful for inspecting
  214. * or storing all paths App knows about. For a paths to a specific package
  215. * use App::path()
  216. *
  217. * @return array An array of packages and their associated paths.
  218. */
  219. public static function paths() {
  220. return self::$_packages;
  221. }
  222. /**
  223. * Sets up each package location on the file system. You can configure multiple search paths
  224. * for each package, those will be used to look for files one folder at a time in the specified order
  225. * All paths should be terminated with a Directory separator
  226. *
  227. * Usage:
  228. *
  229. * `App::build(array(Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
  230. *
  231. * `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
  232. *
  233. * `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers`
  234. *
  235. * If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
  236. *
  237. * @param array $paths associative array with package names as keys and a list of directories for new search paths
  238. * @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths, [default] App::PREPEND
  239. * @return void
  240. */
  241. public static function build($paths = array(), $mode = App::PREPEND) {
  242. if (empty(self::$_packageFormat)) {
  243. self::$_packageFormat = array(
  244. 'Model' => array(
  245. '%s' . 'Model' . DS,
  246. '%s' . 'models' . DS
  247. ),
  248. 'Model/Behavior' => array(
  249. '%s' . 'Model' . DS . 'Behavior' . DS,
  250. '%s' . 'models' . DS . 'behaviors' . DS
  251. ),
  252. 'Model/Datasource' => array(
  253. '%s' . 'Model' . DS . 'Datasource' . DS,
  254. '%s' . 'models' . DS . 'datasources' . DS
  255. ),
  256. 'Model/Datasource/Database' => array(
  257. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
  258. '%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
  259. ),
  260. 'Model/Datasource/Session' => array(
  261. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
  262. '%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
  263. ),
  264. 'Controller' => array(
  265. '%s' . 'Controller' . DS,
  266. '%s' . 'controllers' . DS
  267. ),
  268. 'Controller/Component' => array(
  269. '%s' . 'Controller' . DS . 'Component' . DS,
  270. '%s' . 'controllers' . DS . 'components' . DS
  271. ),
  272. 'Controller/Component/Auth' => array(
  273. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
  274. '%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
  275. ),
  276. 'View' => array(
  277. '%s' . 'View' . DS,
  278. '%s' . 'views' . DS
  279. ),
  280. 'View/Helper' => array(
  281. '%s' . 'View' . DS . 'Helper' . DS,
  282. '%s' . 'views' . DS . 'helpers' . DS
  283. ),
  284. 'Console' => array(
  285. '%s' . 'Console' . DS,
  286. '%s' . 'console' . DS
  287. ),
  288. 'Console/Command' => array(
  289. '%s' . 'Console' . DS . 'Command' . DS,
  290. '%s' . 'console' . DS . 'shells' . DS,
  291. ),
  292. 'Console/Command/Task' => array(
  293. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
  294. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
  295. ),
  296. 'Lib' => array(
  297. '%s' . 'Lib' . DS,
  298. '%s' . 'libs' . DS
  299. ),
  300. 'locales' => array(
  301. '%s' . 'Locale' . DS,
  302. '%s' . 'locale' . DS
  303. ),
  304. 'Vendor' => array('%s' . 'Vendor' . DS, VENDORS),
  305. 'Plugin' => array(
  306. APP . 'Plugin' . DS,
  307. APP . 'plugins' . DS,
  308. dirname(dirname(CAKE)) . DS . 'plugins' . DS,
  309. )
  310. );
  311. }
  312. if ($mode === App::RESET) {
  313. foreach ($paths as $type => $new) {
  314. if (!empty(self::$legacy[$type])) {
  315. $type = self::$legacy[$type];
  316. }
  317. self::$_packages[$type] = (array)$new;
  318. self::objects($type, null, false);
  319. }
  320. return $paths;
  321. }
  322. //Provides Backwards compatibility for old-style package names
  323. $legacyPaths = array();
  324. foreach ($paths as $type => $path) {
  325. if (!empty(self::$legacy[$type])) {
  326. $type = self::$legacy[$type];
  327. }
  328. $legacyPaths[$type] = $path;
  329. }
  330. $paths = $legacyPaths;
  331. $defaults = array();
  332. foreach (self::$_packageFormat as $package => $format) {
  333. foreach ($format as $f) {
  334. $defaults[$package][] = sprintf($f, APP);
  335. }
  336. }
  337. foreach ($defaults as $type => $default) {
  338. if (empty(self::$_packages[$type]) || empty($paths)) {
  339. self::$_packages[$type] = $default;
  340. }
  341. if (!empty($paths[$type])) {
  342. if ($mode === App::PREPEND) {
  343. $path = array_merge((array)$paths[$type], self::$_packages[$type]);
  344. } else {
  345. $path = array_merge(self::$_packages[$type], (array)$paths[$type]);
  346. }
  347. } else {
  348. $path = self::$_packages[$type];
  349. }
  350. self::$_packages[$type] = array_values(array_unique($path));
  351. }
  352. }
  353. /**
  354. * Gets the path that a plugin is on. Searches through the defined plugin paths.
  355. *
  356. * Usage:
  357. *
  358. * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
  359. *
  360. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  361. * @return string full path to the plugin.
  362. */
  363. public static function pluginPath($plugin) {
  364. return CakePlugin::path($plugin);
  365. }
  366. /**
  367. * Finds the path that a theme is on. Searches through the defined theme paths.
  368. *
  369. * Usage:
  370. *
  371. * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
  372. *
  373. * @param string $theme theme name to find the path of.
  374. * @return string full path to the theme.
  375. */
  376. public static function themePath($theme) {
  377. $themeDir = 'Themed' . DS . Inflector::camelize($theme);
  378. foreach (self::$_packages['View'] as $path) {
  379. if (is_dir($path . $themeDir)) {
  380. return $path . $themeDir . DS ;
  381. }
  382. }
  383. return self::$_packages['View'][0] . $themeDir . DS;
  384. }
  385. /**
  386. * Returns the full path to a package inside the CakePHP core
  387. *
  388. * Usage:
  389. *
  390. * `App::core('Cache/Engine'); will return the full path to the cache engines package`
  391. *
  392. * @param string $type
  393. * @return string full path to package
  394. */
  395. public static function core($type) {
  396. return array(CAKE . str_replace('/', DS, $type) . DS);
  397. }
  398. /**
  399. * Returns an array of objects of the given type.
  400. *
  401. * Example usage:
  402. *
  403. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  404. *
  405. * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  406. *
  407. * You can also search only within a plugin's objects by using the plugin dot
  408. * syntax.
  409. *
  410. * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  411. *
  412. * When scanning directories, files and directories beginning with `.` will be excluded as these
  413. * are commonly used by version control systems.
  414. *
  415. * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  416. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  417. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  418. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  419. */
  420. public static function objects($type, $path = null, $cache = true) {
  421. $extension = '/\.php$/';
  422. $includeDirectories = false;
  423. $name = $type;
  424. if ($type === 'plugin') {
  425. $type = 'plugins';
  426. }
  427. if ($type == 'plugins') {
  428. $extension = '/.*/';
  429. $includeDirectories = true;
  430. }
  431. list($plugin, $type) = pluginSplit($type);
  432. if (isset(self::$legacy[$type . 's'])) {
  433. $type = self::$legacy[$type . 's'];
  434. }
  435. if ($type === 'file' && !$path) {
  436. return false;
  437. } elseif ($type === 'file') {
  438. $extension = '/\.php$/';
  439. $name = $type . str_replace(DS, '', $path);
  440. }
  441. if (empty(self::$_objects) && $cache === true) {
  442. self::$_objects = Cache::read('object_map', '_cake_core_');
  443. }
  444. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  445. if ($cache !== true || !isset(self::$_objects[$cacheLocation][$name])) {
  446. $objects = array();
  447. if (empty($path)) {
  448. $path = self::path($type, $plugin);
  449. }
  450. foreach ((array)$path as $dir) {
  451. if ($dir != APP && is_dir($dir)) {
  452. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  453. foreach ($files as $file) {
  454. $fileName = basename($file);
  455. if (!$file->isDot() && $fileName[0] !== '.') {
  456. $isDir = $file->isDir() ;
  457. if ($isDir && $includeDirectories) {
  458. $objects[] = $fileName;
  459. } elseif (!$includeDirectories && !$isDir) {
  460. $objects[] = substr($fileName, 0, -4);
  461. }
  462. }
  463. }
  464. }
  465. }
  466. if ($type !== 'file') {
  467. foreach ($objects as $key => $value) {
  468. $objects[$key] = Inflector::camelize($value);
  469. }
  470. }
  471. sort($objects);
  472. if ($plugin) {
  473. return $objects;
  474. }
  475. self::$_objects[$cacheLocation][$name] = $objects;
  476. if ($cache) {
  477. self::$_objectCacheChange = true;
  478. }
  479. }
  480. return self::$_objects[$cacheLocation][$name];
  481. }
  482. /**
  483. * Declares a package for a class. This package location will be used
  484. * by the automatic class loader if the class is tried to be used
  485. *
  486. * Usage:
  487. *
  488. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  489. *
  490. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  491. *
  492. * @param string $className the name of the class to configure package for
  493. * @param string $location the package name
  494. * @return void
  495. */
  496. public static function uses($className, $location) {
  497. self::$_classMap[$className] = $location;
  498. }
  499. /**
  500. * Method to handle the automatic class loading. It will look for each class' package
  501. * defined using App::uses() and with this information it will resolve the package name to a full path
  502. * to load the class from. File name for each class should follow the class name. For instance,
  503. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  504. *
  505. * @param string $className the name of the class to load
  506. * @return boolean
  507. */
  508. public static function load($className) {
  509. if (!isset(self::$_classMap[$className])) {
  510. return false;
  511. }
  512. if ($file = self::_mapped($className)) {
  513. return include $file;
  514. }
  515. $parts = explode('.', self::$_classMap[$className], 2);
  516. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  517. $paths = self::path($package, $plugin);
  518. if (empty($plugin)) {
  519. $appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
  520. $paths[] = $appLibs . $package . DS;
  521. $paths[] = CAKE . $package . DS;
  522. }
  523. foreach ($paths as $path) {
  524. $file = $path . $className . '.php';
  525. if (file_exists($file)) {
  526. self::_map($file, $className);
  527. return include $file;
  528. }
  529. }
  530. //To help apps migrate to 2.0 old style file names are allowed
  531. foreach ($paths as $path) {
  532. $underscored = Inflector::underscore($className);
  533. $tries = array($path . $underscored . '.php');
  534. $parts = explode('_', $underscored);
  535. if (count($parts) > 1) {
  536. array_pop($parts);
  537. $tries[] = $path . implode('_', $parts) . '.php';
  538. }
  539. foreach ($tries as $file) {
  540. if (file_exists($file)) {
  541. self::_map($file, $className);
  542. return include $file;
  543. }
  544. }
  545. }
  546. return false;
  547. }
  548. /**
  549. * Returns the package name where a class was defined to be located at
  550. *
  551. * @param string $className name of the class to obtain the package name from
  552. * @return string package name or null if not declared
  553. */
  554. public static function location($className) {
  555. if (!empty(self::$_classMap[$className])) {
  556. return self::$_classMap[$className];
  557. }
  558. return null;
  559. }
  560. /**
  561. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  562. * not construct any classes contained in the files. It will only find and require() the file.
  563. *
  564. * @link http://book.cakephp.org/view/934/Using-App-import
  565. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  566. * an single array to $type,
  567. * @param string $name Name of the Class or a unique name for the file
  568. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  569. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  570. * $ext allows setting the extension of the file name
  571. * based on Inflector::underscore($name) . ".$ext";
  572. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  573. * @param string $file full name of the file to search for including extension
  574. * @param boolean $return, return the loaded file, the file must have a return
  575. * statement in it to work: return $variable;
  576. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  577. */
  578. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  579. $ext = null;
  580. if (is_array($type)) {
  581. extract($type, EXTR_OVERWRITE);
  582. }
  583. if (is_array($parent)) {
  584. extract($parent, EXTR_OVERWRITE);
  585. }
  586. if ($name == null && $file == null) {
  587. return false;
  588. }
  589. if (is_array($name)) {
  590. foreach ($name as $class) {
  591. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  592. return false;
  593. }
  594. }
  595. return true;
  596. }
  597. $originalType = strtolower($type);
  598. $specialPackage = in_array($originalType, array('file', 'vendor'));
  599. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  600. $type = self::$legacy[$originalType . 's'];
  601. }
  602. list($plugin, $name) = pluginSplit($name);
  603. if (!empty($plugin)) {
  604. if (!CakePlugin::loaded($plugin)) {
  605. return false;
  606. }
  607. }
  608. if (!$specialPackage) {
  609. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  610. }
  611. if ($originalType == 'file' && !empty($file)) {
  612. return self::_loadFile($name, $plugin, $search, $file, $return);
  613. }
  614. if ($originalType == 'vendor') {
  615. return self::_loadVendor($name, $plugin, $file, $ext);
  616. }
  617. return false;
  618. }
  619. /**
  620. * Helper function to include classes
  621. * This is a compatibility wrapper around using App::uses() and automatic class loading
  622. *
  623. * @param string $name unique name of the file for identifying it inside the application
  624. * @param string $plugin camel cased plugin name if any
  625. * @param string $type name of the packed where the class is located
  626. * @param string $originalType type name as supplied initially by the user
  627. * @param boolean $parent whether to load the class parent or not
  628. * @return boolean true indicating the successful load and existence of the class
  629. */
  630. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  631. if ($type == 'Console/Command' && $name == 'Shell') {
  632. $type = 'Console';
  633. } else if (isset(self::$types[$originalType]['suffix'])) {
  634. $suffix = self::$types[$originalType]['suffix'];
  635. $name .= ($suffix == $name) ? '' : $suffix;
  636. }
  637. if ($parent && isset(self::$types[$originalType]['extends'])) {
  638. $extends = self::$types[$originalType]['extends'];
  639. $extendType = $type;
  640. if (strpos($extends, '/') !== false) {
  641. $parts = explode('/', $extends);
  642. $extends = array_pop($parts);
  643. $extendType = implode('/', $parts);
  644. }
  645. App::uses($extends, $extendType);
  646. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  647. App::uses($plugin . $extends, $plugin . '.' .$type);
  648. }
  649. }
  650. if ($plugin) {
  651. $plugin .= '.';
  652. }
  653. $name = Inflector::camelize($name);
  654. App::uses($name, $plugin . $type);
  655. return class_exists($name);
  656. }
  657. /**
  658. * Helper function to include single files
  659. *
  660. * @param string $name unique name of the file for identifying it inside the application
  661. * @param string $plugin camel cased plugin name if any
  662. * @param array $search list of paths to search the file into
  663. * @param string $file filename if known, the $name param will be used otherwise
  664. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  665. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  666. */
  667. protected function _loadFile($name, $plugin, $search, $file, $return) {
  668. $mapped = self::_mapped($name, $plugin);
  669. if ($mapped) {
  670. $file = $mapped;
  671. } else if (!empty($search)) {
  672. foreach ($search as $path) {
  673. $found = false;
  674. if (file_exists($path . $file)) {
  675. $file = $path . $file;
  676. $found = true;
  677. break;
  678. }
  679. if (empty($found)) {
  680. $file = false;
  681. }
  682. }
  683. }
  684. if (!empty($file) && file_exists($file)) {
  685. self::_map($file, $name, $plugin);
  686. $returnValue = include $file;
  687. if ($return) {
  688. return $returnValue;
  689. }
  690. return (bool) $returnValue;
  691. }
  692. return false;
  693. }
  694. /**
  695. * Helper function to load files from vendors folders
  696. *
  697. * @param string $name unique name of the file for identifying it inside the application
  698. * @param string $plugin camel cased plugin name if any
  699. * @param string $file file name if known
  700. * @param string $ext file extension if known
  701. * @return boolean true if the file was loaded successfully, false otherwise
  702. */
  703. protected function _loadVendor($name, $plugin, $file, $ext) {
  704. if ($mapped = self::_mapped($name, $plugin)) {
  705. return (bool) include_once($mapped);
  706. }
  707. $fileTries = array();
  708. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  709. if (empty($ext)) {
  710. $ext = 'php';
  711. }
  712. if (empty($file)) {
  713. $fileTries[] = $name . '.' . $ext;
  714. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  715. } else {
  716. $fileTries[] = $file;
  717. }
  718. foreach ($fileTries as $file) {
  719. foreach ($paths as $path) {
  720. if (file_exists($path . $file)) {
  721. self::_map($path . $file, $name, $plugin);
  722. return (bool) include($path . $file);
  723. }
  724. }
  725. }
  726. return false;
  727. }
  728. /**
  729. * Initializes the cache for App, registers a shutdown function.
  730. *
  731. * @return void
  732. */
  733. public static function init() {
  734. self::$_map += (array)Cache::read('file_map', '_cake_core_');
  735. self::$_objects += (array)Cache::read('object_map', '_cake_core_');
  736. register_shutdown_function(array('App', 'shutdown'));
  737. self::uses('CakePlugin', 'Core');
  738. }
  739. /**
  740. * Maps the $name to the $file.
  741. *
  742. * @param string $file full path to file
  743. * @param string $name unique name for this map
  744. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  745. * @return void
  746. */
  747. protected static function _map($file, $name, $plugin = null) {
  748. if ($plugin) {
  749. self::$_map['Plugin'][$plugin][$name] = $file;
  750. } else {
  751. self::$_map[$name] = $file;
  752. }
  753. if (!self::$bootstrapping) {
  754. self::$_cacheChange = true;
  755. }
  756. }
  757. /**
  758. * Returns a file's complete path.
  759. *
  760. * @param string $name unique name
  761. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  762. * @return mixed file path if found, false otherwise
  763. */
  764. protected static function _mapped($name, $plugin = null) {
  765. if ($plugin) {
  766. if (isset(self::$_map['Plugin'][$plugin][$name])) {
  767. return self::$_map['Plugin'][$plugin][$name];
  768. }
  769. return false;
  770. }
  771. if (isset(self::$_map[$name])) {
  772. return self::$_map[$name];
  773. }
  774. return false;
  775. }
  776. /**
  777. * Object destructor.
  778. *
  779. * Writes cache file if changes have been made to the $_map
  780. *
  781. * @return void
  782. */
  783. public static function shutdown() {
  784. if (self::$_cacheChange) {
  785. Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
  786. }
  787. if (self::$_objectCacheChange) {
  788. Cache::write('object_map', self::$_objects, '_cake_core_');
  789. }
  790. }
  791. }