App.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core;
  16. use Cake\Core\Plugin;
  17. /**
  18. * App is responsible for resource location, and path management.
  19. *
  20. * ### Adding paths
  21. *
  22. * Additional paths for Templates and Plugins are configured with Configure now. See config/app.php for an
  23. * example. The `App.paths.plugins` and `App.paths.templates` variables are used to configure paths for plugins
  24. * and templates respectively. All class based resources should be mapped using your application's autoloader.
  25. *
  26. * ### Inspecting loaded paths
  27. *
  28. * You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
  29. * controller paths.
  30. *
  31. * It is also possible to inspect paths for plugin classes, for instance, to get
  32. * the path to a plugin's helpers you would call `App::path('View/Helper', 'MyPlugin')`
  33. *
  34. * ### Locating plugins
  35. *
  36. * Plugins can be located with App as well. Using Plugin::path('DebugKit') for example, will
  37. * give you the full path to the DebugKit plugin.
  38. *
  39. * @link http://book.cakephp.org/3.0/en/core-libraries/app.html
  40. */
  41. class App
  42. {
  43. /**
  44. * Return the class name namespaced. This method checks if the class is defined on the
  45. * application/plugin, otherwise try to load from the CakePHP core
  46. *
  47. * @param string $class Class name
  48. * @param string $type Type of class
  49. * @param string $suffix Class name suffix
  50. * @return bool|string False if the class is not found or namespaced class name
  51. */
  52. public static function className($class, $type = '', $suffix = '')
  53. {
  54. if (strpos($class, '\\') !== false) {
  55. return $class;
  56. }
  57. list($plugin, $name) = pluginSplit($class);
  58. if ($plugin) {
  59. $base = $plugin;
  60. } else {
  61. $base = Configure::read('App.namespace');
  62. }
  63. $base = str_replace('/', '\\', rtrim($base, '\\'));
  64. $fullname = '\\' . str_replace('/', '\\', $type . '\\' . $name) . $suffix;
  65. if (static::_classExistsInBase($fullname, $base)) {
  66. return $base . $fullname;
  67. }
  68. if ($plugin) {
  69. return false;
  70. }
  71. if (static::_classExistsInBase($fullname, 'Cake')) {
  72. return 'Cake' . $fullname;
  73. }
  74. return false;
  75. }
  76. /**
  77. * _classExistsInBase
  78. *
  79. * Test isolation wrapper
  80. *
  81. * @param string $name Class name.
  82. * @param string $namespace Namespace.
  83. * @return bool
  84. */
  85. protected static function _classExistsInBase($name, $namespace)
  86. {
  87. return class_exists($namespace . $name);
  88. }
  89. /**
  90. * Used to read information stored path
  91. *
  92. * Usage:
  93. *
  94. * `App::path('Plugin');`
  95. *
  96. * Will return the configured paths for plugins. This is a simpler way to access
  97. * the `App.paths.plugins` configure variable.
  98. *
  99. * `App::path('Model/Datasource', 'MyPlugin');`
  100. *
  101. * Will return the path for datasources under the 'MyPlugin' plugin.
  102. *
  103. * @param string $type type of path
  104. * @param string $plugin name of plugin
  105. * @return array
  106. * @link http://book.cakephp.org/3.0/en/core-libraries/app.html#finding-paths-to-namespaces
  107. */
  108. public static function path($type, $plugin = null)
  109. {
  110. if ($type === 'Plugin') {
  111. return (array)Configure::read('App.paths.plugins');
  112. }
  113. if (empty($plugin) && $type === 'Locale') {
  114. return (array)Configure::read('App.paths.locales');
  115. }
  116. if (empty($plugin) && $type === 'Template') {
  117. return (array)Configure::read('App.paths.templates');
  118. }
  119. if (!empty($plugin)) {
  120. return [Plugin::classPath($plugin) . $type . DS];
  121. }
  122. return [APP . $type . DS];
  123. }
  124. /**
  125. * Returns the full path to a package inside the CakePHP core
  126. *
  127. * Usage:
  128. *
  129. * `App::core('Cache/Engine');`
  130. *
  131. * Will return the full path to the cache engines package.
  132. *
  133. * @param string $type Package type.
  134. * @return array Full path to package
  135. */
  136. public static function core($type)
  137. {
  138. return [CAKE . str_replace('/', DS, $type) . DS];
  139. }
  140. }