diff options
| -rw-r--r-- | src/FileString.php | 42 | ||||
| -rw-r--r-- | tests/Unit/FileStringTest.php | 21 | 
2 files changed, 63 insertions, 0 deletions
| diff --git a/src/FileString.php b/src/FileString.php new file mode 100644 index 0000000..0bdca09 --- /dev/null +++ b/src/FileString.php @@ -0,0 +1,42 @@ +<?php + +namespace Lightscale\LaralightAssets; + +use Illuminate\Support\Str; +use Illuminate\Support\Collection; + +class FileString +{ +    public ?string $manifestName = null; +    public string $fileUri; + + +    public function __construct(string $file) +    { +        $escape = '\\'; +        $seperator = '::'; +        $seperatorLen = strlen($seperator); + +        $pos = 0; +        while( +            $pos !== false +        ) { +            $pos = strpos($file, $seperator, $pos); +            if ($pos > 0 && $file[$pos - 1] === $escape) { +                $pos += $seperatorLen; +            } +            else break; +        } + + +        $unescape = fn($v) => str_replace($escape . $seperator, $seperator, $v); + +        if ($pos === false) { +            $this->fileUri = $unescape($file); +        } +        else { +            $this->manifestName = $unescape(substr($file, 0, $pos)); +            $this->fileUri = $unescape(substr($file, $pos + $seperatorLen)); +        } +    } +} diff --git a/tests/Unit/FileStringTest.php b/tests/Unit/FileStringTest.php new file mode 100644 index 0000000..2d8bc26 --- /dev/null +++ b/tests/Unit/FileStringTest.php @@ -0,0 +1,21 @@ +<?php + +use Lightscale\LaralightAssets\FileString; + +test(':dataset', function(string $input, ?string $manifest, string $file) { +    $f = new FileString($input); +    expect($f->manifestName)->toBe($manifest); +    expect($f->fileUri)->toBe($file); +})->with([ +    'manifest string 1' => ['main::fake.css', 'main', 'fake.css'], +    'manifest string 2' => ['main::test::fake.css', 'main', 'test::fake.css'], +    'manifest string 3' => ['main::test\::fake.css', 'main', 'test::fake.css'], +    'manifest escaped 1' => ['main\::test::fake.css', 'main::test', 'fake.css'], +    'manifest escaped 2' => ['main\::test::fake::fake.css', 'main::test', 'fake::fake.css'], +    'manifest escaped 3' => ['main\::test::fake\::fake.css', 'main::test', 'fake::fake.css'], +    'manifest escaped 4' => ['main\::test\::fake::fake.css', 'main::test::fake', 'fake.css'], +    'no manifest 1' => ['fake.css', null, 'fake.css'], +    'no manifest 2' => ['http://lightscale.co.uk/fake.css', null, 'http://lightscale.co.uk/fake.css'], +    'no manifest 3' => ['test\::fake.css', null, 'test::fake.css'], +    'no manifest 4' => ['test\::test2\::fake.css', null, 'test::test2::fake.css'], +]); | 
