locomotivemtl/charcoal-contrib-filepond

Charcoal 文件pond 服务器适配器

0.3.1.1 2020-07-27 16:24 UTC

This package is auto-updated.

Last update: 2024-08-28 01:28:06 UTC


README

License Latest Stable Version Code Quality Coverage Status Build Status

FilePond 的 Charcoal 服务提供者。

FilePond 是一个JavaScript库,可以上传任何你扔给它的东西,优化图像以提高上传速度,并提供极佳、易于访问、顺滑的用户体验。

此贡献实际上作为一个上传服务器与 FilePond 配合使用。

目录

安装

首选(也是唯一支持的)方法是使用 Composer

$ composer require locomotivemtl/charcoal-contrib-filepond

然后按如下方式将模块添加到你的项目模块列表中

{
    "modules": {
        "charcoal/file-pond/file-pond": {}
    }
}

依赖项

必需

  • PHP 5.6+:推荐使用 PHP 7
  • ext-fileinfo: "*",
  • league/flysystem ^1.0,
  • locomotivemtl/charcoal-app >= 0.7

服务提供者

Charcoal\FilePond\ServiceProvider\FilePondServiceProvider

服务提供者将由模块自动实例化。

服务

  • FilePondService.php FilePondService 可以使用服务器配置标识符调用,以便在需要时将其绑定到服务器。

    $action = new RequestAction([
        'logger'          => $container['logger'],
        'filePondService' => $container['file-pond/service']($serverIdent),
    ]);
  • FilePondConfig.php

配置

FilePond 的配置可以在此处找到: FilePondConfig.php

它使用配置文件 file-pond.json 作为默认配置集,并且可以在你的项目配置中覆盖。

getServer($ident)getServers() 可以在配置上调用以检索服务器配置(s):ServerConfig.php

配置定义了一个服务器列表。

服务器是端点与文件系统的映射以及有关文件路径的信息的组合。

服务器选项。

用法

根据需要覆盖模块配置,将 file-pond.json 结构复制到你的项目配置文件中

{
    "contrib": {
        "file-pond": {
            "config": {
                "servers": {
                    "default": {
                        "route": "/file-pond",
                        "filesystem_ident": "private",
                        "upload_path": "uploads/file-pond",
                        "transfer_dir": "tmp/file-pond"
                    },
                    "s3": {
                        "route": "/file-pond/s3",
                        "filesystem_ident": "s3",
                        "upload_path": "uploads/file-pond",
                        "transfer_dir": "tmp/file-pond"
                    }
                }
            }
        }
    }
}

为 FilePond.js 前端框架设置一个 route。将自动创建一个使用所需模式的精简路由。完成后,使用正确的 FilePond 文档配置前端文件 pond 模块。

此过程的关键部分是,当表单提交时,服务器处理将先前上传的文件转移到其最终路径。这允许将上传移动到包含用户特定 id 的文件夹中,并出于安全原因隐藏服务器的真实路径映射。

你可以在动作控制器上使用 FilePondAwareTrait 来处理 filepond 转移。首先设置所需的依赖项,如下所示

<?php

// From 'charcoal-contrib-filepond'
use Charcoal\FilePond\Service\Helper\FilePondAwareTrait;

/**
 * Create a new instructor.
 */
class SomeAction extends AbstractAction
{
    use FilePondAwareTrait;

    /**
     * Inject dependencies from a DI Container.
     *
     * @param Container $container A Pimple DI service container.
     * @return void
     */
    public function setDependencies(Container $container)
    {
        parent::setDependencies($container);

        $this->setFilePondService($container['file-pond/service']('private'));
    }
}

然后你可以使用 handleTransfer() 方法来简化上传转移过程。

handleTransfer() 方法参考

/**
 * Attempts to handle the transfer of uploads given the proper parameters.
 * If possible, it will try to :
 * - fetch the upload path from the property.
 * - fetch the filesystem based on the public access of the property.
 * - Pass it through the best processor depending on wether their were ids passed or not.
 *
 * @param string|array|null             $ids        The uploaded files ids. Let to null to force $_FILES and $_POST.
 * @param string|PropertyInterface|null $property   The property ident or a property.
 * @param ModelInterface|string|null    $context    The context object as model or class ident.
 * @param string|null                   $pathSuffix Path suffix.
 * @return array
 */
protected function handleTransfer(
    $ids = null,
    $property = null,
    $context = null,
    $pathSuffix = null
)

用法示例

/** Transfer uploaded files */
$files = $this->handleTransfer(
    $submissionData['someProp'],
    'some_prop',
    $someModel,
    $someModel->property('id')->autoGenerate()
);

开发

安装开发环境

$ composer install

运行脚本(phplint, phpcs 和 phpunit)

$ composer test

API 文档

开发依赖项

  • [php-coveralls/php-coveralls][phpcov]
  • [phpunit/phpunit][phpunit]
  • [squizlabs/php_codesniffer][phpcs]

编码风格

charcoal-contrib-filepond 模块遵循 Charcoal 编码风格

编码风格验证/执行可以通过 composer phpcs 进行。同时,也有自动修复工具 composer phpcbf 可用。

致谢

许可证

Charcoal 采用 MIT 许可证。详细信息请查看 LICENSE