tboileau / lifecycle-bundle
此包已被放弃,不再维护。没有建议的替代包。
Symfony 4 包,一个遵守 SOLID 原则的对象生命周期管理器
1.2
2018-06-29 09:05 UTC
Requires
- php: ^7.1.3
- ext-iconv: *
- symfony/framework-bundle: ^4
Requires (Dev)
- codeclimate/php-test-reporter: ^0.3.0@dev
- phpunit/php-code-coverage: ^6.0@dev
- sensio/framework-extra-bundle: ^5.1
- symfony/browser-kit: ^4.2@dev
- symfony/console: ^4.2@dev
- symfony/dotenv: ^4
- symfony/maker-bundle: ^1.4
- symfony/phpunit-bridge: ^4
- symfony/security-bundle: ^4
- symfony/twig-bundle: ^4
- symfony/yaml: ^4.2@dev
This package is not auto-updated.
Last update: 2019-02-20 19:50:09 UTC
README
正如您所知,SOLID 原则中的 "S" 代表 单一职责。这意味着每个类应该只有一个职责。因此,您不能在控制器中放置持久化实体的逻辑代码。它必须在为该目的而专门创建的服务中。
为了管理您的对象的生命周期(如状态:添加、更新和删除),此包使该过程更加简单。
安装
步骤 1:下载包
打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本
$ composer require tboileau/form-handler-bundle
此命令要求您全局安装 Composer,如 Composer 文档中的安装章节所述。
步骤 2:启用包
然后,通过将其添加到项目 config/bundles.php
文件中注册的包列表中来启用该包。
<?php // config/bundles.php return [ //... TBoileau\LifecycleBundle\TBoileauLifecycleBundle::class => ['all' => true], //... ];
创建一个新的生命周期管理器
步骤 1:使用生成器生成您的生命周期类
打开命令行,进入您的项目目录,并执行以下命令以生成新的生命周期管理器
$ php bin/console make:lifecycle FooLifecycle Please enter a new state : > name_of_your_state Please enter a new state : > created: src/Lifecycle/FooLifecycle.php Success !
步骤 2:配置您的新的生命周期管理器
不要忘记在 config/services.yaml
中配置您的新处理程序
services: # ... App\Lifecycle\FooLifecycle: tags: - { name: t_boileau.lifecycle }
步骤 3:编辑您的新的生命周期管理器
最重要的是在 getSubscribedStates
中添加状态并声明方法
<?php // App\Lifecycle\FooLifecycle.php namespace App\Lifecycle; use App\Entity\Foo; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Response; use TBoileau\LifecycleBundle\EventSubscriber\LifecycleSubscriber; class FooLifecycle extends LifecycleSubscriber { /** * @var EntityManagerInterface */ private $entityManager; /** * FooSubscriber constructor. * @param EntityManagerInterface $entityManager */ public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } /** * @return array */ public static function getSubscribedStates(): array { return [ "ADD" => "onAdd", ]; } /** * @param $object * @return Response */ public function onAdd(Foo $foo): Response { $this->entityManager->persist($foo); $this->entityManager->flush(); return $this->redirectToRoute("homepage"); } }
步骤 4:使用调度器
您只需调用调度器的 setState
方法来更改对象的状态
<?php namespace App\Controller; use App\Form\FooType; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use TBoileau\LifecycleBundle\Dispatcher; class FooController extends Controller { /** * @Route("/add", name="add") * @param Dispatcher $dispatcher * @param Request $request * @return Response */ public function show(Dispatcher $dispatcher, Request $request) { $form = $this->createForm(FooType::class)->handleRequest($request); if($form->isSubmitted() and $form->isValid()) { return $dispatcher->setState("ADD", $form->getData()); } return $this->render("foo/add.html.twig", ["form" => $form->createView()]); } }