PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.2
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.2
6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Discovery / PolicyDiscovery.php
kirki / libraries / framework / Discovery Last commit date
ListenerDiscovery.php 4 days ago PolicyDiscovery.php 4 days ago
PolicyDiscovery.php
197 lines
1 <?php
2
3 /**
4 * Discovers authorization policy classes in app/Policies and associates them with model classes by naming convention.
5 * Caches the policy map for production and validates policy class names.
6 * Feeds PolicyManager with model-to-policy bindings.
7 *
8 * @package Framework
9 * @subpackage Discovery
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Discovery;
13
14 \defined('ABSPATH') || exit;
15 use Kirki\Framework\Contracts\Cacheable;
16 use Kirki\Framework\Contracts\Discoverable;
17 use Kirki\Framework\Supports\Facades\File;
18 use function Kirki\Framework\app;
19 use function Kirki\Framework\app_path;
20 use function Kirki\Framework\config_path;
21 use function Kirki\Framework\Polyfill\str_ends_with;
22 class PolicyDiscovery implements Discoverable, Cacheable
23 {
24 /**
25 * The discovered policies array.
26 *
27 * @var array
28 *
29 * @since 1.0.0
30 */
31 protected array $policies = [];
32 /**
33 * Discover the policies from the file system.
34 *
35 * @return self
36 *
37 * @since 1.0.0
38 */
39 public function discover()
40 {
41 // For the production mode we will cache the policies to improve the performance.
42 // So don't need to discover the policies from the filesystem.
43 if (!app()->is_dev_mode()) {
44 return $this;
45 }
46 return $this->create_cache_file();
47 }
48 /**
49 * Create the cache file.
50 *
51 * @return self
52 *
53 * @since 1.0.0
54 */
55 protected function create_cache_file()
56 {
57 $policies_directory = app_path('Policies');
58 if (!\file_exists($policies_directory)) {
59 return $this;
60 }
61 $policy_files = \glob($policies_directory . '/*.php');
62 if (empty($policy_files)) {
63 return $this;
64 }
65 foreach ($policy_files as $policy_file) {
66 $policy_name = $this->filename($policy_file);
67 if (!$this->is_valid_policy_name($policy_name)) {
68 continue;
69 }
70 $policy = $this->policy_class($policy_name);
71 $model = $this->associated_model($policy_name);
72 $this->add_policy($model, $policy);
73 }
74 return $this;
75 }
76 /**
77 * Get the filename of the policy.
78 *
79 * @param string $path The path.
80 *
81 * @return string
82 *
83 * @since 1.0.0
84 */
85 protected function filename(string $path)
86 {
87 return \basename($path, '.php');
88 }
89 /**
90 * Add a policy to the policies array.
91 *
92 * @param string $model The model instance.
93 * @param string $policy The policy.
94 *
95 * @return void
96 *
97 * @since 1.0.0
98 */
99 protected function add_policy(string $model, string $policy)
100 {
101 $this->policies[] = \compact('model', 'policy');
102 }
103 /**
104 * Get the policies array.
105 *
106 * @return array
107 *
108 * @since 1.0.0
109 */
110 public function policies()
111 {
112 return $this->policies;
113 }
114 /**
115 * Get the policy class from the policy name.
116 *
117 * @param string $policy_name The policy name.
118 *
119 * @return string
120 *
121 * @since 1.0.0
122 */
123 protected function policy_class(string $policy_name)
124 {
125 $app_namespace = \rtrim(app()->get_namespace(), '\\');
126 $policy_base_namespace = $app_namespace . '\\Policies\\';
127 return $policy_base_namespace . $policy_name;
128 }
129 /**
130 * Get the model class from the policy name.
131 *
132 * @param string $policy_name The policy name.
133 *
134 * @return string
135 *
136 * @since 1.0.0
137 */
138 protected function associated_model(string $policy_name)
139 {
140 $app_namespace = \rtrim(app()->get_namespace(), '\\');
141 $model_base_namespace = $app_namespace . '\\Models\\';
142 $model_name = \str_replace('Policy', '', $policy_name);
143 return $model_base_namespace . $model_name;
144 }
145 /**
146 * Check if the policy name is valid.
147 *
148 * @param string $policy_name The policy name.
149 *
150 * @return bool
151 *
152 * @since 1.0.0
153 */
154 protected function is_valid_policy_name(string $policy_name)
155 {
156 if (!str_ends_with($policy_name, 'Policy')) {
157 return \false;
158 }
159 $model_class = $this->associated_model($policy_name);
160 if (!\class_exists($model_class)) {
161 return \false;
162 }
163 return \true;
164 }
165 /**
166 * Cache the policies.
167 *
168 * @param ?string $path The path.
169 *
170 * @return $this
171 *
172 * @since 1.0.0
173 */
174 public function cache(?string $path = null)
175 {
176 $path = $path ?? config_path('policies.cache.php');
177 if (!$this->is_cacheable($path)) {
178 return $this;
179 }
180 File::put($path, "<?php \r\n\r\n/** Auto generated by discovering the policies. Don't edit this file manually." . " */\r\n\r\nreturn " . \var_export($this->policies(), \true) . ';');
181 return $this;
182 }
183 /**
184 * Check if the policies are cacheable.
185 *
186 * @param string $path The path.
187 *
188 * @return bool
189 *
190 * @since 1.0.0
191 */
192 public function is_cacheable(string $path)
193 {
194 return app()->is_dev_mode() || File::missing($path);
195 }
196 }
197