Skip to content

TharinduWijayarathna/FacadeGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Domain Generator

Laravel Domain Generator is a package that provides a convenient way to generate domain services and facades in your Laravel application.


Installation

You can install the package via Composer:

composer require tharindu/facade_generator

Usage

To generate a domain service and facade, use the make:domain Artisan command:

php artisan make:domain User

This command will create the following files in the domain directory:

  • ServiceNameFacade.php
  • ServiceNameService.php

You can then use these files to implement your domain logic.


Autoloading the Domain Directory

To ensure Laravel autoloads your custom domain directory, you need to update the composer.json file of your Laravel application. Follow these steps:

  1. Open your Laravel application's composer.json file.

  2. Locate the autoload section. It should look something like this:

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
  3. Add your domain directory to the PSR-4 autoloading section. If your domain directory is located at the root of your Laravel application, you can add it like this:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Domain\\": "domain/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },

    Make sure to adjust the path "Domain\\" and "domain/" according to the actual structure of your application.

  4. Run composer dump-autoload to regenerate the Composer autoloader files:

    composer dump-autoload

Once you've made these changes, Laravel will autoload classes from your domain directory just like it does with the app directory, making your domain logic easily accessible throughout your application.


Functionalities

The package provides the following functionalities:

  1. Generation of Domain Service and Facade: Easily create domain services and facades using the make:domain command.
  2. CRUD Functionality: The generated service stubs include basic CRUD operations for managing domain entities.
    • Create: Create new domain entities.
    • Read: Retrieve specific domain entities.
    • Update: Update existing domain entities.
    • Delete: Delete domain entities.
    • List: Retrieve a list of domain entities.

Examples

Generating a Domain

To generate a domain service and facade named User, run:

php artisan make:domain User

This will create UserFacade.php and UserService.php in the domain directory.

Using the Generated Files

Once the files are generated, you can implement your domain logic inside the service methods. For example:

<?php

namespace Domain\Services\UserService;

use App\Models\User;

class UserService
{
    protected $user;

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

    public function get(int $user_id)
    {
        return $this->user->find($user_id);
    }

    public function create(array $data)
    {
       return $this->user->create($data);
    }

    public function read($id)
    {
        // Implement read functionality
    }

     protected function edit(User $user, array $data)
    {
        return array_merge($user->toArray(), $data);
    }

    public function update($user_id , array $data)
    {
        $user = $this->user->find($user_id);
        return $user->update($this->edit($user, $data));
    }

    public function delete($id)
    {
       $user = $this->user->find($id);
       return $user->delete();
    }

    public function list()
    {
       return $this->user->all();
    }
}

Contributing

Contributions are welcome! Please feel free to submit a pull request.


License

The Laravel Domain Generator is open-sourced software licensed under the MIT license.