rmed19 / simple-behaviors-bundle
一个提供简单行为(如可生成缩略名和可记录时间戳的实体)的Symfony包。
Requires
- php: >=8.0
- doctrine/orm: ^2.10 || ^3.0
- symfony/config: ^6.0 || ^7.0
- symfony/dependency-injection: ^6.0 || ^7.0
- symfony/http-kernel: ^6.0 || ^7.0
- symfony/serializer: ^6.0 || ^7.0
- symfony/string: ^6.0 || ^7.0
Requires (Dev)
- symfony/phpunit-bridge: ^6.0 || ^7.0
This package is auto-updated.
Last update: 2024-09-11 16:42:13 UTC
README
MrSimpleBehaviorsBundle 是一个提供 Doctrine 实体的简单、可重用行为的 Symfony 包,例如 Sluggable 和 Timestampable 功能。
特性
- 可生成缩略名:根据指定字段自动为实体生成友好的 URL 缩略名。
- 可记录时间戳:自动管理实体的
createdAt
和updatedAt
字段。
需求
- PHP 8.0 或更高版本
- Symfony 6.0 或更高版本
- Doctrine ORM
- Symfony 字符串组件
- Symfony 序列化组件
安装
使用 Composer 安装包
composer require rmed19/simple-behaviors-bundle
如果你不使用 Symfony Flex,则需要手动在 config/bundles.php
中启用包
return [ // Other bundles... Mr\SimpleBehaviorsBundle\MrSimpleBehaviorsBundle::class => ['all' => true], ];
使用
可生成缩略名的实体
要在你的实体中使用可生成缩略名功能,只需包含 SluggableEntity
特性并实现 SluggableInterface
use Doctrine\ORM\Mapping as ORM; use Mr\SimpleBehaviorsBundle\Model\SluggableInterface; use Mr\SimpleBehaviorsBundle\Traits\SluggableEntity; #[ORM\Entity] class Article implements SluggableInterface { use SluggableEntity; #[ORM\Column(type: 'string', length: 255)] private string $title; public function getTitle(): string { return $this->title; } public function setTitle(string $title): self { $this->title = $title; return $this; } public function __toString(): string { return $this->title; } }
可记录时间戳的实体
要在你的实体中使用可记录时间戳功能,包含 TimestampableEntity
特性并实现 TimestampableInterface
use Doctrine\ORM\Mapping as ORM; use Mr\SimpleBehaviorsBundle\Model\TimestampableInterface; use Mr\SimpleBehaviorsBundle\Traits\TimestampableEntity; #[ORM\Entity] class Post implements TimestampableInterface { use TimestampableEntity; #[ORM\Column(type: 'string', length: 255)] private string $content; public function getContent(): string { return $this->content; } public function setContent(string $content): self { $this->content = $content; return $this; } }
序列化组
MrSimpleBehaviorsBundle
在特性中直接提供序列化组,以控制缩略名和时间戳的序列化方式。
- 缩略名:
SluggableEntity
特性包含组mr_simple_behaviors:slug:read
。 - 时间戳:
TimestampableEntity
特性包含组mr_simple_behaviors:timestamp:read
。
这些组允许你有效管理不同上下文中这些字段的序列化。
示例
在你的实体中,SluggableEntity
特性自动将 mr_simple_behaviors:slug:read
组应用于 slug
字段,而 TimestampableEntity
特性将 mr_simple_behaviors:timestamp:read
组应用于 createdAt
和 updateAt
字段。
使用 Symfony 序列化组件的示例使用
use Symfony\Component\Serializer\SerializerInterface; $serializedData = $serializer->serialize($article, 'json', ['groups' => ['mr_simple_behaviors:slug:read']]);
在上面的示例中,只有与 mr_simple_behaviors:slug:read
组关联的字段,包括缩略名,将被序列化。
配置
该包使用 Doctrine 事件监听器来自动处理可生成缩略名和可记录时间戳的行为。无需任何额外的配置。
但是,如果您需要自定义行为,可以通过修改项目中位于的 services.xml
文件来覆盖默认服务
<services> <service id="mr.simple_behaviors.listener.sluggable" class="Mr\SimpleBehaviorsBundle\EntityListener\SluggableEntityListener"> <argument type="service" id="slugger"/> <tag name="doctrine.event_listener" event="prePersist"/> <tag name="doctrine.event_listener" event="preUpdate"/> </service> <service id="mr.simple_behaviors.listener.timestampable" class="Mr\SimpleBehaviorsBundle\EntityListener\TimestampableEntityListener"> <tag name="doctrine.event_listener" event="prePersist"/> <tag name="doctrine.event_listener" event="preUpdate"/> </service> </services>
测试
要运行测试套件,请使用 PHPUnit
vendor/bin/phpunit
请确保您已安装并配置了 PHPUnit。
贡献
欢迎贡献!请提交一个拉取请求或创建一个问题来讨论您的想法。
许可证
此包根据 MIT 许可证授权。有关详细信息,请参阅 LICENSE 文件。
致谢
MrSimpleBehaviorsBundle 由 Mohamed RHAMNIA 开发和维护。特别感谢 Symfony 和 Doctrine 社区提供的出色工具和文档。