
在 symfony 中,当您配置了 flysystem 存储服务后,框架会自动将这些服务注册到依赖注入容器中。默认情况下,这些服务是私有的,这意味着您不能直接通过 containerinterface 来获取它们,但可以通过类型提示和名称(例如 filesystemoperator $firststorage)在服务构造函数中直接注入。
例如,如果您有以下 Flysystem 配置:
# config/packages/flysystem.yaml
flysystem:
    storages:
        first.storage:
            adapter: 'local'
            options:
                directory: '%kernel.project_dir%/var/storage/first'
        second.storage:
            adapter: 'local'
            options:
                directory: '%kernel.project_dir%/var/storage/second'您可以这样直接注入特定的存储服务:
// src/Service/SomeService.php
namespace App\Service;
use League\Flysystem\FilesystemOperator;
class SomeService
{
    private FilesystemOperator $firstStorage;
    public function __construct(FilesystemOperator $firstStorage)
    {
        $this->firstStorage = $firstStorage;
    }
    public function doSomethingWithFirstStorage(): void
    {
        // 使用 $this->firstStorage
    }
}然而,当您需要根据运行时参数动态选择不同的存储实例时,例如在一个工厂类中,直接注入的方式就不再适用。因为 Flysystem 的服务默认是私有的,您无法通过 ContainerInterface::get() 方法直接获取它们。
要实现动态获取 Flysystem 存储实例,我们需要结合 Symfony 的服务别名(Alias)和 ContainerInterface。
由于 Flysystem 注册的服务默认是私有的,我们首先需要在 config/services.yaml 中为需要动态访问的 Flysystem 存储服务创建公共别名。这使得它们可以通过容器被检索。
# config/services.yaml
services:
    # ... 其他服务配置
    # 为 Flysystem 存储服务创建公共别名
    # 'first.storage' 和 'second.storage' 是 flysystem.yaml 中定义的存储名称
    first.storage.alias:
        alias: 'first.storage'
        public: true
    second.storage.alias:
        alias: 'second.storage'
        public: true在上述配置中,我们为 first.storage 和 second.storage 分别创建了 first.storage.alias 和 second.storage.alias 两个公共别名。通过将 public: true 设置为 true,这些别名服务就可以通过 ContainerInterface::get() 方法访问。
接下来,在您的工厂类中,注入 Symfony 的 ContainerInterface。然后,您可以使用 ContainerInterface::get() 方法,传入您在 services.yaml 中定义的公共别名,来动态获取所需的 Flysystem 存储实例。
// src/Factory/FileSystemFactory.php
namespace App\Factory;
use League\Flysystem\FilesystemOperator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use InvalidArgumentException;
class FileSystemFactory
{
    private ContainerInterface $container;
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }
    /**
     * 根据名称获取指定的 Flysystem 存储实例。
     *
     * @param string $storageName 存储的名称(例如 'first' 或 'second')
     * @return FilesystemOperator
     * @throws InvalidArgumentException 如果指定的存储不存在
     */
    public function getStorage(string $storageName): FilesystemOperator
    {
        // 根据传入的名称构建服务别名
        $serviceId = sprintf('%s.storage.alias', $storageName);
        // 检查服务是否存在,以提供更友好的错误信息
        if (!$this->container->has($serviceId)) {
            throw new InvalidArgumentException(sprintf('Flysystem storage "%s" (service ID: "%s") not found or not publicly aliased.', $storageName, $serviceId));
        }
        // 从容器中获取 Flysystem 存储实例
        $fileSystemStorage = $this->container->get($serviceId);
        // 确保获取到的实例是 FilesystemOperator 类型
        if (!$fileSystemStorage instanceof FilesystemOperator) {
            throw new \RuntimeException(sprintf('Service "%s" is not an instance of FilesystemOperator.', $serviceId));
        }
        return $fileSystemStorage;
    }
}现在,您可以在任何需要动态选择 Flysystem 存储的服务中注入 FileSystemFactory,并调用其 getStorage() 方法。
// src/Service/FileProcessor.php
namespace App\Service;
use App\Factory\FileSystemFactory;
class FileProcessor
{
    private FileSystemFactory $fileSystemFactory;
    public function __construct(FileSystemFactory $fileSystemFactory)
    {
        $this->fileSystemFactory = $fileSystemFactory;
    }
    public function processFile(string $storageType, string $filePath): void
    {
        $storage = $this->fileSystemFactory->getStorage($storageType);
        // 使用获取到的 $storage 进行文件操作
        $content = $storage->read($filePath);
        // ...
    }
}通过上述方法,您可以在 Symfony 应用中灵活地管理和动态访问 Flysystem 的多个存储实例,从而构建更具适应性和可维护性的文件操作逻辑。
以上就是在 Symfony 中通过依赖注入和别名动态访问 Flysystem 存储服务的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号