Skip to content

Commit 41719d9

Browse files
Merge pull request #1 from Mezcalito/feature/add-maker-command
Add maker command for generate Search
2 parents ab8bd09 + c998d94 commit 41719d9

File tree

10 files changed

+433
-140
lines changed

10 files changed

+433
-140
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ static: ## Run static analysis tools
5454
$(PHP) -d memory_limit=-1 vendor/bin/rector
5555

5656
test: ## Run tests
57-
$(DOCKER_COMP) exec -e XDEBUG_MODE=coverage -w /srv/app php vendor/bin/phpunit --coverage-html coverage
57+
$(DOCKER_COMP) exec -e XDEBUG_MODE=coverage -w /srv/app php vendor/bin/phpunit --coverage-html coverage

README.md

+7-139
Original file line numberDiff line numberDiff line change
@@ -59,153 +59,21 @@ You can also [create your own Adapter](docs/create-own-adapter.md) to use other
5959
6060
## Usage
6161
62-
To use the bundle, create your first `Search`. To do this, simply create a class and add the `AsSearch` attribute.
62+
To use the bundle, create your first `Search`. To do this, just use the `make:search` command and follow indications.
63+
64+
```bash
65+
php bin/console make:search
66+
```
6367

6468
In the case of Meilisearch, you need to specify the name of the index to use, and for Doctrine, the FQCN of the entity to use.
6569

6670
By default, the name of your search will be the name of your class with the `Search` suffix removed. You can change this by specifying a custom name.
6771

6872
Also, by default, the Adapter used is the one specified in the configuration under `default_adapter`. You can specify the name of the Adapter to use, for example, if you have multiple `Search` instances that use different Adapters.
6973

70-
### Create a search
71-
```php
72-
<?php
73-
74-
declare(strict_types=1);
75-
76-
namespace App\Search;
77-
78-
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
79-
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
80-
81-
#[AsSearch('products')]
82-
class ListingSearch extends AbstractSearch
83-
{
84-
}
85-
```
86-
### Add facets
87-
88-
If you wish, you can add facets to your Search. To do this, you need to use the `addFacet` method.
89-
This method takes the following parameters:
90-
91-
| Parameter | Description | Type | Required |
92-
|------------------|-----------------------------|--------|----------|
93-
| property | Property name | string | ✅ |
94-
| label | Label displayed | string | ✅ |
95-
| displayComponent | FQCN of your Twig component | string | ❌ |
96-
| props | Props to pass to component | array | ❌ |
97-
98-
99-
```php
100-
<?php
101-
102-
declare(strict_types=1);
103-
104-
namespace Mezcalito\UxSearchBundle\Tests\TestApplication\Search;
105-
106-
use Mezcalito\UxSearchBundle\Adapter\Meilisearch\MeilisearchAdapter;
107-
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
108-
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
109-
use Mezcalito\UxSearchBundle\Search\Facet;
110-
use Mezcalito\UxSearchBundle\Twig\Component\Facet\RangeInput;
111-
112-
#[AsSearch('products', name: 'listing', adapter: 'meilisearch')]
113-
class MeilisearchSearch extends AbstractSearch
114-
{
115-
public function build(array $options = []): void
116-
{
117-
$this
118-
->addFacet('type', 'Type')
119-
->addFacet('price', 'Price', RangeInput::class)
120-
->addFacet('price', 'Price', null, ['limit' => 20])
121-
;
122-
}
123-
}
124-
```
125-
126-
### Add sort
127-
128-
You can also add sorting options to your Search. To do this, you need to use the `addAvailableSort` method.
129-
This method takes 2 mandatory parameters:
130-
131-
| Parameter | Description | Type |
132-
|-----------|-----------------------------------------|---------|
133-
| key | Attribute key and order separate by ':' | ?string |
134-
| label | Label displayed | string |
74+
After that, you have a fully functional simple search.
75+
Feel free to check the documentation to [customize your search](docs/usage/customize-your-search.md) (adding facets, sorting, ...) or if you prefer not to use the maker.
13576

