@@ -74,29 +74,50 @@ public function extend($id, Closure $callable)
7474 }
7575 $ parent = $ this ->values [$ id ];
7676 $ this ->values [$ id ] = function ($ c ) use ($ callable , $ parent ) {
77- $ isFactory = is_object ($ parent ) && method_exists ($ parent , '__invoke ' );
78- return $ callable ($ isFactory ? $ parent ($ c ) : $ parent , $ c );
77+ return $ callable (self ::fabricate ($ parent , $ c ), $ c );
7978 };
8079 if (property_exists ($ this , $ id )) {
8180 $ this ->$ id = $ callable ($ this ->$ id , $ this );
8281 }
8382 return $ this ;
8483 }
8584
85+ /**
86+ * Invokes a callable or returns the value "as is".
87+ *
88+ * @param mixed $value
89+ * @param mixed $context
90+ * @return mixed
91+ */
92+ public static function fabricate ($ value , $ context )
93+ {
94+ return is_object ($ value ) && method_exists ($ value , '__invoke ' ) ? $ value ($ context ) : $ value ;
95+ }
96+
8697 /**
8798 * Gets a parameter or an object.
8899 *
89100 * @param string $id
90101 * @return mixed
91102 * @throws InvalidArgumentException
92103 */
93- public function __get ($ id )
104+ public function getValue ($ id )
94105 {
95106 if (!isset ($ this ->values [$ id ])) {
96107 throw new InvalidArgumentException (sprintf ('Identifier "%s" is undefined. ' , $ id ));
97108 }
98- $ value = $ this ->values [$ id ];
99- $ isFactory = is_object ($ value ) && method_exists ($ value , '__invoke ' );
100- return $ this ->$ id = $ isFactory ? $ value ($ this ) : $ value ;
109+ return self ::fabricate ($ this ->values [$ id ], $ this );
110+ }
111+
112+ /**
113+ * Gets a parameter or an object after caching it.
114+ *
115+ * @param string $id
116+ * @return mixed
117+ * @throws InvalidArgumentException
118+ */
119+ public function __get ($ id )
120+ {
121+ return $ this ->$ id = $ this ->getValue ($ id );
101122 }
102123}
0 commit comments