-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
BaseRequest.php
120 lines (100 loc) · 3.47 KB
/
BaseRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace BBSLab\NovaFileManager\Http\Requests;
use BBSLab\NovaFileManager\Contracts\Services\FileManagerContract;
use BBSLab\NovaFileManager\Contracts\Support\InteractsWithFilesystem;
use BBSLab\NovaFileManager\FileManager;
use BBSLab\NovaFileManager\NovaFileManager;
use Illuminate\Validation\ValidationException;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Nova;
use Laravel\Nova\Tool;
/**
* @property-read ?string $disk
* @property-read ?string $attribute
* @property-read ?string $resource
* @property-read ?string $resourceId
* @property-read ?bool $fieldMode
*/
class BaseRequest extends NovaRequest
{
public function manager(): FileManagerContract
{
return once(function () {
/** @var NovaFileManager $element */
$element = $this->element();
/** @var \BBSLab\NovaFileManager\Services\FileManagerService $manager */
$manager = app(
abstract: FileManagerContract::class,
parameters: $element?->hasCustomFilesystem() ? ['disk' => $element?->resolveFilesystem($this)] : [],
);
if ($element?->hasUrlResolver()) {
$manager->resolveUrlUsing($element?->getUrlResolver());
}
return $manager;
});
}
public function element(): ?InteractsWithFilesystem
{
return $this->fieldMode ? $this->resolveField() : $this->resolveTool();
}
public function resolveField(): ?InteractsWithFilesystem
{
$resource = $this->resourceId ? $this->findResourceOrFail() : $this->newResource();
return $resource->availableFields($this)
->whereInstanceOf(FileManager::class)
->findFieldByAttribute($this->attribute, function () {
abort(404);
});
}
public function resolveTool(): ?InteractsWithFilesystem
{
return tap(once(function () {
return collect(Nova::registeredTools())->first(fn (Tool $tool) => $tool instanceof NovaFileManager);
}), function (?NovaFileManager $tool) {
abort_if(is_null($tool), 404);
});
}
public function resource()
{
return tap(once(function () {
return Nova::resourceForKey($this->input('resource'));
}), function ($resource) {
abort_if(is_null($resource), 404);
});
}
public function canCreateFolder(): bool
{
return $this->element()?->resolveCanCreateFolder($this) ?? true;
}
public function canRenameFolder(): bool
{
return $this->element()?->resolveCanRenameFolder($this) ?? true;
}
public function canDeleteFolder(): bool
{
return $this->element()?->resolveCanDeleteFolder($this) ?? true;
}
public function canUploadFile(): bool
{
return $this->element()?->resolveCanUploadFile($this) ?? true;
}
public function canRenameFile(): bool
{
return $this->element()?->resolveCanRenameFile($this) ?? true;
}
public function canDeleteFile(): bool
{
return $this->element()?->resolveCanDeleteFile($this) ?? true;
}
protected function failedAuthorization(): void
{
throw ValidationException::withMessages([
$this->authorizationAttribute() => __('This action is unauthorized.'),
]);
}
public function authorizationAttribute(): string
{
return strtolower(str(static::class)->classBasename()->ucsplit()->get(1, ''));
}
}