vendor/contao/manager-plugin/src/PluginLoader.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of Contao.
  5. *
  6. * (c) Leo Feyer
  7. *
  8. * @license LGPL-3.0-or-later
  9. */
  10. namespace Contao\ManagerPlugin;
  11. use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
  12. use Contao\ManagerPlugin\Config\ConfigPluginInterface;
  13. use Contao\ManagerPlugin\Config\ExtensionPluginInterface;
  14. use Contao\ManagerPlugin\Routing\RoutingPluginInterface;
  15. class PluginLoader
  16. {
  17. public const BUNDLE_PLUGINS = BundlePluginInterface::class;
  18. public const CONFIG_PLUGINS = ConfigPluginInterface::class;
  19. public const EXTENSION_PLUGINS = ExtensionPluginInterface::class;
  20. public const ROUTING_PLUGINS = RoutingPluginInterface::class;
  21. /**
  22. * @var array
  23. */
  24. private $plugins = [];
  25. /**
  26. * @var array
  27. */
  28. private $disabled = [];
  29. public function __construct(?string $installedJson = null, ?array $plugins = null)
  30. {
  31. if (null !== $installedJson) {
  32. @trigger_error('Passing the path to the Composer installed.json as first argument is no longer supported in version 2.3.', E_USER_DEPRECATED);
  33. }
  34. if (null !== $plugins) {
  35. $this->plugins = $plugins;
  36. } elseif (is_file(self::getGeneratedPath())) {
  37. $this->plugins = (array) include self::getGeneratedPath();
  38. }
  39. }
  40. public static function getGeneratedPath(): string
  41. {
  42. return __DIR__.'/../.generated/plugins.php';
  43. }
  44. /**
  45. * Returns all active plugin instances.
  46. *
  47. * @return array<string,BundlePluginInterface>
  48. */
  49. public function getInstances(): array
  50. {
  51. return array_diff_key($this->plugins, array_flip($this->disabled));
  52. }
  53. /**
  54. * Returns the active plugin instances of a given type (see class constants).
  55. *
  56. * @return array<string,BundlePluginInterface>
  57. */
  58. public function getInstancesOf(string $type, bool $reverseOrder = false): array
  59. {
  60. $plugins = array_filter(
  61. $this->getInstances(),
  62. static function ($plugin) use ($type) {
  63. return is_a($plugin, $type);
  64. }
  65. );
  66. if ($reverseOrder) {
  67. $plugins = array_reverse($plugins, true);
  68. }
  69. return array_diff_key($plugins, array_flip($this->disabled));
  70. }
  71. /**
  72. * @return array<string>
  73. */
  74. public function getDisabledPackages(): array
  75. {
  76. return $this->disabled;
  77. }
  78. public function setDisabledPackages(array $packages): void
  79. {
  80. $this->disabled = $packages;
  81. }
  82. }