|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Axiom\Rivescript\Cortex\ContentLoader; |
|
4
|
|
|
|
|
5
|
|
|
use Axiom\Rivescript\Exceptions\ContentLoadingException; |
|
6
|
|
|
use DirectoryIterator; |
|
7
|
|
|
use SplFileObject; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The ContentLoader assists in loading information |
|
11
|
|
|
* into the Rivescript interpreter. The data will be |
|
12
|
|
|
* store in a stream for the brain to learn |
|
13
|
|
|
* from. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Rivescript-php |
|
16
|
|
|
* @subpackage Core |
|
17
|
|
|
* @category ContentLoader |
|
18
|
|
|
* @author Johnny Mast <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ContentLoader class. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ContentLoader |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The stream resource. |
|
28
|
|
|
* |
|
29
|
|
|
* @var false|resource |
|
30
|
|
|
*/ |
|
31
|
|
|
private $stream; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The stream wrapper name. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $name = 'rivescript'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ContentLoader constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \Axiom\Rivescript\Exceptions\ContentLoadingException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
|
|
$existed = in_array($this->name, stream_get_wrappers()); |
|
48
|
|
|
if ($existed) { |
|
49
|
|
|
stream_wrapper_unregister($this->name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (stream_wrapper_register($this->name, ContentStream::class)) { |
|
53
|
|
|
$this->stream = fopen($this->name . "://input", "r+"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (is_resource($this->stream) === false) { |
|
57
|
|
|
throw new ContentLoadingException("Could not instantiate new rivescript content stream."); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* ContentLoader deconstruct. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __destruct() |
|
65
|
|
|
{ |
|
66
|
|
|
fclose($this->getStream()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Return the stream. |
|
71
|
|
|
* |
|
72
|
|
|
* @return false|resource |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getStream() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->stream; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Load strings / files or whole directories |
|
81
|
|
|
* into the brain. |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $info |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function load($info) |
|
88
|
|
|
{ |
|
89
|
|
|
if (is_string($info) === true && file_exists($info) === false) { |
|
90
|
|
|
$this->writeToMemory($info); |
|
91
|
|
|
} elseif (is_string($info) === true && is_dir($info) === true) { |
|
92
|
|
|
$this->loadDirectory($info); |
|
93
|
|
|
} elseif (is_string($info) === true && file_exists($info) === true) { |
|
94
|
|
|
$this->loadFile($info); |
|
95
|
|
|
} elseif (is_array($info) === true and count($info) > 0) { |
|
96
|
|
|
foreach ($info as $file) { |
|
97
|
|
|
if (file_exists($file)) { |
|
98
|
|
|
$this->loadFile($file); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Load rivescript files from a given path. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $path Path to the directory to load. |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function loadDirectory(string $path) |
|
112
|
|
|
{ |
|
113
|
|
|
foreach (new DirectoryIterator($path) as $file) { |
|
114
|
|
|
if ($file->isDot()) |
|
115
|
|
|
continue; |
|
116
|
|
|
|
|
117
|
|
|
$this->loadFile($file->getPathname()); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Load a single file into memory. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $file The Rivescript file to load. |
|
125
|
|
|
* |
|
126
|
|
|
* @return void |
|
127
|
|
|
*/ |
|
128
|
|
|
public function loadFile(string $file) |
|
129
|
|
|
{ |
|
130
|
|
|
$file = new SplFileObject($file); |
|
131
|
|
|
|
|
132
|
|
|
while (!$file->eof()) { |
|
133
|
|
|
$line = $file->fgets(); |
|
134
|
|
|
$this->writeToMemory($line); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Write rivescript interpretable strings into |
|
140
|
|
|
* its collective brain. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $text The text to write to memory. |
|
143
|
|
|
* |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function writeToMemory(string $text) |
|
147
|
|
|
{ |
|
148
|
|
|
if ($this->stream) { |
|
149
|
|
|
fwrite($this->stream, $text); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|