Skip to content

Commit

Permalink
DependencyAnalyzer: Add markAsNamedValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Nov 19, 2014
1 parent 7747398 commit da02e7f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
54 changes: 42 additions & 12 deletions src/DependencyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class DependencyAnalyzer
*/
private $namedTypes = [];

/**
* @var array
*/
private $namedValues = [];

/**
* Registers the type and class to resolve the dependent.
*
Expand Down Expand Up @@ -70,6 +75,23 @@ public function markAsNamedType($typeName)
return $this;
}

/**
* Marks to use name-based binding.
*
* @param string $valueName The name of constructor's parameter
* @return DependencyAnalyzer
*/
public function markAsNamedValue($valueName)
{
if (isset($this->namedValues[$valueName])) {
throw new \InvalidArgumentException("`$valueName` is already marked as named value.");
}

$this->namedValues[$valueName] = true;

return $this;
}

/**
* @param array $classNames The class names to resolve the dependent.
* @return DependencyGraph
Expand All @@ -88,31 +110,39 @@ public function execute(array $classNames)
$nextServices = [];

foreach ($services as $service) {
$class = $service->getClass();
$dependencies = [];

if ($service->isDynamic()) {
$params = [];
} else {
$class = $service->getClass();
$constructor = $class->getConstructor();
$params = $constructor ? $constructor->getParameters() : [];
}

foreach ($params as $param) {
$paramClass = $param->getClass();
if (!$paramClass) {
throw new \InvalidArgumentException(
"The `{$param->getName()}` argument of `{$class->getName()}` is not specified type."
);
}

$paramClassName = $paramClass->getName();
if (isset($this->namedTypes[$paramClassName])) {
$dependency = new NamedService($param->getName(), $paramClass);
if ($paramClass) {
$paramClassName = $paramClass->getName();

if (isset($this->namedTypes[$paramClassName])) {
$dependency = new NamedService($param);
} else {
$dependency = isset($this->bindings[$paramClassName])
? $this->bindings[$paramClassName]
: new Service($paramClass, $paramClass);
}
} else {
$dependency = isset($this->bindings[$paramClassName])
? $this->bindings[$paramClassName]
: new Service($paramClass, $paramClass);
$paramName = $param->getName();

if (isset($this->namedValues[$paramName])) {
$dependency = new NamedService($param);
} else {
throw new \UnexpectedValueException(
"The `{$param->getName()}` dependent of `{$class->getName()}` can not be resolved."
);
}
}

$dependencies[] = $nextServices[] = $dependency;
Expand Down
25 changes: 10 additions & 15 deletions src/NamedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,42 @@
class NamedService
{
/**
* @var string
* @var \ReflectionParameter
*/
private $alias;
private $param;

/**
* @var \ReflectionClass $type
* @param \ReflectionParameter $type
*/
private $class;

/**
* @param string $alias
* @param \ReflectionClass $type
*/
public function __construct($alias, \ReflectionClass $class)
public function __construct(\ReflectionParameter $param)
{
$this->alias = $alias;
$this->class = $class;
$this->param = $param;
}

/**
* @return string
*/
public function __toString()
{
return "{$this->alias}@{$this->class->getName()}";
$name = $this->param->getName();
$class = $this->param->getClass();
return $class ? "{$name}@{$class->getName()}" : "{$name}@var";
}

/**
* @return \ReflectionClass
*/
public function getType()
{
return $this->class;
return $this->param->getClass();
}

/**
* @return \ReflectionClass
*/
public function getClass()
{
return $this->class;
return $this->param->getClass();
}

/**
Expand Down

0 comments on commit da02e7f

Please sign in to comment.