1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Form; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Tinyissue\Model\Project; |
|
|
|
|
16
|
|
|
use Tinyissue\Model\User; |
|
|
|
|
17
|
|
|
use Illuminate\Contracts\Auth\Guard; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* FormAbstract is an abstract class for Form classes. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class FormAbstract implements FormInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* An instance of Model. |
28
|
|
|
* |
29
|
|
|
* @var Model |
30
|
|
|
*/ |
31
|
|
|
protected $model; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* An instance of Current logged user. |
35
|
|
|
* |
36
|
|
|
* @var User |
37
|
|
|
*/ |
38
|
|
|
protected $user; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set an instance of current logged user. |
42
|
|
|
* |
43
|
|
|
* @param User $user |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
50 |
|
public function setLoggedUser(User $user = null) |
48
|
|
|
{ |
49
|
50 |
|
$this->user = $user; |
50
|
|
|
|
51
|
50 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns an instance of current logged user. |
56
|
|
|
* |
57
|
|
|
* @return User |
58
|
|
|
*/ |
59
|
15 |
|
public function getLoggedUser() |
60
|
|
|
{ |
61
|
15 |
|
if (null === $this->user) { |
62
|
2 |
|
$this->user = auth()->user(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
15 |
|
return $this->user; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set an instance of model currently being edited. |
70
|
|
|
* |
71
|
|
|
* @param Model $model |
72
|
|
|
* |
73
|
|
|
* @return void|FormInterface |
74
|
|
|
*/ |
75
|
11 |
|
public function editingModel(Model $model) |
76
|
|
|
{ |
77
|
11 |
|
$this->model = $model; |
78
|
|
|
|
79
|
11 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Setup the object from the route parameters. |
84
|
|
|
* |
85
|
|
|
* @param array $params |
86
|
|
|
* |
87
|
|
|
* @return FormInterface |
88
|
|
|
*/ |
89
|
|
|
public function setup(array $params) |
90
|
|
|
{ |
91
|
24 |
|
$model = array_first($params, function ($key, $value) { |
92
|
3 |
|
return $value instanceof Model; |
93
|
24 |
|
}); |
94
|
24 |
|
if ($model) { |
95
|
3 |
|
$this->editingModel($model); |
96
|
|
|
} |
97
|
|
|
|
98
|
24 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Whether or not the form is in editing of a model. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
30 |
|
public function isEditing() |
107
|
|
|
{ |
108
|
30 |
|
return $this->model instanceof Model; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return an instance of the model being edited. |
113
|
|
|
* |
114
|
|
|
* @return Model |
115
|
|
|
*/ |
116
|
48 |
|
public function getModel() |
117
|
|
|
{ |
118
|
48 |
|
return $this->model; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns form type. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
39 |
|
public function openType() |
127
|
|
|
{ |
128
|
39 |
|
return 'open'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns an array of form actions. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
14 |
|
public function actions() |
137
|
|
|
{ |
138
|
14 |
|
return []; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns an array of form fields. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function fields() |
147
|
|
|
{ |
148
|
|
|
return []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns an array form rules. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
29 |
|
public function rules() |
157
|
|
|
{ |
158
|
29 |
|
return []; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the form redirect url on error. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getRedirectUrl() |
167
|
|
|
{ |
168
|
|
|
return ''; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns project upload fields. |
173
|
|
|
* |
174
|
|
|
* @param string $name |
175
|
|
|
* @param Project $project |
176
|
|
|
* @param User $user |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
13 |
|
protected function projectUploadFields($name, Project $project, User $user) |
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
|
|
$name => [ |
184
|
13 |
|
'type' => 'FileUpload', |
185
|
13 |
|
'data_message_success' => trans('tinyissue.success_upload'), |
186
|
13 |
|
'data_message_failed' => trans('tinyissue.error_uploadfailed'), |
187
|
|
|
'multiple' => null, |
188
|
13 |
|
], |
189
|
13 |
|
$name . '_token' => [ |
190
|
13 |
|
'type' => 'hidden', |
191
|
13 |
|
'value' => md5($project->id . time() . $user->id . rand(1, 100)), |
192
|
|
|
], |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: