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