@@ -47,6 +47,9 @@ class PermissionRegistrar
4747 /** @var string */
4848 public static $ cacheKey ;
4949
50+ /** @var array */
51+ private $ cachedRoles = [];
52+
5053 /**
5154 * PermissionRegistrar constructor.
5255 *
@@ -153,30 +156,36 @@ public function clearClassPermissions()
153156 */
154157 private function loadPermissions ()
155158 {
156- if ($ this ->permissions === null ) {
157- $ this ->permissions = $ this ->cache ->remember (self ::$ cacheKey , self ::$ cacheExpirationTime , function () {
158- // make the cache smaller using an array with only required fields
159- return $ this ->getPermissionClass ()->select ('id ' , 'id as i ' , 'name as n ' , 'guard_name as g ' )
160- ->with ('roles:id,id as i,name as n,guard_name as g ' )->get ()
161- ->map (function ($ permission ) {
162- return $ permission ->only ('i ' , 'n ' , 'g ' ) +
163- ['r ' => $ permission ->roles ->map ->only ('i ' , 'n ' , 'g ' )->all ()];
164- })->all ();
159+ if ($ this ->permissions !== null ) {
160+ return ;
161+ }
162+
163+ $ this ->permissions = $ this ->cache ->remember (self ::$ cacheKey , self ::$ cacheExpirationTime , function () {
164+ // make the cache smaller using an array with only required fields
165+ return $ this ->getPermissionClass ()->select ('id ' , 'id as i ' , 'name as n ' , 'guard_name as g ' )
166+ ->with ('roles:id,id as i,name as n,guard_name as g ' )->get ()
167+ ->map (function ($ permission ) {
168+ return $ permission ->only ('i ' , 'n ' , 'g ' ) +
169+ ['r ' => $ permission ->roles ->map ->only ('i ' , 'n ' , 'g ' )->all ()];
170+ })->all ();
171+ });
172+
173+ if (is_array ($ this ->permissions )) {
174+ $ this ->permissions = $ this ->getPermissionClass ()::hydrate (
175+ collect ($ this ->permissions )->map (function ($ item ) {
176+ return ['id ' => $ item ['i ' ] ?? $ item ['id ' ], 'name ' => $ item ['n ' ] ?? $ item ['name ' ], 'guard_name ' => $ item ['g ' ] ?? $ item ['guard_name ' ]];
177+ })->all ()
178+ )
179+ ->each (function ($ permission , $ i ) {
180+ $ roles = Collection::make ($ this ->permissions [$ i ]['r ' ] ?? $ this ->permissions [$ i ]['roles ' ] ?? [])
181+ ->map (function ($ item ) {
182+ return $ this ->getHydratedRole ($ item );
183+ });
184+
185+ $ permission ->setRelation ('roles ' , $ roles );
165186 });
166- if (is_array ($ this ->permissions )) {
167- $ this ->permissions = $ this ->getPermissionClass ()::hydrate (
168- collect ($ this ->permissions )->map (function ($ item ) {
169- return ['id ' => $ item ['i ' ] ?? $ item ['id ' ], 'name ' => $ item ['n ' ] ?? $ item ['name ' ], 'guard_name ' => $ item ['g ' ] ?? $ item ['guard_name ' ]];
170- })->all ()
171- )
172- ->each (function ($ permission , $ i ) {
173- $ permission ->setRelation ('roles ' , $ this ->getRoleClass ()::hydrate (
174- collect ($ this ->permissions [$ i ]['r ' ] ?? $ this ->permissions [$ i ]['roles ' ] ?? [])->map (function ($ item ) {
175- return ['id ' => $ item ['i ' ] ?? $ item ['id ' ], 'name ' => $ item ['n ' ] ?? $ item ['name ' ], 'guard_name ' => $ item ['g ' ] ?? $ item ['guard_name ' ]];
176- })->all ()
177- ));
178- });
179- }
187+
188+ $ this ->cachedRoles = [];
180189 }
181190 }
182191
@@ -247,4 +256,22 @@ public function getCacheStore(): \Illuminate\Contracts\Cache\Store
247256 {
248257 return $ this ->cache ->getStore ();
249258 }
259+
260+ private function getHydratedRole (array $ item )
261+ {
262+ $ roleId = $ item ['i ' ] ?? $ item ['id ' ];
263+
264+ if (isset ($ this ->cachedRoles [$ roleId ])) {
265+ return $ this ->cachedRoles [$ roleId ];
266+ }
267+
268+ $ roleClass = $ this ->getRoleClass ();
269+ $ roleInstance = new $ roleClass ;
270+
271+ return $ this ->cachedRoles [$ roleId ] = $ roleInstance ->newFromBuilder ([
272+ 'id ' => $ roleId ,
273+ 'name ' => $ item ['n ' ] ?? $ item ['name ' ],
274+ 'guard_name ' => $ item ['g ' ] ?? $ item ['guard_name ' ],
275+ ]);
276+ }
250277}
0 commit comments