我只是想知道如何在 flysystem 包中獲取指定的檔案存盤實體。例如,如果我有這樣的配置:
flysystem:
storages:
first.storage:
adapter: 'local'
options:
directory: '%kernel.project_dir%/var/storage/storage/first'
second.storage:
adapter: 'local'
options:
directory: '%kernel.project_dir%/var/storage/default/second'
我想根據例如工廠中的某些引數來獲取它。類似的東西:
$fileSystemStorage = (new FileSystemFactory()->getStorage('second');
這是我的工廠:
class FileSystemFactory
{
public function getStorage(string $storage): FilesystemOperator
{
switch ($storage) {
case 'first':
break;
case 'second':
break;
}
}
}
我只是不知道如何手動定義我想從 flysystem.yaml 中獲取的選項。
在檔案中它說我可以注入類似的東西(從配置中命名駝峰):https : //github.com/thephpleague/flysystem-bundle
public function __construct(FilesystemOperator $firstStorage)
{
$this->storage = $firstStorage;
}
但在我的情況下,我想根據引數手動定義它。當然,我可以創建兩個具有 2 個不同注入($firstStorage 和 $secondStorage)的類,然后從這些類回傳物件,但也許有一些更簡單的方法?
uj5u.com熱心網友回復:
如果您通讀 FlySystemBundle 的檔案,您會發現它支持在運行時延遲加載存盤:
鏈接到檔案
如果通過 ENV 變數(或引數)設定它不能滿足您的需求,您可以利用LazyFactory本身并直接通過Lazyfactory::createStorage方法使用它。
如果這不適合您的需要,您可以復制該類并為其分配CompilerPass并根據需要對其進行配置。
uj5u.com熱心網友回復:
更新!!!
我遇到了非常相似的問題,我通過使用 ContainerInterface 和服務別名(flysystem 服務不是公開的)解決了它:
// config/services.yaml
services:
// ...
// we need this for every storage,
// flysystems services aren't public and we can solve this using aliases
first.storage.alias:
alias: 'first.storage'
public: true
<?php
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileSystemFactory
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getStorage(string $storage)
{
$storageContainer = $this->container->get($storage); // ex. first.storage.alias
switch ($storageContainer) {
case 'first':
break;
case 'second':
break;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/334801.html
