-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to disable the global search by admin #6609
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you try to extract some smaller methods to reduce complexity?