gbprod / algolia-specification-bundle
此包已被废弃,不再维护。未建议替代包。
此包集成了 algolia-specification 与 Symfony
v2.1.0
2018-02-26 08:29 UTC
Requires
- php: ^7.0
- gbprod/algolia-specification: ^2.0
- symfony/expression-language: ^2.|^3.1|^4.0
- symfony/framework-bundle: ^2.|^3.1|^4.0
- symfony/yaml: ^2.|^3.1|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0
This package is auto-updated.
Last update: 2021-08-07 10:28:55 UTC
README
此包集成了 algolia-specification 与 Symfony。
安装
使用 composer 下载包
composer require gbprod/algolia-specification-bundle
在您的 app/AppKernel.php
文件中声明
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new GBProd\AlgoliaSpecificationBundle\AlgoliaSpecificationBundle(), // ... ); }
创建您的规范和查询工厂
查看 Specification 和 Algolia Specification 库以获取更多信息。
创建规范
<?php namespace GBProd\Acme\CoreDomain\Specification\Product; use GBProd\Specification\Specification; class IsAvailable implements Specification { public function isSatisfiedBy($candidate) { return $candidate->isSellable() && $candidate->expirationDate() > new \DateTime('now') ; } }
创建查询工厂
<?php namespace GBProd\Acme\Infrastructure\Algolia\QueryFactory\Product; use GBProd\AlgoliaSpecification\QueryFactory\Factory; use GBProd\Specification\Specification; use Algolia\QueryBuilder; class IsAvailableFactory implements Factory { public function create(Specification $spec) { return 'available=1'; } }
配置
声明您的工厂
# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml services: acme.algolia.query_factory.is_available: class: GBProd\Acme\Infrastructure\Algolia\QueryFactory\Product\IsAvailableFactory tags: - { name: algolia.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\AlgoliaProductRepository arguments: - "@algolia.client" - "@gbprod.algolia_specification_handler"
<?php namespace GBProd\Acme\Infrastructure\Product; use Algolia\Client; use GBProd\AlgoliaSpecification\Handler; use GBProd\Specification\Specification; class AlgoliaProductRepository implements ProductRepository { private $client; private $handler; public function __construct(Client $em, Handler $handler) { $this->client = $client; $this->handler = $handler; } public function findSatisfying(Specification $specification) { $index = $this->client->initIndex('products'); $query = $this->handler->handle($specification); return $type->search(['filters' => $query]); } }
用法
<?php $products = $productRepository->findSatisfying( new AndX( new IsAvailable(), new IsLowStock() ) );