App.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. <?php
  2. /**
  3. * App class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs
  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/libs/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/view/933/The-App-Class
  60. * @package cake.libs
  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. * Reset paths instead of merging
  77. *
  78. * @constant RESET
  79. */
  80. const RESET = true;
  81. /**
  82. * List of object types and their properties
  83. *
  84. * @var array
  85. */
  86. public static $types = array(
  87. 'class' => array('extends' => null, 'core' => true),
  88. 'file' => array('extends' => null, 'core' => true),
  89. 'model' => array('extends' => 'AppModel', 'core' => false),
  90. 'behavior' => array( 'suffix' => 'Behavior', 'extends' => 'Model/ModelBehavior', 'core' => true),
  91. 'controller' => array('suffix' => 'Controller', 'extends' => 'AppController', 'core' => true),
  92. 'component' => array('suffix' => 'Component', 'extends' => null, 'core' => true),
  93. 'lib' => array('extends' => null, 'core' => true),
  94. 'view' => array('suffix' => 'View', 'extends' => null, 'core' => true),
  95. 'helper' => array('suffix' => 'Helper', 'extends' => 'AppHelper', 'core' => true),
  96. 'vendor' => array('extends' => null, 'core' => true),
  97. 'shell' => array('suffix' => 'Shell', 'extends' => 'Shell', 'core' => true),
  98. 'plugin' => array('extends' => null, 'core' => true)
  99. );
  100. /**
  101. * Paths to search for files.
  102. *
  103. * @var array
  104. */
  105. public static $search = array();
  106. /**
  107. * Whether or not to return the file that is loaded.
  108. *
  109. * @var boolean
  110. */
  111. public static $return = false;
  112. /**
  113. * Determines if $__maps and $__paths cache should be written.
  114. *
  115. * @var boolean
  116. */
  117. private static $__cache = false;
  118. /**
  119. * Holds key/value pairs of $type => file path.
  120. *
  121. * @var array
  122. */
  123. private static $__map = array();
  124. /**
  125. * Holds paths for deep searching of files.
  126. *
  127. * @var array
  128. */
  129. private static $__paths = array();
  130. /**
  131. * Holds loaded files.
  132. *
  133. * @var array
  134. */
  135. private static $__loaded = array();
  136. /**
  137. * Holds and key => value array of object types.
  138. *
  139. * @var array
  140. */
  141. private static $__objects = array();
  142. /**
  143. * Holds the location of each class
  144. *
  145. */
  146. private static $__classMap = array();
  147. /**
  148. * Holds the possible paths for each package name
  149. *
  150. */
  151. private static $__packages = array();
  152. /**
  153. * Holds the templates for each customizable package path in the application
  154. *
  155. */
  156. private static $__packageFormat = array();
  157. /**
  158. * Maps an old style CakePHP class type to the corresponding package
  159. *
  160. */
  161. public static $legacy = array(
  162. 'models' => 'Model',
  163. 'behaviors' => 'Model/Behavior',
  164. 'datasources' => 'Model/Datasource',
  165. 'controllers' => 'Controller',
  166. 'components' => 'Controller/Component',
  167. 'views' => 'View',
  168. 'helpers' => 'View/Helper',
  169. 'shells' => 'Console/Command',
  170. 'libs' => 'Lib'
  171. );
  172. /**
  173. * Inicates whether the class cache should be stored again because of an addition to it
  174. *
  175. */
  176. private static $_cacheChange = false;
  177. /**
  178. * Inicates whether the object cache should be stored again because of an addition to it
  179. *
  180. */
  181. private static $_objectCacheChange = false;
  182. /**
  183. * Used to read information stored path
  184. *
  185. * Usage:
  186. *
  187. * `App::path('Model'); will return all paths for models`
  188. *
  189. * `App::path('Model/Datasource', 'MyPlugin'); will return the path for datasources under the 'MyPlugin' plugin`
  190. *
  191. * @param string $type type of path
  192. * @param string $plugin name of plugin
  193. * @return string array
  194. */
  195. public static function path($type, $plugin = null) {
  196. if (!empty(self::$legacy[$type])) {
  197. $type = self::$legacy[$type];
  198. }
  199. if (!empty($plugin)) {
  200. $path = array();
  201. $pluginPath = self::pluginPath($plugin);
  202. if (!empty(self::$__packageFormat[$type])) {
  203. foreach (self::$__packageFormat[$type] as $f) {
  204. $path[] = sprintf($f, $pluginPath);
  205. }
  206. }
  207. $path[] = $pluginPath . 'Lib' . DS . $type . DS;
  208. return $path;
  209. }
  210. if (!isset(self::$__packages[$type])) {
  211. return array();
  212. }
  213. return self::$__packages[$type];
  214. }
  215. /**
  216. * Sets up each package location on the file system. You can configure multiple search paths
  217. * for each package, those will be used to look for files one folder at a time in the specified order
  218. * All paths should be terminated with a Directory separator
  219. *
  220. * Usage:
  221. *
  222. * `App::build(array(Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
  223. *
  224. * `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
  225. *
  226. * `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/))); will setup multiple search paths for helpers`
  227. *
  228. * If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
  229. *
  230. * @param array $paths associative array with package names as keys and a list of directories for new search paths
  231. * @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths, [default] App::PREPEND
  232. * @return void
  233. */
  234. public static function build($paths = array(), $mode = App::PREPEND) {
  235. if (empty(self::$__packageFormat)) {
  236. self::$__packageFormat = array(
  237. 'Model' => array(
  238. '%s' . 'Model' . DS,
  239. '%s' . 'models' . DS
  240. ),
  241. 'Model/Behavior' => array(
  242. '%s' . 'Model' . DS . 'Behavior' . DS,
  243. '%s' . 'models' . DS . 'behaviors' . DS
  244. ),
  245. 'Model/Datasource' => array(
  246. '%s' . 'Model' . DS . 'Datasource' . DS,
  247. '%s' . 'models' . DS . 'datasources' . DS
  248. ),
  249. 'Model/Datasource/Database' => array(
  250. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
  251. '%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
  252. ),
  253. 'Model/Datasource/Session' => array(
  254. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
  255. '%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
  256. ),
  257. 'Controller' => array(
  258. '%s' . 'Controller' . DS,
  259. '%s' . 'controllers' . DS
  260. ),
  261. 'Controller/Component' => array(
  262. '%s' . 'Controller' . DS . 'Component' . DS,
  263. '%s' . 'controllers' . DS . 'components' . DS
  264. ),
  265. 'View' => array(
  266. '%s' . 'View' . DS,
  267. '%s' . 'views' . DS
  268. ),
  269. 'View/Helper' => array(
  270. '%s' . 'View' . DS . 'Helper' . DS,
  271. '%s' . 'views' . DS . 'helpers' . DS
  272. ),
  273. 'Console' => array(
  274. '%s' . 'Console' . DS,
  275. '%s' . 'console' . DS
  276. ),
  277. 'Console/Command' => array(
  278. '%s' . 'Console' . DS . 'Command' . DS,
  279. '%s' . 'console' . DS . 'shells' . DS,
  280. ),
  281. 'Console/Command/Task' => array(
  282. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
  283. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
  284. ),
  285. 'Lib' => array(
  286. '%s' . 'Lib' . DS,
  287. '%s' . 'libs' . DS
  288. ),
  289. 'locales' => array(
  290. '%s' . 'locale' . DS
  291. ),
  292. 'vendors' => array('%s' . 'Vendor' . DS, VENDORS),
  293. 'plugins' => array(
  294. APP . 'Plugin' . DS,
  295. APP . 'plugins' . DS,
  296. dirname(dirname(CAKE)) . DS . 'plugins' . DS,
  297. )
  298. );
  299. }
  300. if ($mode === App::RESET) {
  301. foreach ($paths as $type => $new) {
  302. if (!empty(self::$legacy[$type])) {
  303. $type = self::$legacy[$type];
  304. }
  305. self::$__packages[$type] = (array)$new;
  306. self::objects($type, null, false);
  307. }
  308. return $paths;
  309. }
  310. //Provides Backwards compatibility for old-style package names
  311. $legacyPaths = array();
  312. foreach ($paths as $type => $path) {
  313. if (!empty(self::$legacy[$type])) {
  314. $type = self::$legacy[$type];
  315. }
  316. $legacyPaths[$type] = $path;
  317. }
  318. $paths = $legacyPaths;
  319. $defaults = array();
  320. foreach (self::$__packageFormat as $package => $format) {
  321. foreach ($format as $f) {
  322. $defaults[$package][] = sprintf($f, APP);
  323. }
  324. }
  325. foreach ($defaults as $type => $default) {
  326. if (empty(self::$__packages[$type]) || empty($paths)) {
  327. self::$__packages[$type] = $default;
  328. }
  329. if (!empty($paths[$type])) {
  330. if ($mode === App::PREPEND) {
  331. $path = array_merge((array)$paths[$type], self::$__packages[$type]);
  332. } else {
  333. $path = array_merge(self::$__packages[$type], (array)$paths[$type]);
  334. }
  335. } else {
  336. $path = self::$__packages[$type];
  337. }
  338. self::$__packages[$type] = array_values(array_unique($path));
  339. }
  340. }
  341. /**
  342. * Gets the path that a plugin is on. Searches through the defined plugin paths.
  343. *
  344. * Usage:
  345. *
  346. * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
  347. *
  348. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  349. * @return string full path to the plugin.
  350. */
  351. public static function pluginPath($plugin) {
  352. return CakePlugin::path($plugin);
  353. }
  354. /**
  355. * Finds the path that a theme is on. Searches through the defined theme paths.
  356. *
  357. * Usage:
  358. *
  359. * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
  360. *
  361. * @param string $theme theme name to find the path of.
  362. * @return string full path to the theme.
  363. */
  364. public static function themePath($theme) {
  365. $themeDir = 'Themed' . DS . Inflector::camelize($theme);
  366. foreach (self::$__packages['View'] as $path) {
  367. if (is_dir($path . $themeDir)) {
  368. return $path . $themeDir . DS ;
  369. }
  370. }
  371. return self::$__packages['View'][0] . $themeDir . DS;
  372. }
  373. /**
  374. * Returns the full path to a package inside the CakePHP core
  375. *
  376. * Usage:
  377. *
  378. * `App::core('Cache/Engine'); will return the full path to the cache engines package`
  379. *
  380. * @param string $type
  381. * @return string full path to package
  382. */
  383. public static function core($type) {
  384. return array(CAKE . str_replace('/', DS, $type) . DS);
  385. }
  386. /**
  387. * Returns an array of objects of the given type.
  388. *
  389. * Example usage:
  390. *
  391. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  392. *
  393. * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  394. *
  395. * You can also search only within a plugin's objects by using the plugin dot
  396. * syntax.
  397. *
  398. * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  399. *
  400. * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  401. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  402. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  403. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  404. */
  405. public static function objects($type, $path = null, $cache = true) {
  406. $extension = '/\.php$/';
  407. $includeDirectories = false;
  408. $name = $type;
  409. if ($type === 'plugin') {
  410. $type = 'plugins';
  411. }
  412. if ($type == 'plugins') {
  413. $extension = '/.*/';
  414. $includeDirectories = true;
  415. }
  416. list($plugin, $type) = pluginSplit($type);
  417. if (isset(self::$legacy[$type . 's'])) {
  418. $type = self::$legacy[$type . 's'];
  419. }
  420. if ($type === 'file' && !$path) {
  421. return false;
  422. } elseif ($type === 'file') {
  423. $extension = '/\.php$/';
  424. $name = $type . str_replace(DS, '', $path);
  425. }
  426. if (empty(self::$__objects) && $cache === true) {
  427. self::$__objects = Cache::read('object_map', '_cake_core_');
  428. }
  429. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  430. if ($cache !== true || !isset(self::$__objects[$cacheLocation][$name])) {
  431. $objects = array();
  432. if (empty($path)) {
  433. $path = self::path($type, $plugin);
  434. }
  435. foreach ((array)$path as $dir) {
  436. if ($dir != APP && is_dir($dir)) {
  437. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  438. foreach ($files as $file) {
  439. if (!$file->isDot()) {
  440. $isDir = $file->isDir() ;
  441. if ($isDir && $includeDirectories) {
  442. $objects[] = basename($file);
  443. } elseif (!$includeDirectories && !$isDir) {
  444. $objects[] = substr(basename($file), 0, -4);
  445. }
  446. }
  447. }
  448. }
  449. }
  450. if ($type !== 'file') {
  451. foreach ($objects as $key => $value) {
  452. $objects[$key] = Inflector::camelize($value);
  453. }
  454. }
  455. if ($cache === true) {
  456. self::$__cache = true;
  457. }
  458. if ($plugin) {
  459. return $objects;
  460. }
  461. sort($objects);
  462. self::$__objects[$cacheLocation][$name] = $objects;
  463. self::$_objectCacheChange = true;
  464. }
  465. return self::$__objects[$cacheLocation][$name];
  466. }
  467. /**
  468. * Declares a package for a class. This package location will be used
  469. * by the automatic class loader if the class is tried to be used
  470. *
  471. * Usage:
  472. *
  473. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  474. *
  475. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  476. *
  477. * @param string $className the name of the class to configure package for
  478. * @param string $location the package name
  479. */
  480. public static function uses($className, $location) {
  481. self::$__classMap[$className] = $location;
  482. }
  483. /**
  484. * Method to handle the automatic class loading. It will look for each class' package
  485. * defined using App::uses() and with this information it will resolve the package name to a full path
  486. * to load the class from. File name for each class should follow the class name. For instance,
  487. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  488. *
  489. * @param string $className the name of the class to load
  490. */
  491. public static function load($className) {
  492. if (!isset(self::$__classMap[$className])) {
  493. return false;
  494. }
  495. if ($file = self::__mapped($className)) {
  496. return include $file;
  497. }
  498. $parts = explode('.', self::$__classMap[$className], 2);
  499. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  500. $paths = self::path($package, $plugin);
  501. if (empty($plugin)) {
  502. $appLibs = empty(self::$__packages['Lib']) ? APPLIBS : current(self::$__packages['Lib']);
  503. $paths[] = $appLibs . $package . DS;
  504. $paths[] = CAKE . $package . DS;
  505. }
  506. foreach ($paths as $path) {
  507. $file = $path . $className . '.php';
  508. if (file_exists($file)) {
  509. self::__map($file, $className);
  510. return include $file;
  511. }
  512. }
  513. //To help apps migrate to 2.0 old style file names are allowed
  514. foreach ($paths as $path) {
  515. $underscored = Inflector::underscore($className);
  516. $tries = array($path . $underscored . '.php');
  517. $parts = explode('_', $underscored);
  518. if (count($parts) > 1) {
  519. array_pop($parts);
  520. $tries[] = $path . implode('_', $parts) . '.php';
  521. }
  522. foreach ($tries as $file) {
  523. if (file_exists($file)) {
  524. self::__map($file, $className);
  525. return include $file;
  526. }
  527. }
  528. }
  529. return false;
  530. }
  531. /**
  532. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  533. * not construct any classes contained in the files. It will only find and require() the file.
  534. *
  535. * @link http://book.cakephp.org/view/934/Using-App-import
  536. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  537. * an single array to $type,
  538. * @param string $name Name of the Class or a unique name for the file
  539. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  540. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  541. * $ext allows setting the extension of the file name
  542. * based on Inflector::underscore($name) . ".$ext";
  543. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  544. * @param string $file full name of the file to search for including extension
  545. * @param boolean $return, return the loaded file, the file must have a return
  546. * statement in it to work: return $variable;
  547. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  548. */
  549. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  550. $ext = null;
  551. if (is_array($type)) {
  552. extract($type, EXTR_OVERWRITE);
  553. }
  554. if (is_array($parent)) {
  555. extract($parent, EXTR_OVERWRITE);
  556. }
  557. if ($name == null && $file == null) {
  558. return false;
  559. }
  560. if (is_array($name)) {
  561. foreach ($name as $class) {
  562. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  563. return false;
  564. }
  565. }
  566. return true;
  567. }
  568. $originalType = strtolower($type);
  569. $specialPackage = in_array($originalType, array('file', 'vendor'));
  570. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  571. $type = self::$legacy[$originalType . 's'];
  572. }
  573. list($plugin, $name) = pluginSplit($name);
  574. if (!empty($plugin)) {
  575. if (!CakePlugin::loaded($plugin)) {
  576. return false;
  577. }
  578. }
  579. if (!$specialPackage) {
  580. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  581. }
  582. if ($originalType == 'file' && !empty($file)) {
  583. return self::_loadFile($name, $plugin, $search, $file, $return);
  584. }
  585. if ($originalType == 'vendor') {
  586. return self::_loadVendor($name, $plugin, $file, $ext);
  587. }
  588. return false;
  589. }
  590. /**
  591. * Helper function to include classes
  592. * This is a compatibility wrapper around using App::uses() and automatic class loading
  593. *
  594. * @param string $name unique name of the file for identifying it inside the application
  595. * @param string $plugin camel cased plugin name if any
  596. * @param string $type name of the packed where the class is located
  597. * @param string $file filename if known, the $name param will be used otherwise
  598. * @param string $originalType type name as supplied initially by the user
  599. * @param boolean $parent whether to load the class parent or not
  600. * @return boolean true indicating the successful load and existence of the class
  601. */
  602. private function _loadClass($name, $plugin, $type, $originalType, $parent) {
  603. if ($type == 'Console/Command' && $name == 'Shell') {
  604. $type = 'Console';
  605. } else if (isset(self::$types[$originalType]['suffix'])) {
  606. $suffix = self::$types[$originalType]['suffix'];
  607. $name .= ($suffix == $name) ? '' : $suffix;
  608. }
  609. if ($parent && isset(self::$types[$originalType]['extends'])) {
  610. $extends = self::$types[$originalType]['extends'];
  611. $extendType = $type;
  612. if (strpos($extends, '/') !== false) {
  613. $parts = explode('/', $extends);
  614. $extends = array_pop($parts);
  615. $extendType = implode('/', $parts);
  616. }
  617. App::uses($extends, $extendType);
  618. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  619. App::uses($plugin . $extends, $plugin . '.' .$type);
  620. }
  621. }
  622. if ($plugin) {
  623. $plugin .= '.';
  624. }
  625. $name = Inflector::camelize($name);
  626. App::uses($name, $plugin . $type);
  627. return class_exists($name);
  628. }
  629. /**
  630. * Helper function to include single files
  631. *
  632. * @param string $name unique name of the file for identifying it inside the application
  633. * @param string $plugin camel cased plugin name if any
  634. * @param array $search list of paths to search the file into
  635. * @param string $file filename if known, the $name param will be used otherwise
  636. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  637. * @return mixed, if $return contents of the file after php parses it, boolean indicating success otherwise
  638. */
  639. private function _loadFile($name, $plugin, $search, $file, $return) {
  640. $mapped = self::__mapped($name, $plugin);
  641. if ($mapped) {
  642. $file = $mapped;
  643. } else if (!empty($search)) {
  644. foreach ($search as $path) {
  645. $found = false;
  646. if (file_exists($path . $file)) {
  647. $file = $path . $file;
  648. $found = true;
  649. break;
  650. }
  651. if (empty($found)) {
  652. $file = false;
  653. }
  654. }
  655. }
  656. if (!empty($file) && file_exists($file)) {
  657. self::__map($file, $name, $plugin);
  658. $returnValue = include $file;
  659. if ($return) {
  660. return $returnValue;
  661. }
  662. return (bool) $returnValue;
  663. }
  664. return false;
  665. }
  666. /**
  667. * Helper function to load files from vendors folders
  668. *
  669. * @param string $name unique name of the file for identifying it inside the application
  670. * @param string $plugin camel cased plugin name if any
  671. * @param string $file file name if known
  672. * @param string $ext file extension if known
  673. * @return boolean true if the file was loaded successfully, false otherwise
  674. */
  675. private function _loadVendor($name, $plugin, $file, $ext) {
  676. if ($mapped = self::__mapped($name, $plugin)) {
  677. return (bool) include_once($mapped);
  678. }
  679. $fileTries = array();
  680. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  681. if (empty($ext)) {
  682. $ext = 'php';
  683. }
  684. if (empty($file)) {
  685. $fileTries[] = $name . '.' . $ext;
  686. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  687. } else {
  688. $fileTries[] = $file;
  689. }
  690. foreach ($fileTries as $file) {
  691. foreach ($paths as $path) {
  692. if (file_exists($path . $file)) {
  693. self::__map($path . $file, $name, $plugin);
  694. return (bool) include($path . $file);
  695. }
  696. }
  697. }
  698. return false;
  699. }
  700. /**
  701. * Initializes the cache for App, registers a shutdown function.
  702. *
  703. * @return void
  704. */
  705. public static function init() {
  706. self::$__map = (array)Cache::read('file_map', '_cake_core_');
  707. self::$__objects = (array)Cache::read('object_map', '_cake_core_');
  708. register_shutdown_function(array('App', 'shutdown'));
  709. self::uses('CakePlugin', 'Core');
  710. }
  711. /**
  712. * Maps the $name to the $file.
  713. *
  714. * @param string $file full path to file
  715. * @param string $name unique name for this map
  716. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  717. * @return void
  718. * @access private
  719. */
  720. private static function __map($file, $name, $plugin = null) {
  721. if ($plugin) {
  722. self::$__map['Plugin'][$plugin][$name] = $file;
  723. } else {
  724. self::$__map[$name] = $file;
  725. }
  726. self::$_cacheChange = true;
  727. }
  728. /**
  729. * Returns a file's complete path.
  730. *
  731. * @param string $name unique name
  732. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  733. * @return mixed, file path if found, false otherwise
  734. * @access private
  735. */
  736. private static function __mapped($name, $plugin = null) {
  737. if ($plugin) {
  738. if (isset(self::$__map['Plugin'][$plugin][$name])) {
  739. return self::$__map['Plugin'][$plugin][$name];
  740. }
  741. return false;
  742. }
  743. if (isset(self::$__map[$name])) {
  744. return self::$__map[$name];
  745. }
  746. return false;
  747. }
  748. /**
  749. * Object destructor.
  750. *
  751. * Writes cache file if changes have been made to the $__map or $__paths
  752. *
  753. * @return void
  754. */
  755. public static function shutdown() {
  756. if (self::$__cache && self::$_cacheChange) {
  757. Cache::write('file_map', array_filter(self::$__map), '_cake_core_');
  758. }
  759. if (self::$__cache && self::$_objectCacheChange) {
  760. Cache::write('object_map', self::$__objects, '_cake_core_');
  761. }
  762. }
  763. }