Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Jul 7, 2024
1 parent 7d5d1eb commit ea6891c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 75 deletions.
38 changes: 24 additions & 14 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Sonata\AdminBundle\Form\FormErrorIteratorToConstraintViolationList;
use Sonata\AdminBundle\Model\AuditManagerInterface;
use Sonata\AdminBundle\Request\AdminFetcherInterface;
use Sonata\AdminBundle\Templating\LayoutStorage\CookieLayoutStorage;
use Sonata\AdminBundle\Templating\LayoutStorage\LayoutStorageInterface;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Sonata\AdminBundle\Util\AdminAclUserManagerInterface;
use Sonata\AdminBundle\Util\AdminObjectAclData;
Expand Down Expand Up @@ -94,6 +96,7 @@ public static function getSubscribedServices(): array
'sonata.exporter.exporter' => '?'.ExporterInterface::class,
'sonata.admin.admin_exporter' => '?'.AdminExporter::class,
'sonata.admin.security.acl_user_manager' => '?'.AdminAclUserManagerInterface::class,
'sonata.admin.layout_cookie_storage' => LayoutStorageInterface::class,

'controller_resolver' => 'controller_resolver',
'http_kernel' => HttpKernelInterface::class,
Expand Down Expand Up @@ -127,7 +130,7 @@ public function listAction(Request $request): Response
// set the theme for the current Admin Form
$this->setFormTheme($formView, $this->admin->getFilterTheme());

$template = $this->templateRegistry->getTemplate('list');
$template = $this->getTemplate('list');

if ($this->container->has('sonata.admin.admin_exporter')) {
$exporter = $this->container->get('sonata.admin.admin_exporter');
Expand Down Expand Up @@ -262,7 +265,7 @@ public function deleteAction(Request $request): Response
return $this->redirectTo($request, $object);
}

$template = $this->templateRegistry->getTemplate('delete');
$template = $this->getTemplate('delete');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -373,7 +376,7 @@ public function editAction(Request $request): Response
// set the theme for the current Admin Form
$this->setFormTheme($formView, $this->admin->getFormTheme());

$template = $this->templateRegistry->getTemplate($templateKey);
$template = $this->getTemplate($templateKey);

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -496,7 +499,7 @@ public function batchAction(Request $request): Response
$formView = $datagrid->getForm()->createView();
$this->setFormTheme($formView, $this->admin->getFilterTheme());

$template = $batchAction['template'] ?? $this->templateRegistry->getTemplate('batch_confirmation');
$template = $batchAction['template'] ?? $this->getTemplate('batch_confirmation');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -645,7 +648,7 @@ public function createAction(Request $request): Response
// set the theme for the current Admin Form
$this->setFormTheme($formView, $this->admin->getFormTheme());

$template = $this->templateRegistry->getTemplate($templateKey);
$template = $this->getTemplate($templateKey);

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -680,7 +683,7 @@ public function showAction(Request $request): Response

$fields = $this->admin->getShow();

$template = $this->templateRegistry->getTemplate('show');
$template = $this->getTemplate('show');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -721,7 +724,7 @@ public function historyAction(Request $request): Response

$revisions = $reader->findRevisions($this->admin->getClass(), $objectId);

$template = $this->templateRegistry->getTemplate('history');
$template = $this->getTemplate('history');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -775,7 +778,7 @@ public function historyViewRevisionAction(Request $request, string $revision): R

$this->admin->setSubject($object);

$template = $this->templateRegistry->getTemplate('show');
$template = $this->getTemplate('show');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -838,7 +841,7 @@ public function historyCompareRevisionsAction(Request $request, string $baseRevi

$this->admin->setSubject($baseObject);

$template = $this->templateRegistry->getTemplate('show_compare');
$template = $this->getTemplate('show_compare');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -949,7 +952,7 @@ public function aclAction(Request $request): Response
}
}

$template = $this->templateRegistry->getTemplate('acl');
$template = $this->getTemplate('acl');

