App.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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.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/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. * Indicates whether the class cache should be stored again because of an addition to it
  174. *
  175. */
  176. private static $_cacheChange = false;
  177. /**
  178. * Indicates 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. 'Controller/Component/Auth' => array(
  266. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
  267. '%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
  268. ),
  269. 'View' => array(
  270. '%s' . 'View' . DS,
  271. '%s' . 'views' . DS
  272. ),
  273. 'View/Helper' => array(
  274. '%s' . 'View' . DS . 'Helper' . DS,
  275. '%s' . 'views' . DS . 'helpers' . DS
  276. ),
  277. 'Console' => array(
  278. '%s' . 'Console' . DS,
  279. '%s' . 'console' . DS
  280. ),
  281. 'Console/Command' => array(
  282. '%s' . 'Console' . DS . 'Command' . DS,
  283. '%s' . 'console' . DS . 'shells' . DS,
  284. ),
  285. 'Console/Command/Task' => array(
  286. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
  287. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
  288. ),
  289. 'Lib' => array(
  290. '%s' . 'Lib' . DS,
  291. '%s' . 'libs' . DS
  292. ),
  293. 'locales' => array(
  294. '%s' . 'Locale' . DS,
  295. '%s' . 'locale' . DS
  296. ),
  297. 'vendors' => array('%s' . 'Vendor' . DS, VENDORS),
  298. 'plugins' => array(
  299. APP . 'Plugin' . DS,
  300. APP . 'plugins' . DS,
  301. dirname(dirname(CAKE)) . DS . 'plugins' . DS,
  302. )
  303. );
  304. }
  305. if ($mode === App::RESET) {
  306. foreach ($paths as $type => $new) {
  307. if (!empty(self::$legacy[$type])) {
  308. $type = self::$legacy[$type];
  309. }
  310. self::$__packages[$type] = (array)$new;
  311. self::objects($type, null, false);
  312. }
  313. return $paths;
  314. }
  315. //Provides Backwards compatibility for old-style package names
  316. $legacyPaths = array();
  317. foreach ($paths as $type => $path) {
  318. if (!empty(self::$legacy[$type])) {
  319. $type = self::$legacy[$type];
  320. }
  321. $legacyPaths[$type] = $path;
  322. }
  323. $paths = $legacyPaths;
  324. $defaults = array();
  325. foreach (self::$__packageFormat as $package => $format) {
  326. foreach ($format as $f) {
  327. $defaults[$package][] = sprintf($f, APP);
  328. }
  329. }
  330. foreach ($defaults as $type => $default) {
  331. if (empty(self::$__packages[$type]) || empty($paths)) {
  332. self::$__packages[$type] = $default;
  333. }
  334. if (!empty($paths[$type])) {
  335. if ($mode === App::PREPEND) {
  336. $path = array_merge((array)$paths[$type], self::$__packages[$type]);
  337. } else {
  338. $path = array_merge(self::$__packages[$type], (array)$paths[$type]);
  339. }
  340. } else {
  341. $path = self::$__packages[$type];
  342. }
  343. self::$__packages[$type] = array_values(array_unique($path));
  344. }
  345. }
  346. /**
  347. * Gets the path that a plugin is on. Searches through the defined plugin paths.
  348. *
  349. * Usage:
  350. *
  351. * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
  352. *
  353. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  354. * @return string full path to the plugin.
  355. */
  356. public static function pluginPath($plugin) {
  357. return CakePlugin::path($plugin);
  358. }
  359. /**
  360. * Finds the path that a theme is on. Searches through the defined theme paths.
  361. *
  362. * Usage:
  363. *
  364. * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
  365. *
  366. * @param string $theme theme name to find the path of.
  367. * @return string full path to the theme.
  368. */
  369. public static function themePath($theme) {
  370. $themeDir = 'Themed' . DS . Inflector::camelize($theme);
  371. foreach (self::$__packages['View'] as $path) {
  372. if (is_dir($path . $themeDir)) {
  373. return $path . $themeDir . DS ;
  374. }
  375. }
  376. return self::$__packages['View'][0] . $themeDir . DS;
  377. }
  378. /**
  379. * Returns the full path to a package inside the CakePHP core
  380. *
  381. * Usage:
  382. *
  383. * `App::core('Cache/Engine'); will return the full path to the cache engines package`
  384. *
  385. * @param string $type
  386. * @return string full path to package
  387. */
  388. public static function core($type) {
  389. return array(CAKE . str_replace('/', DS, $type) . DS);
  390. }
  391. /**
  392. * Returns an array of objects of the given type.
  393. *
  394. * Example usage:
  395. *
  396. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  397. *
  398. * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  399. *
  400. * You can also search only within a plugin's objects by using the plugin dot
  401. * syntax.
  402. *
  403. * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  404. *
  405. * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  406. * @param mixed $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  407. * @param boolean $cache Set to false to rescan objects of the chosen type. Defaults to true.
  408. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  409. */
  410. public static function objects($type, $path = null, $cache = true) {
  411. $extension = '/\.php$/';
  412. $includeDirectories = false;
  413. $name = $type;
  414. if ($type === 'plugin') {
  415. $type = 'plugins';
  416. }
  417. if ($type == 'plugins') {
  418. $extension = '/.*/';
  419. $includeDirectories = true;
  420. }
  421. list($plugin, $type) = pluginSplit($type);
  422. if (isset(self::$legacy[$type . 's'])) {
  423. $type = self::$legacy[$type . 's'];
  424. }
  425. if ($type === 'file' && !$path) {
  426. return false;
  427. } elseif ($type === 'file') {
  428. $extension = '/\.php$/';
  429. $name = $type . str_replace(DS, '', $path);
  430. }
  431. if (empty(self::$__objects) && $cache === true) {
  432. self::$__objects = Cache::read('object_map', '_cake_core_');
  433. }
  434. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  435. if ($cache !== true || !isset(self::$__objects[$cacheLocation][$name])) {
  436. $objects = array();
  437. if (empty($path)) {
  438. $path = self::path($type, $plugin);
  439. }
  440. foreach ((array)$path as $dir) {
  441. if ($dir != APP && is_dir($dir)) {
  442. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  443. foreach ($files as $file) {
  444. if (!$file->isDot()) {
  445. $isDir = $file->isDir() ;
  446. if ($isDir && $includeDirectories) {
  447. $objects[] = basename($file);
  448. } elseif (!$includeDirectories && !$isDir) {
  449. $objects[] = substr(basename($file), 0, -4);
  450. }
  451. }
  452. }
  453. }
  454. }
  455. if ($type !== 'file') {
  456. foreach ($objects as $key => $value) {
  457. $objects[$key] = Inflector::camelize($value);
  458. }
  459. }
  460. if ($cache === true) {
  461. self::$__cache = true;
  462. }
  463. sort($objects);
  464. if ($plugin) {
  465. return $objects;
  466. }
  467. self::$__objects[$cacheLocation][$name] = $objects;
  468. self::$_objectCacheChange = true;
  469. }
  470. return self::$__objects[$cacheLocation][$name];
  471. }
  472. /**
  473. * Declares a package for a class. This package location will be used
  474. * by the automatic class loader if the class is tried to be used
  475. *
  476. * Usage:
  477. *
  478. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  479. *
  480. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  481. *
  482. * @param string $className the name of the class to configure package for
  483. * @param string $location the package name
  484. */
  485. public static function uses($className, $location) {
  486. self::$__classMap[$className] = $location;
  487. }
  488. /**
  489. * Method to handle the automatic class loading. It will look for each class' package
  490. * defined using App::uses() and with this information it will resolve the package name to a full path
  491. * to load the class from. File name for each class should follow the class name. For instance,
  492. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  493. *
  494. * @param string $className the name of the class to load
  495. */
  496. public static function load($className) {
  497. if (!isset(self::$__classMap[$className])) {
  498. return false;
  499. }
  500. if ($file = self::__mapped($className)) {
  501. return include $file;
  502. }
  503. $parts = explode('.', self::$__classMap[$className], 2);
  504. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  505. $paths = self::path($package, $plugin);
  506. if (empty($plugin)) {
  507. $appLibs = empty(self::$__packages['Lib']) ? APPLIBS : current(self::$__packages['Lib']);
  508. $paths[] = $appLibs . $package . DS;
  509. $paths[] = CAKE . $package . DS;
  510. }
  511. foreach ($paths as $path) {
  512. $file = $path . $className . '.php';
  513. if (file_exists($file)) {
  514. self::__map($file, $className);
  515. return include $file;
  516. }
  517. }
  518. //To help apps migrate to 2.0 old style file names are allowed
  519. foreach ($paths as $path) {
  520. $underscored = Inflector::underscore($className);
  521. $tries = array($path . $underscored . '.php');
  522. $parts = explode('_', $underscored);
  523. if (count($parts) > 1) {
  524. array_pop($parts);
  525. $tries[] = $path . implode('_', $parts) . '.php';
  526. }
  527. foreach ($tries as $file) {
  528. if (file_exists($file)) {
  529. self::__map($file, $className);
  530. return include $file;
  531. }
  532. }
  533. }
  534. return false;
  535. }
  536. /**
  537. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  538. * not construct any classes contained in the files. It will only find and require() the file.
  539. *
  540. * @link http://book.cakephp.org/view/934/Using-App-import
  541. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  542. * an single array to $type,
  543. * @param string $name Name of the Class or a unique name for the file
  544. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  545. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  546. * $ext allows setting the extension of the file name
  547. * based on Inflector::underscore($name) . ".$ext";
  548. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  549. * @param string $file full name of the file to search for including extension
  550. * @param boolean $return, return the loaded file, the file must have a return
  551. * statement in it to work: return $variable;
  552. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  553. */
  554. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  555. $ext = null;
  556. if (is_array($type)) {
  557. extract($type, EXTR_OVERWRITE);
  558. }
  559. if (is_array($parent)) {
  560. extract($parent, EXTR_OVERWRITE);
  561. }
  562. if ($name == null && $file == null) {
  563. return false;
  564. }
  565. if (is_array($name)) {
  566. foreach ($name as $class) {
  567. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  568. return false;
  569. }
  570. }
  571. return true;
  572. }
  573. $originalType = strtolower($type);
  574. $specialPackage = in_array($originalType, array('file', 'vendor'));
  575. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  576. $type = self::$legacy[$originalType . 's'];
  577. }
  578. list($plugin, $name) = pluginSplit($name);
  579. if (!empty($plugin)) {
  580. if (!CakePlugin::loaded($plugin)) {
  581. return false;
  582. }
  583. }
  584. if (!$specialPackage) {
  585. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  586. }
  587. if ($originalType == 'file' && !empty($file)) {
  588. return self::_loadFile($name, $plugin, $search, $file, $return);
  589. }
  590. if ($originalType == 'vendor') {
  591. return self::_loadVendor($name, $plugin, $file, $ext);
  592. }
  593. return false;
  594. }
  595. /**
  596. * Helper function to include classes
  597. * This is a compatibility wrapper around using App::uses() and automatic class loading
  598. *
  599. * @param string $name unique name of the file for identifying it inside the application
  600. * @param string $plugin camel cased plugin name if any
  601. * @param string $type name of the packed where the class is located
  602. * @param string $file filename if known, the $name param will be used otherwise
  603. * @param string $originalType type name as supplied initially by the user
  604. * @param boolean $parent whether to load the class parent or not
  605. * @return boolean true indicating the successful load and existence of the class
  606. */
  607. private function _loadClass($name, $plugin, $type, $originalType, $parent) {
  608. if ($type == 'Console/Command' && $name == 'Shell') {
  609. $type = 'Console';
  610. } else if (isset(self::$types[$originalType]['suffix'])) {
  611. $suffix = self::$types[$originalType]['suffix'];
  612. $name .= ($suffix == $name) ? '' : $suffix;
  613. }
  614. if ($parent && isset(self::$types[$originalType]['extends'])) {
  615. $extends = self::$types[$originalType]['extends'];
  616. $extendType = $type;
  617. if (strpos($extends, '/') !== false) {
  618. $parts = explode('/', $extends);
  619. $extends = array_pop($parts);
  620. $extendType = implode('/', $parts);
  621. }
  622. App::uses($extends, $extendType);
  623. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  624. App::uses($plugin . $extends, $plugin . '.' .$type);
  625. }
  626. }
  627. if ($plugin) {
  628. $plugin .= '.';
  629. }
  630. $name = Inflector::camelize($name);
  631. App::uses($name, $plugin . $type);
  632. return class_exists($name);
  633. }
  634. /**
  635. * Helper function to include single files
  636. *
  637. * @param string $name unique name of the file for identifying it inside the application
  638. * @param string $plugin camel cased plugin name if any
  639. * @param array $search list of paths to search the file into
  640. * @param string $file filename if known, the $name param will be used otherwise
  641. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  642. * @return mixed, if $return contents of the file after php parses it, boolean indicating success otherwise
  643. */
  644. private function _loadFile($name, $plugin, $search, $file, $return) {
  645. $mapped = self::__mapped($name, $plugin);
  646. if ($mapped) {
  647. $file = $mapped;
  648. } else if (!empty($search)) {
  649. foreach ($search as $path) {
  650. $found = false;
  651. if (file_exists($path . $file)) {
  652. $file = $path . $file;
  653. $found = true;
  654. break;
  655. }
  656. if (empty($found)) {
  657. $file = false;
  658. }
  659. }
  660. }
  661. if (!empty($file) && file_exists($file)) {
  662. self::__map($file, $name, $plugin);
  663. $returnValue = include $file;
  664. if ($return) {
  665. return $returnValue;
  666. }
  667. return (bool) $returnValue;
  668. }
  669. return false;
  670. }
  671. /**
  672. * Helper function to load files from vendors folders
  673. *
  674. * @param string $name unique name of the file for identifying it inside the application
  675. * @param string $plugin camel cased plugin name if any
  676. * @param string $file file name if known
  677. * @param string $ext file extension if known
  678. * @return boolean true if the file was loaded successfully, false otherwise
  679. */
  680. private function _loadVendor($name, $plugin, $file, $ext) {
  681. if ($mapped = self::__mapped($name, $plugin)) {
  682. return (bool) include_once($mapped);
  683. }
  684. $fileTries = array();
  685. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  686. if (empty($ext)) {
  687. $ext = 'php';
  688. }
  689. if (empty($file)) {
  690. $fileTries[] = $name . '.' . $ext;
  691. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  692. } else {
  693. $fileTries[] = $file;
  694. }
  695. foreach ($fileTries as $file) {
  696. foreach ($paths as $path) {
  697. if (file_exists($path . $file)) {
  698. self::__map($path . $file, $name, $plugin);
  699. return (bool) include($path . $file);
  700. }
  701. }
  702. }
  703. return false;
  704. }
  705. /**
  706. * Initializes the cache for App, registers a shutdown function.
  707. *
  708. * @return void
  709. */
  710. public static function init() {
  711. self::$__map = (array)Cache::read('file_map', '_cake_core_');
  712. self::$__objects = (array)Cache::read('object_map', '_cake_core_');
  713. register_shutdown_function(array('App', 'shutdown'));
  714. self::uses('CakePlugin', 'Core');
  715. }
  716. /**
  717. * Maps the $name to the $file.
  718. *
  719. * @param string $file full path to file
  720. * @param string $name unique name for this map
  721. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  722. * @return void
  723. * @access private
  724. */
  725. private static function __map($file, $name, $plugin = null) {
  726. if ($plugin) {
  727. self::$__map['Plugin'][$plugin][$name] = $file;
  728. } else {
  729. self::$__map[$name] = $file;
  730. }
  731. self::$_cacheChange = true;
  732. }
  733. /**
  734. * Returns a file's complete path.
  735. *
  736. * @param string $name unique name
  737. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  738. * @return mixed, file path if found, false otherwise
  739. * @access private
  740. */
  741. private static function __mapped($name, $plugin = null) {
  742. if ($plugin) {
  743. if (isset(self::$__map['Plugin'][$plugin][$name])) {
  744. return self::$__map['Plugin'][$plugin][$name];
  745. }
  746. return false;
  747. }
  748. if (isset(self::$__map[$name])) {
  749. return self::$__map[$name];
  750. }
  751. return false;
  752. }
  753. /**
  754. * Object destructor.
  755. *
  756. * Writes cache file if changes have been made to the $__map or $__paths
  757. *
  758. * @return void
  759. */
  760. public static function shutdown() {
  761. if (self::$__cache && self::$_cacheChange) {
  762. Cache::write('file_map', array_filter(self::$__map), '_cake_core_');
  763. }
  764. if (self::$__cache && self::$_objectCacheChange) {
  765. Cache::write('object_map', self::$__objects, '_cake_core_');
  766. }
  767. }
  768. }