samsonos/php_fs

SamsonPHP 文件系统服务

1.1.7 2015-01-11 07:44 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:54:27 UTC


README

#SamsonPHP 文件服务

此模块提供了在不同文件系统之间独立工作的抽象层,无论使用的是哪种具体的文件系统。

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

##配置

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

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

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

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

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

这使您能够,例如,快速将您的Web应用程序文件系统从本地文件系统更改为由SamsonPHP AWS 文件服务 php_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 = 'samson\fs\AWSFileService';
}

用法

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

/**@var \samson\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('samson\fs\FileService');

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