目的是创建一个Reader类,它是League Flysystem documentation之上的包装器
读者应该提供方便的方式来读取目录中的所有文件,无论文件物理形式(本地文件,还是存档中的文件)
由于DI方法,包装器不应该在其中创建依赖关系的实例,而是将这些依赖关系作为参数引入构造函数或其他setter方法。
下面是一个如何使用League Flysystem(没有提到的包装器)从磁盘读取常规文件的示例:
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new Local(__DIR__.'/path/to/root');
$filesystem = new Filesystem($adapter);
$content = $filesystem->read('path-to-file.txt');
正如您所看到的,首先创建一个在其构造函数中需要路径的适配器Local,然后在其构造函数中创建需要适配器实例的文件系统。
两者的参数:Filesystem和Local不是可选的。从这些类创建对象时必须传递它们。这两个类也没有任何公共setter用于这些参数。
我的问题是如何使用依赖注入编写包装文件系统和本地的Reader类呢?
我通常会做类似的事情:
<?php
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AdapterInterface;
class Reader
{
private $filesystem;
private $adapter
public function __construct(FilesystemInterface $filesystem,
AdapterInterface $adapter)
{
$this->filesystem = $filesystem;
$this->adapter = $adapter;
}
public function readContents(string $pathToDirWithFiles)
{
/**
* uses $this->filesystem and $this->adapter
*
* finds all files in the dir tree
* reads all files
* and returns their content combined
*/
}
}
// and class Reader usage
$reader = new Reader(new Filesytem, new Local);
$pathToDir = 'someDir/';
$contentsOfAllFiles = $reader->readContents($pathToDir);
//somwhere later in the code using the same reader object
$contentsOfAllFiles = $reader->readContents($differentPathToDir);
但这不起作用,因为我需要将一个本地适配器传递给Filesystem构造函数,为了做到这一点,我需要首先传递给本地适配器路径,这完全是针对读者使用的方便性,只是将路径传递给dir所在所有文件都是,而Reader只需一个方法readContents()即可完成所有这些文件的内容。
所以我被卡住了。是否可以将Reader作为Filesystem及其本地适配器的包装器来实现?
我想避免紧密耦合,我使用关键字new并以这种方式获取依赖项的对象:
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
class Reader
{
public function __construct()
{
}
public function readContents(string $pathToDirWithFiles)
{
$adapter = new Local($pathToDirWithFiles);
$filesystem = new Filesystem($adapter);
/**
* do all dir listing..., content reading
* and returning results.
*/
}
}
问题:
无论何时调用Filesystem
方法,您都可以使用工厂模式动态生成readContents
:
<?php
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AdapterInterface;
class Reader
{
private $factory;
public function __construct(LocalFilesystemFactory $factory)
{
$this->filesystem = $factory;
}
public function readContents(string $pathToDirWithFiles)
{
$filesystem = $this->factory->createWithPath($pathToDirWithFiles);
/**
* uses local $filesystem
*
* finds all files in the dir tree
* reads all files
* and returns their content combined
*/
}
}
然后,您的工厂负责创建正确配置的文件系统对象:
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local as LocalAdapter;
class LocalFilesystemFactory {
public function createWithPath(string $path) : Filesystem
{
return new Filesystem(new LocalAdapter($path));
}
}
最后,当你构建你的Reader
时,它看起来像这样:
<?php
$reader = new Reader(new LocalFilesystemFactory);
$fooContents = $reader->readContents('/foo');
$barContents = $reader->readContents('/bar');
您将创建Filesystem的工作委托给工厂,同时仍然通过依赖注入来维护组合的目标。
1.您可以使用Filesystem
和Local
作为依赖注入方式的依赖项。您可以使用默认路径创建Adapter
对象和Filesystem
对象,并在Reader
中传递它们。在readContents
方法中,您可以使用setPathPrefix()
方法修改路径。例如:
class Reader
{
private $filesystem;
private $adapter;
public function __construct(FilesystemInterface $filesystem,
AdapterInterface $adapter)
{
$this->filesystem = $filesystem;
$this->adapter = $adapter;
}
public function readContents(string $pathToDirWithFiles)
{
$this->adapter->setPathPrefix($pathToDirWithFiles);
// some code
}
}
// usage
$adapter = new Local(__DIR__.'/path/to/root');
$filesystem = new Filesystem($adapter);
$reader = new Reader($filesystem, $adapter);
2.Reader
不是适配器模式,因为它没有实现League Flysystem的任何接口。它是用于封装某些逻辑以使用文件系统的类。您可以阅读有关适配器模式here的更多信息。您应该使用接口并避免在类中直接创建对象以减少Reader和Filesystem之间的耦合。
3.是的,您可以在DIC中设置适配器的默认路径...
我希望我能正确理解你的问题。几周前我刚刚经历过这个问题。对我来说,这是一些有趣和有趣的东西。
Reading through this laravel snippet帮助我理解了接口和依赖注入如何运作良好。这篇文章讨论了合同与外观以及为什么你可能想要使用一个而不是另一个。
听起来您希望能够使用一个可以读取远程文件(S3等)或本地文件的Filesystem
实例。由于文件系统只能是远程或本地(不是组合),我认为正确的做法是使用接口以相同的方式进行交互,然后允许用户/开发人员选择(通过依赖注入首选项)哪个文件当他们声明Filesystem
的实例时,应该使用系统(本地或远程)。
// Classes used
use League\Container\Container;
use League\Container\ReflectionContainer;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
// Create your container
$container = new Container;
/**
* Use a reflection container so devs don't have to add in every
* dependency and can autoload them. (Kinda out of scope of the question,
* but still helpful IMO)
*/
$container->delegate((new ReflectionContainer)->cacheResolutions());
/**
* Create available filesystems and adapters
*/
// Local
$localAdapter = new Local($cacheDir);
$localFilesystem = new Filesystem($localAdapter);
// Remote
$client = new S3Client($args);
$s3Adapter = new AwsS3Adapter($client, 'bucket-name');
$remoteFilesystem = new Filesystem($s3Adapter);
/**
* This next part is up to you, and many frameworks do this
* in many different ways, but it almost always comes down
* to declaring a preference for a certain class, or better
* yet, an interface. This example is overly simple.
*
* Set the class in the container to have an instance of either
* the remote or local filesystem.
*/
$container->add(
FileSystemInterface::class,
$userWantsRemoteFilesystem ? $remoteFilesystem : $localFilesystem
);
Magento 2 does this通过编译di.xml
文件并通过声明对另一个的偏好来阅读您想要替换的类。
Symfony does this有点类似的方式。他们的文档对我来说有点粗略,但是经过几天的搜索(以及联赛),我终于走出了另一边,非常了解发生的事情。
假设您的应用程序中有依赖注入工作,并且您希望使用读者类连接到Filesystem
,那么您将包含FilesystemInterface
作为构造函数依赖项,并且当它被注入时它将使用通过$container->add($class, $service)
传递到容器中的任何内容
use League\Flysystem\FilesystemInterface;
class Reader
{
protected $filesystem;
public function __construct(FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
}
public function getFromLocation($location)
{
/**
* We know this will work, because any instance that implements the
* FilesystemInterface will have this read method.
* @see https://github.com/thephpleague/flysystem/blob/dab4e7624efa543a943be978008f439c333f2249/src/FilesystemInterface.php#L27
*
* So it doesn't matter if it is \League\Flysystem\Filesystem or
* a custom one someone else made, this will always work and
* will be served from whatever was declared in your container.
*/
return $this->filesystem->read($location);
}
}