1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\CLImate\TerminalObject\Helper; |
4
|
|
|
|
5
|
|
|
use League\CLImate\Exceptions\UnexpectedValueException; |
6
|
|
|
|
7
|
|
|
use function preg_quote; |
8
|
|
|
|
9
|
|
|
trait Art |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The directories we should be looking for art in |
13
|
|
|
* |
14
|
|
|
* @var array $art_dirs |
15
|
|
|
*/ |
16
|
|
|
protected $art_dirs = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The default art if we can't find what the user requested |
20
|
|
|
* |
21
|
|
|
* @var string $default_art |
22
|
|
|
*/ |
23
|
|
|
protected $default_art = '404'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The art requested by the user |
27
|
|
|
* |
28
|
|
|
* @var string $art |
29
|
|
|
*/ |
30
|
|
|
protected $art = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
80 |
|
* Specify which settings Draw needs to import |
34
|
|
|
* |
35
|
80 |
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function settings() |
38
|
|
|
{ |
39
|
|
|
return ['Art']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
44 |
|
* Import the Art setting (any directories the user added) |
44
|
|
|
* |
45
|
44 |
|
* @param \League\CLImate\Settings\Art $setting |
46
|
44 |
|
*/ |
47
|
44 |
|
public function importSettingArt($setting) |
48
|
44 |
|
{ |
49
|
|
|
foreach ($setting->dirs as $dir) { |
50
|
|
|
$this->addDir($dir); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
80 |
|
* Add a directory to search for art in |
56
|
|
|
* |
57
|
|
|
* @param string $dir |
58
|
|
|
*/ |
59
|
80 |
|
protected function addDir($dir) |
60
|
|
|
{ |
61
|
|
|
// Add any additional directories to the top of the array |
62
|
80 |
|
// so that the user can override art |
63
|
80 |
|
array_unshift($this->art_dirs, rtrim($dir, \DIRECTORY_SEPARATOR)); |
64
|
80 |
|
|
65
|
80 |
|
// Keep the array clean |
66
|
|
|
$this->art_dirs = array_unique($this->art_dirs); |
67
|
|
|
$this->art_dirs = array_filter($this->art_dirs); |
68
|
|
|
$this->art_dirs = array_values($this->art_dirs); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Find a valid art path |
73
|
|
|
* |
74
|
4 |
|
* @param string $art |
75
|
|
|
* |
76
|
4 |
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function artDir($art) |
79
|
|
|
{ |
80
|
|
|
return $this->fileSearch($art, preg_quote(\DIRECTORY_SEPARATOR) . '*.*'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Find a valid art path |
85
|
|
|
* |
86
|
76 |
|
* @param string $art |
87
|
|
|
* |
88
|
76 |
|
* @return string |
89
|
|
|
*/ |
90
|
76 |
|
protected function artFile($art) |
91
|
8 |
|
{ |
92
|
8 |
|
$files = $this->fileSearch($art, '(\.[^' . preg_quote(\DIRECTORY_SEPARATOR) . ']*)?$'); |
93
|
8 |
|
|
94
|
|
|
if (count($files) === 0) { |
95
|
76 |
|
$this->addDir(__DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'ASCII'); |
96
|
|
|
$files = $this->fileSearch($this->default_art, '.*'); |
97
|
|
|
} |
98
|
|
|
|
99
|
76 |
|
if (count($files) === 0) { |
100
|
|
|
throw new UnexpectedValueException("Unable to find an art file with the name '{$art}'"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return reset($files); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Find a set of files in the current art directories |
108
|
|
|
* based on a pattern |
109
|
|
|
* |
110
|
|
|
* @param string $art |
111
|
80 |
|
* @param string $pattern |
112
|
|
|
* |
113
|
80 |
|
* @return array |
114
|
80 |
|
*/ |
115
|
|
|
protected function fileSearch($art, $pattern) |
116
|
80 |
|
{ |
117
|
80 |
|
foreach ($this->art_dirs as $dir) { |
118
|
|
|
$directory_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)); |
119
|
80 |
|
|
120
|
80 |
|
$paths = []; |
121
|
80 |
|
$regex = '~' . preg_quote($art) . $pattern . '~'; |
122
|
|
|
|
123
|
|
|
foreach ($directory_iterator as $file) { |
124
|
|
|
if ($file->isDir()) { |
125
|
80 |
|
continue; |
126
|
80 |
|
} |
127
|
80 |
|
|
128
|
80 |
|
// Look for anything that has the $art filename |
129
|
|
|
if (preg_match($regex, $file)) { |
130
|
80 |
|
$paths[] = $file->getPathname(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
80 |
|
|
134
|
80 |
|
asort($paths); |
135
|
|
|
|
136
|
8 |
|
// If we've got one, no need to look any further |
137
|
|
|
if (!empty($paths)) { |
138
|
8 |
|
return $paths; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return []; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Parse the contents of the file and return each line |
147
|
|
|
* |
148
|
80 |
|
* @param string $path |
149
|
|
|
* |
150
|
80 |
|
* @return array |
151
|
80 |
|
*/ |
152
|
80 |
|
protected function parse($path) |
153
|
|
|
{ |
154
|
80 |
|
$output = file_get_contents($path); |
155
|
|
|
$output = explode("\n", $output); |
156
|
|
|
$output = array_map('rtrim', $output); |
157
|
|
|
|
158
|
|
|
return $output; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|