Skip to content

Commit

Permalink
Merge pull request #1 from Mezcalito/feature/add-maker-command
Browse files Browse the repository at this point in the history
Add maker command for generate Search
  • Loading branch information
AlexandrePetrone authored Feb 18, 2025
2 parents ab8bd09 + c998d94 commit 41719d9
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ static: ## Run static analysis tools
$(PHP) -d memory_limit=-1 vendor/bin/rector

test: ## Run tests
$(DOCKER_COMP) exec -e XDEBUG_MODE=coverage -w /srv/app php vendor/bin/phpunit --coverage-html coverage
$(DOCKER_COMP) exec -e XDEBUG_MODE=coverage -w /srv/app php vendor/bin/phpunit --coverage-html coverage
146 changes: 7 additions & 139 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,153 +59,21 @@ You can also [create your own Adapter](docs/create-own-adapter.md) to use other
## Usage
To use the bundle, create your first `Search`. To do this, simply create a class and add the `AsSearch` attribute.
To use the bundle, create your first `Search`. To do this, just use the `make:search` command and follow indications.

```bash
php bin/console make:search
```

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.

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.

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.

### Create a search
```php
<?php
declare(strict_types=1);
namespace App\Search;
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
#[AsSearch('products')]
class ListingSearch extends AbstractSearch
{
}
```
### Add facets

If you wish, you can add facets to your Search. To do this, you need to use the `addFacet` method.
This method takes the following parameters:

| Parameter | Description | Type | Required |
|------------------|-----------------------------|--------|----------|
| property | Property name | string | ✅ |
| label | Label displayed | string | ✅ |
| displayComponent | FQCN of your Twig component | string | ❌ |
| props | Props to pass to component | array | ❌ |


```php
<?php
declare(strict_types=1);
namespace Mezcalito\UxSearchBundle\Tests\TestApplication\Search;
use Mezcalito\UxSearchBundle\Adapter\Meilisearch\MeilisearchAdapter;
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
use Mezcalito\UxSearchBundle\Search\Facet;
use Mezcalito\UxSearchBundle\Twig\Component\Facet\RangeInput;
#[AsSearch('products', name: 'listing', adapter: 'meilisearch')]
class MeilisearchSearch extends AbstractSearch
{
public function build(array $options = []): void
{
$this
->addFacet('type', 'Type')
->addFacet('price', 'Price', RangeInput::class)
->addFacet('price', 'Price', null, ['limit' => 20])
;
}
}
```

### Add sort

You can also add sorting options to your Search. To do this, you need to use the `addAvailableSort` method.
This method takes 2 mandatory parameters:

| Parameter | Description | Type |
|-----------|-----------------------------------------|---------|
| key | Attribute key and order separate by ':' | ?string |
| label | Label displayed | string |
After that, you have a fully functional simple search.
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.

If you do not specify a sort or if the key is empty, the default sorting of your adapter will be applied.

```php
use Mezcalito\UxSearchBundle\Search\Sort;
// ..
public function build(array $options = []): void
{
$this
// ..
->addAvailableSort(null, 'Relevancy')
->addAvailableSort('price:asc', 'Price ↑')
->addAvailableSort('price:desc', 'Price ↓')
;
}
```

### Add EventListener or EventSubscriber
For example, you can modify the `ResultSet` on the `PostSearchEvent` to enrich a `Hit` with data from database.

```php
use Mezcalito\UxSearchBundle\Event\PreSearchEvent;
use Mezcalito\UxSearchBundle\Event\PostSearchEvent;
// ..
public function build(array $options = []): void
{
$this
// ...
->addEventListener(PreSearchEvent::class, function (PreSearchEvent $event) {
// $event->getSearch();
// $event->getQuery();
})
->addEventListener(PostSearchEvent::class, function (PostSearchEvent $event) {
// $event->getSearch();
// $event->getQuery();
// $event->getResultSet();
})
->addEventSubscriber(YourEventSubscriber::cass)
;
}
```

### Enable urlRewriting and set up an urlFormater
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.

```php
public function build(array $options = []): void
{
$this
// ...
->enableUrlRewriting()
;
}
```

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.

```php
use App\Url\YourCustomUrlFormater;
// ...
public function build(array $options = []): void
{
$this
// ...
->enableUrlRewriting()
->setUrlFormater(YourCustomUrlFormater::class)
;
}
```

