App.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. );
  166. /**
  167. * Indicates whether the class cache should be stored again because of an addition to it
  168. *
  169. * @var boolean
  170. */
  171. protected static $_cacheChange = false;
  172. /**
  173. * Indicates whether the object cache should be stored again because of an addition to it
  174. *
  175. * @var boolean
  176. */
  177. protected static $_objectCacheChange = false;
  178. /**
  179. * Indicates the the Application is in the bootstrapping process. Used to better cache
  180. * loaded classes while the cache libraries have not been yet initialized
  181. *
  182. * @var boolean
  183. */
  184. public static $bootstrapping = false;
  185. /**
  186. * Used to read information stored path
  187. *
  188. * Usage:
  189. *
  190. * `App::path('Model'); will return all paths for models`
  191. *
  192. * `App::path('Model/Datasource', 'MyPlugin'); will return the path for datasources under the 'MyPlugin' plugin`
  193. *
  194. * @param string $type type of path
  195. * @param string $plugin name of plugin
  196. * @return string array
  197. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::path
  198. */
  199. public static function path($type, $plugin = null) {
  200. if (!empty(self::$legacy[$type])) {
  201. $type = self::$legacy[$type];
  202. }
  203. if (!empty($plugin)) {
  204. $path = array();
  205. $pluginPath = self::pluginPath($plugin);
  206. $packageFormat= self::_packageFormat();
  207. if (!empty($packageFormat[$type])) {
  208. foreach ($packageFormat[$type] as $f) {
  209. $path[] = sprintf($f, $pluginPath);
  210. }
  211. }
  212. $path[] = $pluginPath . 'Lib' . DS . $type . DS;
  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 string 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[] = CAKE . $package . DS;
  484. }
  485. foreach ($paths as $path) {
  486. $file = $path . $className . '.php';
  487. if (file_exists($file)) {
  488. self::_map($file, $className);
  489. return include $file;
  490. }
  491. }
  492. // To help apps migrate to 2.0 old style file names are allowed
  493. // if the trailing segment is one of the types that changed, alternates will be tried.
  494. foreach ($paths as $path) {
  495. $underscored = Inflector::underscore($className);
  496. $tries = array($path . $underscored . '.php');
  497. $parts = explode('_', $underscored);
  498. $numParts = count($parts);
  499. if ($numParts > 1 && in_array($parts[$numParts - 1], array('behavior', 'helper', 'component'))) {
  500. array_pop($parts);
  501. $tries[] = $path . implode('_', $parts) . '.php';
  502. }
  503. foreach ($tries as $file) {
  504. if (file_exists($file)) {
  505. self::_map($file, $className);
  506. return include $file;
  507. }
  508. }
  509. }
  510. return false;
  511. }
  512. /**
  513. * Returns the package name where a class was defined to be located at
  514. *
  515. * @param string $className name of the class to obtain the package name from
  516. * @return string package name or null if not declared
  517. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
  518. */
  519. public static function location($className) {
  520. if (!empty(self::$_classMap[$className])) {
  521. return self::$_classMap[$className];
  522. }
  523. return null;
  524. }
  525. /**
  526. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  527. * not construct any classes contained in the files. It will only find and require() the file.
  528. *
  529. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
  530. * @param mixed $type The type of Class if passed as a string, or all params can be passed as
  531. * an single array to $type,
  532. * @param string $name Name of the Class or a unique name for the file
  533. * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
  534. * array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
  535. * $ext allows setting the extension of the file name
  536. * based on Inflector::underscore($name) . ".$ext";
  537. * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
  538. * @param string $file full name of the file to search for including extension
  539. * @param boolean $return, return the loaded file, the file must have a return
  540. * statement in it to work: return $variable;
  541. * @return boolean true if Class is already in memory or if file is found and loaded, false if not
  542. */
  543. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  544. $ext = null;
  545. if (is_array($type)) {
  546. extract($type, EXTR_OVERWRITE);
  547. }
  548. if (is_array($parent)) {
  549. extract($parent, EXTR_OVERWRITE);
  550. }
  551. if ($name == null && $file == null) {
  552. return false;
  553. }
  554. if (is_array($name)) {
  555. foreach ($name as $class) {
  556. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  557. return false;
  558. }
  559. }
  560. return true;
  561. }
  562. $originalType = strtolower($type);
  563. $specialPackage = in_array($originalType, array('file', 'vendor'));
  564. if (!$specialPackage && isset(self::$legacy[$originalType . 's'])) {
  565. $type = self::$legacy[$originalType . 's'];
  566. }
  567. list($plugin, $name) = pluginSplit($name);
  568. if (!empty($plugin)) {
  569. if (!CakePlugin::loaded($plugin)) {
  570. return false;
  571. }
  572. }
  573. if (!$specialPackage) {
  574. return self::_loadClass($name, $plugin, $type, $originalType, $parent);
  575. }
  576. if ($originalType == 'file' && !empty($file)) {
  577. return self::_loadFile($name, $plugin, $search, $file, $return);
  578. }
  579. if ($originalType == 'vendor') {
  580. return self::_loadVendor($name, $plugin, $file, $ext);
  581. }
  582. return false;
  583. }
  584. /**
  585. * Helper function to include classes
  586. * This is a compatibility wrapper around using App::uses() and automatic class loading
  587. *
  588. * @param string $name unique name of the file for identifying it inside the application
  589. * @param string $plugin camel cased plugin name if any
  590. * @param string $type name of the packed where the class is located
  591. * @param string $originalType type name as supplied initially by the user
  592. * @param boolean $parent whether to load the class parent or not
  593. * @return boolean true indicating the successful load and existence of the class
  594. */
  595. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  596. if ($type == 'Console/Command' && $name == 'Shell') {
  597. $type = 'Console';
  598. } else if (isset(self::$types[$originalType]['suffix'])) {
  599. $suffix = self::$types[$originalType]['suffix'];
  600. $name .= ($suffix == $name) ? '' : $suffix;
  601. }
  602. if ($parent && isset(self::$types[$originalType]['extends'])) {
  603. $extends = self::$types[$originalType]['extends'];
  604. $extendType = $type;
  605. if (strpos($extends, '/') !== false) {
  606. $parts = explode('/', $extends);
  607. $extends = array_pop($parts);
  608. $extendType = implode('/', $parts);
  609. }
  610. App::uses($extends, $extendType);
  611. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  612. App::uses($plugin . $extends, $plugin . '.' .$type);
  613. }
  614. }
  615. if ($plugin) {
  616. $plugin .= '.';
  617. }
  618. $name = Inflector::camelize($name);
  619. App::uses($name, $plugin . $type);
  620. return class_exists($name);
  621. }
  622. /**
  623. * Helper function to include single files
  624. *
  625. * @param string $name unique name of the file for identifying it inside the application
  626. * @param string $plugin camel cased plugin name if any
  627. * @param array $search list of paths to search the file into
  628. * @param string $file filename if known, the $name param will be used otherwise
  629. * @param boolean $return whether this function should return the contents of the file after being parsed by php or just a success notice
  630. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  631. */
  632. protected static function _loadFile($name, $plugin, $search, $file, $return) {
  633. $mapped = self::_mapped($name, $plugin);
  634. if ($mapped) {
  635. $file = $mapped;
  636. } else if (!empty($search)) {
  637. foreach ($search as $path) {
  638. $found = false;
  639. if (file_exists($path . $file)) {
  640. $file = $path . $file;
  641. $found = true;
  642. break;
  643. }
  644. if (empty($found)) {
  645. $file = false;
  646. }
  647. }
  648. }
  649. if (!empty($file) && file_exists($file)) {
  650. self::_map($file, $name, $plugin);
  651. $returnValue = include $file;
  652. if ($return) {
  653. return $returnValue;
  654. }
  655. return (bool) $returnValue;
  656. }
  657. return false;
  658. }
  659. /**
  660. * Helper function to load files from vendors folders
  661. *
  662. * @param string $name unique name of the file for identifying it inside the application
  663. * @param string $plugin camel cased plugin name if any
  664. * @param string $file file name if known
  665. * @param string $ext file extension if known
  666. * @return boolean true if the file was loaded successfully, false otherwise
  667. */
  668. protected static function _loadVendor($name, $plugin, $file, $ext) {
  669. if ($mapped = self::_mapped($name, $plugin)) {
  670. return (bool) include_once($mapped);
  671. }
  672. $fileTries = array();
  673. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  674. if (empty($ext)) {
  675. $ext = 'php';
  676. }
  677. if (empty($file)) {
  678. $fileTries[] = $name . '.' . $ext;
  679. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  680. } else {
  681. $fileTries[] = $file;
  682. }
  683. foreach ($fileTries as $file) {
  684. foreach ($paths as $path) {
  685. if (file_exists($path . $file)) {
  686. self::_map($path . $file, $name, $plugin);
  687. return (bool) include($path . $file);
  688. }
  689. }
  690. }
  691. return false;
  692. }
  693. /**
  694. * Initializes the cache for App, registers a shutdown function.
  695. *
  696. * @return void
  697. */
  698. public static function init() {
  699. self::$_map += (array)Cache::read('file_map', '_cake_core_');
  700. self::$_objects += (array)Cache::read('object_map', '_cake_core_');
  701. register_shutdown_function(array('App', 'shutdown'));
  702. }
  703. /**
  704. * Maps the $name to the $file.
  705. *
  706. * @param string $file full path to file
  707. * @param string $name unique name for this map
  708. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  709. * @return void
  710. */
  711. protected static function _map($file, $name, $plugin = null) {
  712. if ($plugin) {
  713. self::$_map['Plugin'][$plugin][$name] = $file;
  714. } else {
  715. self::$_map[$name] = $file;
  716. }
  717. if (!self::$bootstrapping) {
  718. self::$_cacheChange = true;
  719. }
  720. }
  721. /**
  722. * Returns a file's complete path.
  723. *
  724. * @param string $name unique name
  725. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  726. * @return mixed file path if found, false otherwise
  727. */
  728. protected static function _mapped($name, $plugin = null) {
  729. if ($plugin) {
  730. if (isset(self::$_map['Plugin'][$plugin][$name])) {
  731. return self::$_map['Plugin'][$plugin][$name];
  732. }
  733. return false;
  734. }
  735. if (isset(self::$_map[$name])) {
  736. return self::$_map[$name];
  737. }
  738. return false;
  739. }
  740. /**
  741. * Sets then returns the templates for each customizable package path
  742. *
  743. * @return array templates for each customizable package path
  744. */
  745. protected static function _packageFormat() {
  746. if (empty(self::$_packageFormat)) {
  747. self::$_packageFormat = array(
  748. 'Model' => array(
  749. '%s' . 'Model' . DS,
  750. '%s' . 'models' . DS
  751. ),
  752. 'Model/Behavior' => array(
  753. '%s' . 'Model' . DS . 'Behavior' . DS,
  754. '%s' . 'models' . DS . 'behaviors' . DS
  755. ),
  756. 'Model/Datasource' => array(
  757. '%s' . 'Model' . DS . 'Datasource' . DS,
  758. '%s' . 'models' . DS . 'datasources' . DS
  759. ),
  760. 'Model/Datasource/Database' => array(
  761. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
  762. '%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
  763. ),
  764. 'Model/Datasource/Session' => array(
  765. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
  766. '%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
  767. ),
  768. 'Controller' => array(
  769. '%s' . 'Controller' . DS,
  770. '%s' . 'controllers' . DS
  771. ),
  772. 'Controller/Component' => array(
  773. '%s' . 'Controller' . DS . 'Component' . DS,
  774. '%s' . 'controllers' . DS . 'components' . DS
  775. ),
  776. 'Controller/Component/Auth' => array(
  777. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
  778. '%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
  779. ),
  780. 'View' => array(
  781. '%s' . 'View' . DS,
  782. '%s' . 'views' . DS
  783. ),
  784. 'View/Helper' => array(
  785. '%s' . 'View' . DS . 'Helper' . DS,
  786. '%s' . 'views' . DS . 'helpers' . DS
  787. ),
  788. 'Console' => array(
  789. '%s' . 'Console' . DS,
  790. '%s' . 'console' . DS
  791. ),
  792. 'Console/Command' => array(
  793. '%s' . 'Console' . DS . 'Command' . DS,
  794. '%s' . 'console' . DS . 'shells' . DS,
  795. ),
  796. 'Console/Command/Task' => array(
  797. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
  798. '%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
  799. ),
  800. 'Lib' => array(
  801. '%s' . 'Lib' . DS,
  802. '%s' . 'libs' . DS
  803. ),
  804. 'locales' => array(
  805. '%s' . 'Locale' . DS,
  806. '%s' . 'locale' . DS
  807. ),
  808. 'Vendor' => array(
  809. '%s' . 'Vendor' . DS, VENDORS
  810. ),
  811. 'Plugin' => array(
  812. APP . 'Plugin' . DS,
  813. APP . 'plugins' . DS,
  814. dirname(dirname(CAKE)) . DS . 'plugins' . DS
  815. )
  816. );
  817. }
  818. return self::$_packageFormat;
  819. }
  820. /**
  821. * Object destructor.
  822. *
  823. * Writes cache file if changes have been made to the $_map
  824. *
  825. * @return void
  826. */
  827. public static function shutdown() {
  828. if (self::$_cacheChange) {
  829. Cache::write('file_map', array_filter(self::$_map), '_cake_core_');
  830. }
  831. if (self::$_objectCacheChange) {
  832. Cache::write('object_map', self::$_objects, '_cake_core_');
  833. }
  834. }
  835. }