@@ -23,6 +23,16 @@ class CachedContentTypeLoader implements ContentTypeLoader
2323 */
2424 private $ loadedItems = [];
2525
26+ /**
27+ * @var array
28+ */
29+ private $ identifierToIdMap = [];
30+
31+ /**
32+ * @var ContentType[]
33+ */
34+ private $ loadedItemsByIdentifier = [];
35+
2636 public function __construct (ContentTypeLoader $ innerLoader )
2737 {
2838 $ this ->innerLoader = $ innerLoader ;
@@ -31,20 +41,23 @@ public function __construct(ContentTypeLoader $innerLoader)
3141 public function load ($ contentTypeId ): ContentType
3242 {
3343 if (!isset ($ this ->loadedItems [$ contentTypeId ])) {
34- $ this ->loadedItems [$ contentTypeId ] = $ this ->innerLoader ->load ($ contentTypeId );
44+ $ contentType = $ this ->innerLoader ->load ($ contentTypeId );
45+ $ this ->loadedItems [$ contentTypeId ] = $ contentType ;
46+ $ this ->identifierToIdMap [$ contentType ->identifier ] = $ contentTypeId ;
3547 }
3648
3749 return $ this ->loadedItems [$ contentTypeId ];
3850 }
3951
4052 public function loadByIdentifier ($ identifier ): ContentType
4153 {
42- $ contentType = $ this ->innerLoader -> loadByIdentifier ( $ identifier);
43-
44- if (! isset ( $ this ->innerLoader [$ contentType ->id ])) {
45- $ this ->innerLoader [ $ contentType -> id ] = $ contentType ;
54+ if (! isset ( $ this ->identifierToIdMap [ $ identifier])) {
55+ $ contentType = $ this -> innerLoader -> loadByIdentifier ( $ identifier );
56+ $ this ->loadedItems [$ contentType ->id ] = $ contentType ;
57+ $ this ->identifierToIdMap [ $ identifier ] = $ contentType-> id ;
4658 }
4759
48- return $ contentType ;
60+
61+ return $ this ->loadedItems [$ this ->identifierToIdMap [$ identifier ]];
4962 }
5063}
0 commit comments