PHP code example of jfheinrich-eu / laravel-make-commands
1. Go to this page and download the library: Download jfheinrich-eu/laravel-make-commands library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jfheinrich-eu / laravel-make-commands example snippets
declare(strict_types=1);
namespace JfheinrichEu\LaravelMakeCommands\Dto;
use Illuminate\Support\Collection;
/**
* @property int $id
* @property Collection<int,array<string,mixed>> $attributes
*/
final class RepositoryDto extends DataTransferObject
{
/**
* @param null|int $id
* @param null|Collection<int,array<string,mixed>> $attributes
*/
public function __construct(
protected ?int $id = null,
protected ?Collection $attributes = null
) {
}
}
declare(strict_types=1);
namespace App\Repositories
use Illuminate\Database\Eloquent\Collection;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use JfheinrichEu\LaravelMakeCommands\Dto\RepositoryDto;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class TestRepository
{
public function __construct(protected User $user) {}
public function all(): Collection
{
return $this->user::all();
}
/**
* @inheritdoc
*/
public function create(RepositoryDto $dto): Model|User
{
return $this->user::create($dto->attributes->toArray());
}
/**
* @inheritdoc
*/
public function update(RepositoryDto $dto): bool
{
return $this->user->whereId($dto->id)
->update($dto->attributes->toArray());
}
/**
* @inheritdoc
*/
public function delete(int $id): bool
{
return $this->user->whereId($id)->delete($id);
}
/**
* @inheritdoc
*/
public function find(int $id): Model| User
{
$model = $this->user->find($id)
if (null == $model)
{
throw new ModelNotFoundException("Resource not found");
}
return $model;
}
}
declare(strict_types=1);
namespace App\Contracts;
interface UserPostInterface
{
public function get( string|array|null $title, ?int $userId = 0): array|string;
}
declare(strict_types=1);
namespace App\Services;
use App\Contracts\UserPostInterface;
use App\Repositories\UserRepository;
class UserPostService implements UserPostInterface
{
public function __construct(protected UserRepository $userRepository)
{
}
public function get(array|string|null $title,?int $userId = 0): array|string
{
// Implementation
}
}
declare(strict_types=1);
namespace App\Dto;
use JfheinrichEu\LaravelMakeCommands\Dto\DataTransferObject;
final class MyDto extends DataTransferObject
{
public function __construct(
//
) {}
}
class StoreController
{
public function __construct(
private readonly HydratorContract $hydrator,
) {}
public function __invoke(StoreRequest $request)
{
$model = Model::query()->create(
attributes: $this->hydrator->fill(
class: ModelObject::class,
parameters: $request->validated(),
)->toArray(),
);
}
}
class StoreController
{
public function __invoke(StoreRequest $request)
{
$model = Model::query()->create(
attributes: Hydrator::fill(
class: ModelObject::class,
parameters: $request->validated(),
)->toArray(),
);
}
}
declare(strict_types=1);
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use JfheinrichEu\LaravelMakeCommands\Support\Database\Seeder\DatabaseJsonSeeder;
class DatabaseSeeder extends DatabaseJsonSeeder
{
use WithoutModelEvents;
protected array $nonJsonSeeders = [
// Database\Seeders\MyNonJsonSeeder::class,
];
/**
* Seed the application's database.
*
* @return void
*/
public function run(): void
{
parent::setCommand($this->command)->setContainer($this->container);
parent::run();
// run the non JSON seeders
$this->call($this->nonJsonSeeders);
}
}
declare(strict_types=1);
return [
/*
* List of all commands to be registered.
*/
'commands' => [
JfheinrichEu\LaravelMakeCommands\Console\Commands\DtoMakeCommand::class,
JfheinrichEu\LaravelMakeCommands\Console\Commands\InterfaceMakeCommand::class,
JfheinrichEu\LaravelMakeCommands\Console\Commands\RepositoryMakeCommand::class,
JfheinrichEu\LaravelMakeCommands\Console\Commands\ServiceMakeCommand::class,
],
'seeders' => [
// Path to the seeder classes, must match the namespace Database\Seeders.
'path-seeder' => database_path('seeders'),
// The directory where the data files goes in.
'path-datafiles' => database_path('seeders/data'),
// The models which will be used by the JsonSeeder.
'models' => [
App\Models\User::class,
App\Models\Right::class,
],
],
];