Skip to content

Commit 2ee4c12

Browse files
authored
Revert "feat: Add multiple service binding support (#1)" (#2)
This reverts commit 9e23c78.
1 parent 9e23c78 commit 2ee4c12

14 files changed

Lines changed: 31 additions & 127 deletions

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ composer require algoyounes/bindify
3232

3333
1. Use the `#[BindWith]` attribute to bind an interface to its implementation
3434

35-
> [!TIP]
36-
> Bind multiple services :
37-
> ```php
38-
> #[BindWith([DefaultService::class, ... ], BindType::Singleton)]
39-
> ```
40-
4135
```php
36+
4237
namespace App\Contracts;
4338

4439
use AlgoYounes\Bindify\Attributes\BindWith;
@@ -54,6 +49,7 @@ interface ServiceContract
5449
2. Create the implementation of the interface
5550

5651
```php
52+
5753
namespace App\Services;
5854

5955
use App\Contracts\ServiceContract;

src/Attributes/BindWith.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,13 @@
77
#[Attribute(Attribute::TARGET_CLASS)]
88
readonly class BindWith
99
{
10-
/** @param class-string|array<class-string> $concrete */
1110
public function __construct(
12-
public string|array $concrete,
11+
public string $concrete,
1312
public BindType $type = BindType::Transient,
1413
) {}
1514

16-
/**
17-
* @return array<class-string>
18-
*/
19-
public function getConcrete(): array
15+
public function getConcrete(): string
2016
{
21-
if (is_string($this->concrete)) {
22-
return [$this->concrete];
23-
}
24-
2517
return $this->concrete;
2618
}
2719

src/Contexts/BindContext.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66

77
readonly class BindContext
88
{
9-
/** @param array<class-string> $concrete */
109
public function __construct(
1110
private string $abstract,
12-
private array $concrete,
11+
private string $concrete,
1312
private BindType $type,
1413
) {}
1514

16-
/**
17-
* @param class-string|array<class-string> $concrete
18-
*/
19-
public static function create(string $abstract, string|array $concrete, BindType $type): self
15+
public static function create(string $abstract, string $concrete, BindType $type): self
2016
{
21-
if (is_string($concrete)) {
22-
$concrete = [$concrete];
23-
}
24-
2517
return new self($abstract, $concrete, $type);
2618
}
2719

@@ -30,10 +22,7 @@ public function getAbstract(): string
3022
return $this->abstract;
3123
}
3224

33-
/**
34-
* @return array<class-string>
35-
*/
36-
public function getConcrete(): array
25+
public function getConcrete(): string
3726
{
3827
return $this->concrete;
3928
}

src/Providers/BindifyServiceProvider.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@ private function beforeResolvingCallback(): Closure
3535
return;
3636
}
3737

38-
foreach ($binding->getConcrete() as $concrete) {
39-
$container->bind(
40-
$binding->getAbstract(),
41-
$concrete,
42-
$binding->isSingleton(),
43-
);
44-
}
38+
$container->bind(
39+
$binding->getAbstract(),
40+
$binding->getConcrete(),
41+
$binding->isSingleton(),
42+
);
4543
};
4644
}
4745
}

src/Resolvers/AttributeResolver.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ public function resolve(string $abstract): ?BindContext
2828
return null;
2929
}
3030

31-
$concretes = array_filter(
32-
array_unique($bindWith->getConcrete()),
33-
static fn (string $concrete): bool => class_exists($concrete)
34-
&& is_subclass_of($concrete, $abstract)
35-
);
36-
37-
if ($concretes === []) {
38-
return null;
39-
}
40-
41-
return new BindContext($abstract, $concretes, $bindWith->getType());
31+
return BindContext::create($abstract, $bindWith->getConcrete(), $bindWith->getType());
4232
}
4333
}

tests/Feature/BindifyTest.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
use AlgoYounes\Bindify\Attributes\BindType;
44
use AlgoYounes\Bindify\Contexts\BindContext;
5-
use Workbench\App\AlternativeImplementationService;
65
use Workbench\App\Contracts\DefaultBindTypeContract;
7-
use Workbench\App\Contracts\InvalidClassBindingsContract;
8-
use Workbench\App\Contracts\MultiBindContract;
96
use Workbench\App\Contracts\MultipleAttributesContract;
107
use Workbench\App\Contracts\NoAttributeContract;
118
use Workbench\App\Contracts\SingletonBindingContract;
12-
use Workbench\App\DefaultImplementationService;
9+
use Workbench\App\DefaultImplementationContract;
1310

1411
it('returns a binding from the interface attribute with the default type', function () {
1512
$context = BindContext::create(
1613
DefaultBindTypeContract::class,
17-
DefaultImplementationService::class,
14+
DefaultImplementationContract::class,
1815
BindType::Transient
1916
);
2017

@@ -26,7 +23,7 @@
2623
it('returns a binding from the interface attribute with the singleton type', function () {
2724
$context = BindContext::create(
2825
SingletonBindingContract::class,
29-
DefaultImplementationService::class,
26+
DefaultImplementationContract::class,
3027
BindType::Singleton
3128
);
3229

@@ -46,29 +43,3 @@
4643
it('returns null when the interface has more than one attribute', function () {
4744
$this->assertNull($this->attributeResolver->resolve(MultipleAttributesContract::class));
4845
});
49-
50-
it('return null when invalid classes binding', function () {
51-
$this->assertNull($this->attributeResolver->resolve(InvalidClassBindingsContract::class));
52-
});
53-
54-
it('binds multiple valid services to the same interface', function () {
55-
/** @var BindContext $binding */
56-
$binding = $this->attributeResolver->resolve(MultiBindContract::class);
57-
58-
$expected = BindContext::create(
59-
MultiBindContract::class,
60-
[
61-
DefaultImplementationService::class,
62-
AlternativeImplementationService::class,
63-
],
64-
BindType::Transient
65-
);
66-
67-
expect($expected)
68-
->toEqual($binding)
69-
->and($binding->getConcrete())
70-
->toBeArray()
71-
->toHaveCount(2)
72-
->and($binding->getConcrete())
73-
->toEqual([DefaultImplementationService::class, AlternativeImplementationService::class]);
74-
});

workbench/app/AlternativeImplementationService.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

workbench/app/Contracts/DefaultBindTypeContract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Workbench\App\Contracts;
44

55
use AlgoYounes\Bindify\Attributes\BindWith;
6-
use Workbench\App\DefaultImplementationService;
6+
use Workbench\App\DefaultImplementationContract;
77

8-
#[BindWith(DefaultImplementationService::class)]
8+
#[BindWith(DefaultImplementationContract::class)]
99
interface DefaultBindTypeContract {}

workbench/app/Contracts/InvalidClassBindingsContract.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

workbench/app/Contracts/MultiBindContract.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)