Completed
Push — master ( d0a237...a1b9c8 )
by Freek
9s
created

NamespacedDirectory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\BladeX\ComponentDirectory;
4
5
use Illuminate\Support\Facades\View;
6
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
7
use Symfony\Component\Finder\SplFileInfo;
8
9
class NamespacedDirectory extends ComponentDirectory
10
{
11
    /** @var string */
12
    protected $namespace;
13
14
    /** @var string */
15
    protected $viewDirectory;
16
17
    public function __construct(string $viewDirectory)
18
    {
19
        [$this->namespace, $viewDirectory] = explode('::', $viewDirectory);
20
21
        $this->viewDirectory = str_before($viewDirectory, '*');
22
    }
23
24
    public function getAbsoluteDirectory(): string
25
    {
26
        $viewPath = str_replace('.', '/', $this->viewDirectory);
27
28
        $absoluteDirectory = View::getFinder()->getHints()[$this->namespace][0] ?? null;
29
30
        if (! $absoluteDirectory) {
31
            throw CouldNotRegisterComponent::viewPathNotFound($viewPath);
32
        }
33
34
        return $absoluteDirectory;
35
    }
36
37
    public function getViewName(SplFileInfo $viewFile): string
38
    {
39
        $view = str_replace_last('.blade.php', '', $viewFile->getFilename());
40
41
        $viewDirectory = '';
42
43
        if ($this->viewDirectory !== '') {
44
            $viewDirectory = $this->viewDirectory;
45
        }
46
47
        return "{$this->namespace}::{$viewDirectory}{$view}";
48
    }
49
}