gbprod/elastica-specification-bundle

此包已被废弃,不再维护。没有建议的替代包。

此包集成了 elastica-specification 和 Symfony

v2.1.0 2018-02-08 07:17 UTC

This package is auto-updated.

Last update: 2022-03-07 11:53:05 UTC


README

此包将 elastica-specification 与 Symfony 集成。

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

安装

使用 composer 下载包

composer require gbprod/elastica-specification-bundle

在您的 app/AppKernel.php 文件中声明

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new GBProd\ElasticaSpecificationBundle\ElasticaSpecificationBundle(),
        // ...
    );
}

创建您的规范和查询工厂

查看 规范Elastica 规范 库以获取更多信息。

创建规范

<?php

namespace GBProd\Acme\CoreDomain\Specification\Product;

use GBProd\Specification\Specification;

class IsAvailable implements Specification
{
    public function isSatisfiedBy($candidate): bool
    {
        return $candidate->isSellable()
            && $candidate->expirationDate() > new \DateTime('now')
        ;
    }
}

创建查询工厂

<?php

namespace GBProd\Acme\Infrastructure\Elastica\QueryFactory\Product;

use GBProd\ElasticaSpecification\QueryFactory\Factory;
use GBProd\Specification\Specification;
use Elastica\QueryBuilder;

class IsAvailableFactory implements Factory
{
    public function build(Specification $spec, QueryBuilder $qb)
    {
        return $qb->query()->bool()
            ->addMust()
                $qb->query()->term(['available' => "0"]),
            )
        ;
    }
}

配置

声明您的工厂

# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml

services:
    acme.elastica.query_factory.is_available:
        class: GBProd\Acme\Infrastructure\Elastica\QueryFactory\Product\IsAvailableFactory
        tags:
            - { name: elastica.query_factory, specification: GBProd\Acme\CoreDomain\Specification\Product\IsAvailable }

在您的仓库类中注入处理器

# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml

services:
    acme.product_repository:
        class: GBProd\Acme\Infrastructure\Product\ElasticaProductRepository
        arguments:
            - "@elastica.client"
            - "@gbprod.elastica_specification_handler"
<?php

namespace GBProd\Acme\Infrastructure\Product;

use Elastica\Client;
use Elastica\QueryBuilder;
use GBProd\ElasticaSpecification\Handler;
use GBProd\Specification\Specification;

class ElasticaProductRepository implements ProductRepository
{
    private $client;

    private $handler;

    public function __construct(Client $em, Handler $handler)
    {
        $this->client  = $client;
        $this->handler = $handler;
    }

    public function findSatisfying(Specification $specification)
    {
        $type = $this
            ->getIndex('catalog')
            ->getType('product')
        ;

        $query = $this->handler->handle($specification, new QueryBuilder());

        return $type->search($query);
    }
}

用法

<?php

$products = $productRepository->findSatisfying(
    new AndX(
        new IsAvailable(),
        new IsLowStock()
    )
);

Elastica 配置

您可以使用 dsl_version 配置节点指定 Elastica DSL 版本(默认:最新版)。要使用不同版本,只需将其设置为版本类名

# app/config/config.yml
elastica_specification_bundle:
    dsl_version: Version120