From 6cb84507de79aeacd682bd6c8802574e7745c338 Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Thu, 18 Dec 2014 22:38:05 +0100 Subject: [PATCH] Added Container::resolvingType and Container::afterResolvingType --- src/Illuminate/Container/Container.php | 66 ++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 5fceb8522e53..8eeed5d65d50 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -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. * @@ -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. * @@ -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)); } /** @@ -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)); + } + }