PHP code example of williamjss / layers

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

    

williamjss / layers example snippets




return [

    'namespace' => [
        'repositories' => 'Repositories',
        'services' => 'Services',
    ],

    'path' => [
        'models' => app_path('Models'),
        'repositories' => app_path('Repositories'),
        'services' => app_path('Services'),
    ]
];




namespace App\Repositories\User;

use App\Models\User;

interface UserRepositoryInterface
{
    public function __construct(User $user);

    /**
     * Stores a new instance of User in the database
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @return User
     */
    public function store($data);

    /**
     * Returns all instances of User from the database
     * @param array|string $columns
     * @param array<array> $filters
     * @return \Illuminate\Database\Eloquent\Collection<int, static>
     */
    public function getList($columns=['*'], $filters=null);

    /**
     * Returns an instance of User from the given id
     * @param int|string $id
     * @return User
     */
    public function get($id);

    /**
     * Updates the data of an instance of User
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @param int|string $id
     * @return User
     */
    public function update($data, $id);

    /**
     * Removes an instance of User from the database
     * @param int|string $id
     * @return int
     */
    public function destroy($id);
}



namespace App\Repositories;

use App\Models\User;

class UserRepositoryEloquent implements UserRepositoryInterface
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Stores a new instance of User in the database
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @return User
     */
    public function store($data)
    {
        return $this->user->create($data);
    }

    /**
     * Returns all instances of User from the database
     * @param array|string $columns
     * @param array<array> $filters
     * @return \Illuminate\Database\Eloquent\Collection<int, static>
     */
    public function getList($columns=['*'], $filters=null)
    {
        return $this->user->where($filters)->get($columns);
    }

    /**
     * Returns an instance of User from the given id
     * @param int|string $id
     * @return User
     */
    public function get($id)
    {
        return $this->user->find($id);
    }

    /**
     * Updates the data of an instance of User
     * @param \Illuminate\Support\Collection|array|int|string $data
     * @param int|string $id
     * @return User
     */
    public function update($data, $id)
    {
        $user = $this->user->find($id);
        $user->update($data);
        return $user;
    }

    /**
     * Removes an instance of User from the database
     * @param int|string $id
     * @return int
     */
    public function destroy($id)
    {
        return $this->user->find($id)->delete();
    }
}




namespace App\Services;

use App\Repositories\UserRepositoryInterface;

class UserService
{
    private $repoUser;

    public function __construct(
        UserRepositoryInterface $repoUser,
    )
    {
        $this->repoUser = $repoUser;
    }

    // Add your functions here...
}




namespace App\Services;

use App\Repositories\UserRepositoryInterface;
use App\Repositories\User\AddressRepositoryInterface;

class PersonService
{
    private $repoAddress;
    private $repoUser;

    public function __construct(
        AddressRepositoryInterface $repoAddress,
        UserRepositoryInterface $repoUser,
    )
    {
        $this->repoAddress = $repoAddress;
        $this->repoUser = $repoUser;
    }

    // Add your functions here...
}

json
"php": "^8.0.2"
"symfony/finder": "^6.3"
"illuminate/support": "^9.0 || ^10.20"
"illuminate/console": "^9.0 || ^10.20"
bash
php artisan vendor:publish --tag=layers
bash
php artisan layers --repository User.Address
bash
php artisan layers --service --wr=Address --wr=User Person