Plugins.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Grav\Common;
  3. use Grav\Common\Config\Config;
  4. use Grav\Common\Data\Blueprints;
  5. use Grav\Common\Data\Data;
  6. use Grav\Common\GravTrait;
  7. use Grav\Common\File\CompiledYamlFile;
  8. use RocketTheme\Toolbox\Event\EventDispatcher;
  9. use RocketTheme\Toolbox\Event\EventSubscriberInterface;
  10. /**
  11. * The Plugins object holds an array of all the plugin objects that
  12. * Grav knows about
  13. *
  14. * @author RocketTheme
  15. * @license MIT
  16. */
  17. class Plugins extends Iterator
  18. {
  19. use GravTrait;
  20. /**
  21. * Recurses through the plugins directory creating Plugin objects for each plugin it finds.
  22. *
  23. * @return array|Plugin[] array of Plugin objects
  24. * @throws \RuntimeException
  25. */
  26. public function init()
  27. {
  28. /** @var Config $config */
  29. $config = self::getGrav()['config'];
  30. $plugins = (array) $config->get('plugins');
  31. $inflector = self::getGrav()['inflector'];
  32. /** @var EventDispatcher $events */
  33. $events = self::getGrav()['events'];
  34. foreach ($plugins as $plugin => $data) {
  35. if (empty($data['enabled'])) {
  36. // Only load enabled plugins.
  37. continue;
  38. }
  39. $locator = self::getGrav()['locator'];
  40. $filePath = $locator->findResource('plugins://' . $plugin . DS . $plugin . PLUGIN_EXT);
  41. if (!is_file($filePath)) {
  42. self::getGrav()['log']->addWarning(sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $plugin));
  43. continue;
  44. }
  45. require_once $filePath;
  46. $pluginClassFormat = [
  47. 'Grav\\Plugin\\'.ucfirst($plugin).'Plugin',
  48. 'Grav\\Plugin\\'.$inflector->camelize($plugin).'Plugin'
  49. ];
  50. $pluginClassName = false;
  51. foreach ($pluginClassFormat as $pluginClass) {
  52. if (class_exists($pluginClass)) {
  53. $pluginClassName = $pluginClass;
  54. break;
  55. }
  56. }
  57. if (false === $pluginClassName) {
  58. throw new \RuntimeException(sprintf("Plugin '%s' class not found! Try reinstalling this plugin.", $plugin));
  59. }
  60. $instance = new $pluginClassName($plugin, self::getGrav(), $config);
  61. if ($instance instanceof EventSubscriberInterface) {
  62. $events->addSubscriber($instance);
  63. }
  64. }
  65. return $this->items;
  66. }
  67. public function add($plugin)
  68. {
  69. if (is_object($plugin)) {
  70. $this->items[get_class($plugin)] = $plugin;
  71. }
  72. }
  73. /**
  74. * Return list of all plugin data with their blueprints.
  75. *
  76. * @return array
  77. */
  78. public static function all()
  79. {
  80. $list = array();
  81. $locator = Grav::instance()['locator'];
  82. $plugins = (array) $locator->findResources('plugins://', false);
  83. foreach ($plugins as $path) {
  84. $iterator = new \DirectoryIterator($path);
  85. /** @var \DirectoryIterator $directory */
  86. foreach ($iterator as $directory) {
  87. if (!$directory->isDir() || $directory->isDot()) {
  88. continue;
  89. }
  90. $type = $directory->getBasename();
  91. $list[$type] = self::get($type);
  92. }
  93. }
  94. ksort($list);
  95. return $list;
  96. }
  97. public static function get($name)
  98. {
  99. $blueprints = new Blueprints('plugins://');
  100. $blueprint = $blueprints->get("{$name}/blueprints");
  101. $blueprint->name = $name;
  102. // Load default configuration.
  103. $file = CompiledYamlFile::instance("plugins://{$name}/{$name}.yaml");
  104. $obj = new Data($file->content(), $blueprint);
  105. // Override with user configuration.
  106. $obj->merge(self::getGrav()['config']->get('plugins.' . $name) ?: []);
  107. // Save configuration always to user/config.
  108. $file = CompiledYamlFile::instance("config://plugins/{$name}.yaml");
  109. $obj->file($file);
  110. return $obj;
  111. }
  112. }