1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace musa11971\autopolicy; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; |
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\App; |
|
|
|
|
8
|
|
|
use Illuminate\Support\Facades\Cache; |
|
|
|
|
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
class AutoPolicyProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
protected $policies = []; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
const CACHE_KEY = 'autopolicy_map'; |
17
|
|
|
|
18
|
|
|
/** @var int: cache for 24 hours */ |
19
|
|
|
const CACHE_TIME = 1440; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Bootstrap any application services. |
23
|
|
|
* |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function boot() { |
27
|
|
|
$this->policies = $this->getPolicyMap(); |
28
|
|
|
|
29
|
|
|
$this->registerPolicies(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns an array with the policy map. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
protected function getPolicyMap() { |
39
|
|
|
// Try to to get the cached map if there is one |
40
|
|
|
if(Cache::has(self::CACHE_KEY) && !App::isLocal()) { |
41
|
|
|
return Cache::get(self::CACHE_KEY); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Retrieve an array of policies |
45
|
|
|
$policies = $this->getPolicies(); |
46
|
|
|
|
47
|
|
|
// Initialize the policy map |
48
|
|
|
$map = []; |
49
|
|
|
|
50
|
|
|
foreach($policies as $policy) { |
51
|
|
|
$data = $this->getPolicyData($policy); |
52
|
|
|
|
53
|
|
|
// Check if the model is not being used as a duplicate |
54
|
|
|
if(isset($map[$data['model']])) |
55
|
|
|
throw new Exception('The model "' . $data['model'] . '" is being used for more than one policy."'); |
56
|
|
|
|
57
|
|
|
// Assign the policy its spot in the policy map |
58
|
|
|
$map[$data['model']] = $data['policy']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Cache the map and return it |
62
|
|
|
if(!App::isLocal()) |
63
|
|
|
Cache::put(self::CACHE_KEY, $map, self::CACHE_TIME); |
64
|
|
|
|
65
|
|
|
return $map; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieves the application's policies. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
protected function getPolicies() { |
74
|
|
|
$path = app_path('Policies'); |
|
|
|
|
75
|
|
|
|
76
|
|
|
// Return an empty array if there are no policies at all |
77
|
|
|
if(!file_exists($path)) |
78
|
|
|
return []; |
79
|
|
|
|
80
|
|
|
// Scan for policies |
81
|
|
|
$policies = scandir($path); |
82
|
|
|
|
83
|
|
|
// Remove hidden items |
84
|
|
|
foreach ($policies as $key => $policy) |
85
|
|
|
if(starts_with($policy, '.')) unset($policies[$key]); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $policies; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the data necessary to register a policy. |
92
|
|
|
* |
93
|
|
|
* @param $policyFile |
94
|
|
|
* @return mixed |
95
|
|
|
* @throws Exception |
96
|
|
|
*/ |
97
|
|
|
protected function getPolicyData($policyFile) { |
98
|
|
|
// Get the class name of the policy |
99
|
|
|
$policyClassName = chop($policyFile, '.php'); |
100
|
|
|
|
101
|
|
|
// Require the policy |
102
|
|
|
require_once(app_path('Policies/' . $policyFile)); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Create a reflection class of the policy to retrieve data |
105
|
|
|
$reflection = new ReflectionClass('\\App\\Policies\\' . $policyClassName); |
106
|
|
|
|
107
|
|
|
// Attempt to get the model |
108
|
|
|
$model = $reflection->getConstant('MODEL'); |
109
|
|
|
|
110
|
|
|
if(!$model) throw new Exception('The policy "' . $policyClassName . '" is not bound to any model.'); |
111
|
|
|
|
112
|
|
|
// Return the policy data |
113
|
|
|
return [ |
114
|
|
|
'model' => $model, |
115
|
|
|
'policy' => $reflection->getNamespaceName() . '\\' . $policyClassName |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths