Skip to content

Commit 95c4a0b

Browse files
author
Maxim Vanhove
committed
Get filename by searching the dir
1 parent d6399eb commit 95c4a0b

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/FontAwesomeSVG.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,14 @@ public function get_svg($id, $opts=[]) {
174174
public function get_icon_details($id) {
175175
$icon = array();
176176

177-
$id = explode(' ', $id);
178-
$dir = $this->get_icon_dir($id);
179-
$filename = $this->get_icon_filename(end($id));
177+
$classes = explode(' ', $id);
178+
$dir = $this->get_icon_dir($classes);
179+
$filename = $this->get_icon_filename($classes, $dir);
180+
$filepath = $this->get_icon_filepath($dir, $filename);
180181

181182
$icon['dir'] = $dir;
182183
$icon['filename'] = $filename;
183-
$icon['filepath'] = str_replace('/', DIRECTORY_SEPARATOR, "$this->svg_dir/$dir/$filename.svg");
184+
$icon['filepath'] = $filepath;
184185

185186
if(!is_file($icon['filepath'])) {
186187
throw new Exception('File ' . $icon['filepath'] . ' does not exist.');
@@ -233,10 +234,36 @@ public function get_icon_dir($classes) {
233234
/**
234235
* Get the icon's SVG file name
235236
*
236-
* @param string $icon_name
237+
* @param array $classes
238+
* @param string $dir
237239
* @return string
238240
*/
239-
public function get_icon_filename($icon_name) {
240-
return str_replace('fa-', '', $icon_name);
241+
public function get_icon_filename($classes, $dir) {
242+
foreach ($classes as $class) {
243+
$filename = str_replace('fa-', '', $class);
244+
$path = $this->get_icon_filepath($dir, $filename);
245+
246+
if (is_file($path)) {
247+
return $filename;
248+
}
249+
}
250+
251+
$id = join(' ', $classes);
252+
253+
throw new Exception("No icon found for '$id'");
254+
}
255+
256+
257+
258+
259+
/**
260+
* Get the icon's SVG file path
261+
*
262+
* @param string $dir
263+
* @param string $filename
264+
* @return string
265+
*/
266+
public function get_icon_filepath($dir, $filename) {
267+
return str_replace('/', DIRECTORY_SEPARATOR, "$this->svg_dir/$dir/$filename.svg");
241268
}
242269
}

0 commit comments

Comments
 (0)