Skip to content

Commit

Permalink
Merge branch 'feature/service-manager-provides' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
trq committed Jan 18, 2012
2 parents aec0d0a + b62383f commit 46872f8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/Proem/Api/Service/Asset/Generic.php
Expand Up @@ -58,6 +58,27 @@ class Generic
*/
private $asset;

/**
* Store a flag indicating what object this Asset provides.
*
* @var string $provides
*/
private $provides;

/**
* Get or Set a flag indicating what object this Asset provides.
*
*/
public function provides($provides = null)
{
if ($provides !== null) {
$this->provides = $provides;
return $this;
}

return $this->provides;
}

/**
* Set a parameters by named index
*
Expand All @@ -71,7 +92,7 @@ public function setParam($index, $value)
}

/**
* A maigic method shortcut that proxies setParam()
* A magic method shortcut that proxies setParam()
*
* @param string $index
* @param mixed $value
Expand Down Expand Up @@ -102,7 +123,7 @@ public function getParam($index)
}

/**
* A maigic method shortcut that proxies getParam()
* A magic method shortcut that proxies getParam()
*
* @param string $index
*/
Expand Down
33 changes: 33 additions & 0 deletions lib/Proem/Api/Service/Manager.php
Expand Up @@ -54,6 +54,12 @@ class Manager
*/
private $assets = [];

/**
* Store an array containing information about what
* Assets this manager provides
*/
private $provides = [];

/**
* Store an Asset container by named index.
*
Expand All @@ -63,6 +69,9 @@ class Manager
public function set($index, Asset $asset)
{
$this->assets[$index] = $asset;
if ($provides = $asset->provides()) {

This comment has been minimized.

Copy link
@trq

trq Jan 18, 2012

Author Member

We should probably enforce a check here to ensure that the Asset has advertised what object it provides.

Maybe throwing an Exception on failure?

$this->provides[] = $provides;
}
return $this;
}

Expand Down Expand Up @@ -96,4 +105,28 @@ public function has($index)
return isset($this->assets[$index]);
}

/**
* Retrieve a "provides" flag
*
* @param string $provides
*/
public function provides($provides)
{
return in_array($provides, $this->provides);
}

/**
* Retrieve an Asset by what it provides
*
* @param string $provides
*/
public function getProvided($provides)
{
foreach ($this->assets as $asset) {
if ($asset->provides() == $provides) {
return $asset->get($this);
}
}
}

}
35 changes: 35 additions & 0 deletions tests/lib/Proem/Tests/ServiceTest.php
Expand Up @@ -137,6 +137,41 @@ public function testAssetManagerCanStoreAndRetrieve()
$this->assertInstanceOf('Proem\Service\Asset\Bar', $am->get('bar'));
}

public function testAssetProvides()
{
$bar = new Asset;
$bar->provides('foo');
$this->assertEquals('foo', $bar->provides());
}

public function testManagerProvides()
{
$bar = new Asset;
$bar->provides('foo');

$am = new Manager;
$am->set('bar', $bar);
$this->assertTrue($am->provides('foo'));
}

public function testRetrieveByProvides()
{
$bar = new Asset;
$bar->provides('Bar')->set(function() {
return new Bar;
});

$foo = new Asset;
$foo->provides('Foo')->set(function() {
return new Foo;
});

$am = new Manager;
$am->set('bar', $bar)->set('foo', $foo);

$this->assertInstanceOf('Proem\Service\Asset\Bar', $am->getProvided('Bar'));
}

public function testCanGetDepsThroughManager()
{
$bar = new Asset;
Expand Down

0 comments on commit 46872f8

Please sign in to comment.