Skip to content

Commit

Permalink
Correct folder detection
Browse files Browse the repository at this point in the history
  • Loading branch information
martyf committed Jan 10, 2023
1 parent 8334d12 commit 8118815
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
30 changes: 22 additions & 8 deletions src/Fieldtypes/Iconamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace MityDigital\Iconamic\Fieldtypes;

use DirectoryIterator;
use MityDigital\Iconamic\Facades\Iconamic as IconamicFacade;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Statamic\Fields\Fieldtype;

class Iconamic extends Fieldtype
Expand All @@ -25,7 +28,7 @@ public function preload(): array
$pathHelper = $this->config('path_helper', 'default');

$enableRecursiveMode = false;
switch($this->config('recursive', 'default')) {
switch ($this->config('recursive', 'default')) {
case 'false':
$enableRecursiveMode = false;
break;
Expand All @@ -41,15 +44,19 @@ public function preload(): array

if ($enableRecursiveMode) {
// recursively list files
$dir = new \DirectoryIterator(IconamicFacade::getPath($path, $pathHelper));
$dir = new DirectoryIterator(IconamicFacade::getPath($path, $pathHelper));
$path = IconamicFacade::getPath($path, $pathHelper);
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$index = 0;
foreach ($files as $file) {
if ($file->getExtension() == 'svg') {
if ($file->isFile() && $file->getExtension() == 'svg') {
// get the new name
$name = str_replace($path.'/', '', $file->getRealPath());
$key = str_replace('.svg', '', $name);

$key = $name;
if (str_ends_with($name, '.svg')) {
$key = substr($key, 0, -4);
}

$svg = file_get_contents($file->getRealPath());

Expand All @@ -61,11 +68,18 @@ public function preload(): array
}
} else {
// the old way of doing it
$dir = new \DirectoryIterator(IconamicFacade::getPath($path, $pathHelper));
$dir = new DirectoryIterator(IconamicFacade::getPath($path, $pathHelper));
$index = 0;
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot() && $fileinfo->getExtension() == 'svg') {
$key = str_replace('.svg', '', $fileinfo->getBasename());

if (!$fileinfo->isDot() && $fileinfo->isFile() && $fileinfo->getExtension() == 'svg') {

$name = str_replace($path.'/', '', $fileinfo->getBasename());
$key = $name;
if (str_ends_with($name, '.svg')) {
$key = substr($key, 0, -4);
}

$svg = file_get_contents($fileinfo->getRealPath());

// clean that svg (ick)
Expand Down
3 changes: 2 additions & 1 deletion src/Support/Iconamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MityDigital\Iconamic\Support;

use DOMDocument;
use MityDigital\Iconamic\Exceptions\IconamicException;

class Iconamic
Expand All @@ -27,7 +28,7 @@ public function cleanSvg(string $svg, int $index, array $attributes = []): strin
$svg = str_replace('url(#', 'url(#iconamic-'.$index.'-', $svg);

// replace attributes
$dom = new \DOMDocument();
$dom = new DOMDocument();
$dom->loadXML($svg, LIBXML_NOERROR);

// if we have classReplace, do this first because it may be appended to by "class"
Expand Down

0 comments on commit 8118815

Please sign in to comment.