|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Yireo\ThemeCommands\Console\Command; |
| 4 | + |
| 5 | +use Magento\Framework\App\ResourceConnection; |
| 6 | +use Magento\Store\Model\StoreRepository; |
| 7 | +use Magento\Store\Model\WebsiteRepository; |
| 8 | +use Magento\Theme\Model\ResourceModel\Design\Config\Scope\CollectionFactory; |
| 9 | +use Magento\Theme\Model\Theme\ThemeProvider; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Helper\Table; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Throwable; |
| 15 | + |
| 16 | +class ThemeDesignConfigCommand extends Command |
| 17 | +{ |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + private CollectionFactory $designCollectionFactory, |
| 21 | + private WebsiteRepository $websiteRepository, |
| 22 | + private StoreRepository $storeRepository, |
| 23 | + private ThemeProvider $themeProvider, |
| 24 | + private ResourceConnection $resourceConnection, |
| 25 | + string $name = null |
| 26 | + ) { |
| 27 | + parent::__construct($name); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Initialization of the command. |
| 32 | + */ |
| 33 | + protected function configure(): void |
| 34 | + { |
| 35 | + $this->setName('theme:design_config'); |
| 36 | + $this->setDescription('Show currently active themes'); |
| 37 | + parent::configure(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * CLI command description. |
| 42 | + * |
| 43 | + * @param InputInterface $input |
| 44 | + * @param OutputInterface $output |
| 45 | + * |
| 46 | + * @return int |
| 47 | + * @throws Throwable |
| 48 | + */ |
| 49 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 50 | + { |
| 51 | + $table = new Table($output); |
| 52 | + $table->setHeaders([ |
| 53 | + 'Website', |
| 54 | + 'Website ID', |
| 55 | + 'Store View', |
| 56 | + 'Store View ID', |
| 57 | + 'Theme', |
| 58 | + 'Theme ID', |
| 59 | + 'Override', |
| 60 | + ]); |
| 61 | + |
| 62 | + $designCollection = $this->designCollectionFactory->create(); |
| 63 | + foreach ($designCollection as $designConfig) { |
| 64 | + try { |
| 65 | + $website = $this->websiteRepository->get($designConfig->getStoreWebsiteId()); |
| 66 | + $websiteId = $website->getId(); |
| 67 | + $websiteName = $website->getName(); |
| 68 | + } catch (\Exception $exception) { |
| 69 | + $websiteId = ''; |
| 70 | + $websiteName = ''; |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + $store = $this->storeRepository->getById($designConfig->getStoreId()); |
| 75 | + $storeId = $store->getId(); |
| 76 | + $storeName = $store->getName(); |
| 77 | + } catch (\Exception $exception) { |
| 78 | + $storeId = ''; |
| 79 | + $storeName = ''; |
| 80 | + } |
| 81 | + |
| 82 | + $theme = $this->themeProvider->getThemeById($designConfig->getThemeThemeId()); |
| 83 | + $themeId = $theme->getId(); |
| 84 | + $themeName = $theme->getCode(); |
| 85 | + |
| 86 | + $override = null; |
| 87 | + if ($websiteId > 0) { |
| 88 | + $override = $this->isCustomized('websites', $websiteId, $themeId) ? 'x' : ''; |
| 89 | + } |
| 90 | + |
| 91 | + if ($storeId > 0) { |
| 92 | + $override = $this->isCustomized('stores', $storeId, $themeId) ? 'x' : ''; |
| 93 | + } |
| 94 | + |
| 95 | + $table->addRow([ |
| 96 | + $websiteId, |
| 97 | + $websiteName, |
| 98 | + $storeId, |
| 99 | + $storeName, |
| 100 | + $themeId, |
| 101 | + $themeName, |
| 102 | + $override |
| 103 | + ]); |
| 104 | + } |
| 105 | + |
| 106 | + $table->render(); |
| 107 | + |
| 108 | + return Command::SUCCESS; |
| 109 | + } |
| 110 | + |
| 111 | + private function isCustomized(string $scope, $scopeId, $value): bool |
| 112 | + { |
| 113 | + if ($scope === 'default') { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + $connection = $this->resourceConnection->getConnection(); |
| 118 | + $table = $this->resourceConnection->getTableName('core_config_data'); |
| 119 | + $query = 'SELECT `value` FROM `'.$table.'`'; |
| 120 | + $query .= ' WHERE `path` = "design/theme/theme_id" AND `scope` = "'.$scope.'" AND `scope_id` = "'.$scopeId.'"'; |
| 121 | + $col = $connection->fetchCol($query); |
| 122 | + |
| 123 | + if (empty($col)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + return true; |
| 128 | + } |
| 129 | +} |
0 commit comments