vendor/shopware/core/System/Snippet/Files/SnippetFileLoader.php line 124

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Snippet\Files;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\App\ActiveAppsLoader;
  5. use Shopware\Core\Framework\Bundle;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Plugin;
  8. use Symfony\Component\Finder\Finder;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. #[Package('system-settings')]
  11. class SnippetFileLoader implements SnippetFileLoaderInterface
  12. {
  13.     /**
  14.      * @var array<string, string>
  15.      */
  16.     private array $pluginAuthors = [];
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(private readonly KernelInterface $kernel, private readonly Connection $connection, private readonly AppSnippetFileLoader $appSnippetFileLoader, private readonly ActiveAppsLoader $activeAppsLoader)
  21.     {
  22.     }
  23.     public function loadSnippetFilesIntoCollection(SnippetFileCollection $snippetFileCollection): void
  24.     {
  25.         $this->loadPluginSnippets($snippetFileCollection);
  26.         $this->loadAppSnippets($snippetFileCollection);
  27.     }
  28.     private function loadPluginSnippets(SnippetFileCollection $snippetFileCollection): void
  29.     {
  30.         foreach ($this->kernel->getBundles() as $bundle) {
  31.             if (!$bundle instanceof Bundle) {
  32.                 continue;
  33.             }
  34.             $snippetDir $bundle->getPath() . '/Resources/snippet';
  35.             if (!is_dir($snippetDir)) {
  36.                 continue;
  37.             }
  38.             foreach ($this->loadSnippetFilesInDir($snippetDir$bundle) as $snippetFile) {
  39.                 if ($snippetFileCollection->hasFileForPath($snippetFile->getPath())) {
  40.                     continue;
  41.                 }
  42.                 $snippetFileCollection->add($snippetFile);
  43.             }
  44.         }
  45.     }
  46.     private function loadAppSnippets(SnippetFileCollection $snippetFileCollection): void
  47.     {
  48.         foreach ($this->activeAppsLoader->getActiveApps() as $app) {
  49.             $snippetFiles $this->appSnippetFileLoader->loadSnippetFilesFromApp($app['author'] ?? ''$app['path']);
  50.             foreach ($snippetFiles as $snippetFile) {
  51.                 $snippetFile->setTechnicalName($app['name']);
  52.                 $snippetFileCollection->add($snippetFile);
  53.             }
  54.         }
  55.     }
  56.     /**
  57.      * @return AbstractSnippetFile[]
  58.      */
  59.     private function loadSnippetFilesInDir(string $snippetDirBundle $bundle): array
  60.     {
  61.         $finder = new Finder();
  62.         $finder->in($snippetDir)
  63.             ->files()
  64.             ->name('*.json');
  65.         $snippetFiles = [];
  66.         foreach ($finder->getIterator() as $fileInfo) {
  67.             $nameParts explode('.'$fileInfo->getFilenameWithoutExtension());
  68.             $snippetFile null;
  69.             switch (\count($nameParts)) {
  70.                 case 2:
  71.                     $snippetFile = new GenericSnippetFile(
  72.                         implode('.'$nameParts),
  73.                         $fileInfo->getPathname(),
  74.                         $nameParts[1],
  75.                         $this->getAuthorFromBundle($bundle),
  76.                         false,
  77.                         $bundle->getName()
  78.                     );
  79.                     break;
  80.                 case 3:
  81.                     $snippetFile = new GenericSnippetFile(
  82.                         implode('.', [$nameParts[0], $nameParts[1]]),
  83.                         $fileInfo->getPathname(),
  84.                         $nameParts[1],
  85.                         $this->getAuthorFromBundle($bundle),
  86.                         $nameParts[2] === 'base',
  87.                         $bundle->getName()
  88.                     );
  89.                     break;
  90.             }
  91.             if ($snippetFile) {
  92.                 $snippetFiles[] = $snippetFile;
  93.             }
  94.         }
  95.         return $snippetFiles;
  96.     }
  97.     private function getAuthorFromBundle(Bundle $bundle): string
  98.     {
  99.         if (!$bundle instanceof Plugin) {
  100.             return 'Shopware';
  101.         }
  102.         return $this->getPluginAuthors()[$bundle::class] ?? '';
  103.     }
  104.     /**
  105.      * @return array<string, string>
  106.      */
  107.     private function getPluginAuthors(): array
  108.     {
  109.         if (!$this->pluginAuthors) {
  110.             /** @var array<string, string> $authors */
  111.             $authors $this->connection->fetchAllKeyValue('
  112.                 SELECT `base_class` AS `baseClass`, `author`
  113.                 FROM `plugin`
  114.             ');
  115.             $this->pluginAuthors $authors;
  116.         }
  117.         return $this->pluginAuthors;
  118.     }
  119. }