在symfony应用程序中,当您通过flysystem.yaml配置了多个存储服务时,例如:
# 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'
默认情况下,Flysystem Bundle会将这些配置项转换为服务,并可以像这样直接注入到您的服务中:
use League\Flysystem\FilesystemOperator; class MyService { private FilesystemOperator $firstStorage; public function __construct(FilesystemOperator $firstStorage) { $this->firstStorage = $firstStorage; } }
然而,当您需要根据运行时参数(例如,一个工厂方法的输入)动态选择并获取特定的存储实例时,这种直接注入的方式就显得不够灵活。您可能不希望在每个需要访问存储的服务中都注入所有可能的存储实例,尤其当存储实例数量较多时。
Flysystem Bundle创建的服务默认是私有的(public: false),这意味着它们不能直接通过ContainerInterface::get()方法获取。为了实现动态访问,我们需要采取两个关键步骤:
为了使Flysystem服务能够被动态获取,我们需要在config/services.yaml中为每个需要动态访问的Flysystem存储服务创建公共别名。
# config/services.yaml services: # ... 其他服务配置 # 为 'first.storage' Flysystem 服务创建公共别名 first.storage.alias: alias: 'first.storage' public: true # 为 'second.storage' Flysystem 服务创建公共别名 second.storage.alias: alias: 'second.storage' public: true
通过这种方式,我们为原始的Flysystem服务(例如first.storage)创建了一个新的、公共的服务ID(first.storage.alias)。现在,这些别名可以通过容器直接获取。
接下来,我们将创建一个工厂类,它负责根据传入的字符串参数动态地返回对应的FilesystemOperator实例。这个工厂类将注入ContainerInterface。
<?php namespace App\Factory; use League\Flysystem\FilesystemOperator; use Psr\Container\ContainerInterface; // 使用 Psr\Container\ContainerInterface 更通用 class FileSystemFactory { private ContainerInterface $container; public function __construct(ContainerInterface $container) { $this->container = $container; } /** * 根据存储名称获取对应的FilesystemOperator实例。 * * @param string $storageName 存储的名称(例如 'first.storage.alias', 'second.storage.alias') * @return FilesystemOperator * @throws \Psr\Container\NotFoundExceptionInterface 如果指定的存储服务不存在 * @throws \Psr\Container\ContainerExceptionInterface 如果获取服务时发生错误 */ public function getStorage(string $storageName): FilesystemOperator { // 检查服务是否存在,避免直接调用get()导致错误 if (!$this->container->has($storageName)) { throw new \InvalidArgumentException(sprintf('The storage service "%s" does not exist or is not public.', $storageName)); } $fileSystemOperator = $this->container->get($storageName); // 确保获取到的是FilesystemOperator实例 if (!$fileSystemOperator instanceof FilesystemOperator) { throw new \UnexpectedValueException(sprintf('The service "%s" is not an instance of FilesystemOperator.', $storageName)); } return $fileSystemOperator; } }
注意事项:
现在,您可以在任何需要动态获取Flysystem存储实例的服务中注入FileSystemFactory,并使用它来获取所需的存储实例:
<?php namespace App\Service; use App\Factory\FileSystemFactory; use League\Flysystem\FilesystemOperator; class FileProcessor { private FileSystemFactory $fileSystemFactory; public function __construct(FileSystemFactory $fileSystemFactory) { $this->fileSystemFactory = $fileSystemFactory; } public function processFile(string $fileName, string $storageKey): void { // 根据传入的 storageKey 动态获取 Flysystem 存储实例 $storage = $this->fileSystemFactory->getStorage($storageKey . '.alias'); // 现在可以使用获取到的存储实例进行文件操作 if ($storage->fileExists($fileName)) { $contents = $storage->read($fileName); // ... 处理文件内容 echo "文件 '{$fileName}' 的内容: " . $contents . PHP_EOL; } else { echo "文件 '{$fileName}' 在存储 '{$storageKey}' 中不存在." . PHP_EOL; } } } // 假设在控制器或其他服务中调用 // $fileProcessor->processFile('my_document.txt', 'first.storage'); // $fileProcessor->processFile('another_image.jpg', 'second.storage');
通过为Flysystem服务创建公共别名,并结合使用ContainerInterface的工厂模式,我们成功解决了在Symfony中动态获取特定Flysystem存储实例的挑战。这种方法避免了在每个服务中都注入所有可能的存储实例,提高了代码的灵活性和可维护性。尽管直接使用ContainerInterface有时被视为一种“服务定位器”模式,与纯粹的依赖注入有所不同,但在需要动态选择依赖项的特定场景下,它提供了一个简洁有效的解决方案。务必注意在使用ContainerInterface::get()时进行必要的错误处理和类型检查,以确保应用程序的稳定性。
以上就是在Symfony中动态访问Flysystem存储实例教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号