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

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