PHP code example of usman / make-services

1. Go to this page and download the library: Download usman/make-services 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/ */

    

usman / make-services example snippets


// config/make-services.php

return [
    'directories' => [
        'services' => 'app/Services',
        'repositories' => 'app/Repositories',
        'interfaces' => 'app/Interfaces',
        'actions' => 'app/Actions',
        'dto' => 'app/DataTransferObjects',
        'traits' => 'app/Traits',
        'enums' => 'app/Enums',
    ],
    
    'auto_generate' => [
        'service_interface' => false,
        'repository_interface' => false,
        'service_test' => false,
    ],
    
    'templates' => [
        '



namespace App\Services;

use App\Models\User;
use Illuminate\Database\Eloquent\Collection;

class UserService
{
    public function getAll(): Collection
    {
        return User::all();
    }

    public function findById(int $id): ?User
    {
        return User::find($id);
    }

    public function create(array $data): User
    {
        return User::create($data);
    }

    // ... more CRUD methods
}



namespace App\DataTransferObjects;

class UserData
{
    public function __construct(
        public string $name,
        public string $email,
        public int $age,
    ) {
    }

    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'email' => $this->email,
            'age' => $this->age,
        ];
    }

    public static function fromArray(array $data): static
    {
        return new static(
            $data['name'] ?? null,
            $data['email'] ?? null,
            $data['age'] ?? null,
        );
    }
}



namespace App\Enums;

enum UserRole: string
{
    case ADMIN = 'admin';
    case EDITOR = 'editor';
    case VIEWER = 'viewer';

    public static function values(): array
    {
        return array_column(self::cases(), 'value');
    }

    public static function options(): array
    {
        // Perfect for select inputs
    }
}

// In App\Providers\AppServiceProvider::register()
$this->app->bind(
    \App\Interfaces\UserServiceInterface::class,
    \App\Services\UserService::class
);
bash
php artisan make-services:publish-stubs

app/
├── Services/
│   ├── UserService.php
│   └── Billing/
│       └── InvoiceService.php
├── Repositories/
│   ├── UserRepository.php
│   └── ProductRepository.php
├── Interfaces/
│   ├── UserServiceInterface.php
│   └── UserRepositoryInterface.php
├── Actions/
│   ├── ProcessPayment.php
│   └── SendNotification.php
├── DataTransferObjects/
│   ├── UserData.php
│   └── ProductData.php
├── Traits/
│   └── Loggable.php
└── Enums/
    ├── Status.php
    └── UserRole.php
bash
php artisan make-services:publish-stubs
bash
php artisan make:services UserService --model=User --crud