3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\CLI;
17
* Input and Output for CLI.
22
* Is the readline library on the system?
24
private readonly bool $readlineSupport;
26
public function __construct()
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');
35
* Get input from the shell, using readline or the standard STDIN
37
* Named options must be in the following formats:
38
* php index.php user -v --v -name=John --name=John
40
* @param string|null $prefix You may specify a string with which to prompt the user.
42
public function input(?string $prefix = null): string
44
// readline() can't be tested.
45
if ($this->readlineSupport && ENVIRONMENT !== 'testing') {
46
return readline($prefix); // @codeCoverageIgnore
51
$input = fgets(fopen('php://stdin', 'rb'));
53
if ($input === false) {
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.
65
* For now, just echo the content, but look into a better
66
* solution down the road.
68
* @param resource $handle
70
public function fwrite($handle, string $string): void
78
fwrite($handle, $string);