ListModuleCommand::checkModuleNotExists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\UserInterface\Console;
13
14
use Composer\Autoload\ClassLoader;
15
use Slick\ModuleApi\Infrastructure\SlickModuleInterface;
16
use Symfony\Component\Console\Attribute\AsCommand;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Style\SymfonyStyle;
21
22
/**
23
 * ListModuleCommand
24
 *
25
 * @package Slick\WebStack\Infrastructure
26
 */
27
#[AsCommand(
28
    name: "modules:list",
29
    description: "Displays all available modules along with their enabled status.",
30
    aliases: ["modules"]
31
)]
32
final class ListModuleCommand extends Command
33
{
34
35
    use ModuleCommandTrait;
36
37
    protected string $appRoot;
38
39
    /** @var string  */
40
    protected string $moduleListFile;
41
42
    protected ?SymfonyStyle $outputStyle = null;
43
44
    public function __construct(string $appRoot, private readonly ClassLoader $classLoader)
45
    {
46
        parent::__construct();
47
        $this->appRoot = $appRoot;
48
        $this->moduleListFile = $this->appRoot . EnableModuleCommand::ENABLED_MODULES_FILE;
49
    }
50
51
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $this->outputStyle = new SymfonyStyle($input, $output);
55
56
        /** @var array<SlickModuleInterface> $existing */
57
        $existing = [];
58
        $classMap = $this->classLoader->getClassMap();
59
        foreach ($classMap as $className => $file) {
60
            $moduleClass = str_contains($file, 'Module.php')
61
                && !str_contains($file, 'AbstractModule.php')
62
                && !str_contains($file, 'Dummy')
63
            ;
64
            if (!$moduleClass) {
65
                continue;
66
            }
67
68
            $module = new $className();
69
            if ($module instanceof SlickModuleInterface) {
70
                $existing[] = $module;
71
            }
72
        }
73
74
        $this->renderTable($existing, $this->outputStyle);
75
        return Command::SUCCESS;
76
    }
77
78
    /**
79
     * Checks if a module does not exist in the given array of modules.
80
     *
81
     * @param string $moduleName The name of the module to check.
82
     * @param array<string> $modules The array of modules to check against.
83
     *
84
     * @return false|string Returns false if the module exists in the array, otherwise returns the module name.
85
     */
86
    protected function checkModuleNotExists(string $moduleName, array $modules): false|string
87
    {
88
        $retrieveModuleName = $this->retrieveModuleName($moduleName);
89
        return in_array(ltrim($retrieveModuleName, '\\'), $modules) ? false : $retrieveModuleName;
90
    }
91
}
92