Plugin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Core;
  17. /**
  18. * Plugin is used to load and locate plugins.
  19. *
  20. * It also can retrieve plugin paths and load their bootstrap and routes files.
  21. *
  22. * @link https://book.cakephp.org/5/en/plugins.html
  23. */
  24. class Plugin
  25. {
  26. /**
  27. * Holds a list of all loaded plugins and their configuration
  28. *
  29. * @var \Cake\Core\PluginCollection|null
  30. */
  31. protected static ?PluginCollection $plugins = null;
  32. /**
  33. * Returns the filesystem path for a plugin
  34. *
  35. * @param string $name name of the plugin in CamelCase format
  36. * @return string path to the plugin folder
  37. * @throws \Cake\Core\Exception\MissingPluginException If the folder for plugin was not found
  38. * or plugin has not been loaded.
  39. */
  40. public static function path(string $name): string
  41. {
  42. $plugin = static::getCollection()->get($name);
  43. return $plugin->getPath();
  44. }
  45. /**
  46. * Returns the filesystem path for plugin's folder containing class files.
  47. *
  48. * @param string $name name of the plugin in CamelCase format.
  49. * @return string Path to the plugin folder containing class files.
  50. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  51. */
  52. public static function classPath(string $name): string
  53. {
  54. $plugin = static::getCollection()->get($name);
  55. return $plugin->getClassPath();
  56. }
  57. /**
  58. * Returns the filesystem path for plugin's folder containing config files.
  59. *
  60. * @param string $name name of the plugin in CamelCase format.
  61. * @return string Path to the plugin folder containing config files.
  62. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  63. */
  64. public static function configPath(string $name): string
  65. {
  66. $plugin = static::getCollection()->get($name);
  67. return $plugin->getConfigPath();
  68. }
  69. /**
  70. * Returns the filesystem path for plugin's folder containing template files.
  71. *
  72. * @param string $name name of the plugin in CamelCase format.
  73. * @return string Path to the plugin folder containing template files.
  74. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  75. */
  76. public static function templatePath(string $name): string
  77. {
  78. $plugin = static::getCollection()->get($name);
  79. return $plugin->getTemplatePath();
  80. }
  81. /**
  82. * Returns true if the plugin $plugin is already loaded.
  83. *
  84. * @param string $plugin Plugin name.
  85. * @return bool
  86. * @since 3.7.0
  87. */
  88. public static function isLoaded(string $plugin): bool
  89. {
  90. return static::getCollection()->has($plugin);
  91. }
  92. /**
  93. * Return a list of loaded plugins.
  94. *
  95. * @return list<string> A list of plugins that have been loaded
  96. */
  97. public static function loaded(): array
  98. {
  99. $names = [];
  100. foreach (static::getCollection() as $plugin) {
  101. $names[] = $plugin->getName();
  102. }
  103. sort($names);
  104. return $names;
  105. }
  106. /**
  107. * Get the shared plugin collection.
  108. *
  109. * This method should generally not be used during application
  110. * runtime as plugins should be set during Application startup.
  111. *
  112. * @return \Cake\Core\PluginCollection
  113. */
  114. public static function getCollection(): PluginCollection
  115. {
  116. return static::$plugins ??= new PluginCollection();
  117. }
  118. /**
  119. * Set the shared plugin collection.
  120. *
  121. * @param \Cake\Core\PluginCollection $collection
  122. * @return void
  123. */
  124. public static function setCollection(PluginCollection $collection): void
  125. {
  126. static::$plugins = $collection;
  127. }
  128. }