PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Core/App.php

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