2lenet / entity-file-bundle
此包允许您将文件附加到实体
Requires
- php: ^8.1
- doctrine/doctrine-bundle: ^2.6
- doctrine/orm: ^2.12
- gedmo/doctrine-extensions: ^3.7
- league/flysystem-bundle: ^2.2
- symfony/form: ^5.4 || ^6.1
- symfony/string: ^6.0 || ^7.0
- symfony/webpack-encore-bundle: ^1.14 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^1.6
- phpstan/phpstan-doctrine: ^1.3
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-symfony: ^1.1
- slevomat/coding-standard: ^7.1
- squizlabs/php_codesniffer: ^3.6
README
使用此包,您可以将文件附加到实体。
安装
composer require 2lenet/entity-file-bundle
配置
此包使用配置。配置 = 1个实体 1个文件系统。
例如,您可以为多个卖家的徽标和销售的产品图片创建配置。
基本配置
在 lle_entity_file.yaml
lle_entity_file: configurations: seller_logos: class: "App\\Entity\\Seller" storage_adapter: "lle_entity_file.storage.default"
就这样!使用默认的存储适配器配置,这些文件将被保存在 data/seller_logos 下
更改存储适配器
此包使用 FlySystem Symfony Bundle。您可以创建自己的存储适配器(本地磁盘、FTP、驱动器等)。
为此,您需要 配置新的适配器。然后,更改配置中的 storage_adapter
。
使用
首先,您需要获取配置的管理器。为此,请使用 Lle\EntityFileBundle\Service\EntityFileLoader
$manager = $entityFileLoader->get("seller_logos");
创建文件
$entityFile = $manager->save($seller, $data, $path); $this->em->persist($entityFile); $this->em->flush();
$data 可能是一个字符串、一个 Symfony 文件对象(包括 UploadFile)或一个资源。
⚠️ 永远不要忘记持久化并刷新 EntityFile。
- 我想我的 EntityFile 包含额外的属性!
您可以使用自己的实体类,它需要是一个实现 Lle\EntityFileBundle\Entity\EntityFileInterface
的 Doctrine 实体。为了方便,存在 LleEntityFileBundle\Entity\Trait\EntityFileTrait
特性。
您还必须更新您的配置
unicorn: # ... entity_file_class: "App\\Entity\\UnicornEntityFile"
- 我想编辑我的新属性!
$entityFile = $manager->save($seller, $data, "unicorn.png"); $entityFile->setDescription("Picture of a very sexy unicorn"); $this->em->persist($entityFile); $this->em->flush();
- 我想在我的文件结构中有一个动态路径!
$manager->save($order, $data, "you/can/do/this"); // example: $dir = $order->getDate()->format("Y-m"); $name = $order->getId() . ".xml"; $manager->save($order, $data, $dir . "/" . $name)
- 由于某种原因,我想将文件保存在 data 之外的地方
在 flysystem.yaml
中创建自己的存储适配器,它基本上是默认适配器的复制,但具有不同的目录选项。
flysystem: storages: unicorn.storage: adapter: "local" options: directory: "%kernel.project_dir%/unicorns" permissions: file: public: 511 private: 511 dir: public: 511 private: 511
检索文件
$manager->get($seller); $manager->getOne($seller);
从URL检索文件
如果您没有使用 Symfony Flex,您需要在 routes.yaml 中添加路由
lle_entity_file: resource: "@LleEntityFileBundle/Resources/config/routes.yaml"
有两个路由可用
- lle_entityfile_entityfile_read(需要 configName 和 id)
示例:/lle-entity-file/seller_logos/1 - lle_entityfile_entityfile_readbypath(需要 configName 和 path)
示例:/lle-entity-file/seller_logos?path=2le.png
保护您的URL
默认情况下,只有登录用户可以访问这些 URL。您可以在配置中更改 role
键。
operation_reports: # ... role: "ROLE_OPERATOR"
- 我想让文件公开!
您可以在 role
键中使用 "PUBLIC_ACCESS"。
- 我想做更复杂的事情!
更改内容处置(在浏览器中显示或自动下载)
默认情况下,文件以内联方式提供。您可以在配置下更改处置键。
zip_reports: # ... disposition: "attachment"
访问文件内容
$manager->read($file); $manager->readStream($file);
删除文件
// deletes the entity and the actual file $manager->delete($file);
重命名或移动文件
$manager->move($file, "actually_not_an_unicorn.png"); $this->em->flush();
⚠️ 永远不要忘记刷新 EntityFile。
异常处理
https://flysystem.thephpleague.com/docs/usage/exception-handling/
Crudit
此包与 2LE 的 Crudit 包兼容。
您可以使用 EntityFileBrick,例如在标签页中
public function getTabs(): array { return [ "tab.files" => EntityFileBrickConfig::new("seller_logos"), ]; }
它具有一个可以查看、添加、删除和下载文件的拖放区域。