Skip to content
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

Add support for global admin extensions via config #6651

Merged
merged 1 commit into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Full Configuration Options

# Prototype
id:
global: false
admins: []
excludes: []
implements: []
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Using ``config/packages/sonata_admin.yaml`` file has some advantages, it allows
extra options you can use to wire your extensions in a more dynamic way. This means you can change the behavior of all
admins that manage a class of a specific type.

global:
adds the extension to all admins.

admins:
specify one or more admin service ids to which the Extension should be added

Expand Down Expand Up @@ -115,6 +118,7 @@ priority:
sonata_admin:
extensions:
app.publish.extension:
global: true
admins:
- app.admin.article
implements:
Expand Down
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
<code>new ArrayCollection(\array_slice($this-&gt;results, 0, $this-&gt;getMaxPerPage()))</code>
</InvalidPropertyAssignmentValue>
</file>
<file src="src/DependencyInjection/Compiler/ExtensionCompilerPass.php">
<InvalidReturnStatement occurrences="1">
<code>$extensionMap</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="1">
<code>array&lt;string, array&lt;string, array&lt;string, array&lt;string, array&lt;string, string&gt;&gt;&gt;&gt;&gt;</code>
</InvalidReturnType>
</file>
<file src="src/Translator/Extractor/JMSTranslatorBundle/AdminExtractor.php">
<InvalidReturnType occurrences="2">
<code>buildSecurityInformation</code>
Expand Down
12 changes: 11 additions & 1 deletion src/DependencyInjection/Compiler/ExtensionCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ protected function getManagedClass(Definition $admin, ContainerBuilder $containe
}

