Files
kupshop/bundles/KupShop/KupShopBundle/Command/GetConfigCommand.php
2025-08-02 16:30:27 +02:00

124 lines
4.2 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Command;
use KupShop\ComponentsBundle\Utils\ComponentsLocator;
use KupShop\KupShopBundle\Util\Functional\Mapping;
use KupShop\KupShopBundle\Util\StringUtil;
use KupShop\KupShopBundle\Util\System\BundleFinder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Service\Attribute\Required;
class GetConfigCommand extends Command
{
/** @required */
public BundleFinder $bundleFinder;
protected static $defaultName = 'kupshop:config:get';
public function __construct(
protected ?ComponentsLocator $componentsLocator = null,
?string $name = null,
) {
parent::__construct($name);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = [
'modules' => $this->getModulesList(\Modules::getStructure()),
'modulesWithValues' => $this->getModulesList(\Modules::getStructure(), withValues: true),
'bundles' => $this->bundleFinder->getBundles(),
'frontend_bundles' => [],
'components' => iterator_to_array($this->getComponents()),
];
if ($bundlesWithJSResources = $this->bundleFinder->getExistingBundlesPath('Resources/static')) {
$config['frontend_bundles'] = $bundlesWithJSResources;
}
if (findModule(\Modules::COMPONENTS)) {
Mapping::withKeys($this->bundleFinder->getBundles(), function ($bundle, $class) use (&$config) {
$bundles = $this->bundleFinder->getContainer()->getParameter('kernel.bundles_metadata');
if (file_exists("{$bundles[$bundle]['path']}/Resources/views/components")) {
if (StringUtil::startsWith($bundles[$bundle]['namespace'], 'Shop')) {
$config['twig_bundles'][$bundle] = "bundles/{$bundle}/Resources/views/";
} else {
$config['twig_bundles'][$bundle] = "engine/bundles/KupShop/{$bundle}/Resources/views/";
}
}
});
}
$output->write(json_encode($config));
return 0;
}
private function getModulesList(array $structure, ?string $parent = null, bool $withValues = false): array
{
$modules = [];
foreach ($structure as $key => $item) {
$moduleKey = $key;
if ($parent) {
$moduleKey = $parent.'__'.$key;
}
$modules[$moduleKey] = $withValues ?
$this->getModuleValue($parent ?: $key, $parent ? $key : null)
: \Modules::check($parent ?: $key, $parent ? $key : null);
if (!empty($item['submodules'])) {
$modules = array_merge($modules, $this->getModulesList($item['submodules'], $key, $withValues));
}
}
return $modules;
}
private function getModuleValue(string $moduleConst, ?string $submoduleName)
{
$modules = \Modules::getStructure();
$module = $modules[$moduleConst]['value'] ?? null;
$submodule = $modules[$moduleConst]['submodules'][$submoduleName]['value'] ?? null;
if (empty($module)) {
return false;
}
if (!empty($submoduleName) && empty($submodule)) {
return false;
}
return findModule($module, $submodule, false);
}
protected function getComponents(): \Generator
{
if (!$this->componentsLocator) {
return;
}
foreach ($this->componentsLocator->getComponents() as $component) {
// If component not used on shop, skip
if (!isset($component['shop'])) {
continue;
}
yield $component['name'] => [
'version' => $component['shop']['version'],
'version_js' => $component['shop']['js'],
'version_css' => $component['shop']['css'],
'entrypoints' => $component['entrypoints'],
'template' => $component['template'],
'class' => $component['className'],
'selector' => "'[data-component=\"{$component['className']}\"]'",
];
}
}
}