App.php 27 KB

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