/**
* @param array<string, array<string, array<string, string>>> $config
* @param array<string, array<string, array<string, string>|bool>> $config
*
* @return array<string, array<string, array<string, array<string, array<string, string>>>>> an array with the following structure.
*
* [
* 'global' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]],
* 'excludes' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]],
* 'admins' => ['<admin_id>' => ['<extension_id>' => ['priority' => <int>]]],
* 'implements' => ['<interface>' => ['<extension_id>' => ['priority' => <int>]]],
Expand All @@ -193,6 +194,7 @@ protected function getManagedClass(Definition $admin, ContainerBuilder $containe
protected function flattenExtensionConfiguration(array $config)
{
$extensionMap = [
'global' => [],
'excludes' => [],
'admins' => [],
'implements' => [],
Expand All @@ -202,6 +204,12 @@ protected function flattenExtensionConfiguration(array $config)
];

foreach ($config as $extension => $options) {
if (true === $options['global']) {
$options['global'] = [$extension];
} else {
$options['global'] = [];
}

$optionsMap = array_intersect_key($options, $extensionMap);

foreach ($optionsMap as $key => $value) {
Expand Down Expand Up @@ -244,6 +252,8 @@ private function isSubtypeOf(string $type, string $subject, string $class): bool
$classReflection = new \ReflectionClass($class);

switch ($type) {
case 'global':
return true;
case 'instanceof':
$subjectReflection = new \ReflectionClass($subject);

Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,15 @@ public function getConfigTreeBuilder()

->arrayNode('extensions')
->useAttributeAsKey('id')
->defaultValue(['admins' => [], 'excludes' => [], 'implements' => [], 'extends' => [], 'instanceof' => [], 'uses' => []])
->defaultValue([])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config was wrong for many years. If you do not have any extension, you should not register any invalid extension config.

E.g. the 'admins' => [] node tries to register something like this:

        sonata_admin:
            extensions:
                admins: []

The correct config should be something like this:

        sonata_admin:
            extensions:
                app.publish.extension:
                    global: true
                    admins:
                        - app.admin.article
                    implements:
                        - App\Publish\PublishStatusInterface
                    excludes:
                        - app.admin.blog
                        - app.admin.news
                    extends:
                        - App\Document\Blog
                    instanceof:
                        -  App\Document\Page
                    uses:
                        -  App\Trait\Timestampable

->prototype('array')
->fixXmlConfig('admin')
->fixXmlConfig('exclude')
->fixXmlConfig('implement')
->fixXmlConfig('extend')
->fixXmlConfig('use')
->children()
->booleanNode('global')->defaultValue(false)->end()
->arrayNode('admins')
->prototype('scalar')->end()
->end()
Expand Down
36 changes: 21 additions & 15 deletions tests/DependencyInjection/Compiler/ExtensionCompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ public function testAdminExtensionLoad(): void
$this->assertTrue($container->hasParameter(sprintf('%s.extension.map', $this->root)));
$this->assertIsArray($extensionMap = $container->getParameter(sprintf('%s.extension.map', $this->root)));

$this->assertArrayHasKey('admins', $extensionMap);
$this->assertArrayHasKey('excludes', $extensionMap);
$this->assertArrayHasKey('implements', $extensionMap);
$this->assertArrayHasKey('extends', $extensionMap);
$this->assertArrayHasKey('instanceof', $extensionMap);
$this->assertArrayHasKey('uses', $extensionMap);
$this->assertSame([], $extensionMap);
}

/**
Expand All @@ -106,6 +101,7 @@ public function testFlattenEmptyExtensionConfiguration(): void
$this->assertArrayHasKey('instanceof', $extensionMap);
$this->assertArrayHasKey('uses', $extensionMap);

$this->assertEmpty($extensionMap['global']);
$this->assertEmpty($extensionMap['admins']);
$this->assertEmpty($extensionMap['excludes']);
$this->assertEmpty($extensionMap['implements']);
Expand Down Expand Up @@ -245,6 +241,7 @@ public function testProcess(): void
$extensionsPass->process($container);
$container->compile();

$this->assertTrue($container->hasDefinition('sonata_extension_global'));
$this->assertTrue($container->hasDefinition('sonata_extension_publish'));
$this->assertTrue($container->hasDefinition('sonata_extension_history'));
$this->assertTrue($container->hasDefinition('sonata_extension_order'));
Expand All @@ -255,6 +252,7 @@ public function testProcess(): void
$this->assertTrue($container->hasDefinition('sonata_article_admin'));
$this->assertTrue($container->hasDefinition('sonata_news_admin'));

$globalExtension = $container->get('sonata_extension_global');
$securityExtension = $container->get('sonata_extension_security');
$publishExtension = $container->get('sonata_extension_publish');
$historyExtension = $container->get('sonata_extension_history');
Expand All @@ -263,28 +261,31 @@ public function testProcess(): void

$def = $container->get('sonata_post_admin');
$extensions = $def->getExtensions();
$this->assertCount(4, $extensions);
$this->assertCount(5, $extensions);

$this->assertSame($historyExtension, $extensions[0]);
$this->assertSame($publishExtension, $extensions[2]);
$this->assertSame($securityExtension, $extensions[3]);
$this->assertSame($globalExtension, $extensions[4]);

$def = $container->get('sonata_article_admin');
$extensions = $def->getExtensions();
$this->assertCount(5, $extensions);
$this->assertCount(6, $extensions);

$this->assertSame($filterExtension, $extensions[0]);
$this->assertSame($securityExtension, $extensions[1]);
$this->assertSame($publishExtension, $extensions[2]);
$this->assertSame($orderExtension, $extensions[4]);
$this->assertSame($globalExtension, $extensions[5]);

$def = $container->get('sonata_news_admin');
$extensions = $def->getExtensions();
$this->assertCount(5, $extensions);
$this->assertCount(6, $extensions);
$this->assertSame($historyExtension, $extensions[0]);
$this->assertSame($securityExtension, $extensions[1]);
$this->assertSame($filterExtension, $extensions[2]);
$this->assertSame($securityExtension, $extensions[2]);
$this->assertSame($filterExtension, $extensions[3]);
$this->assertSame($orderExtension, $extensions[4]);
$this->assertSame($globalExtension, $extensions[5]);
}

/**
Expand All @@ -308,13 +309,14 @@ public function testProcessThrowsExceptionIfTraitsAreNotAvailable(): void
$container->compile();
}

/**
* @return array
*/
protected function getConfig()
protected function getConfig(): array
{
$config = [
'extensions' => [
'sonata_extension_global' => [
'global' => true,
'priority' => -255,
],
'sonata_extension_publish' => [
'admins' => ['sonata_post_admin'],
'implements' => [Publishable::class],
Expand Down Expand Up @@ -418,6 +420,10 @@ private function getContainer(): ContainerBuilder
// Add admin extension definition's
$extensionClass = \get_class($this->createMock(AdminExtensionInterface::class));

$container
->register('sonata_extension_global')
->setPublic(true)
->setClass($extensionClass);
$container
->register('sonata_extension_publish')
->setPublic(true)
Expand Down