@@ -39,6 +39,9 @@ class ContainerBuilder
3939 /** @var array for auto-wiring */
4040 private $ classList = FALSE ;
4141
42+ /** @var bool */
43+ private $ classListNeedsRefresh = TRUE ;
44+
4245 /** @var string[] of classes excluded from auto-wiring */
4346 private $ excludedClasses = [];
4447
@@ -59,14 +62,21 @@ class ContainerBuilder
5962 */
6063 public function addDefinition ($ name , ServiceDefinition $ definition = NULL )
6164 {
65+ $ this ->classListNeedsRefresh = TRUE ;
6266 if (!is_string ($ name ) || !$ name ) { // builder is not ready for falsy names such as '0'
6367 throw new Nette \InvalidArgumentException (sprintf ('Service name must be a non-empty string, %s given. ' , gettype ($ name )));
6468 }
6569 $ name = isset ($ this ->aliases [$ name ]) ? $ this ->aliases [$ name ] : $ name ;
6670 if (isset ($ this ->definitions [$ name ])) {
6771 throw new Nette \InvalidStateException ("Service ' $ name' has already been added. " );
6872 }
69- return $ this ->definitions [$ name ] = $ definition ?: new ServiceDefinition ;
73+ if (!$ definition ) {
74+ $ definition = new ServiceDefinition ;
75+ }
76+ $ definition ->setNotifier (function () {
77+ $ this ->classListNeedsRefresh = TRUE ;
78+ });
79+ return $ this ->definitions [$ name ] = $ definition ;
7080 }
7181
7282
@@ -77,6 +87,7 @@ public function addDefinition($name, ServiceDefinition $definition = NULL)
7787 */
7888 public function removeDefinition ($ name )
7989 {
90+ $ this ->classListNeedsRefresh = TRUE ;
8091 $ name = isset ($ this ->aliases [$ name ]) ? $ this ->aliases [$ name ] : $ name ;
8192 unset($ this ->definitions [$ name ]);
8293 }
@@ -258,10 +269,9 @@ public function findByTag($tag)
258269
259270 private function getClassList ()
260271 {
261- static $ prev ;
262- if ($ this ->classList !== FALSE && $ prev !== serialize ($ this ->definitions )) {
272+ if ($ this ->classList !== FALSE && $ this ->classListNeedsRefresh ) {
263273 $ this ->prepareClassList ();
264- $ prev = serialize ( $ this ->definitions ) ;
274+ $ this ->classListNeedsRefresh = FALSE ;
265275 }
266276 return $ this ->classList ?: [];
267277 }
0 commit comments