Skeleton is ready
This commit is contained in:
18
app/Feature/Example/Command/DoSomething.php
Normal file
18
app/Feature/Example/Command/DoSomething.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Command;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class DoSomething extends Command
|
||||
{
|
||||
protected $signature = 'example:do-something';
|
||||
protected $description = 'Do something example';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
18
app/Feature/Example/Dto/SearchUsers.php
Normal file
18
app/Feature/Example/Dto/SearchUsers.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Dto;
|
||||
|
||||
use Diffhead\PHP\Dto\Dto;
|
||||
use Diffhead\PHP\Dto\Property;
|
||||
|
||||
/**
|
||||
* @property \Diffhead\PHP\Dto\Property<int> $page
|
||||
* @property \Diffhead\PHP\Dto\Property<int> $perPage
|
||||
*/
|
||||
class SearchUsers extends Dto
|
||||
{
|
||||
protected Property $page;
|
||||
protected Property $perPage;
|
||||
}
|
||||
23
app/Feature/Example/Http/Controller/User.php
Normal file
23
app/Feature/Example/Http/Controller/User.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Http\Controller;
|
||||
|
||||
use App\Feature\Example\Dto\SearchUsers;
|
||||
use App\Feature\Example\Http\Request\Index;
|
||||
use App\Feature\Example\Http\Resource\Users;
|
||||
use App\Feature\Example\Service\UsersRepository;
|
||||
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
private UsersRepository $users,
|
||||
) {}
|
||||
|
||||
public function index(Index $request): Users
|
||||
{
|
||||
$dto = SearchUsers::fromArray($request->validated());
|
||||
return Users::make($this->users->search($dto));
|
||||
}
|
||||
}
|
||||
22
app/Feature/Example/Http/Request/Index.php
Normal file
22
app/Feature/Example/Http/Request/Index.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Http\Request;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* @property int|null $page
|
||||
* @property int|null $per_page
|
||||
*/
|
||||
class Index extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'page' => ['sometimes', 'integer', 'min:1'],
|
||||
'per_page' => ['sometimes', 'integer', 'min:1', 'max:200'],
|
||||
];
|
||||
}
|
||||
}
|
||||
24
app/Feature/Example/Http/Resource/User.php
Normal file
24
app/Feature/Example/Http/Resource/User.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Http\Resource;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @property \App\Models\User\User $resource
|
||||
*/
|
||||
class User extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->resource->getKey(),
|
||||
'login' => $this->resource->login,
|
||||
'created_at' => $this->resource->created_at,
|
||||
'updated_at' => $this->resource->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
21
app/Feature/Example/Http/Resource/Users.php
Normal file
21
app/Feature/Example/Http/Resource/Users.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Http\Resource;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
/**
|
||||
* @property \Illuminate\Pagination\LengthAwarePaginator<\App\Models\User\User> $collection
|
||||
*/
|
||||
class Users extends ResourceCollection
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'data' => User::collection($this->collection)
|
||||
];
|
||||
}
|
||||
}
|
||||
25
app/Feature/Example/Listener/DoSomethingOnEvent.php
Normal file
25
app/Feature/Example/Listener/DoSomethingOnEvent.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Listener;
|
||||
|
||||
use App\Shared\Event\User\Created;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class DoSomethingOnEvent implements ShouldQueue
|
||||
{
|
||||
public function handle(Created $event): void
|
||||
{
|
||||
}
|
||||
|
||||
public function viaConnection(): string
|
||||
{
|
||||
return 'sync';
|
||||
}
|
||||
|
||||
public function viaQueue(): string
|
||||
{
|
||||
return 'default';
|
||||
}
|
||||
}
|
||||
60
app/Feature/Example/Provider.php
Normal file
60
app/Feature/Example/Provider.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example;
|
||||
|
||||
use App\Feature\Example\Command\DoSomething;
|
||||
use App\Feature\Example\Http\Controller\User;
|
||||
use App\Feature\Example\Listener\DoSomethingOnEvent;
|
||||
use App\Kernel\Feature\HasCommandsList;
|
||||
use App\Kernel\Feature\HasEventListeners;
|
||||
use App\Shared\Event\User\Created as UserCreated;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class Provider extends ServiceProvider
|
||||
{
|
||||
use HasCommandsList, HasEventListeners;
|
||||
|
||||
/**
|
||||
* @var array<string,class-string>
|
||||
*/
|
||||
public array $bindings = [
|
||||
/** Bindings section */
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<int,class-string>
|
||||
*/
|
||||
private array $commandsList = [
|
||||
DoSomething::class,
|
||||
];
|
||||
|
||||
private array $eventListeners = [
|
||||
UserCreated::class => [
|
||||
DoSomethingOnEvent::class,
|
||||
],
|
||||
];
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
/** Register something here */
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerListeners();
|
||||
$this->registerRoutes();
|
||||
}
|
||||
|
||||
private function registerRoutes(): void
|
||||
{
|
||||
Route::middleware('auth:sanctum')
|
||||
->controller(User::class)
|
||||
->group(function (): void {
|
||||
Route::get('/users', 'index')->name('users.index');
|
||||
});
|
||||
}
|
||||
}
|
||||
26
app/Feature/Example/Service/UsersRepository.php
Normal file
26
app/Feature/Example/Service/UsersRepository.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Feature\Example\Service;
|
||||
|
||||
use App\Feature\Example\Dto\SearchUsers;
|
||||
use App\Models\User\User;
|
||||
use App\Shared\Service\User\SearchById;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
class UsersRepository extends SearchById
|
||||
{
|
||||
/**
|
||||
* @param \App\Feature\Example\Dto\SearchUsers $dto
|
||||
*
|
||||
* @return \Illuminate\Pagination\LengthAwarePaginator<\App\Models\User\User>
|
||||
*/
|
||||
public function search(SearchUsers $dto): LengthAwarePaginator
|
||||
{
|
||||
return User::query()->paginate(
|
||||
page: $dto->page->exists() ? $dto->page->value() : 1,
|
||||
perPage: $dto->perPage->exists() ? $dto->perPage->value() : 20,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user