抽象系统中的对象

tech2023-10-11  111

抽象系统中的对象

Reading and writing files is an integral aspect of any programming language, but the underlying implementation can vary enormously. For example, the finer details of writing data to the local filesystem compared to uploading over FTP is very different – yet conceptually, it’s very similar.

读写文件是任何编程语言不可或缺的一部分,但是其基础实现可能会有很大差异。 例如,与通过FTP上载相比,将数据写入本地文件系统的详细信息是非常不同的-但从概念上讲,这是非常相似的。

In addition to old warhorses like FTP, online storage is increasingly ubiquitous and inexpensive – with scores of services available such as Dropbox, Amazon’s S3 and Rackspace’s Cloud Files – but these all use subtly different methods for reading and writing.

除了诸如FTP之类的旧式存储之外,在线存储也变得越来越普遍和廉价-诸如Dropbox,Amazon的S3和Rackspace的Cloud Files等大量服务可用,但是这些都使用微妙的读写方法。

That’s where flysystem comes in. It provides a layer of abstraction over multiple file systems, which means you don’t need to worry where the files are, how they’re stored, or need be concerned with low-level I/O operations. All you need to worry about are the high-level operations such as reading, writing and organizing into directories.

这其中的flysystem用武之地它提供了一个抽象了多个文件系统层,其是指其中的文件是你不需要担心,他们是如何存储,或需要低级别的I / O操作有关。 您只需要担心高级操作,例如读取,写入和组织到目录中。

Such abstraction also makes it simpler to switch from one system to another without having to rewrite vast swathes of application code. It also provides a logical approach to moving or copying data from one storage system to another, without worrying about the underlying implementation.

这种抽象还使得从一个系统切换到另一个系统变得更加简单,而不必重写大量的应用程序代码。 它还提供了一种逻辑方法,可将数据从一个存储系统移动或复制到另一个存储系统,而无需担心基础实现。

You can use Dropbox, S3, Cloud Files, FTP or SFTP as if they were local; saving a file becomes the same process whether it’s being saved locally or transferred over the network. You can treat zip archives as if they were a bunch of folders, without worrying about the nitty gritty of creating and compressing the archives themselves.

您可以使用Dropbox,S3,云文件,FTP或SFTP,就好像它们是本地的一样。 无论是将文件保存在本地还是通过网络传输,保存文件的过程都相同。 您可以将zip归档文件视为一堆文件夹,而不必担心创建和压缩归档文件本身的麻烦。

安装和基本用法 (Installation and Basic Usage)

As ever, Composer is the best way to install:

与以往一样,Composer是安装的最佳方法:

"league/flysystem": "0.2.*"

Now you can simply create one or more instances of League\Flysystem\Filesystem, by passing in the appropriate adapter.

现在,您可以通过传入相应的适配器来简单地创建League\Flysystem\Filesystem一个或多个实例。

For example, to use a local directory:

例如,使用本地目录:

use League\Flysystem\Filesystem; use League\Flysystem\Adapter\Local as Adapter; $filesystem = new Filesystem(new Adapter('/path/to/directory'));

To use an Amazon S3 bucket, there’s slightly more configuration involved:

要使用Amazon S3存储桶,需要进行更多配置:

use Aws\S3\S3Client; use League\Flysystem\Adapter\AwsS3 as Adapter; $client = S3Client::factory(array( 'key' => '[your key]', 'secret' => '[your secret]', )); $filesystem = new Filesystem(new Adapter($client, 'bucket-name', 'optional-prefix'));

To use Dropbox:

要使用Dropbox:

use Dropbox\Client; use League\Flysystem\Adapter\Dropbox as Adapter; $client = new Client($token, $appName); $filesystem = new Filesystem(new Adapter($client, 'optional/path/prefix'));

(To get the token and application name, create an application using Dropbox’s App Console.)

(要获取令牌和应用程序名称,请使用Dropbox的App Console创建一个应用程序 。)

Here’s an example for SFTP – you may not need every option listed here:

这是SFTP的示例-您可能不需要此处列出的每个选项:

use League\Flysystem\Adapter\Sftp as Adapter; $filesystem = new Filesystem(new Adapter(array( 'host' => 'example.com', 'port' => 21, 'username' => 'username', 'password' => 'password', 'privateKey' => 'path/to/or/contents/of/privatekey', 'root' => '/path/to/root', 'timeout' => 10, )));

For other adapters such as normal FTP, Predis or WebDAV, refer to the documentation.

对于其他适配器,例如普通的FTP,Predis或WebDAV, 请参阅文档 。

读取和写入文件系统 (Reading and Writing to a File System)

As far as your application code is concerned, you simply need to replace calls such as file_exists(), fopen() / fclose(), fread / fwrite and mkdir() with their flysystem equivalents.

就您的应用程序代码而言,您只需要用它们的flysystem等效项替换诸如file_exists() , fopen() / fclose() , fread / fwrite和mkdir()的调用。

