App.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core;
  16. /**
  17. * App is responsible for resource location, and path management.
  18. *
  19. * ### Adding paths
  20. *
  21. * Additional paths for Templates and Plugins are configured with Configure now. See config/app.php for an
  22. * example. The `App.paths.plugins` and `App.paths.templates` variables are used to configure paths for plugins
  23. * and templates respectively. All class based resources should be mapped using your application's autoloader.
  24. *
  25. * ### Inspecting loaded paths
  26. *
  27. * You can inspect the currently loaded paths using `App::path('Controller')` for example to see loaded
  28. * controller paths.
  29. *
  30. * It is also possible to inspect paths for plugin classes, for instance, to get
  31. * the path to a plugin's helpers you would call `App::path('View/Helper', 'MyPlugin')`
  32. *
  33. * ### Locating plugins
  34. *
  35. * Plugins can be located with App as well. Using Plugin::path('DebugKit') for example, will
  36. * give you the full path to the DebugKit plugin.
  37. *
  38. * @link https://book.cakephp.org/3.0/en/core-libraries/app.html
  39. */
  40. class App
  41. {
  42. /**
  43. * Return the class name namespaced. This method checks if the class is defined on the
  44. * application/plugin, otherwise try to load from the CakePHP core
  45. *
  46. * @param string $class Class name
  47. * @param string $type Type of class
  48. * @param string $suffix Class name suffix
  49. * @return false|string False if the class is not found or namespaced class name
  50. */
  51. public static function className($class, $type = '', $suffix = '')
  52. {
  53. if (strpos($class, '\\') !== false) {
  54. return $class;
  55. }
  56. list($plugin, $name) = pluginSplit($class);
  57. $base = $plugin ?: Configure::read('App.namespace');
  58. $base = str_replace('/', '\\', rtrim($base, '\\'));
  59. $fullname = '\\' . str_replace('/', '\\', $type . '\\' . $name) . $suffix;
  60. if (static::_classExistsInBase($fullname, $base)) {
  61. return $base . $fullname;
  62. }
  63. if ($plugin) {
  64. return false;
  65. }
  66. if (static::_classExistsInBase($fullname, 'Cake')) {
  67. return 'Cake' . $fullname;
  68. }
  69. return false;
  70. }
  71. /**
  72. * Returns the plugin split name of a class
  73. *
  74. * Examples:
  75. *
  76. * ```
  77. * App::shortName(
  78. * 'SomeVendor\SomePlugin\Controller\Component\TestComponent',
  79. * 'Controller/Component',
  80. * 'Component'
  81. * )
  82. * ```
  83. *
  84. * Returns: SomeVendor/SomePlugin.Test
  85. *
  86. * ```
  87. * App::shortName(
  88. * 'SomeVendor\SomePlugin\Controller\Component\Subfolder\TestComponent',
  89. * 'Controller/Component',
  90. * 'Component'
  91. * )
  92. * ```
  93. *
  94. * Returns: SomeVendor/SomePlugin.Subfolder/Test
  95. *
  96. * ```
  97. * App::shortName(
  98. * 'Cake\Controller\Component\AuthComponent',
  99. * 'Controller/Component',
  100. * 'Component'
  101. * )
  102. * ```
  103. *
  104. * Returns: Auth
  105. *
  106. * @param string $class Class name
  107. * @param string $type Type of class
  108. * @param string $suffix Class name suffix
  109. * @return string Plugin split name of class
  110. */
  111. public static function shortName($class, $type, $suffix = '')
  112. {
  113. $class = str_replace('\\', '/', $class);
  114. $type = '/' . $type . '/';
  115. $pos = strrpos($class, $type);
  116. $pluginName = substr($class, 0, $pos);
  117. $name = substr($class, $pos + strlen($type));
  118. if ($suffix) {
  119. $name = substr($name, 0, -strlen($suffix));
  120. }
  121. $nonPluginNamespaces = [
  122. 'Cake',
  123. str_replace('\\', '/', Configure::read('App.namespace'))
  124. ];
  125. if (in_array($pluginName, $nonPluginNamespaces)) {
  126. return $name;
  127. }
  128. return $pluginName . '.' . $name;
  129. }
  130. /**
  131. * _classExistsInBase
  132. *
  133. * Test isolation wrapper
  134. *
  135. * @param string $name Class name.
  136. * @param string $namespace Namespace.
  137. * @return bool
  138. */
  139. protected static function _classExistsInBase($name, $namespace)
  140. {
  141. return class_exists($namespace . $name);
  142. }
  143. /**
  144. * Used to read information stored path
  145. *
  146. * Usage:
  147. *
  148. * ```
  149. * App::path('Plugin');
  150. * ```
  151. *
  152. * Will return the configured paths for plugins. This is a simpler way to access
  153. * the `App.paths.plugins` configure variable.
  154. *
  155. * ```
  156. * App::path('Model/Datasource', 'MyPlugin');
  157. * ```
  158. *
  159. * Will return the path for datasources under the 'MyPlugin' plugin.
  160. *
  161. * @param string $type type of path
  162. * @param string|null $plugin name of plugin
  163. * @return array
  164. * @link https://book.cakephp.org/3.0/en/core-libraries/app.html#finding-paths-to-namespaces
  165. */
  166. public static function path($type, $plugin = null)
  167. {
  168. if ($type === 'Plugin') {
  169. return (array)Configure::read('App.paths.plugins');
  170. }
  171. if (empty($plugin) && $type === 'Locale') {
  172. return (array)Configure::read('App.paths.locales');
  173. }
  174. if (empty($plugin) && $type === 'Template') {
  175. return (array)Configure::read('App.paths.templates');
  176. }
  177. if (!empty($plugin)) {
  178. return [Plugin::classPath($plugin) . $type . DIRECTORY_SEPARATOR];
  179. }
  180. return [APP . $type . DIRECTORY_SEPARATOR];
  181. }
  182. /**
  183. * Returns the full path to a package inside the CakePHP core
  184. *
  185. * Usage:
  186. *
  187. * ```
  188. * App::core('Cache/Engine');
  189. * ```
  190. *
  191. * Will return the full path to the cache engines package.
  192. *
  193. * @param string $type Package type.
  194. * @return array Full path to package
  195. */
  196. public static function core($type)
  197. {
  198. return [CAKE . str_replace('/', DIRECTORY_SEPARATOR, $type) . DIRECTORY_SEPARATOR];
  199. }
  200. }