Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Commit

Permalink
remove storage
Browse files Browse the repository at this point in the history
add more tests
  • Loading branch information
rez1dent3 committed Jan 14, 2017
1 parent 2126222 commit 2fb9632
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/DI/DI.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,6 @@ protected function steps(&$row, array $keys)
}
}

protected function storage($name)
{
if ($this->self->storage[$name] instanceof Instance)
{
/**
* @var $instance Instance
*/
$instance = $this->self->storage[$name];

$value = $instance->get();
unset($this->self->storage[$name]);

$this->self->storage[$name] = $value;
}

return $this->self->storage[$name];
}

protected function build($name, callable $callback)
{
$this->value($name, $callback);
Expand All @@ -114,9 +96,10 @@ protected function path($name)

protected function getFirst(&$path)
{
$name = array_shift($path);
$name = array_shift($path);
$object = $this->self->storage[$name];

return $this->storage($name);
return $this->getInstance($object);
}

public function get($name)
Expand All @@ -134,10 +117,20 @@ public function get($name)
{
if ($row instanceof self)
{
return $row->self->storage[$last];
return $this->getInstance($row->self->storage[$last]);
}

return $row->{$last}();
$row = $row->{$last}();
}

return $this->getInstance($row);
}

protected function getInstance($row)
{
if ($row instanceof Instance)
{
return $row->get();
}

return $row;
Expand Down
22 changes: 22 additions & 0 deletions tests/Test/DITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test;

use Deimos\DI\Argument;
use Deimos\DI\ContainerEmpty;
use Deimos\DI\Group;
use DeimosTest\Person;
Expand Down Expand Up @@ -68,6 +69,9 @@ public function testPerson()
$this->assertInstanceOf(Person::class, $this->di->get('ivan'));

$this->di->call('ivan.setAge', ['@nine']);

$this->assertEquals($this->di->nine(), 9);

$this->assertEquals($this->di->get('ivan.age'), $this->di->nine());
$this->assertEquals($this->di->get('ivan.age'), $this->di->get('nine'));

Expand All @@ -77,4 +81,22 @@ public function testPerson()
$this->assertEquals($this->di->ivan()->age(), 34);
}

public function testTree()
{
// group
$this->assertInstanceOf(Group::class, $this->di->get('l1'));
$this->assertInstanceOf(Group::class, $this->di->get('l1.l2'));
$this->assertInstanceOf(Group::class, $this->di->get('l1.l2.l3'));
$this->assertInstanceOf(Group::class, $this->di->get('l1.l2.l3.l4'));
$this->assertInstanceOf(Group::class, $this->di->get('l1.l2.l3.l4.l5'));

// argument
$this->assertInstanceOf(Argument::class, $this->di->get('l1.l2.l3.l4.l5.argument'));
$this->assertCount(2, $this->di->call('l1.l2.l3.l4.l5.argument.get'));

list($two, $nine) = $this->di->get('l1.l2.l3.l4.l5.argument.get');
$this->assertNotEquals($two, $this->di->get('two'));
$this->assertNotEquals($nine, $this->di->get('nine'));
}

}
26 changes: 26 additions & 0 deletions tests/src/DI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeimosTest;

use Deimos\DI\Argument;

class DI extends \Deimos\DI\DI
{

Expand Down Expand Up @@ -42,6 +44,30 @@ protected function configure()
});

$this->instance('ivan', Person::class, ['@firstName', '@lastName']);

$this->group('l1', function ()
{
$this->group('l2', function ()
{
$this->group('l3', function ()
{
$this->group('l4', function ()
{
$this->group('l5', function ()
{
$this->value('two', mt_rand(3, 9999)); // for debug
$this->value('nine', mt_rand(10, 9999));

$this->instance(
'argument',
Argument::class,
[$this, [$this->two(), $this->nine()]]
);
});
});
});
});
});
}

}

0 comments on commit 2fb9632

Please sign in to comment.