PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Core/App.php

https://bitbucket.org/dosm123/crm
PHP | 891 lines | 459 code | 81 blank | 351 comment | 93 complexity | 2e4ab315ab7727c13a6c0e6954d128ba MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Core
  16. * @since CakePHP(tm) v 1.2.0.6001
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * App is responsible for path management, class location and class loading.
  21. *
  22. * ### Adding paths
  23. *
  24. * You can add paths to the search indexes App uses to find classes using `App::build()`. Adding
  25. * additional controller paths for example would alter where CakePHP looks for controllers.
  26. * This allows you to split your application up across the filesystem.
  27. *
  28. * ### Packages
  29. *
  30. * CakePHP is organized around the idea of packages, each class belongs to a package or folder where other
  31. * classes reside. You can configure each package location in your application using `App::build('APackage/SubPackage', $paths)`
  32. * to inform the framework where should each class be loaded. Almost every class in the CakePHP framework can be swapped
  33. * by your own compatible implementation. If you wish to use you own class instead of the classes the framework provides,
  34. * just add the class to your libs folder mocking the directory location of where CakePHP expects to find it.
  35. *
  36. * For instance if you'd like to use your own HttpSocket class, put it under
  37. *
  38. * app/Network/Http/HttpSocket.php
  39. *
  40. * ### Inspecting loaded paths
  41. *
  42. * You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
  43. * controller paths.
  44. *
  45. * It is also possible to inspect paths for plugin classes, for instance, to see a plugin's helpers you would call
  46. * `App::path('View/Helper', 'MyPlugin')`
  47. *
  48. * ### Locating plugins and themes
  49. *
  50. * Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will
  51. * give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the
  52. * `purple` theme.
  53. *
  54. * ### Inspecting known objects
  55. *
  56. * You can find out which objects App knows about using App::objects('Controller') for example to find
  57. * which application controllers App knows about.
  58. *
  59. * @link http://book.cakephp.org/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. * If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
  245. *
  246. * @param array $paths associative array with package names as keys and a list of directories for new search paths
  247. * @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths, [default] App::PREPEND
  248. * @return void
  249. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
  250. */
  251. public static function build($paths = array(), $mode = App::PREPEND) {
  252. //Provides Backwards compatibility for old-style package names
  253. $legacyPaths = array();
  254. foreach ($paths as $type => $path) {
  255. if (!empty(self::$legacy[$type])) {
  256. $type = self::$legacy[$type];
  257. }
  258. $legacyPaths[$type] = $path;
  259. }
  260. $paths = $legacyPaths;
  261. if ($mode === App::RESET) {
  262. foreach ($paths as $type => $new) {
  263. self::$_packages[$type] = (array)$new;
  264. self::objects($type, null, false);
  265. }
  266. return;
  267. }
  268. $packageFormat = self::_packageFormat();
  269. if ($mode === App::REGISTER) {
  270. if (empty($paths)) {
  271. self::$_packageFormat = null;
  272. $packageFormat = self::_packageFormat();
  273. } else {
  274. foreach ($paths as $package => $formats) {
  275. if (!empty($packageFormat[$package])) {
  276. $formats = array_merge($packageFormat[$package], $formats);
  277. }
  278. $packageFormat[$package] = array_values(array_unique($formats));
  279. }
  280. self::$_packageFormat = $packageFormat;
  281. $paths = array();
  282. }
  283. }
  284. $defaults = array();
  285. foreach ($packageFormat as $package => $format) {
  286. foreach ($format as $f) {
  287. $defaults[$package][] = sprintf($f, APP);
  288. }
  289. }
  290. if (empty($paths)) {
  291. self::$_packages = $defaults;
  292. return;
  293. }
  294. foreach ($defaults as $type => $default) {
  295. if (!empty(self::$_packages[$type])) {
  296. $path = self::$_packages[$type];
  297. }
  298. if (!empty($paths[$type])) {
  299. $newPath = (array)$paths[$type];
  300. if ($mode === App::PREPEND) {
  301. $path = array_merge($newPath, $path);
  302. } else {
  303. $path = array_merge($path, $newPath);
  304. }
  305. $path = array_values(array_unique($path));
  306. }
  307. self::$_packages[$type] = $path;
  308. }
  309. }
  310. /**
  311. * Gets the path that a plugin is on. Searches through the defined plugin paths.
  312. *
  313. * Usage:
  314. *
  315. * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
  316. *
  317. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  318. * @return string full path to the plugin.
  319. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::pluginPath
  320. */
  321. public static function pluginPath($plugin) {
  322. return CakePlugin::path($plugin);
  323. }
  324. /**
  325. * Finds the path that a theme is on. Searches through the defined theme paths.
  326. *
  327. * Usage:
  328. *
  329. * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
  330. *
  331. * @param string $theme theme name to find the path of.
  332. * @return string full path to the theme.
  333. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::themePath
  334. */
  335. public static function themePath($theme) {
  336. $themeDir = 'Themed' . DS . Inflector::camelize($theme);
  337. foreach (self::$_packages['View'] as $path) {
  338. if (is_dir($path . $themeDir)) {
  339. return $path . $themeDir . DS;
  340. }
  341. }
  342. return self::$_packages['View'][0] . $themeDir . DS;
  343. }
  344. /**
  345. * Returns the full path to a package inside the CakePHP core
  346. *
  347. * Usage:
  348. *
  349. * `App::core('Cache/Engine'); will return the full path to the cache engines package`
  350. *
  351. * @param string $type
  352. * @return array full path to package
  353. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::core
  354. */
  355. public static function core($type) {
  356. return array(CAKE . str_replace('/', DS, $type) . DS);
  357. }
  358. /**
  359. * Returns an array of objects of the given type.
  360. *
  361. * Example usage:
  362. *
  363. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  364. *
  365. * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  366. *
  367. * You can also search only within a plugin's objects by using the plugin dot
  368. * syntax.
  369. *
  370. * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  371. *
  372. * When scanning directories, files and directories beginning with `.` will be excluded as these
  373. * are commonly used by version control systems.
  374. *
  375. * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  376. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  377. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  378. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  379. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::objects
  380. */
  381. public static function objects($type, $path = null, $cache = true) {
  382. $extension = '/\.php$/';
  383. $includeDirectories = false;
  384. $name = $type;
  385. if ($type === 'plugin') {
  386. $type = 'plugins';
  387. }
  388. if ($type == 'plugins') {
  389. $extension = '/.*/';
  390. $includeDirectories = true;
  391. }
  392. list($plugin, $type) = pluginSplit($type);
  393. if (isset(self::$legacy[$type . 's'])) {
  394. $type = self::$legacy[$type . 's'];
  395. }
  396. if ($type === 'file' && !$path) {
  397. return false;
  398. } elseif ($type === 'file') {
  399. $extension = '/\.php$/';
  400. $name = $type . str_replace(DS, '', $path);
  401. }
  402. if (empty(self::$_objects) && $cache === true) {
  403. self::$_objects = Cache::read('object_map', '_cake_core_');
  404. }
  405. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  406. if ($cache !== true || !isset(self::$_objects[$cacheLocation][$name])) {
  407. $objects = array();
  408. if (empty($path)) {
  409. $path = self::path($type, $plugin);
  410. }
  411. foreach ((array)$path as $dir) {
  412. if ($dir != APP && is_dir($dir)) {
  413. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  414. foreach ($files as $file) {
  415. $fileName = basename($file);
  416. if (!$file->isDot() && $fileName[0] !== '.') {
  417. $isDir = $file->isDir();
  418. if ($isDir && $includeDirectories) {
  419. $objects[] = $fileName;
  420. } elseif (!$includeDirectories && !$isDir) {
  421. $objects[] = substr($fileName, 0, -4);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. if ($type !== 'file') {
  428. foreach ($objects as $key => $value) {
  429. $objects[$key] = Inflector::camelize($value);
  430. }
  431. }
  432. sort($objects);
  433. if ($plugin) {
  434. return $objects;
  435. }
  436. self::$_objects[$cacheLocation][$name] = $objects;
  437. if ($cache) {
  438. self::$_objectCacheChange = true;
  439. }
  440. }
  441. return self::$_objects[$cacheLocation][$name];
  442. }
  443. /**
  444. * Declares a package for a class. This package location will be used
  445. * by the automatic class loader if the class is tried to be used
  446. *
  447. * Usage:
  448. *
  449. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  450. *
  451. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  452. *
  453. * @param string $className the name of the class to configure package for
  454. * @param string $location the package name
  455. * @return void
  456. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::uses
  457. */
  458. public static function uses($className, $location) {
  459. self::$_classMap[$className] = $location;
  460. }
  461. /**
  462. * Method to handle the automatic class loading. It will look for each class' package
  463. * defined using App::uses() and with this information it will resolve the package name to a full path
  464. * to load the class from. File name for each class should follow the class name. For instance,
  465. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  466. *
  467. * @param string $className the name of the class to load
  468. * @return boolean
  469. */
  470. public static function load($className) {
  471. if (!isset(self::$_classMap[$className])) {
  472. return false;
  473. }
  474. if ($file = self::_mapped($className)) {
  475. return include $file;
  476. }
  477. $parts = explode('.', self::$_classMap[$className], 2);
  478. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  479. $paths = self::path($package, $plugin);
  480. if (empty($plugin)) {
  481. $appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
  482. $paths[] = $appLibs . $package . DS;
  483. $paths[] = APP . $package . DS;
  484. $paths[] = CAKE . $package . DS;
  485. } else {
  486. $pluginPath = self::pluginPath($plugin);
  487. $paths[] = $pluginPath . 'Lib' . DS . $package . DS;
  488. $paths[] = $pluginPath . $package . DS;
  489. }
  490. foreach ($paths as $path) {
  491. $file = $path . $className . '.php';
  492. if (file_exists($file)) {
  493. self::_map($file, $className);
  494. return include $file;
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * Returns the package name where a class was defined to be located at
  501. *
  502. * @param string $className name of the class to obtain the package name from
  503. * @return string package name or null if not declared
  504. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
  505. */
  506. public static function location($className) {
  507. if (!empty(self::$_classMap[$className])) {
  508. return self::$_classMap[$className];
  509. }
  510. return null;
  511. }
  512. /**
  513. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  514. * not construct any classes contained in the files. It will only find and require() the file.
  515. *
  516. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
  517. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  518. * an single array to $type,
  519. * @param string $name Name of the Class or a unique name for the file
  520. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  521. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  522. * $ext allows setting the extension of the file name
  523. * based on Inflector::underscore($name) . ".$ext";
  524. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  525. * @param string $file full name of the file to search for including extension
  526. * @param boolean $return Return the loaded file, the file must have a return
  527. * statement in it to work: return $variable;
  528. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  529. */
  530. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  531. $ext = null;
  532. if (is_array($type)) {
  533. extract($type, EXTR_OVERWRITE);
  534. }
  535. if (is_array($parent)) {
  536. extract($parent, EXTR_OVERWRITE);
  537. }
  538. if ($name == null && $file == null) {
  539. return false;
  540. }
  541. if (is_array($name)) {
  542. foreach ($name as $class) {
  543. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  544. return false;
  545. }
  546. }
  547. return true;
  548. }
  549. $originalType = strtolower($type);
  550. $specialPackage = in_array($originalType, array('file', 'vendor'));
  551. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  552. $type = self::$legacy[$originalType . 's'];
  553. }
  554. list($plugin, $name) = pluginSplit($name);
  555. if (!empty($plugin)) {
  556. if (!CakePlugin::loaded($plugin)) {
  557. return false;
  558. }
  559. }
  560. if (!$specialPackage) {
  561. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  562. }
  563. if ($originalType == 'file' && !empty($file)) {
  564. return self::_loadFile($name, $plugin, $search, $file, $return);
  565. }
  566. if ($originalType == 'vendor') {
  567. return self::_loadVendor($name, $plugin, $file, $ext);
  568. }
  569. return false;
  570. }
  571. /**
  572. * Helper function to include classes
  573. * This is a compatibility wrapper around using App::uses() and automatic class loading
  574. *
  575. * @param string $name unique name of the file for identifying it inside the application
  576. * @param string $plugin camel cased plugin name if any
  577. * @param string $type name of the packed where the class is located
  578. * @param string $originalType type name as supplied initially by the user
  579. * @param boolean $parent whether to load the class parent or not
  580. * @return boolean true indicating the successful load and existence of the class
  581. */
  582. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  583. if ($type == 'Console/Command' && $name == 'Shell') {
  584. $type = 'Console';
  585. } elseif (isset(self::$types[$originalType]['suffix'])) {
  586. $suffix = self::$types[$originalType]['suffix'];
  587. $name .= ($suffix == $name) ? '' : $suffix;
  588. }
  589. if ($parent && isset(self::$types[$originalType]['extends'])) {
  590. $extends = self::$types[$originalType]['extends'];
  591. $extendType = $type;
  592. if (strpos($extends, '/') !== false) {
  593. $parts = explode('/', $extends);
  594. $extends = array_pop($parts);
  595. $extendType = implode('/', $parts);
  596. }
  597. App::uses($extends, $extendType);
  598. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  599. App::uses($plugin . $extends, $plugin . '.' . $type);
  600. }
  601. }
  602. if ($plugin) {
  603. $plugin .= '.';
  604. }
  605. $name = Inflector::camelize($name);
  606. App::uses($name, $plugin . $type);
  607. return class_exists($name);
  608. }
  609. /**
  610. * Helper function to include single files
  611. *
  612. * @param string $name unique name of the file for identifying it inside the application
  613. * @param string $plugin camel cased plugin name if any
  614. * @param array $search list of paths to search the file into
  615. * @param string $file filename if known, the $name param will be used otherwise
  616. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  617. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  618. */
  619. protected static function _loadFile($name, $plugin, $search, $file, $return) {
  620. $mapped = self::_mapped($name, $plugin);
  621. if ($mapped) {
  622. $file = $mapped;
  623. } elseif (!empty($search)) {
  624. foreach ($search as $path) {
  625. $found = false;
  626. if (file_exists($path . $file)) {
  627. $file = $path . $file;
  628. $found = true;
  629. break;
  630. }
  631. if (empty($found)) {
  632. $file = false;
  633. }
  634. }
  635. }
  636. if (!empty($file) && file_exists($file)) {
  637. self::_map($file, $name, $plugin);
  638. $returnValue = include $file;
  639. if ($return) {
  640. return $returnValue;
  641. }
  642. return (bool)$returnValue;
  643. }
  644. return false;
  645. }
  646. /**
  647. * Helper function to load files from vendors folders
  648. *
  649. * @param string $name unique name of the file for identifying it inside the application
  650. * @param string $plugin camel cased plugin name if any
  651. * @param string $file file name if known
  652. * @param string $ext file extension if known
  653. * @return boolean true if the file was loaded successfully, false otherwise
  654. */
  655. protected static function _loadVendor($name, $plugin, $file, $ext) {
  656. if ($mapped = self::_mapped($name, $plugin)) {
  657. return (bool)include_once $mapped;
  658. }
  659. $fileTries = array();
  660. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  661. if (empty($ext)) {
  662. $ext = 'php';
  663. }
  664. if (empty($file)) {
  665. $fileTries[] = $name . '.' . $ext;
  666. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  667. } else {
  668. $fileTries[] = $file;
  669. }
  670. foreach ($fileTries as $file) {
  671. foreach ($paths as $path) {
  672. if (file_exists($path . $file)) {
  673. self::_map($path . $file, $name, $plugin);
  674. return (bool)include $path . $file;
  675. }
  676. }
  677. }
  678. return false;
  679. }
  680. /**
  681. * Initializes the cache for App, registers a shutdown function.
  682. *
  683. * @return void
  684. */
  685. public static function init() {
  686. self::$_map += (array)Cache::read('file_map', '_cake_core_');
  687. self::$_objects += (array)Cache::read('object_map', '_cake_core_');
  688. register_shutdown_function(array('App', 'shutdown'));
  689. }
  690. /**
  691. * Maps the $name to the $file.
  692. *
  693. * @param string $file full path to file
  694. * @param string $name unique name for this map
  695. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  696. * @return void
  697. */
  698. protected static function _map($file, $name, $plugin = null) {
  699. if ($plugin) {
  700. self::$_map['Plugin'][$plugin][$name] = $file;
  701. } else {
  702. self::$_map[$name] = $file;
  703. }
  704. if (!self::$bootstrapping) {
  705. self::$_cacheChange = true;
  706. }
  707. }
  708. /**
  709. * Returns a file's complete path.
  710. *
  711. * @param string $name unique name
  712. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  713. * @return mixed file path if found, false otherwise
  714. */
  715. protected static function _mapped($name, $plugin = null) {
  716. if ($plugin) {
  717. if (isset(self::$_map['Plugin'][$plugin][$name])) {
  718. return self::$_map['Plugin'][$plugin][$name];
  719. }
  720. return false;
  721. }
  722. if (isset(self::$_map[$name])) {
  723. return self::$_map[$name];
  724. }
  725. return false;
  726. }
  727. /**
  728. * Sets then returns the templates for each customizable package path
  729. *
  730. * @return array templates for each customizable package path
  731. */
  732. protected static function _packageFormat() {
  733. if (empty(self::$_packageFormat)) {
  734. self::$_packageFormat = array(
  735. 'Model' => array(
  736. '%s' . 'Model' . DS
  737. ),
  738. 'Model/Behavior' => array(
  739. '%s' . 'Model' . DS . 'Behavior' . DS
  740. ),
  741. 'Model/Datasource' => array(
  742. '%s' . 'Model' . DS . 'Datasource' . DS
  743. ),
  744. 'Model/Datasource/Database' => array(
  745. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
  746. ),
  747. 'Model/Datasource/Session' => array(
  748. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
  749. ),
  750. 'Controller' => array(
  751. '%s' . 'Controller' . DS
  752. ),
  753. 'Controller/Component' => array(
  754. '%s' . 'Controller' . DS . 'Component' . DS
  755. ),
  756. 'Controller/Component/Auth' => array(
  757. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS
  758. ),
  759. 'Controller/Component/Acl' => array(
  760. '%s' . 'Controller' . DS . 'Component' . DS . 'Acl' . DS
  761. ),
  762. 'View' => array(
  763. '%s' . 'View' . DS
  764. ),
  765. 'View/Helper' => array(
  766. '%s' . 'View' . DS . 'Helper' . DS
  767. ),
  768. 'Console' => array(
  769. '%s' . 'Console' . DS
  770. ),
  771. 'Console/Command' => array(
  772. '%s' . 'Console' . DS . 'Command' . DS
  773. ),
  774. 'Console/Command/Task' => array(
  775. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS
  776. ),
  777. 'Lib' => array(
  778. '%s' . 'Lib' . DS
  779. ),
  780. 'Locale' => array(
  781. '%s' . 'Locale' . DS
  782. ),
  783. 'Vendor' => array(
  784. '%s' . 'Vendor' . DS,
  785. dirname(dirname(CAKE)) . DS . 'vendors' . DS,
  786. ),
  787. 'Plugin' => array(
  788. APP . 'Plugin' . DS,
  789. dirname(dirname(CAKE)) . DS . 'plugins' . DS
  790. )
  791. );
  792. }
  793. return self::$_packageFormat;
  794. }
  795. /**
  796. * Object destructor.
  797. *
  798. * Writes cache file if changes have been made to the $_map
  799. *
  800. * @return void
  801. */
  802. public static function shutdown() {
  803. if (self::$_cacheChange) {
  804. Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
  805. }
  806. if (self::$_objectCacheChange) {
  807. Cache::write('object_map', self::$_objects, '_cake_core_');
  808. }
  809. }
  810. }