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

nette / di / 6739035766

02 Nov 2023 10:44PM UTC coverage: 52.037% (-41.8%) from 93.846%
6739035766

push

github

dg
x

1009 of 1939 relevant lines covered (52.04%)

0.52 hits per line

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

53.73
/src/DI/Definitions/ServiceDefinition.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\Definitions;
11

12
use Nette;
13
use Nette\DI\ServiceCreationException;
14

15

16
/**
17
 * Definition of standard service.
18
 *
19
 * @property string|null $class
20
 * @property Statement $factory
21
 * @property Statement[] $setup
22
 */
23
final class ServiceDefinition extends Definition
24
{
25
        /** @var Statement */
26
        private $creator;
27

28
        /** @var Statement[] */
29
        private $setup = [];
30

31

32
        public function __construct()
33
        {
34
                $this->creator = new Statement(null);
1✔
35
        }
1✔
36

37

38
        /** @return static */
39
        public function setType(?string $type)
1✔
40
        {
41
                return parent::setType($type);
1✔
42
        }
43

44

45
        /**
46
         * Alias for setCreator()
47
         * @param  string|array|Definition|Reference|Statement  $factory
48
         * @return static
49
         */
50
        public function setFactory($factory, array $args = [])
51
        {
52
                return $this->setCreator($factory, $args);
×
53
        }
54

55

56
        /**
57
         * Alias for getCreator()
58
         */
59
        public function getFactory(): Statement
60
        {
61
                return $this->getCreator();
×
62
        }
63

64

65
        /**
66
         * @param  string|array|Definition|Reference|Statement  $creator
67
         * @return static
68
         */
69
        public function setCreator($creator, array $args = [])
1✔
70
        {
71
                $this->creator = $creator instanceof Statement
1✔
72
                        ? $creator
1✔
73
                        : new Statement($creator, $args);
1✔
74
                return $this;
1✔
75
        }
76

77

78
        public function getCreator(): Statement
79
        {
80
                return $this->creator;
1✔
81
        }
82

83

84
        /** @return string|array|Definition|Reference|null */
85
        public function getEntity()
86
        {
87
                return $this->creator->getEntity();
1✔
88
        }
89

90

91
        /** @return static */
92
        public function setArguments(array $args = [])
93
        {
94
                $this->creator->arguments = $args;
×
95
                return $this;
×
96
        }
97

98

99
        /** @return static */
100
        public function setArgument($key, $value)
101
        {
102
                $this->creator->arguments[$key] = $value;
×
103
                return $this;
×
104
        }
105

106

107
        /**
108
         * @param  Statement[]  $setup
109
         * @return static
110
         */
111
        public function setSetup(array $setup)
112
        {
113
                foreach ($setup as $v) {
×
114
                        if (!$v instanceof Statement) {
×
115
                                throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Definitions\Statement[].');
×
116
                        }
117
                }
118

119
                $this->setup = $setup;
×
120
                return $this;
×
121
        }
122

123

124
        /** @return Statement[] */
125
        public function getSetup(): array
126
        {
127
                return $this->setup;
1✔
128
        }
129

130

131
        /**
132
         * @param  string|array|Definition|Reference|Statement  $entity
133
         * @return static
134
         */
135
        public function addSetup($entity, array $args = [])
136
        {
137
                $this->setup[] = $entity instanceof Statement
×
138
                        ? $entity
×
139
                        : new Statement($entity, $args);
×
140
                return $this;
×
141
        }
142

143

144
        public function resolveType(Nette\DI\Resolver $resolver): void
1✔
145
        {
146
                if (!$this->getEntity()) {
1✔
147
                        if (!$this->getType()) {
1✔
148
                                throw new ServiceCreationException('Factory and type are missing in definition of service.');
1✔
149
                        }
150

151
                        $this->setCreator($this->getType(), $this->creator->arguments ?? []);
1✔
152

153
                } elseif (!$this->getType()) {
1✔
154
                        $type = $resolver->resolveEntityType($this->creator);
1✔
155
                        if (!$type) {
1✔
156
                                throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory method.');
×
157
                        }
158

159
                        $this->setType($type);
1✔
160
                        $resolver->addDependency(new \ReflectionClass($type));
1✔
161
                }
162

163
                // auto-disable autowiring for aliases
164
                if ($this->getAutowired() === true && $this->getEntity() instanceof Reference) {
1✔
165
                        $this->setAutowired(false);
×
166
                }
167
        }
1✔
168

169

170
        public function complete(Nette\DI\Resolver $resolver): void
1✔
171
        {
172
                $entity = $this->creator->getEntity();
1✔
173
                if ($entity instanceof Reference && !$this->creator->arguments && !$this->setup) {
1✔
174
                        $ref = $resolver->normalizeReference($entity);
×
175
                        $this->setCreator([new Reference(Nette\DI\ContainerBuilder::ThisContainer), 'getService'], [$ref->getValue()]);
×
176
                }
177

178
                $this->creator = $resolver->completeStatement($this->creator);
1✔
179

180
                foreach ($this->setup as &$setup) {
1✔
181
                        if (
182
                                is_string($setup->getEntity())
×
183
                                && strpbrk($setup->getEntity(), ':@?\\') === false
×
184
                        ) { // auto-prepend @self
185
                                $setup = new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments);
×
186
                        }
187

188
                        $setup = $resolver->completeStatement($setup, true);
×
189
                }
190
        }
1✔
191

192

193
        public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
1✔
194
        {
195
                $code = $generator->formatStatement($this->creator) . ";\n";
1✔
196
                if (!$this->setup) {
1✔
197
                        $method->setBody('return ' . $code);
1✔
198
                        return;
1✔
199
                }
200

201
                $code = '$service = ' . $code;
×
202
                foreach ($this->setup as $setup) {
×
203
                        $code .= $generator->formatStatement($setup) . ";\n";
×
204
                }
205

206
                $code .= 'return $service;';
×
207
                $method->setBody($code);
×
208
        }
209

210

211
        public function __clone()
212
        {
213
                parent::__clone();
×
214
                $this->creator = unserialize(serialize($this->creator));
×
215
                $this->setup = unserialize(serialize($this->setup));
×
216
        }
217
}
218

219

220
class_exists(Nette\DI\ServiceDefinition::class);
1✔
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