Skip to content

Commit

Permalink
Add alias support to DI
Browse files Browse the repository at this point in the history
  • Loading branch information
dongilbert committed Oct 29, 2013
1 parent e907c47 commit 674eb8b
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
@@ -0,0 +1,3 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
vendor/
composer.phar
composer.lock
phpunit.xml
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: php

php:
- 5.3
- 5.4

before_script:
- composer update --dev

script:
- phpunit
52 changes: 48 additions & 4 deletions Container.php
Expand Up @@ -17,14 +17,23 @@
*/
class Container
{
/**
* Holds the key aliases.
*
* @var array $aliases
*
* @since 1.0
*/
protected $aliases = array();

/**
* Holds the shared instances.
*
* @var array $instances
*
* @since 1.0
*/
private $instances = array();
protected $instances = array();

/**
* Holds the keys, their callbacks, and whether or not
Expand All @@ -34,7 +43,7 @@ class Container
*
* @since 1.0
*/
private $dataStore = array();
protected $dataStore = array();

/**
* Parent for hierarchical containers.
Expand All @@ -43,7 +52,7 @@ class Container
*
* @since 1.0
*/
private $parent;
protected $parent;

/**
* Constructor for the DI Container
Expand All @@ -57,6 +66,40 @@ public function __construct(Container $parent = null)
$this->parent = $parent;
}

/**
* Create an alias for a given key for easy access.
*
* @param string $alias The alias name
* @param string $key The key to alias
*
* @return Container
*/
public function alias($alias, $key)
{
$this->aliases[$alias] = $key;

return $this;
}

/**
* Search the aliases property for a matching alias key.
*
* @param string $key The key to search for.
*
* @return string
*
* @since 1.0
*/
public function resolveAlias($key)
{
if (isset($this->aliases[$key]))
{
return $this->aliases[$key];
}

return $key;
}

/**
* Build an object of class $key;
*
Expand Down Expand Up @@ -223,7 +266,6 @@ protected function getMethodArgs(\ReflectionMethod $method)
* @return \Joomla\DI\Container This instance to support chaining.
*
* @throws \OutOfBoundsException Thrown if the provided key is already set and is protected.
* @throws \UnexpectedValueException Thrown if the provided callback is not valid.
*
* @since 1.0
*/
Expand Down Expand Up @@ -325,6 +367,8 @@ public function get($key, $forceNew = false)
*/
protected function getRaw($key)
{
$key = $this->resolveAlias($key);

if (isset($this->dataStore[$key]))
{
return $this->dataStore[$key];
Expand Down
69 changes: 68 additions & 1 deletion Tests/ContainerTest.php
Expand Up @@ -154,6 +154,67 @@ public function testConstructorWithParent()
);
}

/**
* Test the alias method.
*
* @return void
*
* @since 1.0
*/
public function testAlias()
{
$this->fixture->alias('foo', 'bar');

$aliases = $this->readAttribute($this->fixture, 'aliases');

$this->assertEquals(
array('foo' => 'bar'),
$aliases,
'When setting an alias, it should be set in the $aliases Container property.'
);
}

/**
* Test resolving an alias that has been set.
*
* @return void
*
* @since 1.0
*/
public function testResolveAliasSet()
{
$reflection = new \ReflectionProperty($this->fixture, 'aliases');
$reflection->setAccessible(true);

$reflection->setValue($this->fixture, array('foo' => 'bar'));

$alias = $this->fixture->resolveAlias('foo');

$this->assertEquals(
'bar',
$alias,
'When resolving an alias that has been set, the aliased key should be returned.'
);
}

/**
* Test resolving an alias that has not been set.
*
* @return void
*
* @since 1.0
*/
public function testResolveAliasNotSet()
{
$alias = $this->fixture->resolveAlias('foo');

$this->assertEquals(
'foo',
$alias,
'When resolving an alias that has not been set, the requested key should be returned.'
);
}

/**
* Tests the buildObject with no dependencies.
*
Expand Down Expand Up @@ -242,7 +303,7 @@ public function testCreateChild()
'Joomla\\DI\\Container',
'parent',
$child,
'When create a child container, the $parent property should be an instance of Joomla\\DI\\Container.'
'When creating a child container, the $parent property should be an instance of Joomla\\DI\\Container.'
);

$this->assertAttributeSame(
Expand Down Expand Up @@ -443,6 +504,12 @@ public function testSetNotClosure()
$dataStore['foo']['callback'],
'Passing a non-closure to set will wrap the item in a closure for easy resolution and extension.'
);

$this->assertEquals(
'bar',
$dataStore['foo']['callback']($this->fixture),
'Resolving a non-closure should return the set value.'
);
}

/**
Expand Down
18 changes: 0 additions & 18 deletions Tests/bootstrap.php

This file was deleted.

2 changes: 1 addition & 1 deletion phpunit.xml.dist
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Tests/bootstrap.php" colors="false">
<phpunit bootstrap="vendor/autoload.php" colors="false">
<testsuites>
<testsuite name="Unit">
<directory>Tests</directory>
Expand Down

0 comments on commit 674eb8b

Please sign in to comment.