/**
* @psalm-suppress DeprecatedMethod
Expand Down Expand Up @@ -997,9 +1000,9 @@ final public function setTwigGlobals(Request $request): void
$this->setTwigGlobal('admin', $this->admin);

if ($this->isXmlHttpRequest($request)) {
$baseTemplate = $this->templateRegistry->getTemplate('ajax');
$baseTemplate = $this->getTemplate('ajax');
} else {
$baseTemplate = $this->templateRegistry->getTemplate('layout');
$baseTemplate = $this->getTemplate('layout');
}

$this->setTwigGlobal('base_template', $baseTemplate);
Expand Down Expand Up @@ -1096,10 +1099,10 @@ protected function getBaseTemplate(): string
\assert(null !== $request);

if ($this->isXmlHttpRequest($request)) {
return $this->templateRegistry->getTemplate('ajax');
return $this->getTemplate('ajax');
}

return $this->templateRegistry->getTemplate('layout');
return $this->getTemplate('layout');
}

/**
Expand Down Expand Up @@ -1613,4 +1616,11 @@ private function equalsOrContains($haystack, object $needle): bool

return false;
}

private function getTemplate(string $name): string
{
$cookieLayoutStorage = $this->container->get('sonata.admin.layout_cookie_storage');

return $this->templateRegistry->getTemplate($name, $cookieLayoutStorage->get());

Check failure on line 1624 in src/Controller/CRUDController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Sonata\AdminBundle\Templating\TemplateRegistryInterface::getTemplate() invoked with 2 parameters, 1 required.

Check failure on line 1624 in src/Controller/CRUDController.php

View workflow job for this annotation

GitHub Actions / Psalm

TooManyArguments

src/Controller/CRUDController.php:1624:41: TooManyArguments: Too many arguments for method Sonata\AdminBundle\Templating\TemplateRegistryInterface::gettemplate - saw 2 (see https://psalm.dev/026)
}
}
42 changes: 29 additions & 13 deletions src/Templating/AbstractTemplateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
abstract class AbstractTemplateRegistry implements TemplateRegistryInterface
{
/**
* @deprecated since sonata-project/admin-bundle x.y, will be removed in 5.0.
*
* @var array<string, string> 'name' => 'file_path.html.twig'
*/
protected $templates = [];
Expand All @@ -34,63 +36,77 @@ public function __construct(array $templates = [])
$this->layoutTemplates['default'] = $templates;
}

