• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / Module / 4270677071

pending completion
4270677071

push

github

Olivier Laviale
Tidy

2 of 2 new or added lines in 2 files covered. (100.0%)

73 of 415 relevant lines covered (17.59%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/Module/ModuleTemplateResolver.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Module;
13

14
use ICanBoogie\Binding\Module\Config;
15
use ICanBoogie\Render\TemplateResolver;
16
use ICanBoogie\Render\TemplateResolverTrait;
17

18
/**
19
 * Decorates a template resolver and adds support for module defined templates.
20
 *
21
 * Templates are inherited from parents.
22
 */
23
final class ModuleTemplateResolver implements TemplateResolver
24
{
25
    use TemplateResolverTrait;
26

27
    public function __construct(
28
        private readonly Config $config
29
    ) {
30
    }
×
31

32
    /**
33
     * @inheritdoc
34
     */
35
    public function resolve(string $name, array $extensions, array &$tried = []): ?string
36
    {
37
        $descriptors = $this->config->descriptors;
×
38
        $module_id = $this->resolve_module_id($name);
×
39

40
        if (!$module_id || empty($descriptors[$module_id])) {
×
41
            return null;
×
42
        }
43

44
        return $this->resolve_from_module($descriptors, $module_id, $name, $extensions, $tried);
×
45
    }
46

47
    /**
48
     * Resolves module identifier form a template name.
49
     *
50
     * @return string|null The module identifier or `null` if it cannot be determined.
51
     */
52
    private function resolve_module_id(string $name): ?string
53
    {
54
        if (!preg_match('#^([^/]+)#', $name, $matches)) {
×
55
            return null;
×
56
        }
57

58
        return $matches[1];
×
59
    }
60

61
    /**
62
     * Resolves a template from a module and its parents.
63
     *
64
     * @param array<string, Descriptor> $descriptors
65
     * @param string[] $extensions
66
     * @param string[] $tried
67
     */
68
    private function resolve_from_module(
69
        array $descriptors,
70
        string $module_id,
71
        string $name,
72
        array $extensions,
73
        array &$tried = []
74
    ): ?string {
75
        $paths = [];
×
76
        $name = substr($name, strlen($module_id) + 1);
×
77

78
        while ($module_id) {
×
79
            $descriptor = $descriptors[$module_id];
×
80

81
            if (!$descriptor->path) {
×
82
                continue;
×
83
            }
84

85
            $paths[] = $descriptor->path . 'templates' . DIRECTORY_SEPARATOR;
×
86

87
            $module_id = $descriptor->parent;
×
88
        }
89

90
        $tries = $this->resolve_tries($paths, $name, $extensions);
×
91

92
        return $this->resolve_path($tries, $tried);
×
93
    }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc