FileSchemeHandlerManager.php in Acquia Content Hub 8.2
File
src/Plugin/FileSchemeHandler/FileSchemeHandlerManager.php
View source
<?php
namespace Drupal\acquia_contenthub\Plugin\FileSchemeHandler;
use Drupal\acquia_contenthub\Annotation\FileSchemeHandler;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\file\FileInterface;
class FileSchemeHandlerManager extends DefaultPluginManager implements FileSchemeHandlerManagerInterface {
const EMPTY_SCHEME = 'empty';
protected $fileSystem;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/FileSchemeHandler', $namespaces, $module_handler, FileSchemeHandlerInterface::class, FileSchemeHandler::class);
$this
->alterInfo('file_scheme_handler');
$this
->setCacheBackend($cache_backend, 'file_scheme_handler_plugins');
}
public function getHandlerForFile(FileInterface $file) {
$scheme = $this
->getFileScheme($file);
return $this
->createInstance($scheme);
}
public function isSupportedFileScheme(FileInterface $file) {
$scheme = $this
->getFileScheme($file);
return in_array($scheme, array_keys($this
->getDefinitions()));
}
protected function getFileScheme(FileInterface $file) {
$uri = $file
->getFileUri();
$scheme = version_compare(\Drupal::VERSION, '8.8.0', '>=') ? StreamWrapperManager::getScheme($uri) : \Drupal::service('file_system')
->uriScheme($uri);
if (!$scheme) {
return self::EMPTY_SCHEME;
}
return $scheme;
}
}