App.php 27 KB

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