## Render a Search

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"rector/rector": "^1.2",
"symfony/asset-mapper": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/maker-bundle": "^1.62",
"symfony/runtime": "^6.4|^7.0",
"symfony/translation": "^7.2",
"symfony/web-profiler-bundle": "^6.4|^7.0"
Expand Down
3 changes: 3 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Mezcalito\UxSearchBundle\Adapter\Meilisearch\QueryBuilder;
use Mezcalito\UxSearchBundle\Context\ContextProvider;
use Mezcalito\UxSearchBundle\EventSubscriber\ContextSubscriber;
use Mezcalito\UxSearchBundle\Maker\MakeSearch;
use Mezcalito\UxSearchBundle\Search\Searcher;
use Mezcalito\UxSearchBundle\Search\SearchProvider;
use Mezcalito\UxSearchBundle\Search\Url\DefaultUrlFormater;
Expand Down Expand Up @@ -120,5 +121,7 @@
->set(DefaultUrlFormater::class)
->arg('$urlGenerator', service(UrlGeneratorInterface::class))
->tag('mezcalito_ux_search.url_formater')
->set('maker.maker.make_search', MakeSearch::class)
->tag('maker.command')
;
};
147 changes: 147 additions & 0 deletions docs/usage/customize-your-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Customize your search

> [!IMPORTANT]
> If you haven't used the maker, you first need to create a class and add the `AsSearch` attribute to it.
**Create a search**
```php
<?php

declare(strict_types=1);

namespace App\Search;

use Mezcalito\UxSearchBundle\Attribute\AsSearch;
use Mezcalito\UxSearchBundle\Search\AbstractSearch;

#[AsSearch('products')]
class ListingSearch extends AbstractSearch
{
}
```

**Now you can add plenty of features to your search.**

## Add facets

If you wish, you can add facets to your Search. To do this, you need to use the `addFacet` method.
This method takes the following parameters:

| Parameter | Description | Type | Required |
|------------------|-----------------------------|--------|----------|
| property | Property name | string ||
| label | Label displayed | string ||
| displayComponent | FQCN of your Twig component | string ||
| props | Props to pass to component | array ||


```php
<?php

declare(strict_types=1);

namespace Mezcalito\UxSearchBundle\Tests\TestApplication\Search;

use Mezcalito\UxSearchBundle\Adapter\Meilisearch\MeilisearchAdapter;
use Mezcalito\UxSearchBundle\Attribute\AsSearch;
use Mezcalito\UxSearchBundle\Search\AbstractSearch;
use Mezcalito\UxSearchBundle\Search\Facet;
use Mezcalito\UxSearchBundle\Twig\Component\Facet\RangeInput;

#[AsSearch('products', name: 'listing', adapter: 'meilisearch')]
class MeilisearchSearch extends AbstractSearch
{
public function build(array $options = []): void
{
$this
->addFacet('type', 'Type')
->addFacet('price', 'Price', RangeInput::class)
->addFacet('price', 'Price', null, ['limit' => 20])
;
}
}
```

## Add sort

You can also add sorting options to your Search. To do this, you need to use the `addAvailableSort` method.
This method takes 2 mandatory parameters:

| Parameter | Description | Type |
|-----------|-----------------------------------------|---------|
| key | Attribute key and order separate by ':' | ?string |
| label | Label displayed | string |

If you do not specify a sort or if the key is empty, the default sorting of your adapter will be applied.

```php
use Mezcalito\UxSearchBundle\Search\Sort;

// ..

public function build(array $options = []): void
{
$this
// ..
->addAvailableSort(null, 'Relevancy')
->addAvailableSort('price:asc', 'Price ↑')
->addAvailableSort('price:desc', 'Price ↓')
;
}
```

## Add EventListener or EventSubscriber
For example, you can modify the `ResultSet` on the `PostSearchEvent` to enrich a `Hit` with data from database.

```php
use Mezcalito\UxSearchBundle\Event\PreSearchEvent;
use Mezcalito\UxSearchBundle\Event\PostSearchEvent;
// ..

public function build(array $options = []): void
{
$this
// ...
->addEventListener(PreSearchEvent::class, function (PreSearchEvent $event) {
// $event->getSearch();
// $event->getQuery();
})
->addEventListener(PostSearchEvent::class, function (PostSearchEvent $event) {
// $event->getSearch();
// $event->getQuery();
// $event->getResultSet();
})
->addEventSubscriber(YourEventSubscriber::cass)
;
}
```

## Enable urlRewriting and set up an urlFormater
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.

```php

public function build(array $options = []): void
{
$this
// ...
->enableUrlRewriting()
;
}
```

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.

```php
use App\Url\YourCustomUrlFormater;
// ...

public function build(array $options = []): void
{
$this
// ...
->enableUrlRewriting()
->setUrlFormater(YourCustomUrlFormater::class)
;
}
```
Loading

0 comments on commit 41719d9

Please sign in to comment.