vendor/shopware/core/Framework/Adapter/Twig/NamespaceHierarchy/BundleHierarchyBuilder.php line 41

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Twig\NamespaceHierarchy;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Bundle;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Symfony\Component\HttpKernel\KernelInterface;
  7. #[Package('core')]
  8. class BundleHierarchyBuilder implements TemplateNamespaceHierarchyBuilderInterface
  9. {
  10.     /**
  11.      * @internal
  12.      */
  13.     public function __construct(private readonly KernelInterface $kernel, private readonly Connection $connection)
  14.     {
  15.     }
  16.     public function buildNamespaceHierarchy(array $namespaceHierarchy): array
  17.     {
  18.         $bundles = [];
  19.         foreach ($this->kernel->getBundles() as $bundle) {
  20.             if (!$bundle instanceof Bundle) {
  21.                 continue;
  22.             }
  23.             $bundlePath $bundle->getPath();
  24.             $directory $bundlePath '/Resources/views';
  25.             if (!file_exists($directory)) {
  26.                 continue;
  27.             }
  28.             $bundles[$bundle->getName()] = $bundle->getTemplatePriority();
  29.         }
  30.         $bundles array_reverse($bundles);
  31.         $apps $this->getAppTemplateNamespaces();
  32.         /** @var array $combinedApps */
  33.         $combinedApps array_combine(array_keys($apps), array_column($apps'template_load_priority'));
  34.         $extensions array_merge($combinedApps$bundles);
  35.         asort($extensions);
  36.         foreach ($apps as $appName => ['version' => $version]) {
  37.             $extensions[$appName] = $version;
  38.         }
  39.         return array_merge(
  40.             $extensions,
  41.             $namespaceHierarchy
  42.         );
  43.     }
  44.     private function getAppTemplateNamespaces(): array
  45.     {
  46.         return $this->connection->fetchAllAssociativeIndexed(
  47.             'SELECT `app`.`name`, `app`.`version`, `app`.`template_load_priority`
  48.              FROM `app`
  49.              INNER JOIN `app_template` ON `app_template`.`app_id` = `app`.`id`
  50.              WHERE `app`.`active` = 1 AND `app_template`.`active` = 1'
  51.         );
  52.     }
  53. }