-
-
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
Showing
7 changed files
with
289 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
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
<?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 | ||
{ | ||
$adminSearch = []; | ||
|
||
foreach ($container->findTaggedServiceIds(self::TAG_ADMIN) as $id => $tags) { | ||
$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 | ||
)); | ||
} | ||
|
||
foreach ($tags as $attributes) { | ||
if (!isset($attributes[self::TAG_ATTRIBUTE_TOGGLE_SEARCH])) { | ||
continue; | ||
} | ||
|
||
$globalSearch = $attributes[self::TAG_ATTRIBUTE_TOGGLE_SEARCH]; | ||
|
||
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) | ||
)); | ||
} | ||
|
||
$adminSearch[$id] = $globalSearch; | ||
} | ||
} | ||
|
||
$searchHandlerDefinition = $container->getDefinition('sonata.admin.search.handler'); | ||
$searchHandlerDefinition->addMethodCall('configureAdminSearch', [$adminSearch]); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
<?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 PHPUnit\Framework\TestCase; | ||
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 TestCase | ||
{ | ||
public function testProcess(): void | ||
{ | ||
$containerBuilderMock = $this->createMock(ContainerBuilder::class); | ||
$containerBuilderMock->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with($this->equalTo('sonata.admin')) | ||
->willReturn([ | ||
'admin.foo' => [ | ||
['global_search' => true], | ||
], | ||
'admin.bar' => [ | ||
['global_search' => false], | ||
], | ||
'admin.baz' => [ | ||
['some_attribute' => 42], | ||
], | ||
]); | ||
|
||
$adminFooDefinition = $this->createMock(Definition::class); | ||
$adminFooDefinition->expects($this->once()) | ||
->method('getClass') | ||
->willReturn(PostAdmin::class); | ||
|
||
$adminBarDefinition = $this->createMock(Definition::class); | ||
$adminBarDefinition->expects($this->once()) | ||
->method('getClass') | ||
->willReturn(PostAdmin::class); | ||
|
||
$adminBazDefinition = $this->createMock(Definition::class); | ||
$adminBazDefinition->expects($this->once()) | ||
->method('getClass') | ||
->willReturn(PostAdmin::class); | ||
|
||
$searchHandlerDefinition = $this->createMock(Definition::class); | ||
$searchHandlerDefinition->expects($this->once()) | ||
->method('addMethodCall') | ||
->with('configureAdminSearch', [['admin.foo' => true, 'admin.bar' => false]]); | ||
|
||
$containerBuilderMock | ||
->method('getDefinition') | ||
->willReturnMap([ | ||
['admin.foo', $adminFooDefinition], | ||
['admin.bar', $adminBarDefinition], | ||
['admin.baz', $adminBazDefinition], | ||
['sonata.admin.search.handler', $searchHandlerDefinition], | ||
]); | ||
|
||
(new AdminSearchCompilerPass())->process($containerBuilderMock); | ||
} | ||
} |
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
Oops, something went wrong.