max107 / upload-bundle
带有flysystem集成的上传包
2.0.1
2021-07-13 10:59 UTC
Requires
- doctrine/doctrine-bundle: ~1.0|~2.0
- doctrine/orm: ~2.0
- jms/metadata: ^2.1
- liip/imagine-bundle: ~2.0
- oneup/flysystem-bundle: ~4.0
- symfony/form: ~5.0
- symfony/property-access: ~5.0
- symfony/twig-bundle: ~5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- matthiasnoback/symfony-dependency-injection-test: ^4.0
- phpstan/phpstan: ^0.12.9
- phpunit/phpunit: ^8.2
- symfony/asset: ~5.0
- symfony/browser-kit: ~5.0
- symfony/css-selector: ~5.0
- symfony/translation: ~5.0
- symfony/var-dumper: ~5.0
This package is auto-updated.
Last update: 2023-10-13 15:58:50 UTC
README
描述
该包基于 VichUploaderBundle。该包的显著特点是API的简洁性,因为它仅与 flysystem 交互,并强制保存文件的相对路径。
该包还提供了两个用于表单的字段,用于预览上传的文件。
安装
composer require max107/upload-bundle
配置
use Max107\Bundle\UploadBundle\Upload\Annotation as Upload;
// @Upload\Uploadable для сущности и
// @Upload\UploadableField(filesystem="default", path="image") для поля
其中 filesystem
是在 flysystem
中挂载的文件系统,而 path
是变量映射,用于保存保存文件的相对路径。同时支持VichUploadBundle的基本参数:name
、size
、mimeType
、originalName
、dimensions
示例
<?php use Doctrine\ORM\Mapping as ORM; use Max107\Bundle\UploadBundle\Upload\Annotation as Upload; /** * @ORM\Entity * @Upload\Uploadable */ class Product { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="datetime") */ protected $updated_at; /** * @Upload\UploadableField(filesystem="default", path="image") * * @var File */ protected $image_file; /** * @ORM\Column(type="string", nullable=true) * * @var string */ protected $image; public function getImage(): ?string { return $this->image; } public function setImage(string $image): self { $this->image = $image; return $this; } /** * If manually uploading a file (i.e. not using Symfony Form) ensure an instance * of 'UploadedFile' is injected into this setter to trigger the update. If this * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter * must be able to accept an instance of 'File' as the bundle will inject one here * during Doctrine hydration. * * @param \SplFileInfo $image * * @throws \Exception */ public function setImageFile(\SplFileInfo $image = null) { $this->image_file = $image; if (null !== $image) { // It is required that at least one field changes if you are using doctrine // otherwise the event listeners won't be called and the file is lost $this->updated_at = new \DateTime(); } } public function getImageFile(): ?\SplFileInfo { return $this->image_file; } }
手动安装文件
<?php use Symfony\Component\HttpFoundation\File\File; $product = new Product; $product->setName('beer'); $product->setImageFile(new File(__DIR__.'/images/beer.png'));