ci4

Форк
0
/
InputOutput.php 
80 строк · 1.9 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\CLI;
15

16
/**
17
 * Input and Output for CLI.
18
 */
19
class InputOutput
20
{
21
    /**
22
     * Is the readline library on the system?
23
     */
24
    private readonly bool $readlineSupport;
25

26
    public function __construct()
27
    {
28
        // Readline is an extension for PHP that makes interactivity with PHP
29
        // much more bash-like.
30
        // http://www.php.net/manual/en/readline.installation.php
31
        $this->readlineSupport = extension_loaded('readline');
32
    }
33

34
    /**
35
     * Get input from the shell, using readline or the standard STDIN
36
     *
37
     * Named options must be in the following formats:
38
     * php index.php user -v --v -name=John --name=John
39
     *
40
     * @param string|null $prefix You may specify a string with which to prompt the user.
41
     */
42
    public function input(?string $prefix = null): string
43
    {
44
        // readline() can't be tested.
45
        if ($this->readlineSupport && ENVIRONMENT !== 'testing') {
46
            return readline($prefix); // @codeCoverageIgnore
47
        }
48

49
        echo $prefix;
50

51
        $input = fgets(fopen('php://stdin', 'rb'));
52

53
        if ($input === false) {
54
            $input = '';
55
        }
56

57
        return $input;
58
    }
59

60
    /**
61
     * While the library is intended for use on CLI commands,
62
     * commands can be called from controllers and elsewhere
63
     * so we need a way to allow them to still work.
64
     *
65
     * For now, just echo the content, but look into a better
66
     * solution down the road.
67
     *
68
     * @param resource $handle
69
     */
70
    public function fwrite($handle, string $string): void
71
    {
72
        if (! is_cli()) {
73
            echo $string;
74

75
            return;
76
        }
77

78
        fwrite($handle, $string);
79
    }
80
}
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.