You are here

class FlysystemServiceProvider in Flysystem 3.x

Same name and namespace in other branches
  1. 8 src/FlysystemServiceProvider.php \Drupal\flysystem\FlysystemServiceProvider
  2. 2.0.x src/FlysystemServiceProvider.php \Drupal\flysystem\FlysystemServiceProvider
  3. 3.0.x src/FlysystemServiceProvider.php \Drupal\flysystem\FlysystemServiceProvider

Flysystem dependency injection container.

Hierarchy

Expanded class hierarchy of FlysystemServiceProvider

1 file declares its use of FlysystemServiceProvider
FlysystemServiceProviderTest.php in tests/src/Unit/FlysystemServiceProviderTest.php

File

src/FlysystemServiceProvider.php, line 13

Namespace

Drupal\flysystem
View source
class FlysystemServiceProvider implements ServiceProviderInterface {
  use SchemeExtensionTrait;

  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) {
    $this
      ->swapDumper($container, 'js');
    $this
      ->swapDumper($container, 'css');
    foreach (Settings::get('flysystem', []) as $scheme => $settings) {

      // Just some sanity checking, so things don't explode.
      if (empty($settings['driver'])) {
        continue;
      }
      $container
        ->register('flysystem_stream_wrapper.' . $scheme, 'Drupal\\flysystem\\FlysystemBridge')
        ->addTag('stream_wrapper', [
        'scheme' => $scheme,
      ]);

      // Register the path processors for local files.
      if ($settings['driver'] === 'local' && !empty($settings['config']['public'])) {
        $container
          ->register('flysystem.' . $scheme . '.path_processor', 'Drupal\\flysystem\\PathProcessor\\LocalPathProcessor')
          ->addTag('path_processor_inbound', [
          'priority' => 400,
        ])
          ->addArgument($scheme);
      }
    }
  }

  /**
   * Swaps the js/css dumper if a scheme is configured to serve it.
   *
   * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
   *   The container.
   * @param string $extension
   *   The file extension to swap.
   */
  protected function swapDumper(ContainerBuilder $container, $extension) {
    if (!$container
      ->has('asset.' . $extension . '.dumper')) {
      return;
    }
    if (!$container
      ->has('asset.' . $extension . '.collection_optimizer')) {
      return;
    }

    // Don't rewrite if there's nothing to change.
    if ($this
      ->getSchemeForExtension($extension) === 'public') {
      return;
    }
    $container
      ->getDefinition('asset.' . $extension . '.dumper')
      ->setClass('Drupal\\flysystem\\Asset\\AssetDumper');
    $container
      ->getDefinition('asset.' . $extension . '.collection_optimizer')
      ->setClass('Drupal\\flysystem\\Asset\\' . ucfirst($extension) . 'CollectionOptimizer');
    if ($extension === 'css') {
      $container
        ->getDefinition('asset.css.optimizer')
        ->setClass('Drupal\\flysystem\\Asset\\CssOptimizer');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlysystemServiceProvider::register public function Registers services to the container. Overrides ServiceProviderInterface::register
FlysystemServiceProvider::swapDumper protected function Swaps the js/css dumper if a scheme is configured to serve it.
SchemeExtensionTrait::getSchemeForExtension public function Returns the scheme that should serve an extension.