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

Allow AdminExtractor to be used for abstract admins #8118

Merged
merged 1 commit into from
Oct 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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