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

nette / application / 20834855301

08 Jan 2026 10:54PM UTC coverage: 84.605% (+1.7%) from 82.856%
20834855301

push

github

dg
added CLAUDE.md

1940 of 2293 relevant lines covered (84.61%)

0.85 hits per line

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

91.18
/src/Bridges/ApplicationLatte/TemplateFactory.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Bridges\ApplicationLatte;
11

12
use Nette;
13
use Nette\Application\UI;
14
use function class_exists, is_a, preg_replace, property_exists, rtrim;
15

16

17
/**
18
 * Creates a class of parameters of Latte templates.
19
 */
20
class TemplateFactory implements UI\TemplateFactory
21
{
22
        /** @var array<callable(Template): void>  Occurs when a new template is created */
23
        public array $onCreate = [];
24
        private string $templateClass;
25

26

27
        public function __construct(
1✔
28
                private readonly LatteFactory $latteFactory,
29
                private readonly ?Nette\Http\IRequest $httpRequest = null,
30
                private readonly ?Nette\Security\User $user = null,
31
                $templateClass = null,
32
        ) {
33
                if ($templateClass && (!class_exists($templateClass) || !is_a($templateClass, Template::class, allow_string: true))) {
1✔
34
                        throw new Nette\InvalidArgumentException("Class $templateClass does not implement " . Template::class . ' or it does not exist.');
1✔
35
                }
36

37
                $this->templateClass = $templateClass ?? DefaultTemplate::class;
1✔
38
        }
1✔
39

40

41
        /**
42
         * @template T of Template = Template
43
         * @param class-string<T>|null $class
44
         * @return T
45
         */
46
        public function createTemplate(?UI\Control $control = null, ?string $class = null): UI\Template
1✔
47
        {
48
                $class ??= $this->templateClass;
1✔
49
                if (!is_a($class, Template::class, allow_string: true)) {
1✔
50
                        throw new Nette\InvalidArgumentException("Class $class does not implement " . Template::class . ' or it does not exist.');
×
51
                }
52

53
                $latte = $this->latteFactory->create($control);
1✔
54
                $template = new $class($latte);
1✔
55
                $this->injectDefaultVariables($template, $control);
1✔
56

57
                Nette\Utils\Arrays::invoke($this->onCreate, $template);
1✔
58

59
                return $template;
1✔
60
        }
61

62

63
        private function injectDefaultVariables(Template $template, ?UI\Control $control): void
1✔
64
        {
65
                $presenter = $control?->getPresenterIfExists();
1✔
66
                $baseUrl = $this->httpRequest
1✔
67
                        ? rtrim($this->httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/')
1✔
68
                        : null;
1✔
69
                $flashes = $presenter instanceof UI\Presenter && $presenter->hasFlashSession()
1✔
70
                        ? (array) $presenter->getFlashSession()->get($control->getParameterId('flash'))
×
71
                        : [];
1✔
72

73
                $vars = [
1✔
74
                        'user' => $this->user,
1✔
75
                        'baseUrl' => $baseUrl,
1✔
76
                        'basePath' => $baseUrl ? preg_replace('#https?://[^/]+#A', '', $baseUrl) : null,
1✔
77
                        'flashes' => $flashes,
1✔
78
                        'control' => $control,
1✔
79
                        'presenter' => $presenter,
1✔
80
                ];
81

82
                foreach ($vars as $key => $value) {
1✔
83
                        if ($value !== null && property_exists($template, $key)) {
1✔
84
                                try {
85
                                        $template->$key = $value;
1✔
86
                                } catch (\TypeError) {
×
87
                                }
88
                        }
89
                }
90
        }
1✔
91
}
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

© 2026 Coveralls, Inc