@@ -17,7 +17,14 @@ class Permissions
1717
1818 private $ globalExcludeControllers = [];
1919
20- private $ manualPermissions = [];
20+ protected $ onlyPermissions = [];
21+
22+ protected $ permissions = [];
23+
24+ public function __construct ()
25+ {
26+ $ this ->generate ();
27+ }
2128
2229 public static function make (): Permissions
2330 {
@@ -26,6 +33,24 @@ public static function make(): Permissions
2633
2734 public function get (): array
2835 {
36+ return $ this ->getCachedPermissions ();
37+ }
38+
39+ public function getOnlyPermissions ()
40+ {
41+ if (! $ this ->hasCachedPermissions ()) {
42+ return $ this ->onlyPermissions ;
43+ }
44+
45+ return Cache::get (Constant::CACHE_ONLY_PERMISSIONS );
46+ }
47+
48+ protected function generate (): Permissions
49+ {
50+ if ($ this ->hasCachedPermissions ()) {
51+ return $ this ;
52+ }
53+
2954 $ this ->controllerNamespacePrefixes = config (
3055 'permission-generator.controller-namespace-prefixes '
3156 );
@@ -40,16 +65,6 @@ public function get(): array
4065
4166 $ routes = Route::getRoutes ();
4267
43- $ routesCount = count ($ routes );
44-
45- $ cacheRoutes = self ::getCacheRoutes ($ routesCount );
46-
47- if ($ cacheRoutes !== null ) {
48- return $ cacheRoutes ;
49- }
50-
51- $ permissions = [];
52-
5368 $ tempRoutes = [];
5469
5570 foreach ($ routes as $ route ) {
@@ -60,7 +75,6 @@ public function get(): array
6075 continue ;
6176 }
6277
63-
6478 $ actionName = $ route ->getActionName ();
6579
6680 $ actionExtract = explode ('@ ' , $ actionName );
@@ -97,38 +111,54 @@ public function get(): array
97111 continue ;
98112 }
99113
100- $ title = self :: generatePermissionTitle ($ controllerInstance );
114+ $ title = $ this -> generatePermissionTitle ($ controllerInstance );
101115
102116 $ key = strtolower (Str::slug ($ title , "- " ));
103117
104- $ permissions [$ key ][] = [
118+ $ this -> permissions [$ key ][] = [
105119 'slug ' => $ routeName ,
106120 'name ' => ucwords (str_replace ($ splitter , ' ' , $ routeName )),
107121 ];
108122
109- $ tempRoutes = $ permissions [$ key ];
110- }
123+ $ this ->onlyPermissions [] = $ routeName ;
111124
112- // add manual permissions to rendered permissions
113- if ( ! empty ($ this ->manualPermissions )) {
114- foreach ($ this ->manualPermissions as $ key => $ permission ) {
115- $ permissions [$ key ][] = $ permission ;
116- }
125+ $ tempRoutes = $ this ->permissions [$ key ];
117126 }
118127
119- self ::cachePermissions ($ routesCount , $ permissions );
128+ // add custom permissions to rendered permissions
129+ $ this ->customPermissions ();
130+
131+ $ this ->cachePermissions ();
120132
121- return $ permissions ;
133+ return $ this ;
122134 }
123135
124- public function withManualPermissions ( array $ permissions ): Permissions
136+ protected function customPermissions ( ): Permissions
125137 {
126- $ this ->manualPermissions = $ permissions ;
138+ $ customPermissions = config ('permission-generator.custom-permissions ' );
139+
140+ if (is_array ($ customPermissions ) && ! empty ($ customPermissions )) {
141+ foreach ($ customPermissions as $ key => $ permission ) {
142+ if (array_key_exists (0 , $ permission )
143+ && is_array (
144+ $ permission [0 ]
145+ )
146+ ) {
147+ foreach ($ permission as $ item ) {
148+ $ this ->permissions [$ key ][] = $ item ;
149+ }
150+
151+ continue ;
152+ }
153+
154+ $ this ->permissions [$ key ][] = $ permission ;
155+ }
156+ }
127157
128158 return $ this ;
129159 }
130160
131- protected static function generatePermissionTitle ($ controllerInstance )
161+ protected function generatePermissionTitle ($ controllerInstance )
132162 {
133163 // if the controller use the WithPermissible interface then get the title
134164 if ($ controllerInstance instanceof WithPermissionGenerator) {
@@ -174,38 +204,33 @@ protected function isControllerValid($controller): bool
174204 return false ;
175205 }
176206
177- protected static function getCacheRoutes ( int $ routesCount )
207+ protected function getCachedPermissions ( )
178208 {
179- if ( ! config ('permission-generator.cache-permissions.cacheable ' )
180- || ! Cache::has (Constant::CACHE_ROUTES_COUNT_KEY )
181- ) {
182- return null ;
209+ if ( ! $ this ->hasCachedPermissions ()) {
210+ return $ this ->permissions ;
183211 }
184212
185- if ($ routesCount !== Cache::get (Constant::CACHE_ROUTES_COUNT_KEY )) {
186- Cache::forget (Constant::CACHE_ROUTES_COUNT_KEY );
187- Cache::forget (Constant::CACHE_PERMISSIONS_KEY );
188-
189- return null ;
190- }
213+ return Cache::get (Constant::CACHE_PERMISSIONS );
214+ }
191215
192- return Cache::get (Constant::CACHE_PERMISSIONS_KEY );
216+ protected function hasCachedPermissions (): bool
217+ {
218+ return config ('permission-generator.cache-permissions.cacheable ' )
219+ && Cache::has (Constant::CACHE_PERMISSIONS );
193220 }
194221
195- protected static function cachePermissions (int $ routesCount , $ permissions )
222+ protected function cachePermissions ()
196223 {
197- if (config ('permission-generator.cache-permissions.cacheable ' )
198- && ! Cache::has (Constant::CACHE_ROUTES_COUNT_KEY )
199- && ! empty ($ permissions )
200- ) {
224+ if (! $ this ->hasCachedPermissions () && ! empty ($ this ->permissions )) {
201225 Cache::put (
202- Constant::CACHE_ROUTES_COUNT_KEY ,
203- $ routesCount ,
226+ Constant::CACHE_PERMISSIONS ,
227+ $ this -> permissions ,
204228 now ()->addDay ()
205229 );
230+
206231 Cache::put (
207- Constant::CACHE_PERMISSIONS_KEY ,
208- $ permissions ,
232+ Constant::CACHE_ONLY_PERMISSIONS ,
233+ $ this -> onlyPermissions ,
209234 now ()->addDay ()
210235 );
211236 }
0 commit comments