initial commit
This commit is contained in:
commit
3f64f4a49a
221 files changed
+26665
No files matched your search
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers\Web;
|
||||
|
||||
use App\Models\{{class}};
|
||||
use Rsgrinko\Proton\Access\Scope;
|
||||
use Rsgrinko\Proton\Access\Viewer;
|
||||
use Rsgrinko\Proton\Http\Controller;
|
||||
use Rsgrinko\Proton\Http\Request;
|
||||
use Rsgrinko\Proton\Http\Response;
|
||||
use Rsgrinko\Proton\Support\Audit;
|
||||
|
||||
/**
|
||||
* Раздел «{{title}}»: список с поиском и страницами, карточка, форма, удаление.
|
||||
*
|
||||
* Область видимости уходит в запрос ($scope->apply), поэтому чужая запись сюда
|
||||
* просто не приходит — «не найдено», а не «нет доступа».
|
||||
*/
|
||||
final class {{plural}}Controller extends Controller
|
||||
{
|
||||
public function index(Request $request, Scope $scope): Response
|
||||
{
|
||||
$search = $request->text('q');
|
||||
|
||||
$query = $scope->apply({{class}}::query())
|
||||
->when($search, static function ($query, string $needle): void {
|
||||
$query->whereLike('{{first}}', $needle);
|
||||
})
|
||||
->orderBy('id', 'desc');
|
||||
|
||||
$page = $query->paginate($this->page($request), $this->perPage());
|
||||
|
||||
return $this->view('{{views}}/index', [
|
||||
'active' => '{{route}}',
|
||||
'page' => $page,
|
||||
'search' => $search,
|
||||
], '{{title}}');
|
||||
}
|
||||
|
||||
public function create(): Response
|
||||
{
|
||||
return $this->view('{{views}}/form', [
|
||||
'active' => '{{route}}',
|
||||
'record' => new {{class}}(),
|
||||
], '{{title}}: новая запись');
|
||||
}
|
||||
|
||||
public function store(Request $request, Viewer $viewer): Response
|
||||
{
|
||||
$data = $this->validate($request, $this->rules(), $this->labels());
|
||||
|
||||
$record = new {{class}}($data);
|
||||
|
||||
$record->setAttribute('user_id', $viewer->id());
|
||||
$record->save();
|
||||
|
||||
Audit::created('{{route}}', $record->id(), '{{title}}: ' . (string) $record->raw('{{first}}'));
|
||||
|
||||
$this->flash('Сохранено');
|
||||
|
||||
return $this->redirect('{{route}}.show', ['id' => $record->id()]);
|
||||
}
|
||||
|
||||
public function show(int $id, Scope $scope): Response
|
||||
{
|
||||
$record = $this->find($id, $scope);
|
||||
|
||||
return $this->view('{{views}}/show', [
|
||||
'active' => '{{route}}',
|
||||
'record' => $record,
|
||||
'author' => $record->author,
|
||||
], (string) $record->raw('{{first}}'));
|
||||
}
|
||||
|
||||
public function edit(int $id, Scope $scope): Response
|
||||
{
|
||||
return $this->view('{{views}}/form', [
|
||||
'active' => '{{route}}',
|
||||
'record' => $this->find($id, $scope),
|
||||
], '{{title}}: правка');
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id, Scope $scope): Response
|
||||
{
|
||||
$record = $this->find($id, $scope);
|
||||
|
||||
$data = $this->validate($request, $this->rules(), $this->labels());
|
||||
|
||||
$before = [{{before}}];
|
||||
|
||||
$record->fill($data);
|
||||
$record->save();
|
||||
|
||||
Audit::updated('{{route}}', $record->id(), '{{title}}: ' . (string) $record->raw('{{first}}'), Audit::between($before, [
|
||||
{{after}}
|
||||
]));
|
||||
|
||||
$this->flash('Сохранено');
|
||||
|
||||
return $this->redirect('{{route}}.show', ['id' => $record->id()]);
|
||||
}
|
||||
|
||||
public function delete(int $id, Scope $scope): Response
|
||||
{
|
||||
$record = $this->find($id, $scope);
|
||||
|
||||
$record->delete();
|
||||
|
||||
Audit::deleted('{{route}}', $record->id(), '{{title}}: ' . (string) $record->raw('{{first}}'));
|
||||
|
||||
$this->flash('Удалено');
|
||||
|
||||
return $this->redirect('{{route}}.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Правила проверки — одни и те же на создание и на правку.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function rules(): array
|
||||
{
|
||||
return [
|
||||
{{rules}}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Подписи полей: с ними сообщения об ошибках читаются человеком.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function labels(): array
|
||||
{
|
||||
return [
|
||||
{{labels}}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Запись с оглядкой на область видимости.
|
||||
*/
|
||||
private function find(int $id, Scope $scope): {{class}}
|
||||
{
|
||||
/** @var {{class}}|null $record */
|
||||
$record = $scope->apply({{class}}::query())->where('id', $id)->first();
|
||||
|
||||
/** @var {{class}} $found */
|
||||
$found = $this->require($record, '{{route}}.index', 'Запись не найдена');
|
||||
|
||||
return $found;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Форма: {{title}}. Одна на создание и на правку.
|
||||
*
|
||||
* @var \App\Models\{{class}} $record
|
||||
*/
|
||||
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
$isNew = !$record->existsInDatabase();
|
||||
$action = $isNew
|
||||
? View::route('{{route}}.create')
|
||||
: View::route('{{route}}.edit', ['id' => $record->id()]);
|
||||
?>
|
||||
<h1><?= $isNew ? 'Новая запись' : 'Правка записи' ?></h1>
|
||||
|
||||
<div class="card">
|
||||
<form method="post" action="<?= View::e($action) ?>">
|
||||
<?= View::csrf() ?>
|
||||
|
||||
{{form}}
|
||||
|
||||
<div class="row">
|
||||
<button type="submit" class="primary">Сохранить</button>
|
||||
<a class="btn" href="<?= View::e(View::route('{{route}}.index')) ?>">Отмена</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Список: {{title}}.
|
||||
*
|
||||
* @var array{items: array<int, \App\Models\{{class}}>, total: int, page: int, pages: int, per_page: int} $page
|
||||
* @var string $search
|
||||
*/
|
||||
|
||||
use Rsgrinko\Proton\Support\Str;
|
||||
use Rsgrinko\Proton\View\View;
|
||||
?>
|
||||
<div class="row">
|
||||
<h1 style="margin: 0;">{{title}}</h1>
|
||||
<span class="spacer"></span>
|
||||
|
||||
<?php if (View::can('{{route}}.manage')) { ?>
|
||||
<a class="btn primary" href="<?= View::e(View::route('{{route}}.create')) ?>">Добавить</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<form method="get" action="<?= View::e(View::route('{{route}}.index')) ?>">
|
||||
<div class="filters">
|
||||
<label>
|
||||
<span>Поиск: {{firstLabel}}</span>
|
||||
<input type="search" name="q" value="<?= View::e($search) ?>">
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="filter-actions row">
|
||||
<button type="submit" class="primary">Искать</button>
|
||||
<?php if ($search !== '') { ?>
|
||||
<a class="btn" href="<?= View::e(View::route('{{route}}.index')) ?>">Сбросить</a>
|
||||
<?php } ?>
|
||||
<span class="spacer"></span>
|
||||
<span class="muted small">Всего: <?= (int) $page['total'] ?></span>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<?php if ($page['items'] === []) { ?>
|
||||
<p class="muted">Пока ничего нет.</p>
|
||||
<?php } else { ?>
|
||||
<div class="table-wrap">
|
||||
<table class="list">
|
||||
<tr class="head">
|
||||
<th>{{firstLabel}}</th>{{heads}}
|
||||
<th class="hide-sm">Изменено</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
||||
<?php foreach ($page['items'] as $record) { ?>
|
||||
<tr>
|
||||
<td>
|
||||
<a href="<?= View::e(View::route('{{route}}.show', ['id' => $record->id()])) ?>"><?= View::e((string) $record->raw('{{first}}')) ?></a>
|
||||
</td>{{cells}}
|
||||
<td class="hide-sm muted small"><?= View::e(View::ago((string) $record->raw('updated_at'))) ?></td>
|
||||
<td class="right">
|
||||
<?php if (View::can('{{route}}.manage')) { ?>
|
||||
<a class="btn small" href="<?= View::e(View::route('{{route}}.edit', ['id' => $record->id()])) ?>">Править</a>
|
||||
<?php } ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<?= View::partial('pagination', [
|
||||
'route' => '{{route}}.index',
|
||||
'page' => $page['page'],
|
||||
'pages' => $page['pages'],
|
||||
'params' => ['q' => $search],
|
||||
]) ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Migrations;
|
||||
|
||||
use Rsgrinko\Proton\Database\Migration;
|
||||
use Rsgrinko\Proton\Database\Schema\Blueprint;
|
||||
|
||||
/**
|
||||
* Таблица раздела «{{title}}».
|
||||
*/
|
||||
final class Create{{plural}} extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->create('{{table}}', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id');
|
||||
{{schema}}
|
||||
$table->timestamps();{{softSchema}}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->drop('{{table}}');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Rsgrinko\Proton\Database\Model\Model;
|
||||
use Rsgrinko\Proton\Database\Model\Relations\BelongsTo;
|
||||
use Rsgrinko\Proton\Models\User;
|
||||
|
||||
/**
|
||||
* {{title}}.
|
||||
*/
|
||||
final class {{class}} extends Model
|
||||
{
|
||||
protected static string $table = '{{table}}';
|
||||
|
||||
/** Массово заполняем только это: остальное ставит код, а не форма */
|
||||
protected array $fillable = [{{fillable}}];
|
||||
|
||||
protected array $casts = [
|
||||
{{casts}}
|
||||
];
|
||||
{{soft}}
|
||||
/**
|
||||
* Кто завёл запись — по этому полю работает область видимости.
|
||||
*/
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Карточка: {{title}}.
|
||||
*
|
||||
* @var \App\Models\{{class}} $record
|
||||
* @var \Rsgrinko\Proton\Models\User|null $author
|
||||
*/
|
||||
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
$props = [
|
||||
{{props}}
|
||||
];
|
||||
?>
|
||||
<div class="row">
|
||||
<h1 style="margin: 0;"><?= View::e((string) $record->raw('{{first}}')) ?></h1>
|
||||
<span class="spacer"></span>
|
||||
|
||||
<?php if (View::can('{{route}}.manage')) { ?>
|
||||
<a class="btn" href="<?= View::e(View::route('{{route}}.edit', ['id' => $record->id()])) ?>">Править</a>
|
||||
|
||||
<form method="post" action="<?= View::e(View::route('{{route}}.delete', ['id' => $record->id()])) ?>">
|
||||
<?= View::csrf() ?>
|
||||
<button type="submit" class="danger">Удалить</button>
|
||||
</form>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<dl class="props">
|
||||
<?php foreach ($props as $label => $value) { ?>
|
||||
<dt><?= View::e((string) $label) ?></dt>
|
||||
<dd><?= View::e((string) $value) === '' ? '—' : View::e((string) $value) ?></dd>
|
||||
<?php } ?>
|
||||
|
||||
<dt>Автор</dt>
|
||||
<dd><?= View::e($author === null ? '—' : (string) $author->login) ?></dd>
|
||||
|
||||
<dt>Создано</dt>
|
||||
<dd><?= View::e(View::date((string) $record->raw('created_at'))) ?></dd>
|
||||
|
||||
<dt>Изменено</dt>
|
||||
<dd><?= View::e(View::date((string) $record->raw('updated_at'))) ?></dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<p><a href="<?= View::e(View::route('{{route}}.index')) ?>">← Ко всем записям</a></p>
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Раздел «{{title}}»: список, создание, правка, удаление и чужая запись.
|
||||
*
|
||||
* Проверяется настоящим ядром — с маршрутами, правами и токеном формы.
|
||||
* Помощники httpRequest() и httpAdmin() живут в tests/HttpTest.php.
|
||||
*/
|
||||
|
||||
use App\Models\{{class}};
|
||||
|
||||
test('{{route}}: список открывается', function (): void {
|
||||
$admin = httpAdmin();
|
||||
|
||||
assertStatus(200, httpRequest('GET', '/{{route}}', $admin));
|
||||
});
|
||||
|
||||
test('{{route}}: запись заводится, правится и удаляется', function (): void {
|
||||
$admin = httpAdmin();
|
||||
|
||||
$response = httpRequest('POST', '/{{route}}/new', $admin, ['{{first}}' => {{testValue}}]);
|
||||
|
||||
assertStatus(302, $response, 'после сохранения — переход на карточку');
|
||||
|
||||
/** @var {{class}}|null $record */
|
||||
$record = {{class}}::query()->where('user_id', $admin->id())->orderBy('id', 'desc')->first();
|
||||
|
||||
assertNotNull($record, 'запись должна появиться в базе');
|
||||
|
||||
assertStatus(200, httpRequest('GET', '/{{route}}/' . $record->id(), $admin));
|
||||
assertStatus(200, httpRequest('GET', '/{{route}}/' . $record->id() . '/edit', $admin));
|
||||
|
||||
assertStatus(302, httpRequest('POST', '/{{route}}/' . $record->id() . '/edit', $admin, [
|
||||
'{{first}}' => {{testValue}},
|
||||
]));
|
||||
|
||||
assertStatus(302, httpRequest('POST', '/{{route}}/' . $record->id() . '/delete', $admin));
|
||||
|
||||
// Запись убрана из списков; если мягкое удаление выключено — её нет вовсе
|
||||
assertNull({{class}}::query()->where('id', $record->id())->first());
|
||||
|
||||
{{class}}::query()->withTrashed()->where('id', $record->id())->forceDelete();
|
||||
});
|
||||
|
||||
test('{{route}}: форма без токена ничего не меняет', function (): void {
|
||||
$admin = httpAdmin();
|
||||
|
||||
$before = {{class}}::query()->count();
|
||||
|
||||
$response = httpRequest('POST', '/{{route}}/new', $admin, [
|
||||
'{{first}}' => {{testValue}},
|
||||
Rsgrinko\Proton\Auth\Csrf::FIELD => 'подделка',
|
||||
]);
|
||||
|
||||
assertStatus(403, $response);
|
||||
assertSame($before, {{class}}::query()->count(), 'без токена запись не заводится');
|
||||
});
|
||||
Reference in new issue
Block a user