From 330ff48571b9c853545ce65426f8c24d24687856 Mon Sep 17 00:00:00 2001 From: Toby Date: Sat, 13 Apr 2024 17:40:49 +0200 Subject: [PATCH] Add search of mail templates in symfony-bundles (#66) If you used Symfony bundles to add multiple themes, they weren't found when the template finder searched for the matching theme of the salesChannel resulting in it taking the first theme in alphabetical order. When no Shopware app or plugin was found, it now searches for matching bundles. --- src/Services/MailFinderService.php | 50 +++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/Services/MailFinderService.php b/src/Services/MailFinderService.php index 9338e63..4c4893a 100644 --- a/src/Services/MailFinderService.php +++ b/src/Services/MailFinderService.php @@ -10,6 +10,7 @@ use Symfony\Component\DependencyInjection\Attribute\AsAlias; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; +use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Twig\Loader\FilesystemLoader; #[AsAlias] @@ -21,6 +22,7 @@ class MailFinderService implements MailFinderServiceInterface /** * @param LoaderInterface[] $availableLoaders + * @param iterable $bundles */ public function __construct( #[Autowire(service: 'twig.loader.native_filesystem')] @@ -28,7 +30,9 @@ public function __construct( #[TaggedIterator('frosh_template_mail.loader')] private readonly iterable $availableLoaders, private readonly SearchPathProvider $searchPathProvider, - private readonly Connection $connection + private readonly Connection $connection, + #[Autowire(service: 'kernel.bundles')] + private readonly iterable $bundles, ) {} public function findTemplateByTechnicalName( @@ -41,16 +45,8 @@ public function findTemplateByTechnicalName( $searchFolder = $this->searchPathProvider->buildPaths($businessEvent); - $stmt = $this->connection->prepare( - 'SELECT IFNULL(a.path, p.path) AS `path` FROM `theme` AS t ' - . 'LEFT JOIN `theme_sales_channel` AS tsc ON tsc.`theme_id` = t.`id` ' - . 'LEFT JOIN `plugin` AS p ON p.`name` = t.`technical_name` ' - . 'LEFT JOIN `app` AS a ON a.`name` = t.`technical_name` ' - . 'WHERE tsc.`sales_channel_id` = ?;' - ); - - $stmt->bindValue(1, Uuid::fromHexToBytes($businessEvent->getSalesChannelId())); - $themePath = $stmt->executeQuery()->fetchOne(); + $themePath = $this->findPathOfThemeFromPluginOrApp($businessEvent->getSalesChannelId()) + ?? $this->findPathOfThemeFromSymfonyBundle($businessEvent->getSalesChannelId()); if (\is_string($themePath)) { usort($paths, static function ($a, $b) use ($themePath) { @@ -83,4 +79,36 @@ public function findTemplateByTechnicalName( return null; } + + public function findPathOfThemeFromPluginOrApp(string $salesChannelId): ?string { + $stmt = $this->connection->prepare( + 'SELECT IFNULL(a.path, p.path) AS `path` FROM `theme` AS t ' + . 'LEFT JOIN `theme_sales_channel` AS tsc ON tsc.`theme_id` = t.`id` ' + . 'LEFT JOIN `plugin` AS p ON p.`name` = t.`technical_name` ' + . 'LEFT JOIN `app` AS a ON a.`name` = t.`technical_name` ' + . 'WHERE tsc.`sales_channel_id` = ?;' + ); + + $stmt->bindValue(1, Uuid::fromHexToBytes($salesChannelId)); + + return $stmt->executeQuery()->fetchOne(); + } + + public function findPathOfThemeFromSymfonyBundle(string $salesChannelId): ?string { + $stmt = $this->connection->prepare( + 'SELECT t.`technical_name` FROM `theme` AS t ' + . 'LEFT JOIN `theme_sales_channel` AS tsc ON tsc.`theme_id` = t.`id` ' + . 'WHERE tsc.`sales_channel_id` = ?;' + ); + + $stmt->bindValue(1, Uuid::fromHexToBytes($salesChannelId)); + + $technicalName = $stmt->executeQuery()->fetchOne(); + + if (isset($this->bundles[$technicalName])) { + return $this->bundles[$technicalName]->getPath(); + } + + return null; + } }