Skip to content

Commit

Permalink
Allow AdminExtractor to be used for abstract admins (#8118)
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Oct 18, 2023
1 parent 2a65e55 commit 2334082
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Translator/Extractor/AdminExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Translator\Extractor;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
Expand Down Expand Up @@ -70,6 +71,10 @@ public function extract($resource, MessageCatalogue $catalogue): void
foreach ($this->adminPool->getAdminServiceCodes() as $code) {
$admin = $this->adminPool->getInstance($code);

if (!$this->isValidAdmin($admin)) {
continue;
}

$this->labelStrategy = $admin->getLabelTranslatorStrategy();
$this->domain = $admin->getTranslationDomain();

Expand All @@ -79,6 +84,7 @@ public function extract($resource, MessageCatalogue $catalogue): void
}

$admin->setLabelTranslatorStrategy($this);

$admin->setSubject($admin->getNewInstance());

foreach (self::PUBLIC_ADMIN_METHODS as $method) {
Expand Down Expand Up @@ -117,4 +123,18 @@ public function getLabel(string $label, string $context = '', string $type = '')

return $label;
}

/**
* @param AdminInterface<object> $admin
*/
private function isValidAdmin(AdminInterface $admin): bool
{
$class = $admin->getClass();

if (!class_exists($class)) {
return false;
}

return !(new \ReflectionClass($class))->isAbstract();
}
}
6 changes: 6 additions & 0 deletions tests/Translator/Extractor/AdminExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ protected function setUp(): void

$this->fooAdmin->method('getShow')->willReturn(new FieldDescriptionCollection());
$this->fooAdmin->method('getList')->willReturn(new FieldDescriptionCollection());
$this->fooAdmin
->method('getClass')
->willReturn(\stdClass::class);
$this->barAdmin->method('getShow')->willReturn(new FieldDescriptionCollection());
$this->barAdmin->method('getList')->willReturn(new FieldDescriptionCollection());
$this->barAdmin
->method('getClass')
->willReturn(\stdClass::class);

$container = new Container();
$container->set('foo_admin', $this->fooAdmin);
Expand Down

0 comments on commit 2334082

Please sign in to comment.