pfcode/symfony-attachment-storage

Doctrine ORM 的附件存储抽象层

1.0.3 2020-05-06 00:42 UTC

This package is auto-updated.

Last update: 2024-09-06 10:27:40 UTC


README

这个库为存储在由 Doctrine ORM 访问的数据库中索引的附件(图片、视频和任何文件)提供抽象层。考虑到扩展性,允许开发者快速集成自己的存储平台、slug 生成方法和附件下载方法。

安装

通过以下方式将其添加到您的项目中:

composer require pfcode/symfony-attachment-storage

示例配置

首先,您需要创建一个实现 Pfcode\AttachmentStorage\Entity\AttachmentInterface 的 Doctrine 实体,并实现其 getter 和 setter。以下是一个示例

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Pfcode\AttachmentStorage\Entity\AttachmentInterface;

/**
 * @ORM\Entity()
 */
class MyAttachment implements AttachmentInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer", nullable=false)
     * @var int|null
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     * @var string|null
     */
    private $storageIdentifier;

    /**
     * @ORM\Column(type="string", length=8, nullable=false)
     * @var string|null
     */
    private $slug;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @var string|null
     */
    private $mimeType;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @var string|null
     */
    private $extension;

    /**
     * @ORM\Column(type="integer", nullable=false)
     * @var int
     */
    private $fileSize = 0;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @var string|null
     */
    private $originalName;

    public function setStorageIdentifier(?string $storageIdentifier): void {
        $this->storageIdentifier = $storageIdentifier;
    }

    public function getStorageIdentifier(): ?string {
        return $this->storageIdentifier;
    }

    public function setSlug(?string $slug): void {
        $this->slug = $slug;
    }

    public function getSlug(): ?string {
        return $this->slug;
    }

    public function setMimeType(?string $mimeType): void {
        $this->mimeType = $mimeType;
    }

    public function getMimeType(): ?string {
        return $this->mimeType;
    }

    public function setExtension(?string $extension): void {
        $this->extension = $extension;
    }

    public function getExtension(): ?string {
        return $this->extension;
    }

    public function setFileSize(int $bytes): void {
        $this->fileSize = $bytes;
    }

    public function getFileSize(): int {
        return $this->fileSize;
    }

    public function setOriginalName(?string $originalName): void {
        $this->originalName = $originalName;
    }

    public function getOriginalName(): ?string {
        return $this->originalName;
    }
    
    public function setId(?int $id): void {
        $this->id = $id;    
    }   

    public function getId(): ?int {
        return $this->id;
    }
}

然后,您应该在您的 config/services.yml 文件中注册一些服务

# Implementation of a simple storage service that uses directory on a local machine that is accessible publicly by URL
Pfcode\AttachmentStorage\Storage\LocalStorage:
    public: true
    bind:
      # Absolute path to directory used to store all files by this service 
      $absolutePath: '/var/www/public/images'
      # Relative path from directory accessible publicly by URL address in browser
      $baseUrl: '/images'

# Registry for all storage services that should be available in your project
Pfcode\AttachmentStorage\StorageRegistry\StorageRegistry:
    public: true
    calls: 
      # Register all storage services that you need. At least one is required
      - [registerStorage, ['@Pfcode\AttachmentStorage\Storage\LocalStorage']]
      # You should set default storage that will be used as a default method for uploading
      - [setDefaultStorage, ['@Pfcode\AttachmentStorage\Storage\LocalStorage']]

# Sample class used by AttachmentUploader to store files accessible on remote servers by URL
Pfcode\AttachmentStorage\Utils\Downloader\CurlDownloader:
    public: true
    
# Utility service used to recognize if file is an image, video or other type
Pfcode\AttachmentStorage\Utils\AttachmentDescriber:
    public: true

# Utility service used to suggest a file extension, when a file being uploaded doesn't have one
Pfcode\AttachmentStorage\Utils\ExtensionSuggester:
    public: true
    
# Sample service used to generate a slug for new uploaded attachments
  Pfcode\AttachmentStorage\Utils\SlugGenerator\SampleSlugGenerator:
    public: true
    arguments: ['@doctrine.orm.default_entity_manager']
    bind:
      $entityClass: 'App\Entity\MyAttachment'

# Service used to upload new attachments
Pfcode\AttachmentStorage\Uploader\AttachmentUploader:
    public: true
    arguments: [
      '@Pfcode\AttachmentStorage\StorageRegistry\StorageRegistry', 
      '@Pfcode\AttachmentStorage\Utils\ExtensionSuggester',
      '@Pfcode\AttachmentStorage\Utils\SlugGenerator\SampleSlugGenerator',
      '@Pfcode\AttachmentStorage\Utils\Downloader\CurlDownloader']
    bind:
      # Specify class of entity that implements AttachmentInterface.
      # Warning! This is not a service! Just a string with Fully Qualified Class Name
      $entityClass: 'App\Entity\MyAttachment'

扩展组件

这个库的所有组件都是考虑到扩展性而创建的,因此您可以实现可用的接口或创建现有服务的新的类,将它们注册到您的 services.yml 文件中,并在其他服务的配置中引用它们。

许可证

这个库是在 MIT 许可证下发布的。