You are here

class FlysystemFactory in Flysystem 3.0.x

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

A factory for Flysystem filesystems.

Hierarchy

Expanded class hierarchy of FlysystemFactory

8 files declare their use of FlysystemFactory
ConfigForm.php in src/Form/ConfigForm.php
ConfigFormTest.php in tests/src/Unit/Form/ConfigFormTest.php
FlysystemBridgeTest.php in tests/src/Unit/FlysystemBridgeTest.php
FlysystemFactoryTest.php in tests/src/Unit/FlysystemFactoryTest.php
FlysystemRoutes.php in src/Routing/FlysystemRoutes.php

... See full list

1 string reference to 'FlysystemFactory'
flysystem.services.yml in ./flysystem.services.yml
flysystem.services.yml
1 service uses FlysystemFactory
flysystem_factory in ./flysystem.services.yml
Drupal\flysystem\FlysystemFactory

File

src/FlysystemFactory.php, line 21

Namespace

Drupal\flysystem
View source
class FlysystemFactory {
  use SerializationStopperTrait;

  /**
   * Default settings.
   *
   * @var array
   */
  protected $defaults = [
    'driver' => '',
    'config' => [],
    'replicate' => FALSE,
    'cache' => FALSE,
    'name' => '',
    'description' => '',
  ];

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackend;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * A cache of filesystems.
   *
   * @var \League\Flysystem\FilesystemInterface[]
   */
  protected $filesystems = [];

  /**
   * The Flysystem plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $pluginManager;

  /**
   * Created plugins.
   *
   * @var \Drupal\flysystem\Plugin\FlysystemPluginInterface[]
   */
  protected $plugins = [];

  /**
   * Settings for stream wrappers.
   *
   * @var array
   */
  protected $settings = [];

  /**
   * Constructs a FlysystemFactory object.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   *   The plugin manager.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
   *   The stream wrapper manager service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(PluginManagerInterface $plugin_manager, StreamWrapperManagerInterface $stream_wrapper_manager, CacheBackendInterface $cache, EventDispatcherInterface $event_dispatcher) {
    $this->pluginManager = $plugin_manager;
    $this->cacheBackend = $cache;
    $this->eventDispatcher = $event_dispatcher;

    // Apply defaults and validate registered services.
    foreach (Settings::get('flysystem', []) as $scheme => $configuration) {

      // The settings.php file could be changed before rebuilding the container.
      if (!$stream_wrapper_manager
        ->isValidScheme($scheme)) {
        continue;
      }
      $this->settings[$scheme] = $configuration + $this->defaults;
    }
  }

  /**
   * Returns the filesystem for a given scheme.
   *
   * @param string $scheme
   *   The scheme.
   *
   * @return \League\Flysystem\FilesystemInterface
   *   The filesystem for the scheme.
   */
  public function getFilesystem($scheme) {
    if (!isset($this->filesystems[$scheme])) {
      $this->filesystems[$scheme] = new Filesystem($this
        ->getAdapter($scheme));
    }
    return $this->filesystems[$scheme];
  }

  /**
   * Returns the plugin for a scheme.
   *
   * @param string $scheme
   *   The scheme.
   *
   * @return \Drupal\flysystem\Plugin\FlysystemPluginInterface
   *   The plugin.
   */
  public function getPlugin($scheme) {
    if (!isset($this->plugins[$scheme])) {
      $settings = $this
        ->getSettings($scheme);
      $this->plugins[$scheme] = $this->pluginManager
        ->createInstance($settings['driver'], $settings['config']);
    }
    return $this->plugins[$scheme];
  }

  /**
   * Returns a list of valid schemes.
   *
   * @return string[]
   *   The list of valid schemes.
   */
  public function getSchemes() {
    return array_keys($this->settings);
  }

  /**
   * Finds the settings for a given scheme.
   *
   * @param string $scheme
   *   The scheme.
   *
   * @return array
   *   The settings array from settings.php.
   */
  public function getSettings($scheme) {
    return isset($this->settings[$scheme]) ? $this->settings[$scheme] : $this->defaults;
  }

  /**
   * Calls FlysystemPluginInterface::ensure() on each plugin.
   *
   * @param bool $force
   *   (optional) Wheter to force the insurance. Defaults to false.
   *
   * @return array
   *   Errors keyed by scheme.
   */
  public function ensure($force = FALSE) {
    $errors = [];
    foreach ($this
      ->getSchemes() as $scheme) {
      foreach ($this
        ->getPlugin($scheme)
        ->ensure($force) as $error) {
        $event = new EnsureEvent($scheme, $error['severity'], $error['message'], $error['context']);
        $this->eventDispatcher
          ->dispatch(FlysystemEvents::ENSURE, $event);
        $errors[$scheme][] = $error;
      }
    }
    return $errors;
  }

  /**
   * Returns the adapter for a scheme.
   *
   * @param string $scheme
   *   The scheme to find an adapter for.
   *
   * @return \League\Flysystem\AdapterInterface
   *   The correct adapter from settings.
   */
  protected function getAdapter($scheme) {
    $settings = $this
      ->getSettings($scheme);
    $adapter = $this
      ->getPlugin($scheme)
      ->getAdapter();
    if ($settings['replicate']) {
      $replica = $this
        ->getAdapter($settings['replicate']);
      $adapter = new ReplicateAdapter($adapter, $replica);
    }
    if ($settings['cache']) {
      $cache_item_backend = new CacheItemBackend($scheme, $this->cacheBackend);
      $adapter = new DrupalCacheAdapter($scheme, $adapter, $cache_item_backend);
    }
    return $adapter;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlysystemFactory::$cacheBackend protected property The cache backend.
FlysystemFactory::$defaults protected property Default settings.
FlysystemFactory::$eventDispatcher protected property The event dispatcher.
FlysystemFactory::$filesystems protected property A cache of filesystems.
FlysystemFactory::$pluginManager protected property The Flysystem plugin manager.
FlysystemFactory::$plugins protected property Created plugins.
FlysystemFactory::$settings protected property Settings for stream wrappers.
FlysystemFactory::ensure public function Calls FlysystemPluginInterface::ensure() on each plugin.
FlysystemFactory::getAdapter protected function Returns the adapter for a scheme.
FlysystemFactory::getFilesystem public function Returns the filesystem for a given scheme.
FlysystemFactory::getPlugin public function Returns the plugin for a scheme.
FlysystemFactory::getSchemes public function Returns a list of valid schemes.
FlysystemFactory::getSettings public function Finds the settings for a given scheme.
FlysystemFactory::__construct public function Constructs a FlysystemFactory object.
SerializationStopperTrait::__sleep public function Prevents the class from being serialized.