Passed
Branch 6.0 (c154c1)
by Olivier
11:35
created

Namespaced::resolve_tries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
namespace ICanBoogie\Render\TemplateResolver;
4
5
use ICanBoogie\Render\ResolvePathTrait;
6
use ICanBoogie\Render\TemplateName;
7
use ICanBoogie\Render\TemplateResolver;
8
9
use function array_map;
10
use function explode;
11
use function rtrim;
12
use function var_dump;
13
14
use const DIRECTORY_SEPARATOR;
15
16
final class Namespaced implements TemplateResolver
17
{
18
    use ResolvePathTrait;
19
20
    /**
21
     * @var array<string, string> $namespaces
22
     *     Where _key_ is a namespace e.g. 'articles' and _value_ a directory.
23
     */
24
    private readonly array $namespaces;
25
26
    /**
27
     * @param array<string, string> $namespaces
28
     *     Where _key_ is a namespace e.g. 'articles' and _value_ a directory.
29
     */
30
    public function __construct(array $namespaces)
31
    {
32
        $this->namespaces = array_map(
0 ignored issues
show
Bug introduced by
The property namespaces is declared read-only in ICanBoogie\Render\TemplateResolver\Namespaced.
Loading history...
33
            fn(string $path) => rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR,
34
            $namespaces
35
        );
36
    }
37
38
    public function resolve(string $name, array $extensions, array &$tried = []): ?string
39
    {
40
        [ $namespace, $name ] = explode(TemplateName::TEMPLATE_NAMESPACE_SEPARATOR, $name);
41
42
        $base = $this->namespaces[$namespace] ?? null;
43
44
        if (!$base) {
45
            return null;
46
        }
47
48
        $tries = $this->resolve_tries($base, $name, $extensions);
49
50
        var_dump($tries);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($tries) looks like debug code. Are you sure you do not want to remove it?
Loading history...
51
52
        return $this->resolve_path($tries, $tried);
53
    }
54
55
    /**
56
     * @param string[] $extensions
57
     *
58
     * @return string[]
59
     */
60
    private function resolve_tries(string $base, string $name, array $extensions): array
61
    {
62
        $paths = [];
63
64
        foreach ($extensions as $extension) {
65
            $paths[] = $base . $name . $extension;
66
        }
67
68
        return $paths;
69
    }
70
}
71