Skip to content

Commit 9ff0c64

Browse files
committed
Allow for JSON output of theme list
1 parent c8ff4a8 commit 9ff0c64

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

Console/Command/ThemeListCommand.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
use Magento\Framework\View\DesignInterface;
77
use Magento\Store\Model\ScopeInterface;
88
use Magento\Store\Model\StoreManagerInterface;
9-
use Magento\Theme\Model\ResourceModel\Theme as ThemeResourceModel;
109
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
1110
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory as ThemeCollectionFactory;
1211
use Symfony\Component\Console\Command\Command;
1312
use Symfony\Component\Console\Helper\Table;
1413
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Throwable;
1717

@@ -33,6 +33,7 @@ protected function configure(): void
3333
{
3434
$this->setName('theme:list');
3535
$this->setDescription('Show all available themes');
36+
$this->addOption('json', null, InputOption::VALUE_OPTIONAL, 'Output as JSON');
3637
parent::configure();
3738
}
3839

@@ -47,12 +48,16 @@ protected function configure(): void
4748
*/
4849
protected function execute(InputInterface $input, OutputInterface $output): int
4950
{
51+
$json = $input->getOption('json');
52+
if ((bool)$json) {
53+
echo $this->getJson();
54+
return Command::SUCCESS;
55+
}
56+
5057
$table = new Table($output);
5158
$table->setHeaders(['ID', 'Theme', 'Path', 'Area', 'Active']);
5259

53-
/** @var ThemeCollection $themeCollection */
54-
$themeCollection = $this->themeCollectionFactory->create();
55-
foreach ($themeCollection as $theme) {
60+
foreach ($this->getThemes() as $theme) {
5661
$table->addRow([
5762
$theme->getId(),
5863
$theme->getThemeTitle(),
@@ -67,6 +72,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6772
return Command::SUCCESS;
6873
}
6974

75+
private function getJson(): string
76+
{
77+
$themes = [];
78+
foreach ($this->getThemes() as $theme) {
79+
$themes[] = [
80+
'id' => $theme->getId(),
81+
'name' => $theme->getThemeTitle(),
82+
'path' => $theme->getThemePath(),
83+
'area' => $theme->getArea(),
84+
'active' => $this->isActive((int)$theme->getId())
85+
];
86+
}
87+
88+
return json_encode($themes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
89+
}
90+
91+
private function getThemes(): ThemeCollection
92+
{
93+
return $this->themeCollectionFactory->create();
94+
}
95+
7096
private function isActive(int $themeId): bool
7197
{
7298
foreach ($this->storeManager->getStores() as $store) {

0 commit comments

Comments
 (0)