This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Brickables.php
170 lines (151 loc) · 5.51 KB
/
Brickables.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Okipa\LaravelBrickables;
use Closure;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Http\Request;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Okipa\LaravelBrickables\Abstracts\Brickable;
use Okipa\LaravelBrickables\Contracts\HasBrickables;
use Okipa\LaravelBrickables\Controllers\DispatchController;
use Okipa\LaravelBrickables\Middleware\CRUDBrickable;
use Okipa\LaravelBrickables\Models\Brick;
class Brickables implements Htmlable
{
/** @property string $html */
protected $html;
/**
* Display all the model-related content bricks html at once.
*
* @param \Okipa\LaravelBrickables\Contracts\HasBrickables $model
* @param string|null $brickableClass
*
* @return $this
*/
public function displayBricks(HasBrickables $model, ?string $brickableClass = null): self
{
$this->html = view('laravel-brickables::bricks', compact('model', 'brickableClass'));
return $this;
}
/**
* Display the model-related content bricks admin panel html.
*
* @param \Okipa\LaravelBrickables\Contracts\HasBrickables $model
*
* @return $this
*/
public function displayAdminPanel(HasBrickables $model): self
{
$this->html = view('laravel-brickables::admin.panel.layout', compact('model'));
return $this;
}
/** @inheritDoc */
public function toHtml()
{
return (string) $this->html;
}
/**
* Register the brickables routes.
*
* @param \Closure|null $additionalRoutes
*/
public function routes(Closure $additionalRoutes = null): void
{
Route::middleware([
CRUDBrickable::class,
SubstituteBindings::class,
])->group(function () use ($additionalRoutes) {
Route::get('brick/create', [DispatchController::class, 'create'])->name('brick.create');
Route::post('brick/store', [DispatchController::class, 'store'])->name('brick.store');
Route::get('brick/edit/{brick}', [DispatchController::class, 'edit'])->name('brick.edit');
Route::put('brick/update/{brick}', [DispatchController::class, 'update'])->name('brick.update');
Route::delete('brick/destroy/{brick}', [DispatchController::class, 'destroy'])->name('brick.destroy');
Route::post('brick/move/up/{brick}', [DispatchController::class, 'moveUp'])->name('brick.move.up');
Route::post('brick/move/down/{brick}', [DispatchController::class, 'moveDown'])->name('brick.move.down');
if ($additionalRoutes) {
$additionalRoutes();
}
});
}
/**
* @param \Illuminate\Http\Request|null $request
*
* @return \Okipa\LaravelBrickables\Contracts\HasBrickables|null
*/
public function getModelFromRequest(Request $request = null): ?HasBrickables
{
$request = $request ?: request();
if ($request->has('model_type') && $request->has('model_id')) {
$model = app($request->model_type);
return $model->find($request->model_id);
}
if ($request->brick) {
return $this->castBrick($request->brick)->model;
}
return null;
}
/**
* Cast given brick to its brickable-related brick model.
*
* @param \Okipa\LaravelBrickables\Models\Brick $brick
*
* @return \Okipa\LaravelBrickables\Models\Brick
*/
public function castBrick(Brick $brick): Brick
{
return $this->castBricks(collect()->push($brick))->first();
}
/**
* Cast given bricks to their brickable-related brick model.
*
* @param \Illuminate\Support\Collection $bricks
*
* @return \Illuminate\Support\Collection
*/
public function castBricks(Collection $bricks): Collection
{
$casted = new Collection;
foreach ($bricks->pluck('brickable_type')->unique() as $brickableClass) {
/** @var \Okipa\LaravelBrickables\Abstracts\Brickable $brickable */
$brickable = app($brickableClass);
/** @var \Okipa\LaravelBrickables\Models\Brick $model */
$model = $brickable->getBrickModel();
$brickableBricksDataArray = $bricks->where('brickable_type', $brickableClass)->map(function ($brick) {
return $brick->getAttributes();
})->toArray();
$castedBricks = $model->hydrate($brickableBricksDataArray);
$casted->push($castedBricks);
}
return $casted->flatten();
}
/**
* Get all registered brickables that can be added to the given model.
*
* @param \Okipa\LaravelBrickables\Contracts\HasBrickables $model
*
* @return \Illuminate\Support\Collection
*/
public function getAdditionableTo(HasBrickables $model): Collection
{
return $this->getAll()->filter(function ($brickable) use ($model) {
$brickableClass = get_class($brickable);
return $model->canHandle($brickableClass) && $model->canAddBricksFrom($brickableClass);
});
}
/**
* Get all registered brickables.
*
* @return \Illuminate\Support\Collection
*/
public function getAll(): Collection
{
$brickables = new Collection;
foreach (config('brickables.registered') as $brickableClass) {
/** @var Brickable $brickable */
$brickable = app($brickableClass);
$brickables->push($brickable);
}
return $brickables;
}
}