Base for a static organization website

App.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. <?php
  2. /**
  3. * App class
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Core
  15. * @since CakePHP(tm) v 1.2.0.6001
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Inflector', 'Utility');
  19. App::uses('CakePlugin', 'Core');
  20. /**
  21. * App is responsible for path management, class location and class loading.
  22. *
  23. * ### Adding paths
  24. *
  25. * You can add paths to the search indexes App uses to find classes using `App::build()`. Adding
  26. * additional controller paths for example would alter where CakePHP looks for controllers.
  27. * This allows you to split your application up across the filesystem.
  28. *
  29. * ### Packages
  30. *
  31. * CakePHP is organized around the idea of packages, each class belongs to a package or folder where other
  32. * classes reside. You can configure each package location in your application using `App::build('APackage/SubPackage', $paths)`
  33. * to inform the framework where should each class be loaded. Almost every class in the CakePHP framework can be swapped
  34. * by your own compatible implementation. If you wish to use your own class instead of the classes the framework provides,
  35. * just add the class to your libs folder mocking the directory location of where CakePHP expects to find it.
  36. *
  37. * For instance if you'd like to use your own HttpSocket class, put it under
  38. *
  39. * app/Network/Http/HttpSocket.php
  40. *
  41. * ### Inspecting loaded paths
  42. *
  43. * You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
  44. * controller paths.
  45. *
  46. * It is also possible to inspect paths for plugin classes, for instance, to see a plugin's helpers you would call
  47. * `App::path('View/Helper', 'MyPlugin')`
  48. *
  49. * ### Locating plugins and themes
  50. *
  51. * Plugins and Themes can be located with App as well. Using App::pluginPath('DebugKit') for example, will
  52. * give you the full path to the DebugKit plugin. App::themePath('purple'), would give the full path to the
  53. * `purple` theme.
  54. *
  55. * ### Inspecting known objects
  56. *
  57. * You can find out which objects App knows about using App::objects('Controller') for example to find
  58. * which application controllers App knows about.
  59. *
  60. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
  61. * @package Cake.Core
  62. */
  63. class App {
  64. /**
  65. * Append paths
  66. *
  67. * @var string
  68. */
  69. const APPEND = 'append';
  70. /**
  71. * Prepend paths
  72. *
  73. * @var string
  74. */
  75. const PREPEND = 'prepend';
  76. /**
  77. * Register package
  78. *
  79. * @var string
  80. */
  81. const REGISTER = 'register';
  82. /**
  83. * Reset paths instead of merging
  84. *
  85. * @var bool
  86. */
  87. const RESET = true;
  88. /**
  89. * List of object types and their properties
  90. *
  91. * @var array
  92. */
  93. public static $types = array(
  94. 'class' => array('extends' => null, 'core' => true),
  95. 'file' => array('extends' => null, 'core' => true),
  96. 'model' => array('extends' => 'AppModel', 'core' => false),
  97. 'behavior' => array('suffix' => 'Behavior', 'extends' => 'Model/ModelBehavior', 'core' => true),
  98. 'controller' => array('suffix' => 'Controller', 'extends' => 'AppController', 'core' => true),
  99. 'component' => array('suffix' => 'Component', 'extends' => null, 'core' => true),
  100. 'lib' => array('extends' => null, 'core' => true),
  101. 'view' => array('suffix' => 'View', 'extends' => null, 'core' => true),
  102. 'helper' => array('suffix' => 'Helper', 'extends' => 'AppHelper', 'core' => true),
  103. 'vendor' => array('extends' => null, 'core' => true),
  104. 'shell' => array('suffix' => 'Shell', 'extends' => 'AppShell', 'core' => true),
  105. 'plugin' => array('extends' => null, 'core' => true)
  106. );
  107. /**
  108. * Paths to search for files.
  109. *
  110. * @var array
  111. */
  112. public static $search = array();
  113. /**
  114. * Whether or not to return the file that is loaded.
  115. *
  116. * @var bool
  117. */
  118. public static $return = false;
  119. /**
  120. * Holds key/value pairs of $type => file path.
  121. *
  122. * @var array
  123. */
  124. protected static $_map = array();
  125. /**
  126. * Holds and key => value array of object types.
  127. *
  128. * @var array
  129. */
  130. protected static $_objects = array();
  131. /**
  132. * Holds the location of each class
  133. *
  134. * @var array
  135. */
  136. protected static $_classMap = array();
  137. /**
  138. * Holds the possible paths for each package name
  139. *
  140. * @var array
  141. */
  142. protected static $_packages = array();
  143. /**
  144. * Holds the templates for each customizable package path in the application
  145. *
  146. * @var array
  147. */
  148. protected static $_packageFormat = array();
  149. /**
  150. * Maps an old style CakePHP class type to the corresponding package
  151. *
  152. * @var array
  153. */
  154. public static $legacy = array(
  155. 'models' => 'Model',
  156. 'behaviors' => 'Model/Behavior',
  157. 'datasources' => 'Model/Datasource',
  158. 'controllers' => 'Controller',
  159. 'components' => 'Controller/Component',
  160. 'views' => 'View',
  161. 'helpers' => 'View/Helper',
  162. 'shells' => 'Console/Command',
  163. 'libs' => 'Lib',
  164. 'vendors' => 'Vendor',
  165. 'plugins' => 'Plugin',
  166. 'locales' => 'Locale'
  167. );
  168. /**
  169. * Indicates whether the class cache should be stored again because of an addition to it
  170. *
  171. * @var bool
  172. */
  173. protected static $_cacheChange = false;
  174. /**
  175. * Indicates whether the object cache should be stored again because of an addition to it
  176. *
  177. * @var bool
  178. */
  179. protected static $_objectCacheChange = false;
  180. /**
  181. * Indicates the the Application is in the bootstrapping process. Used to better cache
  182. * loaded classes while the cache libraries have not been yet initialized
  183. *
  184. * @var bool
  185. */
  186. public static $bootstrapping = false;
  187. /**
  188. * Used to read information stored path
  189. *
  190. * Usage:
  191. *
  192. * `App::path('Model'); will return all paths for models`
  193. *
  194. * `App::path('Model/Datasource', 'MyPlugin'); will return the path for datasources under the 'MyPlugin' plugin`
  195. *
  196. * @param string $type type of path
  197. * @param string $plugin name of plugin
  198. * @return array
  199. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::path
  200. */
  201. public static function path($type, $plugin = null) {
  202. if (!empty(static::$legacy[$type])) {
  203. $type = static::$legacy[$type];
  204. }
  205. if (!empty($plugin)) {
  206. $path = array();
  207. $pluginPath = CakePlugin::path($plugin);
  208. $packageFormat = static::_packageFormat();
  209. if (!empty($packageFormat[$type])) {
  210. foreach ($packageFormat[$type] as $f) {
  211. $path[] = sprintf($f, $pluginPath);
  212. }
  213. }
  214. return $path;
  215. }
  216. if (!isset(static::$_packages[$type])) {
  217. return array();
  218. }
  219. return static::$_packages[$type];
  220. }
  221. /**
  222. * Get all the currently loaded paths from App. Useful for inspecting
  223. * or storing all paths App knows about. For a paths to a specific package
  224. * use App::path()
  225. *
  226. * @return array An array of packages and their associated paths.
  227. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::paths
  228. */
  229. public static function paths() {
  230. return static::$_packages;
  231. }
  232. /**
  233. * Sets up each package location on the file system. You can configure multiple search paths
  234. * for each package, those will be used to look for files one folder at a time in the specified order
  235. * All paths should be terminated with a Directory separator
  236. *
  237. * Usage:
  238. *
  239. * `App::build(array('Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
  240. *
  241. * `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
  242. *
  243. * `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers`
  244. *
  245. * `App::build(array('Service' => array('%s' . 'Service' . DS)), App::REGISTER); will register new package 'Service'`
  246. *
  247. * If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
  248. *
  249. * @param array $paths associative array with package names as keys and a list of directories for new search paths
  250. * @param bool|string $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths (default)
  251. * App::REGISTER will register new packages and their paths, %s in path will be replaced by APP path
  252. * @return void
  253. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
  254. */
  255. public static function build($paths = array(), $mode = App::PREPEND) {
  256. //Provides Backwards compatibility for old-style package names
  257. $legacyPaths = array();
  258. foreach ($paths as $type => $path) {
  259. if (!empty(static::$legacy[$type])) {
  260. $type = static::$legacy[$type];
  261. }
  262. $legacyPaths[$type] = $path;
  263. }
  264. $paths = $legacyPaths;
  265. if ($mode === App::RESET) {
  266. foreach ($paths as $type => $new) {
  267. static::$_packages[$type] = (array)$new;
  268. static::objects($type, null, false);
  269. }
  270. return;
  271. }
  272. if (empty($paths)) {
  273. static::$_packageFormat = null;
  274. }
  275. $packageFormat = static::_packageFormat();
  276. if ($mode === App::REGISTER) {
  277. foreach ($paths as $package => $formats) {
  278. if (empty($packageFormat[$package])) {
  279. $packageFormat[$package] = $formats;
  280. } else {
  281. $formats = array_merge($packageFormat[$package], $formats);
  282. $packageFormat[$package] = array_values(array_unique($formats));
  283. }
  284. }
  285. static::$_packageFormat = $packageFormat;
  286. }
  287. $defaults = array();
  288. foreach ($packageFormat as $package => $format) {
  289. foreach ($format as $f) {
  290. $defaults[$package][] = sprintf($f, APP);
  291. }
  292. }
  293. if (empty($paths)) {
  294. static::$_packages = $defaults;
  295. return;
  296. }
  297. if ($mode === App::REGISTER) {
  298. $paths = array();
  299. }
  300. foreach ($defaults as $type => $default) {
  301. if (!empty(static::$_packages[$type])) {
  302. $path = static::$_packages[$type];
  303. } else {
  304. $path = $default;
  305. }
  306. if (!empty($paths[$type])) {
  307. $newPath = (array)$paths[$type];
  308. if ($mode === App::PREPEND) {
  309. $path = array_merge($newPath, $path);
  310. } else {
  311. $path = array_merge($path, $newPath);
  312. }
  313. $path = array_values(array_unique($path));
  314. }
  315. static::$_packages[$type] = $path;
  316. }
  317. }
  318. /**
  319. * Gets the path that a plugin is on. Searches through the defined plugin paths.
  320. *
  321. * Usage:
  322. *
  323. * `App::pluginPath('MyPlugin'); will return the full path to 'MyPlugin' plugin'`
  324. *
  325. * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
  326. * @return string full path to the plugin.
  327. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::pluginPath
  328. * @deprecated 3.0.0 Use `CakePlugin::path()` instead.
  329. */
  330. public static function pluginPath($plugin) {
  331. return CakePlugin::path($plugin);
  332. }
  333. /**
  334. * Finds the path that a theme is on. Searches through the defined theme paths.
  335. *
  336. * Usage:
  337. *
  338. * `App::themePath('MyTheme'); will return the full path to the 'MyTheme' theme`
  339. *
  340. * @param string $theme theme name to find the path of.
  341. * @return string full path to the theme.
  342. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::themePath
  343. */
  344. public static function themePath($theme) {
  345. $themeDir = 'Themed' . DS . Inflector::camelize($theme);
  346. foreach (static::$_packages['View'] as $path) {
  347. if (is_dir($path . $themeDir)) {
  348. return $path . $themeDir . DS;
  349. }
  350. }
  351. return static::$_packages['View'][0] . $themeDir . DS;
  352. }
  353. /**
  354. * Returns the full path to a package inside the CakePHP core
  355. *
  356. * Usage:
  357. *
  358. * `App::core('Cache/Engine'); will return the full path to the cache engines package`
  359. *
  360. * @param string $type Package type.
  361. * @return array full path to package
  362. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::core
  363. */
  364. public static function core($type) {
  365. return array(CAKE . str_replace('/', DS, $type) . DS);
  366. }
  367. /**
  368. * Returns an array of objects of the given type.
  369. *
  370. * Example usage:
  371. *
  372. * `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
  373. *
  374. * `App::objects('Controller');` returns `array('PagesController', 'BlogController');`
  375. *
  376. * You can also search only within a plugin's objects by using the plugin dot
  377. * syntax.
  378. *
  379. * `App::objects('MyPlugin.Model');` returns `array('MyPluginPost', 'MyPluginComment');`
  380. *
  381. * When scanning directories, files and directories beginning with `.` will be excluded as these
  382. * are commonly used by version control systems.
  383. *
  384. * @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'plugin'
  385. * @param string|array $path Optional Scan only the path given. If null, paths for the chosen type will be used.
  386. * @param bool $cache Set to false to rescan objects of the chosen type. Defaults to true.
  387. * @return mixed Either false on incorrect / miss. Or an array of found objects.
  388. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::objects
  389. */
  390. public static function objects($type, $path = null, $cache = true) {
  391. if (empty(static::$_objects) && $cache === true) {
  392. static::$_objects = (array)Cache::read('object_map', '_cake_core_');
  393. }
  394. $extension = '/\.php$/';
  395. $includeDirectories = false;
  396. $name = $type;
  397. if ($type === 'plugin') {
  398. $type = 'plugins';
  399. }
  400. if ($type === 'plugins') {
  401. $extension = '/.*/';
  402. $includeDirectories = true;
  403. }
  404. list($plugin, $type) = pluginSplit($type);
  405. if (isset(static::$legacy[$type . 's'])) {
  406. $type = static::$legacy[$type . 's'];
  407. }
  408. if ($type === 'file' && !$path) {
  409. return false;
  410. } elseif ($type === 'file') {
  411. $extension = '/\.php$/';
  412. $name = $type . str_replace(DS, '', $path);
  413. }
  414. $cacheLocation = empty($plugin) ? 'app' : $plugin;
  415. if ($cache !== true || !isset(static::$_objects[$cacheLocation][$name])) {
  416. $objects = array();
  417. if (empty($path)) {
  418. $path = static::path($type, $plugin);
  419. }
  420. foreach ((array)$path as $dir) {
  421. if ($dir != APP && is_dir($dir)) {
  422. $files = new RegexIterator(new DirectoryIterator($dir), $extension);
  423. foreach ($files as $file) {
  424. $fileName = basename($file);
  425. if (!$file->isDot() && $fileName[0] !== '.') {
  426. $isDir = $file->isDir();
  427. if ($isDir && $includeDirectories) {
  428. $objects[] = $fileName;
  429. } elseif (!$includeDirectories && !$isDir) {
  430. $objects[] = substr($fileName, 0, -4);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. if ($type !== 'file') {
  437. foreach ($objects as $key => $value) {
  438. $objects[$key] = Inflector::camelize($value);
  439. }
  440. }
  441. sort($objects);
  442. if ($plugin) {
  443. return $objects;
  444. }
  445. static::$_objects[$cacheLocation][$name] = $objects;
  446. if ($cache) {
  447. static::$_objectCacheChange = true;
  448. }
  449. }
  450. return static::$_objects[$cacheLocation][$name];
  451. }
  452. /**
  453. * Declares a package for a class. This package location will be used
  454. * by the automatic class loader if the class is tried to be used
  455. *
  456. * Usage:
  457. *
  458. * `App::uses('MyCustomController', 'Controller');` will setup the class to be found under Controller package
  459. *
  460. * `App::uses('MyHelper', 'MyPlugin.View/Helper');` will setup the helper class to be found in plugin's helper package
  461. *
  462. * @param string $className the name of the class to configure package for
  463. * @param string $location the package name
  464. * @return void
  465. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::uses
  466. */
  467. public static function uses($className, $location) {
  468. static::$_classMap[$className] = $location;
  469. }
  470. /**
  471. * Method to handle the automatic class loading. It will look for each class' package
  472. * defined using App::uses() and with this information it will resolve the package name to a full path
  473. * to load the class from. File name for each class should follow the class name. For instance,
  474. * if a class is name `MyCustomClass` the file name should be `MyCustomClass.php`
  475. *
  476. * @param string $className the name of the class to load
  477. * @return bool
  478. */
  479. public static function load($className) {
  480. if (!isset(static::$_classMap[$className])) {
  481. return false;
  482. }
  483. if (strpos($className, '..') !== false) {
  484. return false;
  485. }
  486. $parts = explode('.', static::$_classMap[$className], 2);
  487. list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
  488. $file = static::_mapped($className, $plugin);
  489. if ($file) {
  490. return include $file;
  491. }
  492. $paths = static::path($package, $plugin);
  493. if (empty($plugin)) {
  494. $appLibs = empty(static::$_packages['Lib']) ? APPLIBS : current(static::$_packages['Lib']);
  495. $paths[] = $appLibs . $package . DS;
  496. $paths[] = APP . $package . DS;
  497. $paths[] = CAKE . $package . DS;
  498. } else {
  499. $pluginPath = CakePlugin::path($plugin);
  500. $paths[] = $pluginPath . 'Lib' . DS . $package . DS;
  501. $paths[] = $pluginPath . $package . DS;
  502. }
  503. $normalizedClassName = str_replace('\\', DS, $className);
  504. foreach ($paths as $path) {
  505. $file = $path . $normalizedClassName . '.php';
  506. if (file_exists($file)) {
  507. static::_map($file, $className, $plugin);
  508. return include $file;
  509. }
  510. }
  511. return false;
  512. }
  513. /**
  514. * Returns the package name where a class was defined to be located at
  515. *
  516. * @param string $className name of the class to obtain the package name from
  517. * @return string|null Package name, or null if not declared
  518. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::location
  519. */
  520. public static function location($className) {
  521. if (!empty(static::$_classMap[$className])) {
  522. return static::$_classMap[$className];
  523. }
  524. return null;
  525. }
  526. /**
  527. * Finds classes based on $name or specific file(s) to search. Calling App::import() will
  528. * not construct any classes contained in the files. It will only find and require() the file.
  529. *
  530. * @param string|array $type The type of Class if passed as a string, or all params can be passed as
  531. * a single array to $type.
  532. * @param string $name Name of the Class or a unique name for the file
  533. * @param bool|array $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 bool $return Return the loaded file, the file must have a return
  540. * statement in it to work: return $variable;
  541. * @return bool true if Class is already in memory or if file is found and loaded, false if not
  542. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
  543. */
  544. public static function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
  545. $ext = null;
  546. if (is_array($type)) {
  547. extract($type, EXTR_OVERWRITE);
  548. }
  549. if (is_array($parent)) {
  550. extract($parent, EXTR_OVERWRITE);
  551. }
  552. if (!$name && !$file) {
  553. return false;
  554. }
  555. if (is_array($name)) {
  556. foreach ($name as $class) {
  557. if (!App::import(compact('type', 'parent', 'search', 'file', 'return') + array('name' => $class))) {
  558. return false;
  559. }
  560. }
  561. return true;
  562. }
  563. $originalType = strtolower($type);
  564. $specialPackage = in_array($originalType, array('file', 'vendor'));
  565. if (!$specialPackage && isset(static::$legacy[$originalType . 's'])) {
  566. $type = static::$legacy[$originalType . 's'];
  567. }
  568. list($plugin, $name) = pluginSplit($name);
  569. if (!empty($plugin)) {
  570. if (!CakePlugin::loaded($plugin)) {
  571. return false;
  572. }
  573. }
  574. if (!$specialPackage) {
  575. return static::_loadClass($name, $plugin, $type, $originalType, $parent);
  576. }
  577. if ($originalType === 'file' && !empty($file)) {
  578. return static::_loadFile($name, $plugin, $search, $file, $return);
  579. }
  580. if ($originalType === 'vendor') {
  581. return static::_loadVendor($name, $plugin, $file, $ext);
  582. }
  583. return false;
  584. }
  585. /**
  586. * Helper function to include classes
  587. * This is a compatibility wrapper around using App::uses() and automatic class loading
  588. *
  589. * @param string $name unique name of the file for identifying it inside the application
  590. * @param string $plugin camel cased plugin name if any
  591. * @param string $type name of the packed where the class is located
  592. * @param string $originalType type name as supplied initially by the user
  593. * @param bool $parent whether to load the class parent or not
  594. * @return bool true indicating the successful load and existence of the class
  595. */
  596. protected static function _loadClass($name, $plugin, $type, $originalType, $parent) {
  597. if ($type === 'Console/Command' && $name === 'Shell') {
  598. $type = 'Console';
  599. } elseif (isset(static::$types[$originalType]['suffix'])) {
  600. $suffix = static::$types[$originalType]['suffix'];
  601. $name .= ($suffix === $name) ? '' : $suffix;
  602. }
  603. if ($parent && isset(static::$types[$originalType]['extends'])) {
  604. $extends = static::$types[$originalType]['extends'];
  605. $extendType = $type;
  606. if (strpos($extends, '/') !== false) {
  607. $parts = explode('/', $extends);
  608. $extends = array_pop($parts);
  609. $extendType = implode('/', $parts);
  610. }
  611. App::uses($extends, $extendType);
  612. if ($plugin && in_array($originalType, array('controller', 'model'))) {
  613. App::uses($plugin . $extends, $plugin . '.' . $type);
  614. }
  615. }
  616. if ($plugin) {
  617. $plugin .= '.';
  618. }
  619. $name = Inflector::camelize($name);
  620. App::uses($name, $plugin . $type);
  621. return class_exists($name);
  622. }
  623. /**
  624. * Helper function to include single files
  625. *
  626. * @param string $name unique name of the file for identifying it inside the application
  627. * @param string $plugin camel cased plugin name if any
  628. * @param array $search list of paths to search the file into
  629. * @param string $file filename if known, the $name param will be used otherwise
  630. * @param bool $return whether this function should return the contents of the file after being parsed by php or just a success notice
  631. * @return mixed if $return contents of the file after php parses it, boolean indicating success otherwise
  632. */
  633. protected static function _loadFile($name, $plugin, $search, $file, $return) {
  634. $mapped = static::_mapped($name, $plugin);
  635. if ($mapped) {
  636. $file = $mapped;
  637. } elseif (!empty($search)) {
  638. foreach ($search as $path) {
  639. $found = false;
  640. if (file_exists($path . $file)) {
  641. $file = $path . $file;
  642. $found = true;
  643. break;
  644. }
  645. if (empty($found)) {
  646. $file = false;
  647. }
  648. }
  649. }
  650. if (!empty($file) && file_exists($file)) {
  651. static::_map($file, $name, $plugin);
  652. $returnValue = include $file;
  653. if ($return) {
  654. return $returnValue;
  655. }
  656. return (bool)$returnValue;
  657. }
  658. return false;
  659. }
  660. /**
  661. * Helper function to load files from vendors folders
  662. *
  663. * @param string $name unique name of the file for identifying it inside the application
  664. * @param string $plugin camel cased plugin name if any
  665. * @param string $file file name if known
  666. * @param string $ext file extension if known
  667. * @return bool true if the file was loaded successfully, false otherwise
  668. */
  669. protected static function _loadVendor($name, $plugin, $file, $ext) {
  670. if ($mapped = static::_mapped($name, $plugin)) {
  671. return (bool)include_once $mapped;
  672. }
  673. $fileTries = array();
  674. $paths = ($plugin) ? App::path('vendors', $plugin) : App::path('vendors');
  675. if (empty($ext)) {
  676. $ext = 'php';
  677. }
  678. if (empty($file)) {
  679. $fileTries[] = $name . '.' . $ext;
  680. $fileTries[] = Inflector::underscore($name) . '.' . $ext;
  681. } else {
  682. $fileTries[] = $file;
  683. }
  684. foreach ($fileTries as $file) {
  685. foreach ($paths as $path) {
  686. if (file_exists($path . $file)) {
  687. static::_map($path . $file, $name, $plugin);
  688. return (bool)include $path . $file;
  689. }
  690. }
  691. }
  692. return false;
  693. }
  694. /**
  695. * Initializes the cache for App, registers a shutdown function.
  696. *
  697. * @return void
  698. */
  699. public static function init() {
  700. static::$_map += (array)Cache::read('file_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. $key = $name;
  713. if ($plugin) {
  714. $key = 'plugin.' . $name;
  715. }
  716. if ($plugin && empty(static::$_map[$name])) {
  717. static::$_map[$key] = $file;
  718. }
  719. if (!$plugin && empty(static::$_map['plugin.' . $name])) {
  720. static::$_map[$key] = $file;
  721. }
  722. if (!static::$bootstrapping) {
  723. static::$_cacheChange = true;
  724. }
  725. }
  726. /**
  727. * Returns a file's complete path.
  728. *
  729. * @param string $name unique name
  730. * @param string $plugin camelized if object is from a plugin, the name of the plugin
  731. * @return mixed file path if found, false otherwise
  732. */
  733. protected static function _mapped($name, $plugin = null) {
  734. $key = $name;
  735. if ($plugin) {
  736. $key = 'plugin.' . $name;
  737. }
  738. return isset(static::$_map[$key]) ? static::$_map[$key] : 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(static::$_packageFormat)) {
  747. static::$_packageFormat = array(
  748. 'Model' => array(
  749. '%s' . 'Model' . DS
  750. ),
  751. 'Model/Behavior' => array(
  752. '%s' . 'Model' . DS . 'Behavior' . DS
  753. ),
  754. 'Model/Datasource' => array(
  755. '%s' . 'Model' . DS . 'Datasource' . DS
  756. ),
  757. 'Model/Datasource/Database' => array(
  758. '%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
  759. ),
  760. 'Model/Datasource/Session' => array(
  761. '%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
  762. ),
  763. 'Controller' => array(
  764. '%s' . 'Controller' . DS
  765. ),
  766. 'Controller/Component' => array(
  767. '%s' . 'Controller' . DS . 'Component' . DS
  768. ),
  769. 'Controller/Component/Auth' => array(
  770. '%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS
  771. ),
  772. 'Controller/Component/Acl' => array(
  773. '%s' . 'Controller' . DS . 'Component' . DS . 'Acl' . DS
  774. ),
  775. 'View' => array(
  776. '%s' . 'View' . DS
  777. ),
  778. 'View/Helper' => array(
  779. '%s' . 'View' . DS . 'Helper' . DS
  780. ),
  781. 'Console' => array(
  782. '%s' . 'Console' . DS
  783. ),
  784. 'Console/Command' => array(
  785. '%s' . 'Console' . DS . 'Command' . DS
  786. ),
  787. 'Console/Command/Task' => array(
  788. '%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS
  789. ),
  790. 'Lib' => array(
  791. '%s' . 'Lib' . DS
  792. ),
  793. 'Locale' => array(
  794. '%s' . 'Locale' . DS
  795. ),
  796. 'Vendor' => array(
  797. '%s' . 'Vendor' . DS,
  798. ROOT . DS . 'vendors' . DS,
  799. dirname(dirname(CAKE)) . DS . 'vendors' . DS
  800. ),
  801. 'Plugin' => array(
  802. APP . 'Plugin' . DS,
  803. ROOT . DS . 'plugins' . DS,
  804. dirname(dirname(CAKE)) . DS . 'plugins' . DS
  805. )
  806. );
  807. }
  808. return static::$_packageFormat;
  809. }
  810. /**
  811. * Object destructor.
  812. *
  813. * Writes cache file if changes have been made to the $_map. Also, check if a fatal
  814. * error happened and call the handler.
  815. *
  816. * @return void
  817. */
  818. public static function shutdown() {
  819. if (static::$_cacheChange) {
  820. Cache::write('file_map', array_filter(static::$_map), '_cake_core_');
  821. }
  822. if (static::$_objectCacheChange) {
  823. Cache::write('object_map', static::$_objects, '_cake_core_');
  824. }
  825. static::_checkFatalError();
  826. }
  827. /**
  828. * Check if a fatal error happened and trigger the configured handler if configured
  829. *
  830. * @return void
  831. */
  832. protected static function _checkFatalError() {
  833. $lastError = error_get_last();
  834. if (!is_array($lastError)) {
  835. return;
  836. }
  837. list(, $log) = ErrorHandler::mapErrorCode($lastError['type']);
  838. if ($log !== LOG_ERR) {
  839. return;
  840. }
  841. if (PHP_SAPI === 'cli') {
  842. $errorHandler = Configure::read('Error.consoleHandler');
  843. } else {
  844. $errorHandler = Configure::read('Error.handler');
  845. }
  846. if (!is_callable($errorHandler)) {
  847. return;
  848. }
  849. call_user_func($errorHandler, $lastError['type'], $lastError['message'], $lastError['file'], $lastError['line'], array());
  850. }
  851. }