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

nette / di / 6739042234

02 Nov 2023 10:31PM UTC coverage: 93.846% (+41.8%) from 52.037%
6739042234

push

github

dg
Option 'class' is allowed again

Partially reverts commit 046f89cc3.

1 of 1 new or added line in 1 file covered. (100.0%)

2257 of 2405 relevant lines covered (93.85%)

0.94 hits per line

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

96.97
/src/DI/Extensions/DecoratorExtension.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\DI\Extensions;
11

12
use Nette;
13
use Nette\DI\Definitions;
14
use Nette\Schema\Expect;
15

16
/**
17
 * Decorators for services.
18
 */
19
final class DecoratorExtension extends Nette\DI\CompilerExtension
20
{
21
        public function getConfigSchema(): Nette\Schema\Schema
22
        {
23
                return Expect::arrayOf(
1✔
24
                        Expect::structure([
1✔
25
                                'setup' => Expect::list(),
1✔
26
                                'tags' => Expect::array(),
1✔
27
                                'inject' => Expect::bool(),
1✔
28
                        ])
29
                );
30
        }
31

32

33
        public function beforeCompile()
34
        {
35
                $this->getContainerBuilder()->resolve();
1✔
36
                foreach ($this->config as $type => $info) {
1✔
37
                        if (!class_exists($type) && !interface_exists($type)) {
1✔
38
                                throw new Nette\DI\InvalidConfigurationException(sprintf("Decorated class '%s' not found.", $type));
×
39
                        }
40

41
                        if ($info->inject !== null) {
1✔
42
                                $info->tags[InjectExtension::TagInject] = $info->inject;
1✔
43
                        }
44

45
                        $this->addSetups($type, Nette\DI\Helpers::filterArguments($info->setup));
1✔
46
                        $this->addTags($type, $info->tags);
1✔
47
                }
48
        }
1✔
49

50

51
        public function addSetups(string $type, array $setups): void
1✔
52
        {
53
                foreach ($this->findByType($type) as $def) {
1✔
54
                        if ($def instanceof Definitions\FactoryDefinition) {
1✔
55
                                $def = $def->getResultDefinition();
1✔
56
                        }
57

58
                        foreach ($setups as $setup) {
1✔
59
                                if (is_array($setup)) {
1✔
60
                                        $setup = new Definitions\Statement(key($setup), array_values($setup));
1✔
61
                                }
62

63
                                $def->addSetup($setup);
1✔
64
                        }
65
                }
66
        }
1✔
67

68

69
        public function addTags(string $type, array $tags): void
1✔
70
        {
71
                $tags = Nette\Utils\Arrays::normalize($tags, true);
1✔
72
                foreach ($this->findByType($type) as $def) {
1✔
73
                        $def->setTags($def->getTags() + $tags);
1✔
74
                }
75
        }
1✔
76

77

78
        private function findByType(string $type): array
1✔
79
        {
80
                return array_filter($this->getContainerBuilder()->getDefinitions(), function (Definitions\Definition $def) use ($type): bool {
1✔
81
                        return is_a($def->getType(), $type, true)
1✔
82
                                || ($def instanceof Definitions\FactoryDefinition && is_a($def->getResultType(), $type, true));
1✔
83
                });
1✔
84
        }
85
}
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