Skip to content

Commit

Permalink
Add service providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vegner committed Aug 2, 2017
1 parent 177e532 commit 073e6fa
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public function __construct(array $config = [], $selfName = '') {
}
}

public function connect(ProviderInterface $provider) {
$this->initializers[$provider->getName()] = function () use ($provider) {
$this->inject($provider);
return $provider->provide();
};
}

public function register($name, $initializer) {
if ($initializer instanceof Value) {
$this->services[$name] = $initializer->getValue();
Expand Down
1 change: 0 additions & 1 deletion src/DeclarerInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace SD\DependencyInjection;

interface DeclarerInterface {
Expand Down
7 changes: 7 additions & 0 deletions src/ProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace SD\DependencyInjection;

interface ProviderInterface {
public function getName(): string;
public function provide();
}
16 changes: 16 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public function testExtend() {
$service = $container->get($serviceName);
$this->assertEquals($name2, $service->getName(), 'Must return modified name');
}

public function testConnect() {
$name = 'Luke Skywalker';
$container = new Container(
[
'name' => $name,
],
'container'
);
$provider = new LegacyProvider();
$container->connect($provider);
$service = $container->get($provider->getName());
$this->assertInstanceOf(LegacyService::class, $service, 'Must return instance of LegacyService');
$this->assertEquals($name, $service->getName(), 'Must inject name from config');
$this->assertEquals($container, $service->getContainer(), 'Must inject container by setter');
}
}
32 changes: 32 additions & 0 deletions tests/LegacyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace tests;

use SD\DependencyInjection\AutoDeclareTrait;
use SD\DependencyInjection\ContainerAwareTrait;
use SD\DependencyInjection\DeclarerInterface;
use SD\DependencyInjection\ProviderInterface;

class LegacyProvider implements DeclarerInterface, ProviderInterface {
use AutoDeclareTrait {
declareDependencies as autoDeclareDependencies;
}
use ContainerAwareTrait;

private $name;

public function declareDependencies() {
return array_merge($this->autoDeclareDependencies(), ['name']);
}

public function setName(string $name) {
$this->name = $name;
}

public function getName(): string {
return 'helloWorld';
}

public function provide() {
return new LegacyService($this->name, $this->container);
}
}
22 changes: 22 additions & 0 deletions tests/LegacyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace tests;

use SD\DependencyInjection\Container;

class LegacyService {
private $container;
private $name;

public function __construct(string $name, Container $container) {
$this->name = $name;
$this->container = $container;
}

public function getName(): string {
return $this->name;
}

public function getContainer() {
return $this->container;
}
}

0 comments on commit 073e6fa

Please sign in to comment.