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

elephox-dev / framework / 4877852653

pending completion
4877852653

push

github

Ricardo Boss
WIP

38 of 38 new or added lines in 6 files covered. (100.0%)

3863 of 5835 relevant lines covered (66.2%)

8.55 hits per line

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

38.6
/modules/DI/src/ServiceCollection.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Elephox\DI;
5

6
use Closure;
7
use Elephox\DI\Contract\Resolver;
8
use Elephox\DI\Contract\RootServiceProvider;
9
use Elephox\DI\Contract\ServiceCollection as ServiceCollectionContract;
10
use InvalidArgumentException;
11

12
readonly class ServiceCollection extends ServiceProvider implements ServiceCollectionContract
13
{
14
        public function __construct()
15
        {
16
                parent::__construct();
3✔
17
        }
18

19
        public function add(ServiceDescriptor $descriptor): self
20
        {
21
                $this->descriptors->put($descriptor->serviceType, $descriptor);
2✔
22

23
                return $this;
2✔
24
        }
25

26
        public function tryAdd(ServiceDescriptor $descriptor): self
27
        {
28
                if ($this->descriptors->containsKey($descriptor->serviceType)) {
×
29
                        return $this;
×
30
                }
31

32
                return $this->add($descriptor);
×
33
        }
34

35
        public function remove(string $service): self
36
        {
37
                $this->descriptors->remove($service);
×
38

39
                return $this;
×
40
        }
41

42
        public function removeAll(): self
43
        {
44
                $this->descriptors->clear();
×
45

46
                return $this;
×
47
        }
48

49
        public function buildProvider(): RootServiceProvider
50
        {
51
                /** @psalm-suppress InvalidArgument */
52
                return new ServiceProvider($this->descriptors, $this->instances);
2✔
53
        }
54

55
        /**
56
         * @param null|class-string $implementationType
57
         * @param null|Closure(mixed): object $factory
58
         */
59
        protected function describe(string $service, ServiceLifetime $lifetime, ?string $implementationType = null, ?Closure $factory = null, ?object $implementation = null): ServiceDescriptor
60
        {
61
                if ($service === '') {
3✔
62
                        throw new InvalidArgumentException('Service name name must not be empty.');
×
63
                }
64

65
                /**
66
                 * @var class-string $service
67
                 * @var null|class-string $implementationType
68
                 */
69
                if ($implementation === null && $factory === null) {
3✔
70
                        if (!class_exists($service) && $implementationType === null) {
3✔
71
                                throw new InvalidArgumentException('Either one of implementationType, implementation or factory must be set if the service is not a class name.');
1✔
72
                        }
73

74
                        $implementationType ??= $service;
2✔
75

76
                        $factory = static function (Resolver $resolver) use ($implementationType): object {
2✔
77
                                /** @var object */
78
                                return $resolver->instantiate($implementationType);
1✔
79
                        };
2✔
80
                }
81

82
                return new ServiceDescriptor($service, $implementationType, $lifetime, $factory, $implementation);
2✔
83
        }
84

85
        public function addTransient(string $service, ?string $concrete = null, ?Closure $factory = null, ?object $instance = null): self
86
        {
87
                return $this->add(
×
88
                        $this->describe(
×
89
                                $service,
×
90
                                ServiceLifetime::Transient,
×
91
                                $concrete,
×
92
                                $factory,
×
93
                                $instance,
×
94
                        ),
×
95
                );
×
96
        }
97

98
        public function addScoped(string $service, ?string $concrete = null, ?Closure $factory = null, ?object $instance = null): self
99
        {
100
                return $this->add(
×
101
                        $this->describe(
×
102
                                $service,
×
103
                                ServiceLifetime::Scoped,
×
104
                                $concrete,
×
105
                                $factory,
×
106
                                $instance,
×
107
                        ),
×
108
                );
×
109
        }
110

111
        public function addSingleton(string $service, ?string $concrete = null, ?Closure $factory = null, ?object $instance = null): self
112
        {
113
                return $this->add(
3✔
114
                        $this->describe(
3✔
115
                                $service,
3✔
116
                                ServiceLifetime::Singleton,
3✔
117
                                $concrete,
3✔
118
                                $factory,
3✔
119
                                $instance,
3✔
120
                        ),
3✔
121
                );
3✔
122
        }
123

124
        public function tryAddSingleton(string $service, ?string $concrete = null, ?Closure $factory = null, ?object $instance = null): ServiceCollectionContract
125
        {
126
                if ($this->has($service)) {
×
127
                        return $this;
×
128
                }
129

130
                return $this->addSingleton($service, $concrete, $factory, $instance);
×
131
        }
132

133
        public function tryAddTransient(string $service, ?string $concrete = null, ?Closure $factory = null, ?object $instance = null): ServiceCollectionContract
134
        {
135
                if ($this->has($service)) {
×
136
                        return $this;
×
137
                }
138

139
                return $this->addTransient($service, $concrete, $factory, $instance);
×
140
        }
141

142
        public function tryAddScoped(string $service, ?string $concrete = null, ?Closure $factory = null, ?object $instance = null): ServiceCollectionContract
143
        {
144
                if ($this->has($service)) {
×
145
                        return $this;
×
146
                }
147

148
                return $this->addScoped($service, $concrete, $factory, $instance);
×
149
        }
150
}
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

© 2025 Coveralls, Inc