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

nette / application / 28120116131

24 Jun 2026 05:59PM UTC coverage: 84.766% (+0.2%) from 84.605%
28120116131

push

github

dg
added CLAUDE.md

1992 of 2350 relevant lines covered (84.77%)

0.85 hits per line

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

88.89
/src/Bridges/ApplicationDI/LatteExtension.php
1
<?php declare(strict_types=1);
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
namespace Nette\Bridges\ApplicationDI;
9

10
use Latte;
11
use Nette;
12
use Nette\Bridges\ApplicationLatte;
13
use Nette\DI\Definitions\Statement;
14
use Nette\Schema\Expect;
15
use Tracy;
16
use function class_exists, is_string;
17

18

19
/**
20
 * Latte extension for Nette DI.
21
 *
22
 * @property object{
23
 *     debugger: bool|'all'|null,
24
 *     macros: array<string>,
25
 *     extensions: array<string|Nette\DI\Definitions\Statement>,
26
 *     templateClass: string|null,
27
 *     strictTypes: bool,
28
 *     strictParsing: bool,
29
 *     scopedLoopVariables: bool,
30
 *     dedent: bool,
31
 *     phpLinter: string|null,
32
 *     locale: string|null,
33
 * } $config
34
 */
35
final class LatteExtension extends Nette\DI\CompilerExtension
36
{
37
        public function __construct(
1✔
38
                private readonly string $tempDir,
39
                private readonly bool $debugMode = false,
40
        ) {
41
        }
1✔
42

43

44
        public function getConfigSchema(): Nette\Schema\Schema
45
        {
46
                return Expect::structure([
1✔
47
                        'debugger' => Expect::anyOf(true, false, 'all'),
1✔
48
                        'extensions' => Expect::arrayOf('string|Nette\DI\Definitions\Statement'),
1✔
49
                        'templateClass' => Expect::string(),
1✔
50
                        'strictTypes' => Expect::bool(false),
1✔
51
                        'strictParsing' => Expect::bool(false),
1✔
52
                        'scopedLoopVariables' => Expect::bool(false),
1✔
53
                        'dedent' => Expect::bool(false),
1✔
54
                        'phpLinter' => Expect::string(),
1✔
55
                        'locale' => Expect::string(),
1✔
56
                ]);
57
        }
58

59

60
        public function loadConfiguration(): void
61
        {
62
                if (!class_exists(Latte\Engine::class)) {
1✔
63
                        return;
×
64
                }
65

66
                $config = $this->config;
1✔
67
                $builder = $this->getContainerBuilder();
1✔
68

69
                $builder->addFactoryDefinition($this->prefix('latteFactory'))
1✔
70
                        ->setImplement(ApplicationLatte\LatteFactory::class)
1✔
71
                        ->getResultDefinition()
1✔
72
                                ->setFactory(Latte\Engine::class)
1✔
73
                                ->addSetup('setCacheDirectory', [$this->tempDir])
1✔
74
                                ->addSetup('setAutoRefresh', [$this->debugMode])
1✔
75
                                ->addSetup('setFeature', [Latte\Feature::StrictTypes, $config->strictTypes])
1✔
76
                                ->addSetup('setFeature', [Latte\Feature::StrictParsing, $config->strictParsing])
1✔
77
                                ->addSetup('setFeature', [Latte\Feature::ScopedLoopVariables, $config->scopedLoopVariables])
1✔
78
                                ->addSetup('setFeature', [Latte\Feature::Dedent, $config->dedent])
1✔
79
                                ->addSetup('enablePhpLinter', [$config->phpLinter])
1✔
80
                                ->addSetup('setLocale', [$config->locale])
1✔
81
                                ->addSetup('addExtension', [new Statement(ApplicationLatte\UIExtension::class, [$builder::literal('$control')])]);
1✔
82

83
                if ($builder->getByType(Nette\Caching\Storage::class)) {
1✔
84
                        $this->addExtension(new Statement(Nette\Bridges\CacheLatte\CacheExtension::class));
×
85
                }
86
                if (class_exists(Nette\Bridges\FormsLatte\FormsExtension::class)) {
1✔
87
                        $this->addExtension(new Statement(Nette\Bridges\FormsLatte\FormsExtension::class));
1✔
88
                }
89

90
                foreach ($config->extensions as $extension) {
1✔
91
                        if ($extension === Latte\Essential\TranslatorExtension::class) {
1✔
92
                                $extension = new Statement($extension, [new Nette\DI\Definitions\Reference(Nette\Localization\Translator::class)]);
1✔
93
                        }
94
                        $this->addExtension($extension);
1✔
95
                }
96

97
                $builder->addDefinition($this->prefix('templateFactory'))
1✔
98
                        ->setFactory(ApplicationLatte\TemplateFactory::class)
1✔
99
                        ->setArguments(['templateClass' => $config->templateClass]);
1✔
100

101
                if ($this->name === 'latte') {
1✔
102
                        $builder->addAlias('nette.latteFactory', $this->prefix('latteFactory'));
1✔
103
                        $builder->addAlias('nette.templateFactory', $this->prefix('templateFactory'));
1✔
104
                }
105
        }
1✔
106

107

108
        public function beforeCompile(): void
109
        {
110
                $builder = $this->getContainerBuilder();
1✔
111

112
                if (
113
                        $this->debugMode
1✔
114
                        && ($this->config->debugger ?? $builder->getByType(Tracy\Bar::class))
1✔
115
                        && class_exists(Latte\Bridges\Tracy\LattePanel::class)
1✔
116
                ) {
117
                        $factory = $builder->getDefinition($this->prefix('templateFactory'));
×
118
                        $factory->addSetup([self::class, 'initLattePanel'], [$factory, 'all' => $this->config->debugger === 'all']);
×
119
                }
120
        }
1✔
121

122

123
        public static function initLattePanel(
124
                Nette\Application\UI\TemplateFactory $factory,
125
                Tracy\Bar $bar,
126
                bool $all = false,
127
        ): void
128
        {
129
                if (!$factory instanceof ApplicationLatte\TemplateFactory) {
×
130
                        return;
×
131
                }
132

133
                $factory->onCreate[] = function (ApplicationLatte\Template $template) use ($all) {
×
134
                        $control = $template->getLatte()->getProviders()['uiControl'] ?? null;
135
                        if ($all || $control instanceof Nette\Application\UI\Presenter) {
136
                                $name = $all && $control ? (new \ReflectionObject($control))->getShortName() : '';
137
                                $template->getLatte()->addExtension(new Latte\Bridges\Tracy\TracyExtension($name));
138
                        }
139
                };
140
        }
141

142

143
        public function addExtension(Statement|string $extension): void
1✔
144
        {
145
                $extension = is_string($extension)
1✔
146
                        ? new Statement($extension)
1✔
147
                        : $extension;
1✔
148

149
                $builder = $this->getContainerBuilder();
1✔
150
                $builder->getDefinition($this->prefix('latteFactory'))
1✔
151
                        ->getResultDefinition()
1✔
152
                        ->addSetup('addExtension', [$extension]);
1✔
153
        }
1✔
154
}
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