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

nette / latte / 24806161885

22 Apr 2026 10:34PM UTC coverage: 95.002% (-0.02%) from 95.019%
24806161885

push

github

dg
Released version 3.1.4

5721 of 6022 relevant lines covered (95.0%)

0.95 hits per line

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

87.93
/src/Latte/Engine.php
1
<?php declare(strict_types=1);
2

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

8
namespace Latte;
9

10
use Latte\Compiler\Nodes\TemplateNode;
11
use function array_map, array_merge, class_exists, extension_loaded, get_debug_type, preg_match, serialize, substr;
12

13

14
/**
15
 * Templating engine Latte.
16
 */
17
class Engine
18
{
19
        public const Version = '3.1.4';
20
        public const VersionId = 30104;
21

22
        /** @deprecated use Engine::Version */
23
        public const
24
                VERSION = self::Version,
25
                VERSION_ID = self::VersionId;
26

27
        #[\Deprecated('use Latte\ContentType::Html')]
28
        public const CONTENT_HTML = ContentType::Html;
29

30
        #[\Deprecated('use Latte\ContentType::Xml')]
31
        public const CONTENT_XML = ContentType::Xml;
32

33
        #[\Deprecated('use Latte\ContentType::JavaScript')]
34
        public const CONTENT_JS = ContentType::JavaScript;
35

36
        #[\Deprecated('use Latte\ContentType::Css')]
37
        public const CONTENT_CSS = ContentType::Css;
38

39
        #[\Deprecated('use Latte\ContentType::ICal')]
40
        public const CONTENT_ICAL = ContentType::ICal;
41

42
        #[\Deprecated('use Latte\ContentType::Text')]
43
        public const CONTENT_TEXT = ContentType::Text;
44

45
        private ?Loader $loader = null;
46
        private Runtime\FilterExecutor $filters;
47
        private Runtime\FunctionExecutor $functions;
48
        private \stdClass $providers;
49

50
        /** @var Extension[] */
51
        private array $extensions = [];
52
        private string $contentType = ContentType::Html;
53
        private Runtime\Cache $cache;
54

55
        /** @var array<string, bool> */
56
        private array $features = [
57
                Feature::StrictTypes->name => true,
58
        ];
59

60
        private ?Policy $policy = null;
61
        private bool $sandboxed = false;
62
        private ?string $phpBinary = null;
63
        private ?string $configurationHash;
64
        private ?string $locale = null;
65
        private ?string $syntax = null;
66

67

68
        public function __construct()
69
        {
70
                $this->cache = new Runtime\Cache;
1✔
71
                $this->filters = new Runtime\FilterExecutor;
1✔
72
                $this->functions = new Runtime\FunctionExecutor;
1✔
73
                $this->providers = new \stdClass;
1✔
74
                $this->addDefaultExtensions();
1✔
75
        }
1✔
76

77

78
        /**
79
         * Renders template to output.
80
         * @param  object|mixed[]  $params
81
         */
82
        public function render(string $name, object|array $params = [], ?string $block = null): void
1✔
83
        {
84
                $template = $this->createTemplate($name, Helpers::resolveParams($this, $params));
1✔
85
                $template->global->coreCaptured = false;
1✔
86
                $template->render($block);
1✔
87
        }
1✔
88

89

90
        /**
91
         * Renders template to string.
92
         * @param  object|mixed[]  $params
93
         */
94
        public function renderToString(string $name, object|array $params = [], ?string $block = null): string
1✔
95
        {
96
                $template = $this->createTemplate($name, Helpers::resolveParams($this, $params));
1✔
97
                $template->global->coreCaptured = true;
1✔
98
                return $template->capture(fn() => $template->render($block));
1✔
99
        }
100

101

102
        /**
103
         * Creates template object.
104
         * @param  mixed[]  $params
105
         */
106
        public function createTemplate(string $name, array $params = [], bool $clearCache = true): Runtime\Template
1✔
107
        {
108
                $this->configurationHash = $clearCache ? null : $this->configurationHash;
1✔
109
                $class = $this->loadTemplate($name);
1✔
110
                $this->providers->fn = $this->functions;
1✔
111
                return new $class(
1✔
112
                        $this,
1✔
113
                        $params,
114
                        $this->filters,
1✔
115
                        $this->providers,
1✔
116
                        $name,
117
                );
118
        }
119

120

121
        /**
122
         * Compiles template to PHP code.
123
         */
124
        public function compile(string $name): string
1✔
125
        {
126
                if ($this->sandboxed && !$this->policy) {
1✔
127
                        throw new \LogicException('In sandboxed mode you need to set a security policy.');
1✔
128
                }
129

130
                $template = $this->getLoader()->getContent($name);
1✔
131

132
                try {
133
                        $node = $this->parse($template);
1✔
134
                        $this->applyPasses($node);
1✔
135
                        $compiled = $this->generate($node, $name);
1✔
136

137
                } catch (\Throwable $e) {
1✔
138
                        if (!$e instanceof CompileException && !$e instanceof SecurityViolationException) {
1✔
139
                                $e = new CompileException("Thrown exception '{$e->getMessage()}'", previous: $e);
1✔
140
                        }
141

142
                        throw $e->setSource($template, $name);
1✔
143
                }
144

145
                if ($this->phpBinary) {
1✔
146
                        Compiler\PhpHelpers::checkCode($this->phpBinary, $compiled, "(compiled $name)");
×
147
                }
148

149
                return $compiled;
1✔
150
        }
151

152

153
        /**
154
         * Parses template to AST node.
155
         */
156
        public function parse(string $template): TemplateNode
1✔
157
        {
158
                $parser = new Compiler\TemplateParser;
1✔
159
                $parser->getLexer()->setSyntax($this->syntax);
1✔
160
                $parser->strict = $this->hasFeature(Feature::StrictParsing);
1✔
161
                $parser->dedent = $this->hasFeature(Feature::Dedent);
1✔
162

163
                foreach ($this->extensions as $extension) {
1✔
164
                        $extension->beforeCompile($this);
1✔
165
                        $parser->addTags($extension->getTags());
1✔
166
                }
167

168
                return $parser
169
                        ->setContentType($this->contentType)
1✔
170
                        ->setPolicy($this->getPolicy(effective: true))
1✔
171
                        ->parse($template);
1✔
172
        }
173

174

175
        /**
176
         * Runs all registered compiler passes over the AST.
177
         */
178
        public function applyPasses(TemplateNode &$node): void
1✔
179
        {
180
                $passes = [];
1✔
181
                foreach ($this->extensions as $extension) {
1✔
182
                        $passes = array_merge($passes, $extension->getPasses());
1✔
183
                }
184

185
                $passes = Helpers::sortBeforeAfter($passes);
1✔
186
                foreach ($passes as $pass) {
1✔
187
                        $pass = $pass instanceof \stdClass ? $pass->subject : $pass;
1✔
188
                        ($pass)($node);
1✔
189
                }
190
        }
1✔
191

192

193
        /**
194
         * Generates compiled PHP code.
195
         */
196
        public function generate(TemplateNode $node, string $name): string
1✔
197
        {
198
                $generator = new Compiler\TemplateGenerator;
1✔
199
                $generator->buildClass($node, $this->features);
1✔
200
                return $generator->generateCode($this->getTemplateClass($name), $name, $this->hasFeature(Feature::StrictTypes));
1✔
201
        }
202

203

204
        /**
205
         * Compiles template to cache.
206
         * @throws \LogicException
207
         */
208
        public function warmupCache(string $name): void
1✔
209
        {
210
                if (!$this->cache->directory) {
1✔
211
                        throw new \LogicException('Path to temporary directory is not set.');
×
212
                }
213

214
                $this->loadTemplate($name);
1✔
215
        }
1✔
216

217

218
        /** @return class-string<Runtime\Template> */
219
        private function loadTemplate(string $name): string
1✔
220
        {
221
                $class = $this->getTemplateClass($name);
1✔
222
                if (class_exists($class, autoload: false)) {
1✔
223
                        // nothing
224
                } elseif ($this->cache->directory) {
1✔
225
                        $this->cache->loadOrCreate($this, $name);
1✔
226
                } else {
227
                        $compiled = $this->compile($name);
1✔
228
                        if (@eval(substr($compiled, 5)) === false) { // @ is escalated to exception, substr removes <?php
1✔
229
                                throw (new CompileException('Error in template: ' . (error_get_last()['message'] ?? '')))
×
230
                                        ->setSource($compiled, "$name (compiled)");
×
231
                        }
232
                }
233
                return $class;
1✔
234
        }
235

236

237
        /**
238
         * Returns the file path where compiled template will be cached.
239
         */
240
        public function getCacheFile(string $name): string
1✔
241
        {
242
                return $this->cache->generateFilePath($this, $name);
1✔
243
        }
244

245

246
        /**
247
         * Returns the PHP class name for compiled template.
248
         */
249
        public function getTemplateClass(string $name): string
1✔
250
        {
251
                return 'Template_' . $this->generateTemplateHash($name);
1✔
252
        }
253

254

255
        /**
256
         * Generates unique hash for template based on current configuration.
257
         * Used to create isolated cache files for different engine configurations.
258
         * @internal
259
         */
260
        public function generateTemplateHash(string $name): string
1✔
261
        {
262
                $hash = $this->configurationHash ?? hash('xxh128', serialize($this->generateConfigurationSignature()));
1✔
263
                $hash .= $this->getLoader()->getUniqueId($name);
1✔
264
                return substr(hash('xxh128', $hash), 0, 10);
1✔
265
        }
266

267

268
        /**
269
         * Returns values that determine isolation for different configurations.
270
         * When any of these values change, a new compiled template is created to avoid conflicts.
271
         * @return list<mixed>
272
         */
273
        protected function generateConfigurationSignature(): array
274
        {
275
                return [
276
                        $this->contentType,
1✔
277
                        $this->features,
1✔
278
                        $this->syntax,
1✔
279
                        array_map(
1✔
280
                                fn($extension) => [get_debug_type($extension), $extension->getCacheKey($this)],
1✔
281
                                $this->extensions,
1✔
282
                        ),
283
                ];
284
        }
285

286

287
        /**
288
         * Registers run-time filter.
289
         */
290
        public function addFilter(string $name, callable $callback): static
1✔
291
        {
292
                if (!preg_match('#^[a-z]\w*$#iD', $name)) {
1✔
293
                        throw new \LogicException("Invalid filter name '$name'.");
×
294
                }
295

296
                $this->filters->add($name, $callback);
1✔
297
                return $this;
1✔
298
        }
299

300

301
        #[\Deprecated('Use addFilter() instead.')]
302
        public function addFilterLoader(callable $loader): static
1✔
303
        {
304
                trigger_error('Filter loader is deprecated, use addFilter() instead.', E_USER_DEPRECATED);
1✔
305
                $this->filters->add(null, $loader);
1✔
306
                return $this;
1✔
307
        }
308

309

310
        /**
311
         * Returns all run-time filters.
312
         * @return array<string, callable>
313
         */
314
        public function getFilters(): array
315
        {
316
                return $this->filters->getAll();
1✔
317
        }
318

319

320
        /**
321
         * Calls a run-time filter.
322
         * @param  mixed[]  $args
323
         */
324
        public function invokeFilter(string $name, array $args): mixed
1✔
325
        {
326
                return ($this->filters->$name)(...$args);
1✔
327
        }
328

329

330
        /**
331
         * Adds new extension.
332
         */
333
        public function addExtension(Extension $extension): static
1✔
334
        {
335
                $this->extensions[] = $extension;
1✔
336
                foreach ($extension->getFilters() as $name => $value) {
1✔
337
                        $this->filters->add($name, $value);
1✔
338
                }
339

340
                foreach ($extension->getFunctions() as $name => $value) {
1✔
341
                        $this->functions->add($name, $value);
1✔
342
                }
343

344
                foreach ($extension->getProviders() as $name => $value) {
1✔
345
                        $this->providers->$name = $value;
×
346
                }
347
                return $this;
1✔
348
        }
349

350

351
        /** @return list<Extension> */
352
        public function getExtensions(): array
353
        {
354
                return array_values($this->extensions);
1✔
355
        }
356

357

358
        /**
359
         * Registers run-time function.
360
         */
361
        public function addFunction(string $name, callable $callback): static
1✔
362
        {
363
                if (!preg_match('#^[a-z]\w*$#iD', $name)) {
1✔
364
                        throw new \LogicException("Invalid function name '$name'.");
×
365
                }
366

367
                $this->functions->add($name, $callback);
1✔
368
                return $this;
1✔
369
        }
370

371

372
        /**
373
         * Calls a run-time function.
374
         * @param  mixed[]  $args
375
         */
376
        public function invokeFunction(string $name, array $args): mixed
1✔
377
        {
378
                return ($this->functions->$name)(null, ...$args);
1✔
379
        }
380

381

382
        /**
383
         * Returns all run-time functions.
384
         * @return array<string, callable>
385
         */
386
        public function getFunctions(): array
387
        {
388
                return $this->functions->getAll();
1✔
389
        }
390

391

392
        /**
393
         * Adds new provider.
394
         */
395
        public function addProvider(string $name, mixed $provider): static
1✔
396
        {
397
                if (!preg_match('#^[a-z]\w*$#iD', $name)) {
1✔
398
                        throw new \LogicException("Invalid provider name '$name'.");
×
399
                }
400

401
                $this->providers->$name = $provider;
1✔
402
                return $this;
1✔
403
        }
404

405

406
        /**
407
         * Returns all providers.
408
         * @return array<string, mixed>
409
         */
410
        public function getProviders(): array
411
        {
412
                return (array) $this->providers;
1✔
413
        }
414

415

416
        public function setPolicy(?Policy $policy): static
1✔
417
        {
418
                $this->policy = $policy;
1✔
419
                return $this;
1✔
420
        }
421

422

423
        public function getPolicy(bool $effective = false): ?Policy
1✔
424
        {
425
                return !$effective || $this->sandboxed
1✔
426
                        ? $this->policy
1✔
427
                        : null;
1✔
428
        }
429

430

431
        /**
432
         * Sets a handler called when an exception occurs during template rendering.
433
         */
434
        public function setExceptionHandler(callable $handler): static
1✔
435
        {
436
                $this->providers->coreExceptionHandler = $handler(...);
1✔
437
                return $this;
1✔
438
        }
439

440

441
        public function setSandboxMode(bool $state = true): static
1✔
442
        {
443
                $this->sandboxed = $state;
1✔
444
                return $this;
1✔
445
        }
446

447

448
        public function setContentType(string $type): static
1✔
449
        {
450
                $this->contentType = $type;
1✔
451
                return $this;
1✔
452
        }
453

454

455
        /**
456
         * Sets path to cache directory.
457
         */
458
        public function setCacheDirectory(?string $path): static
1✔
459
        {
460
                $this->cache->directory = $path;
1✔
461
                return $this;
1✔
462
        }
463

464

465
        /** @deprecated use setCacheDirectory() instead */
466
        public function setTempDirectory(?string $path): static
467
        {
468
                return $this->setCacheDirectory($path);
×
469
        }
470

471

472
        /**
473
         * Sets auto-refresh mode.
474
         */
475
        public function setAutoRefresh(bool $state = true): static
476
        {
477
                $this->cache->autoRefresh = $state;
×
478
                return $this;
×
479
        }
480

481

482
        /**
483
         * Enables or disables an engine feature.
484
         */
485
        public function setFeature(Feature $feature, bool $state = true): static
1✔
486
        {
487
                $this->features[$feature->name] = $state;
1✔
488
                return $this;
1✔
489
        }
490

491

492
        /**
493
         * Checks if a feature is enabled.
494
         */
495
        public function hasFeature(Feature $feature): bool
1✔
496
        {
497
                return $this->features[$feature->name] ?? false;
1✔
498
        }
499

500

501
        /**
502
         * Enables declare(strict_types=1) in templates.
503
         * @deprecated use setFeature(Feature::StrictTypes, ...) instead
504
         */
505
        public function setStrictTypes(bool $state = true): static
506
        {
507
                return $this->setFeature(Feature::StrictTypes, $state);
×
508
        }
509

510

511
        /** @deprecated use setFeature(Feature::StrictParsing, ...) instead */
512
        public function setStrictParsing(bool $state = true): static
513
        {
514
                return $this->setFeature(Feature::StrictParsing, $state);
×
515
        }
516

517

518
        /** @deprecated use hasFeature(Feature::StrictParsing) instead */
519
        public function isStrictParsing(): bool
520
        {
521
                return $this->hasFeature(Feature::StrictParsing);
×
522
        }
523

524

525
        /**
526
         * Sets the locale. It uses the same identifiers as the PHP intl extension.
527
         */
528
        public function setLocale(?string $locale): static
529
        {
530
                if ($locale && !extension_loaded('intl')) {
×
531
                        throw new RuntimeException("Setting a locale requires the 'intl' extension to be installed.");
×
532
                }
533
                $this->locale = $locale;
×
534
                return $this;
×
535
        }
536

537

538
        public function getLocale(): ?string
539
        {
540
                return $this->locale;
1✔
541
        }
542

543

544
        public function setLoader(Loader $loader): static
1✔
545
        {
546
                $this->loader = $loader;
1✔
547
                return $this;
1✔
548
        }
549

550

551
        public function getLoader(): Loader
552
        {
553
                return $this->loader ??= new Loaders\FileLoader;
1✔
554
        }
555

556

557
        /**
558
         * Validates compiled PHP code using the given PHP binary. Pass null to disable.
559
         */
560
        public function enablePhpLinter(?string $phpBinary): static
561
        {
562
                $this->phpBinary = $phpBinary;
×
563
                return $this;
×
564
        }
565

566

567
        /**
568
         * Sets default Latte syntax. Available options: 'single', 'double', 'off'
569
         */
570
        public function setSyntax(string $syntax): static
1✔
571
        {
572
                $this->syntax = $syntax;
1✔
573
                return $this;
1✔
574
        }
575

576

577
        /** @deprecated use setFeature(Feature::MigrationWarnings, ...) instead */
578
        public function setMigrationWarnings(bool $state = true): static
579
        {
580
                return $this->setFeature(Feature::MigrationWarnings, $state);
×
581
        }
582

583

584
        protected function addDefaultExtensions(): void
585
        {
586
                $this->addExtension(new Essential\CoreExtension);
1✔
587
                $this->addExtension(new Sandbox\SandboxExtension);
1✔
588
        }
1✔
589
}
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