-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to disable the global search by admin
- Loading branch information
1 parent
fd2cd04
commit 78b1db8
Showing
7 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/DependencyInjection/Compiler/AdminSearchCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\DependencyInjection\Compiler; | ||
|
||
use Sonata\AdminBundle\Admin\AdminInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
|
||
/** | ||
* This class configures which admins must be considered for global search at `SearchHandler`. | ||
* | ||
* @author Javier Spagnoletti <[email protected]> | ||
*/ | ||
final class AdminSearchCompilerPass implements CompilerPassInterface | ||
{ | ||
public const TAG_ATTRIBUTE_TOGGLE_SEARCH = 'global_search'; | ||
|
||
private const TAG_ADMIN = 'sonata.admin'; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition('sonata.admin.search.handler')) { | ||
return; | ||
} | ||
|
||
$adminSearch = []; | ||
|
||
foreach ($container->findTaggedServiceIds(self::TAG_ADMIN) as $id => $tags) { | ||
$this->validateAdminClass($container, $id); | ||
|
||
foreach ($tags as $attributes) { | ||
$globalSearch = $this->getGlobalSearchValue($attributes, $id); | ||
|
||
if (null === $globalSearch) { | ||
continue; | ||
} | ||
|
||
$adminSearch[$id] = $globalSearch; | ||
} | ||
} | ||
|
||
$searchHandlerDefinition = $container->getDefinition('sonata.admin.search.handler'); | ||
$searchHandlerDefinition->addMethodCall('configureAdminSearch', [$adminSearch]); | ||
} | ||
|
||
/** | ||
* @throws LogicException if the class in the given service definition is not | ||
* a subclass of `AdminInterface` | ||
*/ | ||
private function validateAdminClass(ContainerBuilder $container, string $id): void | ||
{ | ||
$definition = $container->getDefinition($id); | ||
|
||
// Trim possible parameter delimiters ("%") from the class name. | ||
$adminClass = trim($definition->getClass(), '%'); | ||
if (!class_exists($adminClass) && $container->hasParameter($adminClass)) { | ||
$adminClass = $container->getParameter($adminClass); | ||
} | ||
|
||
if (!is_subclass_of($adminClass, AdminInterface::class)) { | ||
throw new LogicException(sprintf( | ||
'Service "%s" must implement `%s`.', | ||
$id, | ||
AdminInterface::class | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $attributes | ||
* | ||
* @throws LogicException if the attribute value is not of type boolean | ||
*/ | ||
private function getGlobalSearchValue(array $attributes, string $id): ?bool | ||
{ | ||
$globalSearch = $attributes[self::TAG_ATTRIBUTE_TOGGLE_SEARCH] ?? null; | ||
|
||
if (null === $globalSearch) { | ||
return null; | ||
} | ||
|
||
if (!\is_bool($globalSearch)) { | ||
throw new LogicException(sprintf( | ||
'Attribute "%s" in tag "%s" at service "%s" must be of type boolean, "%s" given.', | ||
self::TAG_ATTRIBUTE_TOGGLE_SEARCH, | ||
self::TAG_ADMIN, | ||
$id, | ||
\gettype($globalSearch) | ||
)); | ||
} | ||
|
||
return $globalSearch; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/DependencyInjection/Compiler/AdminSearchCompilerPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminSearchCompilerPass; | ||
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* @author Javier Spagnoletti <[email protected]> | ||
*/ | ||
final class AdminSearchCompilerPassTest extends AbstractCompilerPassTestCase | ||
{ | ||
public function testProcess(): void | ||
{ | ||
$adminFooDefinition = new Definition(PostAdmin::class); | ||
$adminFooDefinition->addTag('sonata.admin', ['global_search' => true]); | ||
$this->setDefinition('admin.foo', $adminFooDefinition); | ||
|
||
$adminBarDefinition = new Definition(PostAdmin::class); | ||
$adminBarDefinition->addTag('sonata.admin', ['global_search' => false]); | ||
$this->setDefinition('admin.bar', $adminBarDefinition); | ||
|
||
$adminBazDefinition = new Definition(PostAdmin::class); | ||
$adminBazDefinition->addTag('sonata.admin', ['some_attribute' => 42]); | ||
$this->setDefinition('admin.baz', $adminBazDefinition); | ||
|
||
$searchHandlerDefinition = new Definition(); | ||
$this->setDefinition('sonata.admin.search.handler', $searchHandlerDefinition); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall( | ||
'sonata.admin.search.handler', | ||
'configureAdminSearch', | ||
[['admin.foo' => true, 'admin.bar' => false]] | ||
); | ||
} | ||
|
||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new AdminSearchCompilerPass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters