Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/ru/providers/key-value/PHP.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ assert($container->get('username') === 'root');
* [JSON](json.md)
* [Custom format](custom-format.md)
* [LoaderInterface](loader-interface.md)
* [Transform](transform.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#### KeyValue transform.
#### KeyValue closureToService.

В некоторых случаях людям хочется использовать анонимные функции как Service.

Expand All @@ -16,7 +16,7 @@ $providers[] = new KeyValue([
'host' => '127.0.0.1',
'dbName' => 'test'
]);
$providers[] = KeyValue::transform([
$providers[] = KeyValue::closureToService([
'dsn' => function (ContainerInterface $c) {
// можно вернуть что угодно и создавать как угодно.
return "{$c->get('type')}:dbname={$c->get('dbName')};host={$c->get('host')}";
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/providers/key-value/custom-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ assert($container->get('username') === 'root');
* [JSON](json.md)
* [PHP](PHP.md)
* [LoaderInterface](loader-interface.md)
* [Transform](transform.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
2 changes: 1 addition & 1 deletion docs/ru/providers/key-value/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ echo $container->get('PATH');
* [PHP](PHP.md)
* [Custom format](custom-format.md)
* [LoaderInterface](loader-interface.md)
* [Transform](transform.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
2 changes: 1 addition & 1 deletion docs/ru/providers/key-value/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ ext-json required
* [PHP](PHP.md)
* [Custom format](custom-format.md)
* [LoaderInterface](loader-interface.md)
* [Transform](transform.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
2 changes: 1 addition & 1 deletion docs/ru/providers/key-value/key-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ assert($container->get(stdClass::class) instanceof stdClass);
* [PHP](PHP.md)
* [Custom format](custom-format.md)
* [LoaderInterface](loader-interface.md)
* [Transform](transform.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
2 changes: 1 addition & 1 deletion docs/ru/providers/key-value/loader-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ assert($container->get('dsn') ==='mysql:dbname=test;host=127.0.0.1');
* [JSON](json.md)
* [PHP](PHP.md)
* [Custom format](custom-format.md)
* [Transform](transform.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
38 changes: 38 additions & 0 deletions docs/ru/providers/key-value/stringToAlias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#### KeyValue stringToAlias.

В некоторых случаях людям хочется вынести список все используемых интерфейсов и их реализаций в отдельный файлик,

Для реализаций этих потребностей есть специальный метод хелпер.

```php
<?php
use Cekta\DI\Container;
use Cekta\DI\Provider\KeyValue;
use Psr\Container\ContainerInterface;

interface SomeInterface{}

class SomeImplementation implements SomeInterface{}

$providers[] = KeyValue::closureToService([
SomeInterface::class => SomeImplementation::class,
'example' => 123
]);
$container = new Container(...$providers);
assert($container->get(SomeInterface::class) instanceof SomeImplementation);
assert($container->get('example') === 123);
```

Во втором провайдере любая строка становится Alias, остальные значения не изменяются.

---
* [KeyValue](key-value.md)
* [Environment](environment.md)
* [JSON](json.md)
* [PHP](PHP.md)
* [Custom format](custom-format.md)
* [LoaderInterface](loader-interface.md)
* [ClosureToService](closureToService.md)
---
[Вернуться на главную](../../readme.md)
3 changes: 2 additions & 1 deletion docs/ru/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* [Custom format](providers/key-value/custom-format.md)
* Зависимости и анонимных функций:
* [KeyValue return LoaderInterface](providers/key-value/loader-interface.md)
* [KeyValue transform](providers/key-value/transform.md)
* [KeyValue closureToService](providers/key-value/closureToService.md)
* [KeyValue stringToAlias](providers/key-value/stringToAlias.md)
* [Autowiring](providers/autowiring/autowiring.md):
* [Autowiring и interface](providers/autowiring/interface.md)
* [Autowiring и RuleInterface](providers/autowiring/rule-interface.md)
Expand Down
15 changes: 14 additions & 1 deletion src/Provider/KeyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Cekta\DI\Provider;

use Cekta\DI\Loader\Alias;
use Cekta\DI\Loader\Service;
use Cekta\DI\Provider\Exception\NotFound;
use Cekta\DI\ProviderInterface;
Expand All @@ -15,7 +16,7 @@ class KeyValue implements ProviderInterface
*/
private $values;

public static function transform(array $values): self
public static function closureToService(array $values): self
{
$result = [];
foreach ($values as $key => $value) {
Expand All @@ -27,6 +28,18 @@ public static function transform(array $values): self
return new self($result);
}

public static function stringToAlias(array $values): self
{
$result = [];
foreach ($values as $key => $value) {
if (is_string($value)) {
$value = new Alias($value);
}
$result[$key] = $value;
}
return new self($result);
}

public function __construct(array $values)
{
$this->values = $values;
Expand Down
22 changes: 18 additions & 4 deletions tests/Provider/KeyValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Cekta\DI\Test\Provider;

use Cekta\DI\Loader\Alias;
use Cekta\DI\Loader\Service;
use Cekta\DI\Provider\Exception\NotFound;
use Cekta\DI\LoaderInterface;
use Cekta\DI\Provider\KeyValue;
Expand Down Expand Up @@ -66,16 +68,28 @@ public function testProvideLoader(): void
/**
* @throws ProviderExceptionInterface
*/
public function testTransform(): void
public function testClosureToService(): void
{
$provider = KeyValue::transform([
$provider = KeyValue::closureToService([
'a' => function () {
return new stdClass();
},
'b' => 123
]);
assert($this->container instanceof ContainerInterface);
$this->assertSame(123, $provider->provide('b'));
$this->assertInstanceOf(stdClass::class, $provider->provide('a')($this->container));
$this->assertInstanceOf(Service::class, $provider->provide('a'));
}

/**
* @throws ProviderExceptionInterface
*/
public function testStringToAlias(): void
{
$provider = KeyValue::stringToAlias([
'a' => stdClass::class,
'b' => 123
]);
$this->assertSame(123, $provider->provide('b'));
$this->assertInstanceOf(Alias::class, $provider->provide('a'));
}
}