-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleInput.php
63 lines (57 loc) · 1.29 KB
/
ConsoleInput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* OriginPHP Framework
* Copyright 2018 - 2021 Jamiel Sharief.
*
* Licensed under The MIT License
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* @copyright Copyright (c) Jamiel Sharief
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types = 1);
namespace Origin\Console;
class ConsoleInput
{
/**
* Holds the stream for reading
*
* @var resource
*/
protected $stream = null;
/**
* Constructs a new instance
* @param string $stream fopen stream php://stdin
*/
public function __construct(string $stream = 'php://stdin')
{
$this->stream = fopen($stream, 'r');
}
/**
* Reads from the stream
*
* @return string|null
*/
public function read(): ?string
{
$data = fgets($this->stream);
return $data ? trim($data) : null;
}
/**
* Closes the stream
*
* @return void
*/
public function close(): void
{
if (is_resource($this->stream)) {
fclose($this->stream);
}
}
public function __destruct()
{
$this->close();
}
}