Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ class Container implements ArrayAccess, ContainerContract {
*/
protected $globalAfterResolvingCallbacks = array();

/**
* All of the after resolving callbacks by class type.
*
* @var array
*/
protected $resolvingCallbacksByType = array();

/**
* All of the after resolving callbacks by class type.
*
* @var array
*/
protected $afterResolvingCallbacksByType = array();

/**
* Define a contextual binding.
*
Expand Down Expand Up @@ -949,6 +963,24 @@ public function afterResolvingAny(Closure $callback)
$this->globalAfterResolvingCallbacks[] = $callback;
}

/**
* @param $type
* @param callable $callback
*/
public function resolvingType($type, Closure $callback)
{
$this->resolvingCallbacksByType[$type][] = $callback;
}

/**
* @param $type
* @param callable $callback
*/
public function afterResolvingType($type, Closure $callback)
{
$this->afterResolvingCallbacksByType[$type][] = $callback;
}

/**
* Fire all of the resolving callbacks.
*
Expand All @@ -964,8 +996,10 @@ protected function fireResolvingCallbacks($abstract, $object)
}

$this->fireCallbackArray($object, $this->globalResolvingCallbacks);
$this->fireCallbackArray($object, $this->getCallbacksForType($object, $this->resolvingCallbacksByType));

$this->fireCallbackArray($object, $this->globalAfterResolvingCallbacks);
$this->fireCallbackArray($object, $this->getCallbacksForType($object, $this->afterResolvingCallbacksByType));
}

/**
Expand Down Expand Up @@ -1180,4 +1214,36 @@ public function __set($key, $value)
$this[$key] = $value;
}

/**
* Get all callbacks for all subtypes and interfaces for specific $abstract type.
*
* @param object $object
* @param array $callbacksPerType
*
* @return array
*/
protected function getCallbacksForType($object, array $callbacksPerType)
{
$callbacks = $this->getItemsForType($object, $callbacksPerType);

return $callbacks ? call_user_func_array('array_merge', $callbacks) : [];
}

/**
* Return all array items which are defined by a key which is a subtype of $abstract
*
* @param object $object
* @param array $array
*
* @return array
*/
protected function getItemsForType($object, array $array)
{
$types = array_filter(array_keys($array), function ($type) use ($object) {
return is_a($object, $type);
});

return array_intersect_key($array, array_flip($types));
}

}