Skip to content

Commit 281a703

Browse files
committed
Add --reset flag to theme:change command
1 parent 22af428 commit 281a703

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Console/Command/ThemeChangeCommand.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Yireo\ThemeCommands\Console\Command;
44

55
use Magento\Framework\App\Cache\Manager as CacheManager;
6+
use Magento\Framework\App\ResourceConnection;
67
use Magento\Indexer\Model\IndexerFactory;
78
use Magento\Store\Model\ResourceModel\Store as StoreResourceModel;
89
use Magento\Store\Model\StoreFactory as StoreModelFactory;
@@ -13,6 +14,7 @@
1314
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
1618
use Symfony\Component\Console\Output\OutputInterface;
1719
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
1820
use Throwable;
@@ -28,6 +30,7 @@ class ThemeChangeCommand extends Command
2830
private WebsiteModelFactory $websiteFactory;
2931
private StoreResourceModel $storeResourceModel;
3032
private StoreModelFactory $storeFactory;
33+
private ResourceConnection $resourceConnection;
3134

3235
public function __construct(
3336
ConfigInterface $config,
@@ -39,6 +42,7 @@ public function __construct(
3942
WebsiteModelFactory $websiteFactory,
4043
StoreResourceModel $storeResourceModel,
4144
StoreModelFactory $storeFactory,
45+
ResourceConnection $resourceConnection,
4246
string $name = null
4347
) {
4448
parent::__construct($name);
@@ -51,6 +55,7 @@ public function __construct(
5155
$this->websiteFactory = $websiteFactory;
5256
$this->storeResourceModel = $storeResourceModel;
5357
$this->storeFactory = $storeFactory;
58+
$this->resourceConnection = $resourceConnection;
5459
}
5560

5661
/**
@@ -61,8 +66,9 @@ protected function configure(): void
6166
$this->setName('theme:change');
6267
$this->setDescription('Change a StoreView to use a specific theme');
6368
$this->addArgument('theme_name', InputArgument::REQUIRED, 'Theme name (example: Magento/luma');
64-
$this->addArgument('scope_id', InputArgument::OPTIONAL, 'Scope ID (example: 42)');
6569
$this->addArgument('scope', InputArgument::OPTIONAL, 'Scope (values: stores, websites, default)');
70+
$this->addArgument('scope_id', InputArgument::OPTIONAL, 'Scope ID (example: 42)');
71+
$this->addOption('reset', null, InputOption::VALUE_OPTIONAL, 'Reset all themes', false);
6672
parent::configure();
6773
}
6874

@@ -77,6 +83,8 @@ protected function configure(): void
7783
*/
7884
protected function execute(InputInterface $input, OutputInterface $output): int
7985
{
86+
$reset = $input->getOption('reset') !== false;
87+
8088
$themeName = trim($input->getArgument('theme_name'));
8189
$themeId = $this->getThemeId($themeName);
8290

@@ -104,6 +112,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104112
case 'stores':
105113
$scopeId = $this->getStoreId($scopeId);
106114
break;
115+
default:
116+
$scopeId = 0;
107117
}
108118
}
109119

@@ -112,7 +122,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
112122
return Command::FAILURE;
113123
}
114124

125+
if (!in_array($scope, ['default', 'website', 'stores'])) {
126+
$output->writeln('<error>Not a valid scope. Can only be: default, website, stores</error>');
127+
return Command::FAILURE;
128+
}
129+
130+
$output->writeln('Saving '.$themeId.' for scope '.$scope.' '.$scopeId);
115131
$this->config->saveConfig('design/theme/theme_id', $themeId, $scope, $scopeId);
132+
133+
if ($scope === 'default' && $reset === true) {
134+
$connection = $this->resourceConnection->getConnection();
135+
$table = $this->resourceConnection->getTableName('core_config_data');
136+
$query = 'DELETE FROM `'.$table.'`';
137+
$query .= ' WHERE `path` = "design/theme/theme_id" AND `scope` != "default"';
138+
$connection->query($query);
139+
}
140+
116141
$this->cacheManager->clean(['config', 'layout', 'block_html']);
117142

118143
$indexer = $this->indexerFactory->create();

0 commit comments

Comments
 (0)