For example, take the following legacy code, which copies a local file to an S3 bucket:

例如,采用以下旧代码,该代码将本地文件复制到S3存储桶:

$filename = "/usr/local/something.txt"; if (file_exists($filename)) { $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $aws = Aws::factory('/path/to/config.php'); $s3 = $aws->get('s3'); $s3->putObject(array( 'Bucket' => 'my-bucket', 'Key' => 'data.txt', 'Body' => $content, 'ACL' => 'public-read', )); }

Using flysystem, it might look a little like this:

使用flysystem,它可能看起来像这样:

$local = new Filesystem(new Adapter('/usr/local/')); $remote = new Filesystem( S3Client::factory(array( 'key' => '[your key]', 'secret' => '[your secret]', )), 'my-bucket' ); if ($local->has('something.txt')) { $contents = $local->read('something.txt'); $remote->write('data.txt', $contents, ['visibility' : 'public']); }

Notice how we’re using terminology such as reading and writing, local and remote – high level abstractions, with no worrying about things like creating and destroying file handles.

请注意,我们是如何使用术语,如阅读和写作 , 本地和远程 -高层次的抽象,没有担心的东西,如创建和销毁的文件句柄。

Here’s a summary of the most important methods on the League\Flysystem\Filesystem class:

这是League\Flysystem\Filesystem类上最重要方法的摘要:

MethodExampleReading$filesystem->read('filename.txt')Writing$filesystem->write('filename.txt', $contents)Updating$filesystem->update('filename.txt')Writing or updating$filesystem->put('filename.txt')Checking existence$filesystem->has('filename.txt')Deleting$filesystem->delete('filename.txt')Renaming$filesystem->rename('old.txt', 'new.txt')Reading Files$filesystem->read('filename.txt')Getting file info$filesystem->getMimetype('filename.txt')$filesystem->getSize('filename.txt')$filesystem->getTimestamp('filename.txt')Creating directories$filesystem->createDir('path/to/directory')Deleting directories$filesystem->deleteDir('path/to/directory') 方法 例 读 $filesystem->read('filename.txt') 写作 $filesystem->write('filename.txt', $contents) 更新中 $filesystem->update('filename.txt') 编写或更新 $filesystem->put('filename.txt') 检查存在 $filesystem->has('filename.txt') 删除中 $filesystem->delete('filename.txt') 重命名 $filesystem->rename('old.txt', 'new.txt') 读取文件 $filesystem->read('filename.txt') 获取文件信息 $filesystem->getMimetype('filename.txt') $filesystem->getSize('filename.txt') $filesystem->getTimestamp('filename.txt') 创建目录 $filesystem->createDir('path/to/directory') 删除目录 $filesystem->deleteDir('path/to/directory')

自动创建目录 (Automatically Creating Directories)

When you call $filesystem->write(), it ensures that the directory you’re trying to write to exists – if it doesn’t, it creates it for you recursively.

当您调用$filesystem->write() ,它将确保您尝试写入的目录存在-如果不存在,它将为您递归创建它。

So this…

所以这…

$filesystem->write('/path/to/a/directory/somewhere/somefile.txt', $contents);

…is basically the equivalent of:

…基本上等于:

$path = '/path/to/a/directory/somewhere/somefile.txt'; if (!file_exists(dirname($path))) { mkdir(dirname($path), 0755, true); } file_put_contents($path, $contents);

管理可见性 (Managing Visibility)

Visibility – i.e., permissions – can vary in implementation or semantics across different storage mechanisms, but flysystem abstracts them as either “private” or “public”. So, you don’t need to worry about specifics of chmod, S3 ACLs, or whatever terminology a particular mechanism happens to use.

可见性(即权限)在不同存储机制的实现或语义上可能有所不同,但是flysystem将它们抽象为“私有”或“公共”。 因此,您无需担心chmod ,S3 ACL的细节,也无需担心使用某种特定机制的任何术语。

You can either set the visibility when calling write():

您可以在调用write()时设置可见性:

$filesystem->write('filename.txt', $data, ['visibility' : 'private']);

Prior to 5.4, or according to preference:

5.4之前或根据喜好:

$filesystem->write('filename.txt', $data, array('visibility' => 'private'));

Alternatively, you can set the visibility of an existing object using setVisibility:

另外,您可以使用setVisibility设置现有对象的可见性:

$filesystem->setVisibility('filename.txt', 'private');

Rather than set it on a file-by-file basis, you can set a default visibility for a given instance in its constructor:

您可以在给定实例的构造函数中为其设置默认可见性,而不是逐个文件地设置它:

$filesystem = new League\Flysystem\Filesystem($adapter, $cache, [ 'visibility' => AdapterInterface::VISIBILITY_PRIVATE ]);

You can also use getVisibility to determine a file’s permissions:

您还可以使用getVisibility确定文件的权限:

