@@ -80,6 +80,15 @@ class Factories
8080 */
8181 protected static $ instances = [];
8282
83+ /**
84+ * Whether the component instances are updated?
85+ *
86+ * @var array<string, true> [component => true]
87+ *
88+ * @internal For caching only
89+ */
90+ protected static $ updated = [];
91+
8392 /**
8493 * Define the class to load. You can *override* the concrete class.
8594 *
@@ -153,6 +162,7 @@ public static function __callStatic(string $component, array $arguments)
153162
154163 self ::$ instances [$ options ['component ' ]][$ class ] = new $ class (...$ arguments );
155164 self ::$ aliases [$ options ['component ' ]][$ alias ] = $ class ;
165+ self ::$ updated [$ options ['component ' ]] = true ;
156166
157167 // If a short classname is specified, also register FQCN to share the instance.
158168 if (! isset (self ::$ aliases [$ options ['component ' ]][$ class ])) {
@@ -383,7 +393,8 @@ public static function reset(?string $component = null)
383393 unset(
384394 static ::$ options [$ component ],
385395 static ::$ aliases [$ component ],
386- static ::$ instances [$ component ]
396+ static ::$ instances [$ component ],
397+ static ::$ updated [$ component ]
387398 );
388399
389400 return ;
@@ -392,6 +403,7 @@ public static function reset(?string $component = null)
392403 static ::$ options = [];
393404 static ::$ aliases = [];
394405 static ::$ instances = [];
406+ static ::$ updated = [];
395407 }
396408
397409 /**
@@ -440,4 +452,46 @@ public static function getBasename(string $alias): string
440452
441453 return $ alias ;
442454 }
455+
456+ /**
457+ * Gets component data for caching.
458+ *
459+ * @internal For caching only
460+ */
461+ public static function getComponentInstances (string $ component ): array
462+ {
463+ if (! isset (static ::$ aliases [$ component ])) {
464+ return [
465+ 'aliases ' => [],
466+ 'instances ' => [],
467+ ];
468+ }
469+
470+ return [
471+ 'aliases ' => static ::$ aliases [$ component ],
472+ 'instances ' => self ::$ instances [$ component ],
473+ ];
474+ }
475+
476+ /**
477+ * Sets component data
478+ *
479+ * @internal For caching only
480+ */
481+ public static function setComponentInstances (string $ component , array $ data ): void
482+ {
483+ static ::$ aliases [$ component ] = $ data ['aliases ' ];
484+ self ::$ instances [$ component ] = $ data ['instances ' ];
485+ unset(self ::$ updated [$ component ]);
486+ }
487+
488+ /**
489+ * Whether the component instances are updated?
490+ *
491+ * @internal For caching only
492+ */
493+ public static function isUpdated (string $ component ): bool
494+ {
495+ return isset (self ::$ updated [$ component ]);
496+ }
443497}
0 commit comments