• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Jagepard / Rudra-Cli / 28709538676

04 Jul 2026 02:39PM UTC coverage: 87.179% (+0.3%) from 86.842%
28709538676

push

github

web-flow
Merge pull request #58 from Jagepard/wip

Wip

34 of 39 relevant lines covered (87.18%)

2.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.18
/src/Console.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * This Source Code Form is subject to the terms of the Mozilla Public
5
 * License, v. 2.0. If a copy of the MPL was not distributed with this
6
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
 *
8
 * @author  Korotkov Danila (Jagepard) <jagepard@yandex.ru>
9
 * @license https://mozilla.org/MPL/2.0/  MPL-2.0
10
 */
11

12
namespace Rudra\Cli;
13

14
use Rudra\Exceptions\LogicException;
15

16
class Console implements ConsoleInterface
17
{
18
    // Colors of text decoration in the console
19
    public const array COLOR = [
20
        "default"       => 39,
21
        "black"         => 30,
22
        "red"           => 31,
23
        "green"         => 32,
24
        "yellow"        => 33,
25
        "blue"          => 34,
26
        "magenta"       => 35,
27
        "cyan"          => 36,
28
        "light_gray"    => 37,
29
        "dark_gray"     => 90,
30
        "light_red"     => 91,
31
        "light_green"   => 92,
32
        "light_yellow"  => 93,
33
        "light_blue"    => 94,
34
        "light_magenta" => 95,
35
        "light_cyan"    => 96,
36
        "white"         => 97,
37
    ];
38

39
    private array $registry = [];
40

41
    /** @var resource|null */
42
    private mixed $stdin    = null;
43

44
    /**
45
     * @throws \InvalidArgumentException
46
     */
47
    public function setStdin(mixed $stream): void
3✔
48
    {
49
        if ($stream !== null && !is_resource($stream)) {
3✔
50
            throw new \InvalidArgumentException('Argument #1 ($stream) must be of type resource or null');
×
51
        }
52
        
53
        $this->stdin = $stream;
3✔
54
    }
55

56
    /**
57
     * Prints formatted text with foreground and background colors
58
     */
59
    #[\Override]
60
    public function printer(string $text, string $fg = "default", string $bg = "default"): void
4✔
61
    {
62
        $this->checkColorExists($fg);
4✔
63
        $this->checkColorExists($bg);
4✔
64

65
        $fgCode = self::COLOR[$fg];
4✔
66
        $bgCode = self::COLOR[$bg] + 10;
4✔
67

68
        // Remove trailing newlines
69
        $text = rtrim($text, "\n\r");
4✔
70
        
71
        // \e[49m explicitly resets background color
72
        echo "\e[{$fgCode};{$bgCode}m{$text}\e[49m\e[0m" . PHP_EOL;
4✔
73
    }
74

75
    /**
76
     * Get the data entered in the console
77
     * 
78
     * @throws LogicException
79
     */
80
    #[\Override]
81
    public function reader(): string
3✔
82
    {
83
        $this->stdin ??= fopen("php://stdin", "r");
3✔
84
        
85
        if ($this->stdin === false) {
3✔
86
            throw new LogicException('Failed to open stdin stream');
×
87
        }
88
        
89
        $result = fgets($this->stdin);
3✔
90

91
        if ($result === false) {
3✔
92
            throw new LogicException('Failed to read from stdin or EOF reached');
×
93
        }
94

95
        return $result;
3✔
96
    }
97

98
    /**
99
     * @throws LogicException
100
     */
101
    #[\Override]
102
    public function addCommand(string $name, array $command): void
2✔
103
    {
104
        if (array_key_exists($name, $this->registry)) {
2✔
105
            throw new LogicException("Command $name already exists");
1✔
106
        }
107
        
108
        $this->registry[$name] = $command;
2✔
109
    }
110

111
    /**
112
     * Calls command methods
113
     */
114
    #[\Override]
115
    public function invoke(array $inputArgs): void
2✔
116
    {
117
        $firstKey = array_key_first($inputArgs);
2✔
118

119
        if ($firstKey === null) {
2✔
120
            $this->printer("⚠️  No command provided" . PHP_EOL, 'light_yellow');
×
121
            return;
×
122
        }
123

124
        if (!array_key_exists($firstKey, $this->registry)) {
2✔
125
            $this->printer("⚠️  Command \"$firstKey\" not found" . PHP_EOL, 'light_yellow');
1✔
126
            return;
1✔
127
        }
128

129
        $class  = new $this->registry[$firstKey][0];
1✔
130
        $method = $this->registry[$firstKey][1] ?? "actionIndex";
1✔
131

132
        $class->$method();
1✔
133
    }
134

135
    /**
136
     * @return array<string, array>
137
     */
138
    #[\Override]
139
    public function getRegistry(): array
1✔
140
    {
141
        return $this->registry;
1✔
142
    }
143

144
    /**
145
     * @throws LogicException
146
     */
147
    private function checkColorExists(string $key): void
5✔
148
    {
149
        if (!array_key_exists($key, self::COLOR)) {
5✔
150
            throw new LogicException("Color $key doesn't exist");
1✔
151
        }
152
    }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc