1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use A17\Twill\Services\Blocks\BlockCollection; |
4
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Facades\Event; |
|
|
|
|
7
|
|
|
use \Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
if (!function_exists('dumpUsableSqlQuery')) { |
10
|
|
|
function dumpUsableSqlQuery($query) |
11
|
|
|
{ |
12
|
|
|
dd(vsprintf(str_replace('?', '%s', $query->toSql()), array_map(function ($binding) { |
13
|
|
|
return is_numeric($binding) ? $binding : "'{$binding}'"; |
14
|
|
|
}, $query->getBindings()))); |
15
|
|
|
} |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
if (!function_exists('classUsesDeep')) { |
19
|
|
|
/** |
20
|
|
|
* @param mixed $class |
21
|
|
|
* @param bool $autoload |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
function classUsesDeep($class, $autoload = true) |
25
|
|
|
{ |
26
|
36 |
|
$traits = []; |
27
|
|
|
|
28
|
|
|
// Get traits of all parent classes |
29
|
|
|
do { |
30
|
36 |
|
$traits = array_merge(class_uses($class, $autoload), $traits); |
31
|
36 |
|
} while ($class = get_parent_class($class)); |
32
|
|
|
|
33
|
|
|
// Get traits of all parent traits |
34
|
36 |
|
$traitsToSearch = $traits; |
35
|
36 |
|
while (!empty($traitsToSearch)) { |
36
|
36 |
|
$newTraits = class_uses(array_pop($traitsToSearch), $autoload); |
37
|
36 |
|
$traits = array_merge($newTraits, $traits); |
38
|
36 |
|
$traitsToSearch = array_merge($newTraits, $traitsToSearch); |
39
|
|
|
} |
40
|
|
|
|
41
|
36 |
|
foreach ($traits as $trait => $same) { |
42
|
36 |
|
$traits = array_merge(class_uses($trait, $autoload), $traits); |
43
|
|
|
} |
44
|
|
|
|
45
|
36 |
|
return array_unique($traits); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!function_exists('classHasTrait')) { |
50
|
|
|
/** |
51
|
|
|
* @param mixed $class |
52
|
|
|
* @param string $trait |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
function classHasTrait($class, $trait) |
56
|
|
|
{ |
57
|
36 |
|
$traits = classUsesDeep($class); |
58
|
|
|
|
59
|
36 |
|
if (in_array($trait, array_keys($traits))) { |
60
|
33 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
return false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!function_exists('getFormFieldsValue')) { |
68
|
|
|
/** |
69
|
|
|
* @param array $formFields |
70
|
|
|
* @param string $name |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
function getFormFieldsValue($formFields, $name) |
74
|
|
|
{ |
75
|
2 |
|
return Arr::get($formFields, str_replace(']', '', str_replace('[', '.', $name)), ''); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!function_exists('fireCmsEvent')) { |
80
|
|
|
/** |
81
|
|
|
* @param string $eventName |
82
|
|
|
* @param array $input |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
function fireCmsEvent($eventName, $input = []) |
86
|
|
|
{ |
87
|
25 |
|
$method = method_exists(\Illuminate\Events\Dispatcher::class, 'dispatch') ? 'dispatch' : 'fire'; |
88
|
25 |
|
Event::$method($eventName, [$eventName, $input]); |
89
|
25 |
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!function_exists('twill_path')) { |
93
|
|
|
/** |
94
|
|
|
* @param string $path |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
function twill_path($path = '') |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
// Is it a full application path? |
100
|
69 |
|
if (Str::startsWith($path, base_path())) { |
101
|
1 |
|
return $path; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Split to separate root namespace |
105
|
69 |
|
preg_match('/(\w*)\W?(.*)/', config('twill.namespace'), $namespaceParts); |
106
|
|
|
|
107
|
69 |
|
$twillBase = app_path( |
108
|
|
|
fix_directory_separator( |
109
|
69 |
|
$namespaceParts[1] == 'App' ? $namespaceParts[2] : $namespaceParts[0] |
110
|
|
|
) |
111
|
69 |
|
) . '/'; |
112
|
|
|
|
113
|
|
|
// Remove base path from path |
114
|
69 |
|
if (Str::startsWith($path, $twillBase)) { |
115
|
|
|
$path = Str::after($path, $twillBase); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Namespace App is unchanged in config? |
119
|
69 |
|
if ($namespaceParts[0] === 'App') { |
120
|
69 |
|
return app_path($path); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// If it it still starts with App, use the left part, otherwise use the whole namespace |
124
|
|
|
// This can be a problem for those using a completely different app path for the application |
125
|
|
|
$left = ($namespaceParts[1] === 'App' ? $namespaceParts[2] : $namespaceParts[0]); |
126
|
|
|
|
127
|
|
|
// Join, fix slashes for the current operating system, and return path |
128
|
|
|
return app_path(fix_directory_separator( |
129
|
|
|
$left . (filled($path) ? '\\' . $path : '') |
130
|
|
|
)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!function_exists('make_twill_directory')) { |
135
|
|
|
/** |
136
|
|
|
* @param string $path |
137
|
|
|
* @param bool $recursive |
138
|
|
|
* @param \Illuminate\Filesystem\Filesystem|null $fs |
139
|
|
|
*/ |
140
|
|
|
function make_twill_directory($path, $recursive = true, $fs = null) |
141
|
|
|
{ |
142
|
1 |
|
$fs = filled($fs) |
143
|
|
|
? $fs |
144
|
1 |
|
: app(Filesystem::class); |
145
|
|
|
|
146
|
1 |
|
$path = twill_path($path); |
147
|
|
|
|
148
|
1 |
|
if (!$fs->isDirectory($path)) { |
|
|
|
|
149
|
1 |
|
$fs->makeDirectory($path, 0755, $recursive); |
150
|
|
|
} |
151
|
1 |
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!function_exists('twill_put_stub')) { |
155
|
|
|
/** |
156
|
|
|
* @param string $path |
157
|
|
|
* @param bool $recursive |
158
|
|
|
* @param \Illuminate\Filesystem\Filesystem|null $fs |
159
|
|
|
*/ |
160
|
|
|
function twill_put_stub($path, $stub, $fs = null) |
161
|
|
|
{ |
162
|
1 |
|
$fs = filled($fs) |
163
|
|
|
? $fs |
164
|
1 |
|
: app(Filesystem::class); |
165
|
|
|
|
166
|
1 |
|
$stub = str_replace( |
167
|
1 |
|
'namespace App\\', |
168
|
1 |
|
sprintf('namespace %s\\', config('twill.namespace')), |
169
|
1 |
|
$stub |
170
|
|
|
); |
171
|
|
|
|
172
|
1 |
|
if (!$fs->exists($path)) { |
173
|
1 |
|
$fs->put($path, $stub); |
174
|
|
|
} |
175
|
1 |
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (!function_exists('fix_directory_separator')) { |
179
|
|
|
/** |
180
|
|
|
* @param string $path |
181
|
|
|
* @param bool $recursive |
182
|
|
|
* @param int $mode |
183
|
|
|
*/ |
184
|
|
|
function fix_directory_separator($path) |
185
|
|
|
{ |
186
|
69 |
|
return str_replace( |
187
|
69 |
|
'\\', |
188
|
69 |
|
DIRECTORY_SEPARATOR, |
189
|
69 |
|
$path |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (!function_exists('generate_list_of_allowed_blocks')) { |
195
|
|
|
/** |
196
|
|
|
* @param array $blocks |
197
|
|
|
* @param array $groups |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
function generate_list_of_available_blocks($blocks, $groups) |
201
|
|
|
{ |
202
|
4 |
|
$blockList = app(BlockCollection::class)->getBlockList(); |
203
|
|
|
|
204
|
|
|
$appBlocksList = $blockList->filter(function ($block) { |
205
|
4 |
|
return $block['source'] !== A17\Twill\Services\Blocks\Block::SOURCE_TWILL; |
206
|
4 |
|
}); |
207
|
|
|
|
208
|
4 |
|
return $blockList->filter( |
209
|
|
|
function ($block) use ($blocks, $groups, $appBlocksList) { |
210
|
4 |
|
if ($block['group'] === A17\Twill\Services\Blocks\Block::SOURCE_TWILL) { |
211
|
4 |
|
if (!collect( |
212
|
4 |
|
config('twill.block_editor.use_twill_blocks') |
213
|
4 |
|
)->contains($block['name'])) { |
214
|
4 |
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (count($appBlocksList) > 0 && $appBlocksList->contains( |
218
|
|
|
function ($appBlock) use ($block) { |
219
|
|
|
return $appBlock['name'] === $block['name']; |
220
|
|
|
}) |
221
|
|
|
) { |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return (filled($blocks) ? collect($blocks)->contains($block['name']) : true) |
227
|
|
|
&& (filled($groups) ? collect($groups)->contains($block['group']) : true); |
228
|
4 |
|
} |
229
|
4 |
|
)->toArray(); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
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: