PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Core/App.php

https://bitbucket.org/npitts/easyuat2.0
PHP | 897 lines | 463 code | 80 blank | 354 comment | 96 complexity | 3786a82d5e50e873da1832e7caec41e4 MD5 | raw file
  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 mixed $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 mixed $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. foreach ($paths as $path) {
  498. $file = $path . $className . '.php';
  499. if (file_exists($file)) {
  500. self::_map($file, $className, $plugin);
  501. return include $file;
  502. }
  503. }
  504. return false;
  505. }
  506. /**
  507. * Returns the package name where a class was defined to be located at
  508. *
  509. * @param string $className name of the class to obtain the package name from
  510. * @return string package name or null if not declared
  511. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
  512. */
  513. public static function location($className) {
  514. if (!empty(self::$_classMap[$className])) {
  515. return self::$_classMap[$className];
  516. }
  517. return null;
  518. }
  519. /**
  520. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  521. * not construct any classes contained in the files. It will only find and require() the file.
  522. *
  523. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
  524. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  525. * an single array to $type,
  526. * @param string $name Name of the Class or a unique name for the file
  527. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  528. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  529. * $ext allows setting the extension of the file name
  530. * based on Inflector::underscore($name) . ".$ext";
  531. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  532. * @param string $file full name of the file to search for including extension
  533. * @param boolean $return Return the loaded file, the file must have a return
  534. * statement in it to work: return $variable;
  535. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  536. */
  537. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  538. $ext = null;
  539. if (is_array($type)) {
  540. extract($type, EXTR_OVERWRITE);
  541. }
  542. if (is_array($parent)) {
  543. extract($parent, EXTR_OVERWRITE);
  544. }
  545. if ($name == null && $file == null) {
  546. return false;
  547. }
  548. if (is_array($name)) {
  549. foreach ($name as $class) {
  550. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  551. return false;
  552. }
  553. }
  554. return true;
  555. }
  556. $originalType = strtolower($type);
  557. $specialPackage = in_array($originalType, array('file', 'vendor'));
  558. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  559. $type = self::$legacy[$originalType . 's'];
  560. }
  561. list($plugin, $name) = pluginSplit($name);
  562. if (!empty($plugin)) {
  563. if (!CakePlugin::loaded($plugin)) {
  564. return false;
  565. }
  566. }
  567. if (!$specialPackage) {
  568. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  569. }
  570. if ($originalType == 'file' && !empty($file)) {
  571. return self::_loadFile($name, $plugin, $search, $file, $return);
  572. }
  573. if ($originalType == 'vendor') {
  574. return self::_loadVendor($name, $plugin, $file, $ext);
  575. }
  576. return false;
  577. }
  578. /**
  579. * Helper function to include classes
  580. * This is a compatibility wrapper around using App::uses() and automatic class loading
  581. *
  582. * @param string $name unique name of the file for identifying it inside the application
  583. * @param string $plugin camel cased plugin name if any
  584. * @param string $type name of the packed where the class is located
  585. * @param string $originalType type name as supplied initially by the user
  586. * @param boolean $parent whether to load the class parent or not
  587. * @return boolean true indicating the successful load and existence of the class
  588. */
  589. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  590. if ($type == 'Console/Command' && $name == 'Shell') {
  591. $type = 'Console';
  592. } elseif (isset(self::$types[$originalType]['suffix'])) {
  593. $suffix = self::$types[$originalType]['suffix'];
  594. $name .= ($suffix == $name) ? '' : $suffix;
  595. }
  596. if ($parent && isset(self::$types[$originalType]['extends'])) {
  597. $extends = self::$types[$originalType]['extends'];
  598. $extendType = $type;
  599. if (strpos($extends, '/') !== false) {
  600. $parts = explode('/', $extends);
  601. $extends = array_pop($parts);
  602. $extendType = implode('/', $parts);
  603. }
  604. App::uses($extends, $extendType);
  605. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  606. App::uses($plugin . $extends, $plugin . '.' . $type);
  607. }
  608. }
  609. if ($plugin) {
  610. $plugin .= '.';
  611. }
  612. $name = Inflector::camelize($name);
  613. App::uses($name, $plugin . $type);
  614. return class_exists($name);
  615. }
  616. /**
  617. * Helper function to include single files
  618. *
  619. * @param string $name unique name of the file for identifying it inside the application
  620. * @param string $plugin camel cased plugin name if any
  621. * @param array $search list of paths to search the file into
  622. * @param string $file filename if known, the $name param will be used otherwise
  623. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  624. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  625. */
  626. protected static function _loadFile($name, $plugin, $search, $file, $return) {
  627. $mapped = self::_mapped($name, $plugin);
  628. if ($mapped) {
  629. $file = $mapped;
  630. } elseif (!empty($search)) {
  631. foreach ($search as $path) {
  632. $found = false;
  633. if (file_exists($path . $file)) {
  634. $file = $path . $file;
  635. $found = true;
  636. break;
  637. }
  638. if (empty($found)) {
  639. $file = false;
  640. }
  641. }
  642. }
  643. if (!empty($file) && file_exists($file)) {
  644. self::_map($file, $name, $plugin);
  645. $returnValue = include $file;
  646. if ($return) {
  647. return $returnValue;
  648. }
  649. return (bool)$returnValue;
  650. }
  651. return false;
  652. }
  653. /**
  654. * Helper function to load files from vendors folders
  655. *
  656. * @param string $name unique name of the file for identifying it inside the application
  657. * @param string $plugin camel cased plugin name if any
  658. * @param string $file file name if known
  659. * @param string $ext file extension if known
  660. * @return boolean true if the file was loaded successfully, false otherwise
  661. */
  662. protected static function _loadVendor($name, $plugin, $file, $ext) {
  663. if ($mapped = self::_mapped($name, $plugin)) {
  664. return (bool)include_once $mapped;
  665. }
  666. $fileTries = array();
  667. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  668. if (empty($ext)) {
  669. $ext = 'php';
  670. }
  671. if (empty($file)) {
  672. $fileTries[] = $name . '.' . $ext;
  673. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  674. } else {
  675. $fileTries[] = $file;
  676. }
  677. foreach ($fileTries as $file) {
  678. foreach ($paths as $path) {
  679. if (file_exists($path . $file)) {
  680. self::_map($path . $file, $name, $plugin);
  681. return (bool)include $path . $file;
  682. }
  683. }
  684. }
  685. return false;
  686. }
  687. /**
  688. * Initializes the cache for App, registers a shutdown function.
  689. *
  690. * @return void
  691. */
  692. public static function init() {
  693. self::$_map += (array)Cache::read('file_map', '_cake_core_');
  694. self::$_objects += (array)Cache::read('object_map', '_cake_core_');
  695. register_shutdown_function(array('App', 'shutdown'));
  696. }
  697. /**
  698. * Maps the $name to the $file.
  699. *
  700. * @param string $file full path to file
  701. * @param string $name unique name for this map
  702. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  703. * @return void
  704. */
  705. protected static function _map($file, $name, $plugin = null) {
  706. $key = $name;
  707. if ($plugin) {
  708. $key = 'plugin.' . $name;
  709. }
  710. if ($plugin && empty(self::$_map[$name])) {
  711. self::$_map[$key] = $file;
  712. }
  713. if (!$plugin && empty(self::$_map['plugin.' . $name])) {
  714. self::$_map[$key] = $file;
  715. }
  716. if (!self::$bootstrapping) {
  717. self::$_cacheChange = true;
  718. }
  719. }
  720. /**
  721. * Returns a file's complete path.
  722. *
  723. * @param string $name unique name
  724. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  725. * @return mixed file path if found, false otherwise
  726. */
  727. protected static function _mapped($name, $plugin = null) {
  728. $key = $name;
  729. if ($plugin) {
  730. $key = 'plugin.' . $name;
  731. }
  732. return isset(self::$_map[$key]) ? self::$_map[$key] : false;
  733. }
  734. /**
  735. * Sets then returns the templates for each customizable package path
  736. *
  737. * @return array templates for each customizable package path
  738. */
  739. protected static function _packageFormat() {
  740. if (empty(self::$_packageFormat)) {
  741. self::$_packageFormat = array(
  742. 'Model' => array(
  743. '%s' . 'Model' . DS
  744. ),
  745. 'Model/Behavior' => array(
  746. '%s' . 'Model' . DS . 'Behavior' . DS
  747. ),
  748. 'Model/Datasource' => array(
  749. '%s' . 'Model' . DS . 'Datasource' . DS
  750. ),
  751. 'Model/Datasource/Database' => array(
  752. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
  753. ),
  754. 'Model/Datasource/Session' => array(
  755. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
  756. ),
  757. 'Controller' => array(
  758. '%s' . 'Controller' . DS
  759. ),
  760. 'Controller/Component' => array(
  761. '%s' . 'Controller' . DS . 'Component' . DS
  762. ),
  763. 'Controller/Component/Auth' => array(
  764. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS
  765. ),
  766. 'Controller/Component/Acl' => array(
  767. '%s' . 'Controller' . DS . 'Component' . DS . 'Acl' . DS
  768. ),
  769. 'View' => array(
  770. '%s' . 'View' . DS
  771. ),
  772. 'View/Helper' => array(
  773. '%s' . 'View' . DS . 'Helper' . DS
  774. ),
  775. 'Console' => array(
  776. '%s' . 'Console' . DS
  777. ),
  778. 'Console/Command' => array(
  779. '%s' . 'Console' . DS . 'Command' . DS
  780. ),
  781. 'Console/Command/Task' => array(
  782. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS
  783. ),
  784. 'Lib' => array(
  785. '%s' . 'Lib' . DS
  786. ),
  787. 'Locale' => array(
  788. '%s' . 'Locale' . DS
  789. ),
  790. 'Vendor' => array(
  791. '%s' . 'Vendor' . DS,
  792. dirname(dirname(CAKE)) . DS . 'vendors' . DS,
  793. ),
  794. 'Plugin' => array(
  795. APP . 'Plugin' . DS,
  796. dirname(dirname(CAKE)) . DS . 'plugins' . DS
  797. )
  798. );
  799. }
  800. return self::$_packageFormat;
  801. }
  802. /**
  803. * Object destructor.
  804. *
  805. * Writes cache file if changes have been made to the $_map
  806. *
  807. * @return void
  808. */
  809. public static function shutdown() {
  810. if (self::$_cacheChange) {
  811. Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
  812. }
  813. if (self::$_objectCacheChange) {
  814. Cache::write('object_map', self::$_objects, '_cake_core_');
  815. }
  816. }
  817. }