vendor/eightpoints/guzzle-bundle/src/EightPointsGuzzleBundle.php line 11

Open in your IDE?
  1. <?php
  2. namespace EightPoints\Bundle\GuzzleBundle;
  3. use EightPoints\Bundle\GuzzleBundle\DependencyInjection\EightPointsGuzzleExtension;
  4. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  5. use Symfony\Component\HttpKernel\Bundle\Bundle;
  6. use Symfony\Component\DependencyInjection\ContainerBuilder;
  7. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  8. class EightPointsGuzzleBundle extends Bundle
  9. {
  10.     /** @var \EightPoints\Bundle\GuzzleBundle\PluginInterface[] */
  11.     protected $plugins = [];
  12.     /**
  13.      * @param \EightPoints\Bundle\GuzzleBundle\PluginInterface[] $plugins
  14.      */
  15.     public function __construct(array $plugins = [])
  16.     {
  17.         foreach ($plugins as $plugin) {
  18.             $this->registerPlugin($plugin);
  19.         }
  20.     }
  21.     /**
  22.      * Build EightPointsGuzzleBundle
  23.      *
  24.      * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
  25.      *
  26.      * @return void
  27.      */
  28.     public function build(ContainerBuilder $container)
  29.     {
  30.         parent::build($container);
  31.         foreach ($this->plugins as $plugin) {
  32.             $plugin->build($container);
  33.         }
  34.     }
  35.     /**
  36.      * Overwrite getContainerExtension
  37.      *  - no naming convention of alias needed
  38.      *  - extension class can be moved easily now
  39.      *
  40.      * @return \Symfony\Component\DependencyInjection\Extension\ExtensionInterface The container extension
  41.      */
  42.     public function getContainerExtension() : ExtensionInterface
  43.     {
  44.         if ($this->extension === null) {
  45.             $this->extension = new EightPointsGuzzleExtension($this->plugins);
  46.         }
  47.         return $this->extension;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      *
  52.      * @return void
  53.      */
  54.     public function boot(): void
  55.     {
  56.         foreach ($this->plugins as $plugin) {
  57.             $plugin->boot();
  58.         }
  59.     }
  60.     /**
  61.      * @param \EightPoints\Bundle\GuzzleBundle\PluginInterface $plugin
  62.      *
  63.      * @throws \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  64.      *
  65.      * @return void
  66.      */
  67.     protected function registerPlugin(PluginInterface $plugin) : void
  68.     {
  69.         // Check plugins name duplication
  70.         foreach ($this->plugins as $registeredPlugin) {
  71.             if ($registeredPlugin->getPluginName() === $plugin->getPluginName()) {
  72.                 throw new InvalidConfigurationException(sprintf(
  73.                     'Trying to connect two plugins with same name: %s',
  74.                     $plugin->getPluginName()
  75.                 ));
  76.             }
  77.         }
  78.         $this->plugins[] = $plugin;
  79.     }
  80. }