From ef110d838b9382a19652ee136beed1b33b17cfa2 Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Thu, 2 Dec 2021 16:55:33 +0100 Subject: [PATCH 1/2] Use Asset for DefaultAvatar Url --- composer.json | 2 +- src/Avatar/StaticAvatarResolver.php | 7 +++++-- src/Resources/config/avatar.php | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 03c87a7c..fbb12281 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "psr/log": "^1.0 || ^2.0 || ^3.0", "sonata-project/admin-bundle": "^3.90 || ^4.0", "sonata-project/doctrine-extensions": "^1.5.1", + "symfony/asset": "^4.4 || ^5.3", "symfony/config": "^4.4 || ^5.3", "symfony/dependency-injection": "^4.4 || ^5.3", "symfony/event-dispatcher": "^4.4 || ^5.3", @@ -57,7 +58,6 @@ "ergebnis/composer-normalize": "^2.0.1", "knplabs/knp-menu": "^2.3 || ^3.0", "sonata-project/doctrine-orm-admin-bundle": "^3.13 || ^4.0", - "symfony/asset": "^4.4 || ^5.3", "symfony/browser-kit": "^4.4 || ^5.3", "symfony/doctrine-bridge": "^4.4 || ^5.3" }, diff --git a/src/Avatar/StaticAvatarResolver.php b/src/Avatar/StaticAvatarResolver.php index d3d47129..a1377e12 100644 --- a/src/Avatar/StaticAvatarResolver.php +++ b/src/Avatar/StaticAvatarResolver.php @@ -11,19 +11,22 @@ namespace Nucleos\UserAdminBundle\Avatar; +use Symfony\Component\Asset\Packages; use Symfony\Component\Security\Core\User\UserInterface; final class StaticAvatarResolver implements AvatarResolver { + private Packages $packages; private string $defaultAvatar; - public function __construct(string $defaultAvatar) + public function __construct(Packages $packages, string $defaultAvatar) { + $this->packages = $packages; $this->defaultAvatar = $defaultAvatar; } public function avatarUrl(?UserInterface $user): string { - return $this->defaultAvatar; + return $this->packages->getUrl($this->defaultAvatar); } } diff --git a/src/Resources/config/avatar.php b/src/Resources/config/avatar.php index 440c8b37..9eecfc0c 100644 --- a/src/Resources/config/avatar.php +++ b/src/Resources/config/avatar.php @@ -15,6 +15,7 @@ use Nucleos\UserAdminBundle\Avatar\StaticAvatarResolver; use Nucleos\UserAdminBundle\Twig\AvatarExtension; use Nucleos\UserAdminBundle\Twig\AvatarRuntime; +use Symfony\Component\Asset\Packages; return static function (ContainerConfigurator $container): void { $container->services() @@ -32,6 +33,7 @@ ->set(StaticAvatarResolver::class) ->args([ + ref(Packages::class), '%nucleos_user_admin.default_avatar%', ]) From d3c65be1de790d6cc5d09f5d6b7f5507d0d0cb7f Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Thu, 2 Dec 2021 17:45:12 +0100 Subject: [PATCH 2/2] ~ make Packages nullable --- src/Avatar/StaticAvatarResolver.php | 10 +++++++--- src/Resources/config/avatar.php | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Avatar/StaticAvatarResolver.php b/src/Avatar/StaticAvatarResolver.php index a1377e12..c04b417d 100644 --- a/src/Avatar/StaticAvatarResolver.php +++ b/src/Avatar/StaticAvatarResolver.php @@ -16,17 +16,21 @@ final class StaticAvatarResolver implements AvatarResolver { - private Packages $packages; private string $defaultAvatar; + private ?Packages $packages; - public function __construct(Packages $packages, string $defaultAvatar) + public function __construct(string $defaultAvatar, ?Packages $packages = null) { - $this->packages = $packages; + $this->packages = $packages; $this->defaultAvatar = $defaultAvatar; } public function avatarUrl(?UserInterface $user): string { + if (null === $this->packages) { + return $this->defaultAvatar; + } + return $this->packages->getUrl($this->defaultAvatar); } } diff --git a/src/Resources/config/avatar.php b/src/Resources/config/avatar.php index 9eecfc0c..d693d054 100644 --- a/src/Resources/config/avatar.php +++ b/src/Resources/config/avatar.php @@ -33,8 +33,8 @@ ->set(StaticAvatarResolver::class) ->args([ - ref(Packages::class), '%nucleos_user_admin.default_avatar%', + ref(Packages::class), ]) ->alias(AvatarResolver::class, StaticAvatarResolver::class)