SamsonPHP 文件系统服务

1.1.11 2015-01-22 15:07 UTC

README

#SamsonPHP 文件服务

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Stories in Ready Total Downloads

此模块为独立于所使用的特定文件系统,对文件系统进行抽象化工作提供了一种方法。

##配置

主要目标是配置 $fileServiceClassName 服务参数。

这通常通过 SamsonPHP 模块/服务配置 完成

默认情况下,$fileServiceClassName 设置为 samsonphp\fs\LocalFileService - 它使用标准本地文件系统服务。此参数必须设置为文件服务类名称,例如 - 本地文件服务 - samsonphp\fs\LocalFileService,不应使用任何模块/服务标识符或其他内容,应避免使用第一个命名空间分隔符 \

  • \samsonphp\fs\LocalFileService - 错误
  • samsonphp\fs\LocalFileService - 正确

在初始化服务时,它将检查配置的文件服务类是否存在,否则将发出致命错误。

这使您能够,例如,快速将您的 Web 应用程序的文件系统从本地文件系统更改为由 SamsonPHP AWS 文件服务 fs_aws 实现的 Amazon Web Services S3 存储桶。您所需要做的就是为此 SamsonPHP 文件服务(fs)添加配置

class FileServiceConfig extends \samson\core\Config 
{
  /**@var string Configured module/service identifier */
  public $__id = 'fs';
  
  /**@var string Set Amazon Web Services as web-application file service using its identifier */
  public $fileServiceID = 'samsonphp\fs\AWSFileService';
}

使用方法

要使用此 SamsonPHP 文件服务,您应该获取文件服务实例指针

/**@var \samsonphp\fs\FileService $fs Pointer to file service */
$fs = & m('fs');

之后,您可以使用 AbstractFileService 接口 中提供的所有可用方法,该接口由此 SamsonPHP 文件服务(fs)实现。所有这些方法调用都像一个代理,将它们传递给当前配置的文件服务(默认为 php_fs_local)。

示例用法

if (!$fs->exists(...)) {
  $fs->write(...);
}

在测试中使用服务

首先,您应该创建服务实例

// Create instance
$this->fileService = new FileService(__DIR__.'../');

在其他地方,在创建服务实例之后,您应该通过工厂方法检索服务对象

// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samsonphp\fs\FileService');

所有其他 SamsonPHP 模块必须在工作与文件时使用此文件服务方法。