Skip to content

Commit d0c9937

Browse files
committed
v0.1
1 parent 0a5ae38 commit d0c9937

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022-present Micro Solutions
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/Container.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct()
3232
*/
3333
public function get(string $id)
3434
{
35-
return $this->lookupService($id);
35+
return $this->lookup($id);
3636
}
3737

3838
/**
@@ -48,18 +48,14 @@ public function has(string $id): bool
4848
*/
4949
public function register(string $id, \Closure $service): void
5050
{
51-
if($this->has($id)) {
52-
throw new ServiceRegistrationException($id);
53-
}
54-
5551
$this->servicesRaw[$id] = $service;
5652
}
5753

5854
/**
59-
* @param string $id
55+
* @param string $id
6056
* @return object
6157
*/
62-
private function lookupService(string $id): object
58+
private function lookup(string $id): object
6359
{
6460
if(!empty($this->services[$id])) {
6561
return $this->services[$id];
@@ -69,7 +65,7 @@ private function lookupService(string $id): object
6965
}
7066

7167
/**
72-
* @param string $id
68+
* @param string $id
7369
* @return object
7470
*/
7571
private function createServiceInstance(string $id): object
@@ -80,7 +76,6 @@ private function createServiceInstance(string $id): object
8076

8177
$raw = $this->servicesRaw[$id];
8278
$service = $raw($this);
83-
8479
$this->services[$id] = $service;
8580

8681
return $service;

src/ContainerRegistryInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
interface ContainerRegistryInterface
77
{
88
/**
9-
* @param string $alias
10-
* @param \Closure $service
9+
* Register new service.
10+
*
11+
* @param string $id service alias
12+
* @param \Closure $service service initialization callback
13+
*
1114
* @return void
1215
*/
1316
public function register(string $id, \Closure $service): void;

src/Exception/ServiceNotRegisteredException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Psr\Container\NotFoundExceptionInterface;
66

7-
class ServiceNotRegisteredException extends \RuntimeException implements NotFoundExceptionInterface
7+
class ServiceNotRegisteredException extends \RuntimeException
8+
implements NotFoundExceptionInterface
89
{
910
}

src/Exception/ServiceRegistrationException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use Psr\Container\ContainerExceptionInterface;
66

7-
class ServiceRegistrationException extends \RuntimeException implements ContainerExceptionInterface
7+
class ServiceRegistrationException extends \RuntimeException
8+
implements ContainerExceptionInterface
89
{
910

1011
}

tests/unit/ContainerTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ public function testContainerResolveDependencies(): void
1313
{
1414
$container = new Container();
1515

16-
$container->register('test', function ( Container $container ) { return new class {
17-
public string $name = 'success';
18-
}; });
16+
$container->register(
17+
'test', function ( Container $container ) {
18+
return new class {
19+
public string $name = 'success';
20+
};
21+
}
22+
);
1923

2024
$service = $container->get('test');
2125
$this->assertIsObject($service);
@@ -27,18 +31,32 @@ public function testRegisterTwoServicesWithEqualAliasesException(): void
2731
$this->expectException(ServiceRegistrationException::class);
2832
$container = new Container();
2933

30-
$container->register('test', function ( Container $container ) { return new class {}; });
31-
$container->register('test', function ( Container $container ) { return new class {}; });
34+
$container->register(
35+
'test', function ( Container $container ) {
36+
return new class {
37+
};
38+
}
39+
);
40+
$container->register(
41+
'test', function ( Container $container ) {
42+
return new class {
43+
};
44+
}
45+
);
3246
}
3347

3448
public function testContainerUnresolvedException(): void
3549
{
3650
$this->expectException(ServiceNotRegisteredException::class);
3751

3852
$container = new Container();
39-
$container->register('test', function ( Container $container ) { return new class {
40-
public string $name = 'success';
41-
}; });
53+
$container->register(
54+
'test', function ( Container $container ) {
55+
return new class {
56+
public string $name = 'success';
57+
};
58+
}
59+
);
4260

4361
$container->get('test2');
4462
}

0 commit comments

Comments
 (0)