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

CPS-IT / handlebars / 25154484073

30 Apr 2026 08:04AM UTC coverage: 87.199% (-2.6%) from 89.817%
25154484073

Pull #563

github

web-flow
Merge 5574587d9 into 000208fdc
Pull Request #563: [BUGFIX] Limit view factory override implementation to certain services

0 of 46 new or added lines in 1 file covered. (0.0%)

1376 of 1578 relevant lines covered (87.2%)

6.91 hits per line

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

0.0
/Classes/DependencyInjection/CompilerPass/HandlebarsViewFactoryPass.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17

18
namespace CPSIT\Typo3Handlebars\DependencyInjection\CompilerPass;
19

20
use CPSIT\Typo3Handlebars\View;
21
use Symfony\Component\DependencyInjection;
22

23
/**
24
 * HandlebarsViewFactoryPass
25
 *
26
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
27
 * @license GPL-2.0-or-later
28
 *
29
 * @phpstan-type TargetService array{calls?: array<string, string>, properties?: list<string>}
30
 */
31
final class HandlebarsViewFactoryPass extends DependencyInjection\Compiler\AbstractRecursivePass
32
{
33
    private ?DependencyInjection\Reference $viewFactoryReference = null;
34

35
    /**
36
     * @var array<string, TargetService>
37
     */
38
    private array $targetServices = [];
39

NEW
40
    public function __construct(
×
41
        private readonly string $viewFactoryServiceId = View\HandlebarsViewFactory::class,
NEW
42
    ) {}
×
43

NEW
44
    public function process(DependencyInjection\ContainerBuilder $container): void
×
45
    {
NEW
46
        if (!$container->has($this->viewFactoryServiceId)) {
×
NEW
47
            return;
×
48
        }
49

NEW
50
        $this->viewFactoryReference = new DependencyInjection\Reference($this->viewFactoryServiceId);
×
51

52
        try {
NEW
53
            parent::process($container);
×
54
        } finally {
NEW
55
            $this->viewFactoryReference = null;
×
56
        }
57
    }
58

NEW
59
    protected function processValue(mixed $value, bool $isRoot = false): mixed
×
60
    {
NEW
61
        $value = parent::processValue($value, $isRoot);
×
62

NEW
63
        if (!($value instanceof DependencyInjection\Definition)
×
NEW
64
            || $this->currentId === null
×
NEW
65
            || $this->viewFactoryReference === null
×
NEW
66
            || !$value->isAutowired()
×
NEW
67
            || $value->isAbstract()
×
NEW
68
            || $value->getClass() === null
×
69
        ) {
NEW
70
            return $value;
×
71
        }
72

NEW
73
        $configuration = $this->targetServices[$this->currentId] ?? $this->resolveBaseConfiguration($value->getClass());
×
74

NEW
75
        if ($configuration === null) {
×
NEW
76
            return $value;
×
77
        }
78

NEW
79
        foreach ($configuration['calls'] ?? [] as $methodName => $argumentName) {
×
NEW
80
            $value->removeMethodCall($methodName);
×
NEW
81
            $value->addMethodCall($methodName, [$argumentName => $this->viewFactoryReference]);
×
82
        }
83

NEW
84
        foreach ($configuration['properties'] ?? [] as $propertyName) {
×
NEW
85
            $value->setArgument($propertyName, $this->viewFactoryReference);
×
86
        }
87

NEW
88
        return $value;
×
89
    }
90

91
    /**
92
     * @return TargetService|null
93
     */
NEW
94
    private function resolveBaseConfiguration(string $className): ?array
×
95
    {
NEW
96
        $reflection = $this->container?->getReflectionClass($className, false);
×
97

NEW
98
        if ($reflection === null) {
×
NEW
99
            return null;
×
100
        }
101

NEW
102
        $current = $reflection;
×
103

104
        do {
NEW
105
            if (array_key_exists($current->getName(), $this->targetServices)) {
×
NEW
106
                return $this->targetServices[$current->getName()];
×
107
            }
108

NEW
109
            $current = $current->getParentClass();
×
NEW
110
        } while ($current !== false);
×
111

NEW
112
        return null;
×
113
    }
114

NEW
115
    public function addMethodCall(string $serviceId, string $methodName, string $argumentName = '$viewFactory'): self
×
116
    {
NEW
117
        $this->targetServices[$serviceId] ??= [];
×
NEW
118
        $this->targetServices[$serviceId]['calls'] ??= [];
×
NEW
119
        $this->targetServices[$serviceId]['calls'][$methodName] = $argumentName;
×
120

NEW
121
        return $this;
×
122
    }
123

NEW
124
    public function addProperty(string $serviceId, string $propertyName = '$viewFactory'): self
×
125
    {
NEW
126
        $this->targetServices[$serviceId] ??= [];
×
NEW
127
        $this->targetServices[$serviceId]['properties'] ??= [];
×
NEW
128
        $this->targetServices[$serviceId]['properties'][] = $propertyName;
×
129

NEW
130
        return $this;
×
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc