Skip to content

Commit

Permalink
Merge e621ac1 into 85d635e
Browse files Browse the repository at this point in the history
  • Loading branch information
feryardiant committed Jun 12, 2020
2 parents 85d635e + e621ac1 commit 18bf177
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
CC_TEST_REPORTER_ID: ${{ secrets.github_token }}
strategy:
matrix:
php-versions: ['7.1', '7.2', '7.3', '7.4']
php-versions: ['7.2', '7.3', '7.4']

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
},
"require": {
"php": "^7.1",
"php": "^7.2",
"psr/container": "^1.0"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions src/Container/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function handle(callable $instance, array $args = [])
{
if (is_string($instance) && false !== strpos($instance, '::')) {
$instance = explode('::', $instance);
} elseif (is_object($instance)) {
$instance = (new \ReflectionMethod($instance, '__invoke'))->getClosure($instance);
}

$params = [];
Expand Down Expand Up @@ -64,11 +66,7 @@ public function resolve($concrete)
return $this->createInstance($concrete);
}

if ($concrete instanceof \Closure) {
return $concrete->bindTo($this->container);
}

if (is_object($concrete) || is_callable($concrete)) {
if ($concrete instanceof \Closure || is_object($concrete) || is_callable($concrete)) {
return $concrete;
}

Expand Down
11 changes: 10 additions & 1 deletion test/spec/Container.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Projek\Container;
use Projek\Container\{ArrayContainer, ContainerInterface, Exception, NotFoundException, Resolver };
use Psr\Container\ContainerInterface as PsrContainer;
use Stubs\ { Dummy, AbstractFoo, ConcreteBar };
use Stubs\ { Dummy, AbstractFoo, ConcreteBar, ServiceProvider};
use function Kahlan\describe;
use function Kahlan\expect;

Expand All @@ -28,6 +28,15 @@
expect($this->c->get(PsrContainer::class))->toBeAnInstanceOf(PsrContainer::class);
});

it('Should resolve serive provider', function () {
$this->c->set('dummy', Dummy::class);
$this->c->set(AbstractFoo::class, ConcreteBar::class);

$this->c->set('myService', ServiceProvider::class);

expect($this->c->get('myService'))->toEqual('dummy lorem');
});

it('Should manage instance', function () {
$this->c->set('dummy', Dummy::class);

Expand Down
2 changes: 1 addition & 1 deletion test/spec/Resolver.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
it('could handle array callable', function () {
expect(
$this->r->handle([$this->dummy, 'lorem'])
)->toEqual('lorem');
)->toEqual('dummy lorem');

expect(
$this->r->handle([ConcreteBar::class, 'std'])
Expand Down
2 changes: 1 addition & 1 deletion test/stub/AbstractFoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

abstract class AbstractFoo
{
public function lorem($text = 'lorem')
public function lorem(string $text = 'lorem')
{
return $text;
}
Expand Down
4 changes: 2 additions & 2 deletions test/stub/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class Dummy
{
public function lorem(AbstractFoo $foo)
public function lorem(AbstractFoo $foo, $text = 'dummy lorem')
{
return $foo->lorem();
return $foo->lorem($text);
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/stub/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Stubs;

class ServiceProvider
{
protected $abs;

public function __construct(AbstractFoo $abs)
{
$this->abs = $abs;
}

/**
* @param Dummy $d
* @return string
*/
public function __invoke($dummy)
{
return $dummy->lorem($this->abs);
}
}

0 comments on commit 18bf177

Please sign in to comment.