FlysystemFactory.php in Flysystem 3.x
File
src/FlysystemFactory.php
View source
<?php
namespace Drupal\flysystem;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\flysystem\Event\EnsureEvent;
use Drupal\flysystem\Event\FlysystemEvents;
use Drupal\flysystem\Flysystem\Adapter\CacheItemBackend;
use Drupal\flysystem\Flysystem\Adapter\DrupalCacheAdapter;
use League\Flysystem\Filesystem;
use League\Flysystem\Replicate\ReplicateAdapter;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class FlysystemFactory {
use SerializationStopperTrait;
protected $defaults = [
'driver' => '',
'config' => [],
'replicate' => FALSE,
'cache' => FALSE,
'name' => '',
'description' => '',
];
protected $cacheBackend;
protected $eventDispatcher;
protected $filesystems = [];
protected $pluginManager;
protected $plugins = [];
protected $settings = [];
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;
foreach (Settings::get('flysystem', []) as $scheme => $configuration) {
if (!$stream_wrapper_manager
->isValidScheme($scheme)) {
continue;
}
$this->settings[$scheme] = $configuration + $this->defaults;
}
}
public function getFilesystem($scheme) {
if (!isset($this->filesystems[$scheme])) {
$this->filesystems[$scheme] = new Filesystem($this
->getAdapter($scheme));
}
return $this->filesystems[$scheme];
}
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];
}
public function getSchemes() {
return array_keys($this->settings);
}
public function getSettings($scheme) {
return isset($this->settings[$scheme]) ? $this->settings[$scheme] : $this->defaults;
}
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;
}
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;
}
}