1 | <?php |
||
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() |
|
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) |
|
49 | |||
50 | /** |
||
51 | * Add a directory to search for art in |
||
52 | * |
||
53 | * @param string $dir |
||
54 | */ |
||
55 | 76 | protected function addDir($dir) |
|
66 | |||
67 | /** |
||
68 | * Find a valid art path |
||
69 | * |
||
70 | * @param string $art |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 4 | protected function artDir($art) |
|
78 | |||
79 | /** |
||
80 | * Find a valid art path |
||
81 | * |
||
82 | * @param string $art |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 72 | protected function artFile($art) |
|
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) |
|
147 | } |
||
148 |