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

ICanBoogie / Module / 4245494566

pending completion
4245494566

push

github

Olivier Laviale
Resolve templates using Config instead of Collection

115 of 432 relevant lines covered (26.62%)

0.76 hits per line

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

4.55
/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 TemplateResolver $template_resolver,
29
        private readonly Config $config
30
    ) {
31
    }
1✔
32

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

40
        if ($template_pathname) {
×
41
            return $template_pathname;
×
42
        }
43

44
        $descriptors = $this->config->descriptors;
×
45
        $module_id = $this->resolve_module_id($name);
×
46

47
        if (!$module_id || empty($descriptors[$module_id])) {
×
48
            return null;
×
49
        }
50

51
        return $this->resolve_from_module($descriptors, $module_id, $name, $extensions, $tried);
×
52
    }
53

54
    /**
55
     * Resolves module identifier form a template name.
56
     *
57
     * @return string|null The module identifier or `null` if it cannot be determined.
58
     */
59
    private function resolve_module_id(string $name): ?string
60
    {
61
        if (!preg_match('#^([^/]+)#', $name, $matches)) {
×
62
            return null;
×
63
        }
64

65
        return $matches[1];
×
66
    }
67

68
    /**
69
     * Resolves a template from a module and its parents.
70
     *
71
     * @param array<string, Descriptor> $descriptors
72
     * @param string[] $extensions
73
     * @param string[] $tried
74
     */
75
    private function resolve_from_module(
76
        array $descriptors,
77
        string $module_id,
78
        string $name,
79
        array $extensions,
80
        array &$tried = []
81
    ): ?string {
82
        $paths = [];
×
83
        $name = substr($name, strlen($module_id) + 1);
×
84

85
        while ($module_id) {
×
86
            $descriptor = $descriptors[$module_id];
×
87

88
            if (!$descriptor->path) {
×
89
                continue;
×
90
            }
91

92
            $paths[] = $descriptor->path . 'templates' . DIRECTORY_SEPARATOR;
×
93

94
            $module_id = $descriptor->parent;
×
95
        }
96

97
        $tries = $this->resolve_tries($paths, $name, $extensions);
×
98

99
        return $this->resolve_path($tries, $tried);
×
100
    }
101
}
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