gnugat/query-bus-bundle

此软件包已被 弃用 且不再维护。作者建议使用 league/tactician-bundle 软件包代替。

Symfony 中的 QueryBus 集成

安装: 35

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 0

类型:symfony-bundle

v2.0.0 2015-05-31 07:32 UTC

This package is auto-updated.

Last update: 2022-02-01 12:47:57 UTC


README

QueryBusSymfony 中的集成。

安装

可以使用 Composer 安装 QueryBusBundle

composer require "gnugat/query-bus-bundle:~2.0"

然后需要在我们的应用程序中注册它

<?php
// File: app/AppKernel.php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Gnugat\QueryBusBundle\GnugatQueryBusBundle(),
        );
        // ...
    }

    // ...
}

使用示例

让我们看一下以下实体

<?php
// File: src/AppBundle/Entity/Article.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="article")
 */
class Article
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    public function __construct($title, $content)
    {
        $this->title = $title;
        $this->content = $content;
    }

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

    public function getTitle()
    {
        return $this->title;
    }

    public function getContent()
    {
        return $this->content;
    }
}

为了使用 QueryBundle 通过 ID 获取一篇文章,我们首先需要创建一个 询问消息

<?php
// File: src/AppBundle/QueryBus/GetArticle.php

namespace AppBundle\QueryBus;

class GetArticle
{
    public $id;

    public function __construct($id)
    {
        if (null === $id) {
            throw new \InvalidArgumentException('Missing required argument: ID');
        }
        $this->id = $id;
    }
}

然后我们需要创建一个 QueryMatcher

<?php
// File: src/AppBundle/Marshaller/ArticleMarshaller.php

namespace AppBundle\QueryBus;

use AppBundle\Entity\Article;
use Doctrine\Common\Persistence\ObjectManager;
use Gnugat\QueryBus\QueryMatcher;

class GetArticleMatcher implements QueryMatcher
{
    private $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function supports($query)
    {
        return $query instanceof GetArticle;
    }

    public function match($query)
    {
        $article = $this->objectManager->find('AppBundle:Article', $query->id);
        if (null === $article) {
            throw new \DomainException(sprintf('Could not find article for ID "%s"', $query->id));
        }

        return $article;
    }
}

下一步是将它定义为服务

# File: app/config/services.yml
services:
    app.get_article_matcher:
        class: AppBundle\QueryBus\GetArticleMatcher
        tags:
            - { name: gnugat_query_bus.query_matcher }

注意:由于有 gnugat_query_bus.query_matcher 标签,GetArticleMatcher 将被注册到主要的 gnugat_query_bus.query_bus 服务中。

最后,我们可以请求文章

<?php
// File: src/AppBundle/Controller/ArticleController.php

namespace AppBundle\Controller;

use AppBundle\QueryBus\GetArticle;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

class ArtcileController extends Controller
{
    /**
     * @Route("/api/v1/articles/{id}")
     * @Method({"GET"})
     */
    public function viewAction($id)
    {
        $article = $this->get('gnugat_query_bus.query_bus')->match(new GetArticle($id));

        return new JsonResponse(array(
            'id' => $article->getId(),
            'title' => $article->getTitle(),
            'content' => $article->getContent(),
        ), 200);
    }
}

更多文档

您可以使用以下方法查看当前和过去的版本:

您可以在以下链接中找到更多文档: