1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Rivescript-php |
4
|
|
|
* |
5
|
|
|
* (c) Johnny Mast <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Axiom\Rivescript\Cortex\ContentLoader; |
12
|
|
|
|
13
|
|
|
use Axiom\Rivescript\Exceptions\ContentLoadingException; |
14
|
|
|
use DirectoryIterator; |
15
|
|
|
use SplFileObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ContentLoader class |
19
|
|
|
* |
20
|
|
|
* The ContentLoader assists in loading information |
21
|
|
|
* into the Rivescript interpreter. The data will be |
22
|
|
|
* store in a stream for the brain to learn |
23
|
|
|
* from. |
24
|
|
|
* |
25
|
|
|
* PHP version 7.4 and higher. |
26
|
|
|
* |
27
|
|
|
* @see https://www.php.net/manual/en/stream.streamwrapper.example-1.php |
28
|
|
|
* @see https://www.php.net/manual/en/class.streamwrapper |
29
|
|
|
* |
30
|
|
|
* @category Core |
31
|
|
|
* @package Cortext\ContentLoader |
32
|
|
|
* @author Johnny Mast <[email protected]> |
33
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
34
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
35
|
|
|
* @since 0.4.0 |
36
|
|
|
*/ |
37
|
|
|
class ContentLoader |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* The stream resource. |
41
|
|
|
* |
42
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
private $stream; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The stream wrapper name. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected string $name = 'rivescript'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Flag indicating if the stream-wrapper |
55
|
|
|
* was successfully registered. |
56
|
|
|
* |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected bool $isRegistered = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* ContentLoader constructor. |
63
|
|
|
* |
64
|
|
|
* @throws \Axiom\Rivescript\Exceptions\ContentLoadingException |
65
|
|
|
*/ |
66
|
|
|
public function __construct() |
67
|
|
|
{ |
68
|
|
|
$existed = in_array($this->name, stream_get_wrappers(), true); |
69
|
|
|
if ($existed) { |
70
|
|
|
stream_wrapper_unregister($this->name); |
71
|
|
|
} |
72
|
|
|
if (stream_wrapper_register($this->name, ContentStream::class) === true) { |
73
|
|
|
$this->stream = fopen($this->name . "://input", 'wb+'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_resource($this->stream) === false) { |
77
|
|
|
throw new ContentLoadingException("Could not instantiate new rivescript content stream."); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* ContentLoader deconstruct. |
83
|
|
|
*/ |
84
|
|
|
public function __destruct() |
85
|
|
|
{ |
86
|
|
|
if (is_resource($this->getStream())) { |
87
|
|
|
fclose($this->getStream()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the stream. |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getStream() |
97
|
|
|
{ |
98
|
|
|
return $this->stream; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Load strings / files or whole directories |
103
|
|
|
* into the brain. |
104
|
|
|
* |
105
|
|
|
* @param mixed $info Information about what to load. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function load($info): void |
110
|
|
|
{ |
111
|
|
|
if (is_string($info) === true && file_exists($info) === false) { |
112
|
|
|
$this->writeToMemory($info); |
113
|
|
|
} elseif (is_string($info) === true && is_dir($info) === true) { |
114
|
|
|
$this->loadDirectory($info); |
115
|
|
|
} elseif (is_string($info) === true && file_exists($info) === true) { |
116
|
|
|
$this->loadFile($info); |
117
|
|
|
} elseif (is_array($info) === true && count($info) > 0) { |
118
|
|
|
foreach ($info as $file) { |
119
|
|
|
if (file_exists($file)) { |
120
|
|
|
$this->loadFile($file); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Load rivescript files from a given path. |
128
|
|
|
* |
129
|
|
|
* @param string $path Path to the directory to load. |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
public function loadDirectory(string $path): void |
134
|
|
|
{ |
135
|
|
|
foreach (new DirectoryIterator($path) as $file) { |
136
|
|
|
if ($file->isDot()) { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->loadFile($file->getPathname()); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Load a single file into memory. |
146
|
|
|
* |
147
|
|
|
* @param string $filename The Rivescript file to load. |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
public function loadFile(string $filename): void |
152
|
|
|
{ |
153
|
|
|
$file = new SplFileObject($filename, "rb"); |
154
|
|
|
|
155
|
|
|
while (!$file->eof()) { |
156
|
|
|
$line = $file->fgets(); |
157
|
|
|
$this->writeToMemory($line); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Write rivescript interpretable strings into |
163
|
|
|
* its collective brain. |
164
|
|
|
* |
165
|
|
|
* @param string $text The text to write to memory. |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public function writeToMemory(string $text): void |
170
|
|
|
{ |
171
|
|
|
if ($this->stream) { |
172
|
|
|
fwrite($this->stream, $text); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|