if ($filesystem->getVisibility === 'private') { // file is secret }

列出文件和目录 (Listing Files and Directories)

You can get a listing of all files and directories in a given directory like this:

您可以像这样获取给定目录中所有文件和目录的列表:

$filesystem->listContents();

The output would look a little like this;

输出看起来像这样;

[0] => array(8) { 'dirname' => string(0) "" 'basename' => string(11) "filters.php" 'extension' => string(3) "php" 'filename' => string(7) "filters" 'path' => string(11) "filters.php" 'type' => string(4) "file" 'timestamp' => int(1393246029) 'size' => int(2089) } [1] => array(5) { 'dirname' => string(0) "" 'basename' => string(4) "lang" 'filename' => string(4) "lang" 'path' => string(4) "lang" 'type' => string(3) "dir" }

If you wish to incorporate additional properties into the returned array, you can use listWith():

如果希望将其他属性合并到返回的数组中,则可以使用listWith() :

$flysystem->listWith(['mimetype', 'size', 'timestamp']);

To get recursive directory listings, the second parameter should be set to TRUE:

要获取递归目录列表,第二个参数应设置为TRUE:

$filesystem->listContents(null, true);

The null simply means we start at the root directory.

null仅表示我们从根目录开始。

To get just the paths:

要获取路径:

$filesystem->listPaths();

挂载文件系统 (Mounting Filesystems)

Mounting filesystems is a concept traditionally used in operating systems, but which can also be applied to your application. Essentially it’s like creating references – shortcuts, in a sense – to filesystems, using some sort of identifier.

挂载文件系统是操作系统中传统上使用的一个概念,但也可以应用于您的应用程序。 从本质上讲,这就像使用某种标识符为文件系统创建引用(某种意义上是快捷方式)。

Flysystem provides the Mount Manager for this. You can pass one or more adapters upon instantiation, using strings as keys:

Flysystem为此提供了Mount Manager。 您可以在实例化时使用字符串作为键来传递一个或多个适配器:

$ftp = new League\Flysystem\Filesystem($ftpAdapter); $s3 = new League\Flysystem\Filesystem($s3Adapter); $manager = new League\Flysystem\MountManager(array( 'ftp' => $ftp, 's3' => $s3, ));

You can also mount a file system at a later time:

您也可以在以后安装文件系统:

$local = new League\Flysystem\Filesystem($localAdapter); $manager->mountFilesystem('local', $local);

Now you can use the identifiers as if they were protocols in URI’s:

现在,您可以使用标识符,就像它们是URI中的协议一样:

// Get the contents of a local file… $contents = $manager->read('local://path/to/file.txt'); // …and upload to S3 $manager->write('s3://path/goes/here/filename.txt', $contents);

It’s perhaps more useful to use identifiers which are generic in nature, e.g.:

使用本质上通用的标识符也许更有用,例如:

$s3 = new League\Flysystem\Filesystem($s3Adapter); $manager = new League\Flysystem\MountManager(array( 'remote' => new League\Flysystem\Filesystem($s3Adapter), )); // Save some data to remote storage $manager->write('remote://path/to/filename', $data);

快取 (Caching)

Flysystem also supports a number of technologies for caching file metadata. By default it uses an in-memory cache, but you can also use Redis or Memcached.

Flysystem还支持多种用于缓存文件元数据的技术。 默认情况下,它使用内存缓存,但是您也可以使用Redis或Memcached。

Here’s an example of using Memcached; the cache adapter is passed to the Filesystem constructor as an optional second parameter:

这是使用Memcached的示例; 高速缓存适配器作为可选的第二个参数传递给Filesystem构造函数:

use League\Flysystem\Adapter\Local as Adapter; use League\Flysystem\Cache\Memcached as Cache; $memcached = new Memcached; $memcached->addServer('localhost', 11211); $filesystem = new Filesystem(new Adapter(__DIR__.'/path/to/root'), new Cache($memcached, 'storageKey', 300)); // Storage Key and expire time are optional

As with the filesystem adpaters, if you wished you could create your own – simply extend League\Flysystem\Cache\AbstractCache.

与文件系统适配器一样,如果您希望可以创建自己的适配器,只需扩展League\Flysystem\Cache\AbstractCache 。

摘要 (Summary)

In this article I’ve introduced flysystem, a package which provides a layer of abstraction over a variety of forms of file storage. Although I haven’t covered every available adapter, the documentation should help fill in the gaps, introduce a couple of other features and document any new adapters in the future.

在本文中,我介绍了flysystem,这是一个软件包,它为各种形式的文件存储提供了抽象层。 尽管我没有介绍所有可用的适配器, 但是文档应有助于填补空白,介绍一些其他功能并在将来记录所有新适配器。

翻译自: https://www.sitepoint.com/abstract-file-systems-flysystem/

抽象系统中的对象

相关资源:Flysystem本地和远程文件系统的抽象
最新回复(0)