Skip to content

Commit 5ca2e7b

Browse files
Реализация компонента
0 parents  commit 5ca2e7b

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Maxim N Epikhin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# php-headers
2+
3+
Компонент для работы с консолью
4+
5+
# Структура
6+
7+
```
8+
src/
9+
--- interfaces/
10+
--- Console.php
11+
```
12+
13+
В директории `interfaces` хранятся необходимые интерфейсы, которые необходимо имплементировать в при реализации
14+
собственного класса `Console`.
15+
16+
Класс `Console` реализует интерфейс `ConsoleInterface` для управления консолью.
17+
18+
# Доступные методы
19+
20+
| Метод | Аргументы | Возвращаемые данные | Исключения | Описание |
21+
|-----------------------------------------------------|---------------------------------------------------------|---------------------|------------|--------------------------------------------|
22+
| static readLine() | | string | | Получает (читает) введенную строку |
23+
| static clearScreen() | | void | | Очищает экран консоли |
24+
| static write(string $text, int $color = 0) | $text Строка для записи; $color Цвет шрифта | void | | Записывает строку без переноса |
25+
| static writeLine(string $text, int $color = 0) | $text Строка для записи; $color Цвет шрифта | void | | Записывает строку с переносом |
26+
| static confirm(string $text, bool $default = false) | $text Сообщение вопроса; $default Значение по умолчанию | bool | | Выводит сообщение о подтверждении действия |
27+
28+
# Контакты
29+
30+
Вы можете связаться со мной в социальной сети ВКонтакте: [ВКонтакте: Максим Епихин](https://vk.com/maximepihin)
31+
32+
Если удобно писать на почту, то можете воспользоваться этим адресом: [email protected]
33+
34+
Мой канал на YouTube, который посвящен разработке веб и игровых
35+
проектов: [YouTube: Максим Епихин](https://www.youtube.com/channel/UCKusRcoHUy6T4sei-rVzCqQ)
36+
37+
Поддержать меня можно переводом на Яндекс.Деньги: [Денежный перевод](https://yoomoney.ru/to/410012382226565)

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "mepihindeveloper/php-console",
3+
"description": "Component for working with console",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"component",
8+
"class",
9+
"php",
10+
"console"
11+
],
12+
"authors": [
13+
{
14+
"name": "mepihindeveloper",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.4"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"mepihindeveloper\\components\\": "src/"
24+
}
25+
},
26+
"minimum-stability": "dev",
27+
"prefer-stable": true
28+
}

src/Console.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace mepihindeveloper\components;
6+
7+
use mepihindeveloper\components\interfaces\ConsoleInterface;
8+
9+
/**
10+
* Класс Console
11+
*
12+
* Класс реализует считываем и записью в консоль
13+
*
14+
* @package mepihindeveloper\components
15+
*/
16+
class Console implements ConsoleInterface {
17+
18+
/**
19+
* Константы для определения цвета текста
20+
*/
21+
public const FG_WHITE = 0;
22+
public const FG_RED = 31;
23+
public const FG_GREEN = 32;
24+
/**
25+
* Константы для определения толщины шрифта
26+
*/
27+
public const FONT_NORMAL = 0;
28+
public const FONT_BOLD = 1;
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public static function clearScreen(): void {
34+
echo "\033[2J";
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public static function writeLine(string $text, int $color = 0): void {
41+
static::write($text . PHP_EOL, $color);
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
public static function write(string $text, int $color = 0): void {
48+
fwrite(STDOUT, "\033[0;{$color}m{$text}\033[0m");
49+
}
50+
51+
/**
52+
* Выводит сообщение о подтверждении действия
53+
*
54+
* @param string $text Сообщение вопроса
55+
* @param bool $default Значение по умолчанию
56+
*
57+
* @return bool
58+
*/
59+
public static function confirm(string $text, bool $default = false): bool {
60+
while (true) {
61+
static::write($text . ' (y|n) [' . ($default ? 'y' : 'n') . ']: ');
62+
$input = strtolower(trim(static::readLine()));
63+
64+
if (empty($input)) {
65+
return $default;
66+
}
67+
68+
if (!strcasecmp($input, 'y')) {
69+
return true;
70+
}
71+
72+
if (!strcasecmp($input, 'n')) {
73+
return false;
74+
}
75+
}
76+
}
77+
78+
/**
79+
* @inheritDoc
80+
*/
81+
public static function readLine(): string {
82+
return rtrim(fgets(STDIN), PHP_EOL);
83+
}
84+
}

src/interfaces/ConsoleInterface.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace mepihindeveloper\components\interfaces;
6+
7+
/**
8+
* Interface ConsoleInterface
9+
*
10+
* Декларирует методы обязательные для реализации компонента Console
11+
*
12+
* @package mepihindeveloper\components\interfaces
13+
*/
14+
interface ConsoleInterface {
15+
16+
/**
17+
* Получает (читает) введенную строку
18+
*
19+
* @return string
20+
*/
21+
public static function readLine(): string;
22+
23+
/**
24+
* Очищает экран консоли
25+
*/
26+
public static function clearScreen(): void;
27+
28+
/**
29+
* Записывает строку без переноса
30+
*
31+
* @param string $text Строка для записи
32+
* @param int $color Цвет шрифта
33+
*/
34+
public static function write(string $text, int $color = 0): void;
35+
36+
/**
37+
* Записывает строку с переносом
38+
*
39+
* @param string $text Строка для записи
40+
* @param int $color Цвет шрифта
41+
*/
42+
public static function writeLine(string $text, int $color = 0): void;
43+
}

0 commit comments

Comments
 (0)