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

Jagepard / Rudra-Cli / 26869090074

03 Jun 2026 07:02AM UTC coverage: 91.429% (-2.5%) from 93.939%
26869090074

push

github

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

Wip

1 of 2 new or added lines in 1 file covered. (50.0%)

32 of 35 relevant lines covered (91.43%)

2.34 hits per line

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

91.43
/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
    /*
19
     * Colors of text decoration in the console
20
     * ----------------
21
     * Цвета оформления текста в консоли.
22
     */
23
    public const array COLOR = [
24
        "default"       => 39,
25
        "black"         => 30,
26
        "red"           => 31,
27
        "green"         => 32,
28
        "yellow"        => 33,
29
        "blue"          => 34,
30
        "magneta"       => 35,
31
        "cyan"          => 36,
32
        "light_gray"    => 37,
33
        "dark_gray"     => 90,
34
        "light_red"     => 91,
35
        "light_green"   => 92,
36
        "light_yellow"  => 93,
37
        "light_blue"    => 94,
38
        "light_magneta" => 95,
39
        "light_cyan"    => 96,
40
        "white"         => 97,
41
    ];
42

43
    private array $registry = [];
44

45
    /** @var resource|null */
46
    private mixed $stdin    = null;
47

48
    /**
49
     * @param  resource|null $stream 
50
     * @return void
51
     */
52
    public function setStdin(mixed $stream): void
3✔
53
    {
54
        if ($stream !== null && !is_resource($stream)) {
3✔
55
            throw new \InvalidArgumentException('Argument #1 ($stream) must be of type resource or null');
×
56
        }
57
        
58
        $this->stdin = $stream;
3✔
59
    }
60

61
    /**
62
     * Prints formatted text with foreground and background colors.
63
     * ----------------
64
     * Выводит форматированный текст с цветами переднего плана и фона.
65
     *
66
     * @param string $text Text to output
67
     * @param string $fg   Foreground color (key from self::COLOR)
68
     * @param string $bg   Background color (key from self::COLOR)
69
     * @return void
70
     */
71
    #[\Override]
72
    public function printer(string $text, string $fg = "default", string $bg = "default"): void
4✔
73
    {
74
        $this->checkColorExists($fg);
4✔
75
        $this->checkColorExists($bg);
4✔
76

77
        $fgCode = self::COLOR[$fg];
4✔
78
        $bgCode = self::COLOR[$bg] + 10;
4✔
79

80
        echo "\e[{$fgCode};{$bgCode}m{$text}\e[0m";
4✔
81
    }
82

83
    /**
84
     * Get the data entered in the console.
85
     * ----------------
86
     * Получает данные, введённые в консоли.
87
     *
88
     * @return string
89
     */
90
    #[\Override]
91
    public function reader(): string
3✔
92
    {
93
        $this->stdin ??= fopen("php://stdin", "r");
3✔
94
        
95
        if ($this->stdin === false) {
3✔
NEW
96
            throw new LogicException('Failed to open stdin stream');
×
97
        }
98
        
99
        $result = fgets($this->stdin);
3✔
100

101
        if ($result === false) {
3✔
102
            throw new LogicException('Failed to read from stdin or EOF reached');
×
103
        }
104

105
        return $result;
3✔
106
    }
107

108
    /**
109
     * @param  string $name
110
     * @param  array  $command
111
     * @return void
112
     */
113
    #[\Override]
114
    public function addCommand(string $name, array $command): void
2✔
115
    {
116
        if (array_key_exists($name, $this->registry)) {
2✔
117
            throw new LogicException("Command $name already exist");
1✔
118
        }
119
        
120
        $this->registry[$name] = $command;
2✔
121
    }
122

123
    /**
124
     * Calls command methods
125
     * ----------------
126
     * Вызывает методы команды.
127
     * 
128
     * @param  array $inputArgs
129
     * @return void
130
     */
131
    #[\Override]
132
    public function invoke(array $inputArgs): void
2✔
133
    {
134
        $firstKey = array_key_first($inputArgs);
2✔
135

136
        if ($firstKey === null || !array_key_exists($firstKey, $this->registry)) {
2✔
137
            $this->printer("⚠️  Command \"$firstKey\" not found" . PHP_EOL, 'light_yellow');
1✔
138
            return;
1✔
139
        }
140

141
        $class  = new $this->registry[$firstKey][0];
1✔
142
        $method = $this->registry[$firstKey][1] ?? "actionIndex";
1✔
143

144
        $class->$method();
1✔
145
    }
146

147
    /**
148
     * @return array<string, array>
149
     */
150
    #[\Override]
151
    public function getRegistry(): array
1✔
152
    {
153
        return $this->registry;
1✔
154
    }
155

156
    /**
157
     * @param  string $key
158
     * @return void
159
     * @throws LogicException
160
     */
161
    private function checkColorExists(string $key): void
5✔
162
    {
163
        if (!array_key_exists($key, self::COLOR)) {
5✔
164
            throw new LogicException("Color $key doesn't exist");
1✔
165
        }
166
    }
167
}
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