136-
If you do not specify a sort or if the key is empty, the default sorting of your adapter will be applied.
137-
138-
```php
139-
use Mezcalito\UxSearchBundle\Search\Sort;
140-
141-
// ..
142-
143-
public function build(array $options = []): void
144-
{
145-
$this
146-
// ..
147-
->addAvailableSort(null, 'Relevancy')
148-
->addAvailableSort('price:asc', 'Price ↑')
149-
->addAvailableSort('price:desc', 'Price ↓')
150-
;
151-
}
152-
```
153-
154-
### Add EventListener or EventSubscriber
155-
For example, you can modify the `ResultSet` on the `PostSearchEvent` to enrich a `Hit` with data from database.
156-
157-
```php
158-
use Mezcalito\UxSearchBundle\Event\PreSearchEvent;
159-
use Mezcalito\UxSearchBundle\Event\PostSearchEvent;
160-
// ..
161-
162-
public function build(array $options = []): void
163-
{
164-
$this
165-
// ...
166-
->addEventListener(PreSearchEvent::class, function (PreSearchEvent $event) {
167-
// $event->getSearch();
168-
// $event->getQuery();
169-
})
170-
->addEventListener(PostSearchEvent::class, function (PostSearchEvent $event) {
171-
// $event->getSearch();
172-
// $event->getQuery();
173-
// $event->getResultSet();
174-
})
175-
->addEventSubscriber(YourEventSubscriber::cass)
176-
;
177-
}
178-
```
179-
180-
### Enable urlRewriting and set up an urlFormater
181-
It is possible to enable a URL rewriting system to allow sharing of configured search URLs. To do this, simply add the `->enableUrlRewriting` method. By default, a `DefaultUrlFormater` is provided in the bundle. This allows you to add query parameters with the values of the selected facets.
182-
183-
```php
184-
185-
public function build(array $options = []): void
186-
{
187-
$this
188-
// ...
189-
->enableUrlRewriting()
190-
;
191-
}
192-
```
193-
194-
You can also create your own UrlFormater. To do so, you need to implement the `UrlFormaterInterface` and define your own logic in the `generateUrl` and `applyFilters` methods. All that is left is to use it in a search via the `->setUrlFormater()` method.
195-
196-
```php
197-
use App\Url\YourCustomUrlFormater;
198-
// ...
199-
200-
public function build(array $options = []): void
201-
{
202-
$this
203-
// ...
204-
->enableUrlRewriting()
205-
->setUrlFormater(YourCustomUrlFormater::class)
206-
;
207-
}
208-
```
20977

21078
## Render a Search
21179

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"rector/rector": "^1.2",
3333
"symfony/asset-mapper": "^6.4|^7.0",
3434
"symfony/framework-bundle": "^6.4|^7.0",
35+
"symfony/maker-bundle": "^1.62",
3536
"symfony/runtime": "^6.4|^7.0",
3637
"symfony/translation": "^7.2",
3738
"symfony/web-profiler-bundle": "^6.4|^7.0"

config/services.php

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Mezcalito\UxSearchBundle\Adapter\Meilisearch\QueryBuilder;
2121
use Mezcalito\UxSearchBundle\Context\ContextProvider;
2222
use Mezcalito\UxSearchBundle\EventSubscriber\ContextSubscriber;
23+
use Mezcalito\UxSearchBundle\Maker\MakeSearch;
2324
use Mezcalito\UxSearchBundle\Search\Searcher;
2425
use Mezcalito\UxSearchBundle\Search\SearchProvider;
2526
use Mezcalito\UxSearchBundle\Search\Url\DefaultUrlFormater;
@@ -120,5 +121,7 @@
120121
->set(DefaultUrlFormater::class)
121122
->arg('$urlGenerator', service(UrlGeneratorInterface::class))
122123
->tag('mezcalito_ux_search.url_formater')
124+
->set('maker.maker.make_search', MakeSearch::class)
125+
->tag('maker.command')
123126
;
124127
};

docs/usage/customize-your-search.md

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Customize your search
2+
3+
> [!IMPORTANT]
4+
> If you haven't used the maker, you first need to create a class and add the `AsSearch` attribute to it.
5+
6+
**Create a search**
7+
```php
8+
<?php
9+
10+
declare(strict_types=1);
11+
12+
namespace App\Search;
13+
14+
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
15+
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
16+
17+
#[AsSearch('products')]
18+
class ListingSearch extends AbstractSearch
19+
{
20+
}
21+
```
22+
23+
**Now you can add plenty of features to your search.**
24+
25+
## Add facets
26+
27+
If you wish, you can add facets to your Search. To do this, you need to use the `addFacet` method.
28+
This method takes the following parameters:
29+
30+
| Parameter | Description | Type | Required |
31+
|------------------|-----------------------------|--------|----------|
32+
| property | Property name | string ||
33+
| label | Label displayed | string ||
34+
| displayComponent | FQCN of your Twig component | string ||
35+
| props | Props to pass to component | array ||
36+
37+
38+
```php
39+
<?php
40+
41+
declare(strict_types=1);
42+
43+
namespace Mezcalito\UxSearchBundle\Tests\TestApplication\Search;
44+
45+
use Mezcalito\UxSearchBundle\Adapter\Meilisearch\MeilisearchAdapter;
46+
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
47+
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
48+
use Mezcalito\UxSearchBundle\Search\Facet;
49+
use Mezcalito\UxSearchBundle\Twig\Component\Facet\RangeInput;
50+
51+
#[AsSearch('products', name: 'listing', adapter: 'meilisearch')]
52+
class MeilisearchSearch extends AbstractSearch
53+
{
54+
public function build(array $options = []): void
55+
{
56+
$this
57+
->addFacet('type', 'Type')
58+
->addFacet('price', 'Price', RangeInput::class)
59+
->addFacet('price', 'Price', null, ['limit' => 20])
60+
;
61+
}
62+
}
63+
```
64+
65+
## Add sort
66+
67+
You can also add sorting options to your Search. To do this, you need to use the `addAvailableSort` method.
68+
This method takes 2 mandatory parameters:
69+
70+
| Parameter | Description | Type |
71+
|-----------|-----------------------------------------|---------|
72+
| key | Attribute key and order separate by ':' | ?string |
73+
| label | Label displayed | string |
74+
75+
If you do not specify a sort or if the key is empty, the default sorting of your adapter will be applied.
76+
77+
```php
78+
use Mezcalito\UxSearchBundle\Search\Sort;
79+
80+
// ..
81+
82+
public function build(array $options = []): void
83+
{
84+
$this
85+
// ..
86+
->addAvailableSort(null, 'Relevancy')
87+
->addAvailableSort('price:asc', 'Price ↑')
88+
->addAvailableSort('price:desc', 'Price ↓')
89+
;
90+
}
91+
```
92+
93+
## Add EventListener or EventSubscriber
94+
For example, you can modify the `ResultSet` on the `PostSearchEvent` to enrich a `Hit` with data from database.
95+
96+
```php
97+
use Mezcalito\UxSearchBundle\Event\PreSearchEvent;
98+
use Mezcalito\UxSearchBundle\Event\PostSearchEvent;
99+
// ..
100+
101+
public function build(array $options = []): void
102+
{
103+
$this
104+
// ...
105+
->addEventListener(PreSearchEvent::class, function (PreSearchEvent $event) {
106+
// $event->getSearch();
107+
// $event->getQuery();
108+
})
109+
->addEventListener(PostSearchEvent::class, function (PostSearchEvent $event) {
110+
// $event->getSearch();
111+
// $event->getQuery();
112+
// $event->getResultSet();
113+
})
114+
->addEventSubscriber(YourEventSubscriber::cass)
115+
;
116+
}
117+
```
118+
119+
## Enable urlRewriting and set up an urlFormater
120+
It is possible to enable a URL rewriting system to allow sharing of configured search URLs. To do this, simply add the `->enableUrlRewriting` method. By default, a `DefaultUrlFormater` is provided in the bundle. This allows you to add query parameters with the values of the selected facets.
121+
122+
```php
123+
124+
public function build(array $options = []): void
125+
{
126+
$this
127+
// ...
128+
->enableUrlRewriting()
129+
;
130+
}
131+
```
132+
133+
You can also create your own UrlFormater. To do so, you need to implement the `UrlFormaterInterface` and define your own logic in the `generateUrl` and `applyFilters` methods. All that is left is to use it in a search via the `->setUrlFormater()` method.
134+
135+
```php
136+
use App\Url\YourCustomUrlFormater;
137+
// ...
138+
139+
public function build(array $options = []): void
140+
{
141+
$this
142+
// ...
143+
->enableUrlRewriting()
144+
->setUrlFormater(YourCustomUrlFormater::class)
145+
;
146+
}
147+
```

0 commit comments

Comments
 (0)