PolicyDiscovery.php
212 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->discover_once(); |
| 45 | } |
| 46 | return $this->create_cache_file(); |
| 47 | } |
| 48 | /** |
| 49 | * Discover the policies from the cache. |
| 50 | * |
| 51 | * @return self |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | protected function discover_once() |
| 56 | { |
| 57 | $policies_cache_path = config_path('policies.cache.php'); |
| 58 | if (!\file_exists($policies_cache_path)) { |
| 59 | return $this; |
| 60 | } |
| 61 | return $this->create_cache_file(); |
| 62 | } |
| 63 | /** |
| 64 | * Create the cache file. |
| 65 | * |
| 66 | * @return self |
| 67 | * |
| 68 | * @since 1.0.0 |
| 69 | */ |
| 70 | protected function create_cache_file() |
| 71 | { |
| 72 | $policies_directory = app_path('Policies'); |
| 73 | if (!\file_exists($policies_directory)) { |
| 74 | return $this; |
| 75 | } |
| 76 | $policy_files = \glob($policies_directory . '/*.php'); |
| 77 | if (empty($policy_files)) { |
| 78 | return $this; |
| 79 | } |
| 80 | foreach ($policy_files as $policy_file) { |
| 81 | $policy_name = $this->filename($policy_file); |
| 82 | if (!$this->is_valid_policy_name($policy_name)) { |
| 83 | continue; |
| 84 | } |
| 85 | $policy = $this->policy_class($policy_name); |
| 86 | $model = $this->associated_model($policy_name); |
| 87 | $this->add_policy($model, $policy); |
| 88 | } |
| 89 | return $this; |
| 90 | } |
| 91 | /** |
| 92 | * Get the filename of the policy. |
| 93 | * |
| 94 | * @param string $path The path. |
| 95 | * |
| 96 | * @return string |
| 97 | * |
| 98 | * @since 1.0.0 |
| 99 | */ |
| 100 | protected function filename(string $path) |
| 101 | { |
| 102 | return \basename($path, '.php'); |
| 103 | } |
| 104 | /** |
| 105 | * Add a policy to the policies array. |
| 106 | * |
| 107 | * @param string $model The model instance. |
| 108 | * @param string $policy The policy. |
| 109 | * |
| 110 | * @return void |
| 111 | * |
| 112 | * @since 1.0.0 |
| 113 | */ |
| 114 | protected function add_policy(string $model, string $policy) |
| 115 | { |
| 116 | $this->policies[] = \compact('model', 'policy'); |
| 117 | } |
| 118 | /** |
| 119 | * Get the policies array. |
| 120 | * |
| 121 | * @return array |
| 122 | * |
| 123 | * @since 1.0.0 |
| 124 | */ |
| 125 | public function policies() |
| 126 | { |
| 127 | return $this->policies; |
| 128 | } |
| 129 | /** |
| 130 | * Get the policy 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 policy_class(string $policy_name) |
| 139 | { |
| 140 | $app_namespace = \rtrim(app()->get_namespace(), '\\'); |
| 141 | $policy_base_namespace = $app_namespace . '\\Policies\\'; |
| 142 | return $policy_base_namespace . $policy_name; |
| 143 | } |
| 144 | /** |
| 145 | * Get the model class from the policy name. |
| 146 | * |
| 147 | * @param string $policy_name The policy name. |
| 148 | * |
| 149 | * @return string |
| 150 | * |
| 151 | * @since 1.0.0 |
| 152 | */ |
| 153 | protected function associated_model(string $policy_name) |
| 154 | { |
| 155 | $app_namespace = \rtrim(app()->get_namespace(), '\\'); |
| 156 | $model_base_namespace = $app_namespace . '\\Models\\'; |
| 157 | $model_name = \str_replace('Policy', '', $policy_name); |
| 158 | return $model_base_namespace . $model_name; |
| 159 | } |
| 160 | /** |
| 161 | * Check if the policy name is valid. |
| 162 | * |
| 163 | * @param string $policy_name The policy name. |
| 164 | * |
| 165 | * @return bool |
| 166 | * |
| 167 | * @since 1.0.0 |
| 168 | */ |
| 169 | protected function is_valid_policy_name(string $policy_name) |
| 170 | { |
| 171 | if (!str_ends_with($policy_name, 'Policy')) { |
| 172 | return \false; |
| 173 | } |
| 174 | $model_class = $this->associated_model($policy_name); |
| 175 | if (!\class_exists($model_class)) { |
| 176 | return \false; |
| 177 | } |
| 178 | return \true; |
| 179 | } |
| 180 | /** |
| 181 | * Cache the policies. |
| 182 | * |
| 183 | * @param ?string $path The path. |
| 184 | * |
| 185 | * @return $this |
| 186 | * |
| 187 | * @since 1.0.0 |
| 188 | */ |
| 189 | public function cache(?string $path = null) |
| 190 | { |
| 191 | $path = $path ?? config_path('policies.cache.php'); |
| 192 | if (!$this->is_cacheable($path)) { |
| 193 | return $this; |
| 194 | } |
| 195 | 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) . ';'); |
| 196 | return $this; |
| 197 | } |
| 198 | /** |
| 199 | * Check if the policies are cacheable. |
| 200 | * |
| 201 | * @param string $path The path. |
| 202 | * |
| 203 | * @return bool |
| 204 | * |
| 205 | * @since 1.0.0 |
| 206 | */ |
| 207 | public function is_cacheable(string $path) |
| 208 | { |
| 209 | return app()->is_dev_mode() || File::missing($path); |
| 210 | } |
| 211 | } |
| 212 |