33namespace Yireo \ThemeCommands \Console \Command ;
44
55use Magento \Framework \App \Cache \Manager as CacheManager ;
6+ use Magento \Framework \App \ResourceConnection ;
67use Magento \Indexer \Model \IndexerFactory ;
78use Magento \Store \Model \ResourceModel \Store as StoreResourceModel ;
89use Magento \Store \Model \StoreFactory as StoreModelFactory ;
1314use Symfony \Component \Console \Command \Command ;
1415use Symfony \Component \Console \Input \InputArgument ;
1516use Symfony \Component \Console \Input \InputInterface ;
17+ use Symfony \Component \Console \Input \InputOption ;
1618use Symfony \Component \Console \Output \OutputInterface ;
1719use Magento \Framework \App \Config \ConfigResource \ConfigInterface ;
1820use 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