1 | <?php |
||
8 | trait HasAccess |
||
9 | { |
||
10 | /** |
||
11 | * The abilities of access. |
||
12 | * |
||
13 | * @var \Illuminate\Support\Collection |
||
14 | */ |
||
15 | protected $abilities; |
||
16 | |||
17 | /** |
||
18 | * Access observer class. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $observer; |
||
23 | |||
24 | /** |
||
25 | * User exposed observable abilities. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $observables = []; |
||
30 | |||
31 | /** |
||
32 | * Initialize access. |
||
33 | */ |
||
34 | public function bootHasAccess() |
||
40 | |||
41 | /** |
||
42 | * Determine if the entity have access to display. |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | public function isDisplay() |
||
47 | { |
||
48 | return method_exists($this, 'callDisplay') && $this->can('display'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Check if the entity have access to create. |
||
53 | * |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function isCreate() |
||
60 | |||
61 | /** |
||
62 | * Check if the entity have access to edit. |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function isEdit() |
||
70 | |||
71 | /** |
||
72 | * Check if the entity have access to delete. |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function isDelete() |
||
80 | |||
81 | /** |
||
82 | * Check if the entity have access to destroy. |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function isDestroy() |
||
90 | |||
91 | /** |
||
92 | * Check if the entity have access to restore. |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function isRestore() |
||
100 | |||
101 | /** |
||
102 | * Whether the model can be restored |
||
103 | * |
||
104 | * @return mixed |
||
105 | */ |
||
106 | protected function isRestorableModel() |
||
110 | |||
111 | /** |
||
112 | * Register an observer with the Component. |
||
113 | * |
||
114 | * @param $class |
||
115 | */ |
||
116 | public function observe($class) |
||
130 | |||
131 | /** |
||
132 | * Get the observable ability names. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getObservableAbilities() |
||
147 | |||
148 | /** |
||
149 | * register ability to access. |
||
150 | * |
||
151 | * @param string $ability |
||
152 | * @param string|\Closure $callback |
||
153 | */ |
||
154 | public function registerAbility($ability, $callback) |
||
158 | |||
159 | /** |
||
160 | * @param string|\Closure $callback |
||
161 | * |
||
162 | * @return \Closure |
||
163 | */ |
||
164 | protected function makeAbilityCallback($callback) |
||
177 | |||
178 | /** |
||
179 | * Determine if the entity has a given ability. |
||
180 | * |
||
181 | * @param string $ability |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | final public function can($ability) |
||
194 | |||
195 | /** |
||
196 | * Get all ability. |
||
197 | * |
||
198 | * @return Collection |
||
199 | */ |
||
200 | public function getAccesses() |
||
206 | } |
||
207 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.