final public function getTemplates(/* string $layout = 'default' */): array
final public function getTemplates(/* string $layout */): array
{
if (\func_num_args() < 1) {
@trigger_error(
'Not passing the "string $layout" argument explicitly is deprecated since sonata-project/admin-bundle x.y and will be required in 5.0.',
\E_USER_DEPRECATED
);

return $this->templates;
$layout = 'default';
} else {
$layout = func_get_arg(0);
}

$layout = func_get_arg(0);
// Keep BC compatibility
if ('default' === $layout) {
return $this->templates;

Check failure on line 54 in src/Templating/AbstractTemplateRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

DeprecatedProperty

src/Templating/AbstractTemplateRegistry.php:54:20: DeprecatedProperty: Sonata\AdminBundle\Templating\AbstractTemplateRegistry::$templates is marked deprecated (see https://psalm.dev/099)
}

// NEXT_MAJOR: Remove code before this comment

return $this->layoutTemplates[$layout] + $this->templates;
return $this->layoutTemplates[$layout] + $this->layoutTemplates['default'];
}

final public function hasTemplate(string $name /* ,string $layout = 'default' */): bool
final public function hasTemplate(string $name /* ,string $layout */): bool
{
if (\func_num_args() < 2) {
@trigger_error(
'Not passing the "string $layout" argument explicitly is deprecated since sonata-project/admin-bundle x.y and will be required in 5.0.',
\E_USER_DEPRECATED
);

return isset($this->templates[$name]);
$layout = 'default';
} else {
$layout = func_get_arg(1);
}

$layout = func_get_arg(1);
// Keep BC compatibility
if ('default' === $layout) {
return isset($this->templates[$name]);

Check failure on line 77 in src/Templating/AbstractTemplateRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

DeprecatedProperty

src/Templating/AbstractTemplateRegistry.php:77:26: DeprecatedProperty: Sonata\AdminBundle\Templating\AbstractTemplateRegistry::$templates is marked deprecated (see https://psalm.dev/099)
}

// NEXT_MAJOR: Remove code before this comment

return isset($this->layoutTemplates[$layout][$name]) || isset($this->templates[$name]);
return isset($this->layoutTemplates[$layout][$name]) || isset($this->layoutTemplates['default'][$name]);
}

final public function getTemplate(string $name /* ,string $layout = 'default' */): string
final public function getTemplate(string $name /* ,string $layout */): string
{
if (\func_num_args() < 2) {
@trigger_error(
'Not passing the "string $layout" argument explicitly is deprecated since sonata-project/admin-bundle x.y and will be required in 5.0.',
\E_USER_DEPRECATED
);

$layout = 'default';
} else {
$layout = func_get_arg(1);
}

// Keep BC compatibility
if ('default' === $layout) {
if ($this->hasTemplate($name)) {
return $this->templates[$name];

Check failure on line 101 in src/Templating/AbstractTemplateRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

DeprecatedProperty

src/Templating/AbstractTemplateRegistry.php:101:24: DeprecatedProperty: Sonata\AdminBundle\Templating\AbstractTemplateRegistry::$templates is marked deprecated (see https://psalm.dev/099)
}

throw new \InvalidArgumentException(sprintf('Template named "%s" doesn\'t exist.', $name));
}

$layout = func_get_arg(1);

// NEXT_MAJOR: Remove code before this comment

if ($this->hasTemplate($name, $layout)) {
return $this->layoutTemplates[$layout][$name] ?? $this->templates[$name];
return $this->layoutTemplates[$layout][$name] ?? $this->layoutTemplates['default'][$name];
}

throw new \InvalidArgumentException(sprintf('Template named "%s" for "%s" layout doesn\'t exist.', $name, $layout));
Expand Down
35 changes: 16 additions & 19 deletions src/Templating/MutableTemplateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,46 @@
*/
final class MutableTemplateRegistry extends AbstractTemplateRegistry implements MutableTemplateRegistryInterface
{
public function setTemplates(array $templates /* ,string $layout = 'default' */): void
public function setTemplates(array $templates /* ,string $layout */): void
{
if (\func_num_args() < 2) {
@trigger_error(
'Not passing the "string $layout" argument explicitly is deprecated since sonata-project/admin-bundle x.y and will be required in 5.0.',
\E_USER_DEPRECATED
);

$this->templates = $templates + $this->templates;

return;
$layout = 'default';
} else {
$layout = func_get_arg(1);
}

$layout = func_get_arg(1);

// NEXT_MAJOR: Remove code before this comment

// Keep BC compatibility
if ('default' === $layout) {
$this->templates = $templates + $this->templates;

Check failure on line 36 in src/Templating/MutableTemplateRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

DeprecatedProperty

src/Templating/MutableTemplateRegistry.php:36:13: DeprecatedProperty: Sonata\AdminBundle\Templating\MutableTemplateRegistry::$templates is marked deprecated (see https://psalm.dev/099)
} else {
$this->layoutTemplates[$layout] = $templates + ($this->layoutTemplates[$layout] ?? []);
}
// NEXT_MAJOR: Remove code before this comment
$this->layoutTemplates[$layout] = $templates + ($this->layoutTemplates[$layout] ?? []);

Check failure on line 39 in src/Templating/MutableTemplateRegistry.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Sonata\AdminBundle\Templating\AbstractTemplateRegistry::$layoutTemplates (array<string, array<string, string>>) does not accept array<int|string, array<string, string>>.
}

public function setTemplate(string $name, string $template /* ,string $layout = 'default' */): void
public function setTemplate(string $name, string $template /* ,string $layout */): void
{
if (\func_num_args() < 3) {
@trigger_error(
'Not passing the "string $layout" argument explicitly is deprecated since sonata-project/admin-bundle x.y and will be required in 5.0.',
\E_USER_DEPRECATED
);

$this->templates[$name] = $template;
$layout = 'default';
} else {
$layout = func_get_arg(2);
}

// NEXT_MAJOR: Remove code before this comment

if ('default' === $layout) {
$this->templates[$name] = $template;
} else {
$this->layoutTemplates[$layout][$name] = $template;
}
// Keep BC compatibility
if ('default' === $layout) {
$this->templates[$name] = $template;
}

// NEXT_MAJOR: Remove code before this comment
$this->layoutTemplates[$layout][$name] = $template;
}
}
6 changes: 3 additions & 3 deletions src/Templating/TemplateRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ interface TemplateRegistryInterface
/**
* @return array<string, string> 'name' => 'file_path.html.twig'
*/
public function getTemplates(/* string $layout = 'default' */): array;
public function getTemplates(/* string $layout */): array;

public function getTemplate(string $name /* ,string $layout = 'default' */): string;
public function getTemplate(string $name /* ,string $layout */): string;

public function hasTemplate(string $name /* ,string $layout = 'default' */): bool;
public function hasTemplate(string $name /* ,string $layout */): bool;
}
15 changes: 3 additions & 12 deletions src/Twig/TemplateRegistryRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ final class TemplateRegistryRuntime implements RuntimeExtensionInterface
public function __construct(
private TemplateRegistryInterface $globalTemplateRegistry,
private Pool $pool,
private LayoutStorageInterface $layoutStorage,
private bool $useLayouts = false
private LayoutStorageInterface $layoutStorage
) {
}

Expand All @@ -40,20 +39,12 @@ public function __construct(
*/
public function getAdminTemplate(string $name, string $adminCode): string
{
if ($this->useLayouts) {
return $this->getTemplateRegistry($adminCode)->getTemplate($name, $this->layoutStorage->get());
}

return $this->getTemplateRegistry($adminCode)->getTemplate($name);
return $this->getTemplateRegistry($adminCode)->getTemplate($name, $this->layoutStorage->get());
}

public function getGlobalTemplate(string $name): string
{
if ($this->useLayouts) {
return $this->globalTemplateRegistry->getTemplate($name, $this->layoutStorage->get());
}

return $this->globalTemplateRegistry->getTemplate($name);
return $this->globalTemplateRegistry->getTemplate($name, $this->layoutStorage->get());
}

/**
Expand Down
Loading

0 comments on commit ea6891c

Please sign in to comment.