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

nette / application / 27919019709

21 Jun 2026 10:08PM UTC coverage: 84.111% (+0.05%) from 84.059%
27919019709

push

github

dg
phpstan fix

2038 of 2423 relevant lines covered (84.11%)

0.84 hits per line

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

83.33
/src/Bridges/ApplicationDI/RoutingExtension.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 Nette;
11
use Nette\DI\Definitions;
12
use Nette\Schema\Expect;
13
use Tracy;
14

15

16
/**
17
 * Routing extension for Nette DI.
18
 *
19
 * @property object{
20
 *     debugger: bool|null,
21
 *     routes: array<string, string>,
22
 *     cache: bool,
23
 * } $config
24
 */
25
final class RoutingExtension extends Nette\DI\CompilerExtension
26
{
27
        public function __construct(
1✔
28
                private readonly bool $debugMode = false,
29
        ) {
30
        }
1✔
31

32

33
        public function getConfigSchema(): Nette\Schema\Schema
34
        {
35
                return Expect::structure([
1✔
36
                        'debugger' => Expect::bool(),
1✔
37
                        'routes' => Expect::arrayOf('string'),
1✔
38
                        'cache' => Expect::bool(false),
1✔
39
                ]);
40
        }
41

42

43
        public function loadConfiguration(): void
44
        {
45
                if (!$this->config->routes) {
1✔
46
                        return;
1✔
47
                }
48

49
                $builder = $this->getContainerBuilder();
1✔
50

51
                $router = $builder->addDefinition($this->prefix('router'))
1✔
52
                        ->setFactory(Nette\Application\Routers\RouteList::class);
1✔
53

54
                foreach ($this->config->routes as $mask => $action) {
1✔
55
                        $router->addSetup('$service->addRoute(?, ?)', [$mask, $action]);
1✔
56
                }
57

58
                if ($this->name === 'routing') {
1✔
59
                        $builder->addAlias('router', $this->prefix('router'));
1✔
60
                }
61
        }
1✔
62

63

64
        public function beforeCompile(): void
65
        {
66
                $builder = $this->getContainerBuilder();
1✔
67

68
                if (
69
                        $this->debugMode &&
1✔
70
                        ($this->config->debugger ?? $builder->getByType(Tracy\Bar::class)) &&
1✔
71
                        ($name = $builder->getByType(Nette\Application\Application::class)) &&
1✔
72
                        ($application = $builder->getDefinition($name)) instanceof Definitions\ServiceDefinition
1✔
73
                ) {
74
                        $application->addSetup('@Tracy\Bar::addPanel', [
×
75
                                new Definitions\Statement(Nette\Bridges\ApplicationTracy\RoutingPanel::class),
×
76
                        ]);
77
                }
78

79
                if (!$builder->getByType(Nette\Routing\Router::class)) {
1✔
80
                        $builder->addDefinition($this->prefix('router'))
×
81
                                ->setType(Nette\Routing\Router::class)
×
82
                                ->setFactory(Nette\Routing\SimpleRouter::class);
×
83
                        $builder->addAlias('router', $this->prefix('router'));
×
84
                }
85
        }
1✔
86

87

88
        public function afterCompile(Nette\PhpGenerator\ClassType $class): void
1✔
89
        {
90
                if ($this->config->cache) {
1✔
91
                        $builder = $this->getContainerBuilder();
1✔
92
                        $def = $builder->getDefinitionByType(Nette\Routing\Router::class);
1✔
93
                        $method = $class->getMethod(Nette\DI\Container::getMethodName((string) $def->getName()));
1✔
94
                        try {
95
                                $router = eval($method->getBody());
1✔
96
                                if ($router instanceof Nette\Application\Routers\RouteList) {
1✔
97
                                        $router->warmupCache();
×
98
                                }
99

100
                                $s = serialize($router);
1✔
101
                        } catch (\Throwable $e) {
1✔
102
                                throw new Nette\DI\ServiceCreationException('Unable to cache router due to error: ' . $e->getMessage(), 0, $e);
1✔
103
                        }
104

105
                        $method->setBody('return unserialize(?);', [$s]);
1✔
106
                }
107
        }
1✔
108
}
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