Power-Components / livewire-powergrid

⚡ PowerGrid generates modern, powerful and easy-to-customize data tables using Laravel Livewire.
https://livewire-powergrid.com
MIT License
1.48k stars 217 forks source link

Create PowerGrid Components in different directories following Livewire Class Namespace #1501

Closed dansysanalyst closed 5 months ago

dansysanalyst commented 5 months ago

⚡ PowerGrid - Pull Request

Welcome and thank you for your interest in contributing to our project!. You must use this template to submit a Pull Request or it will not be accepted.


Motivation

Description

This pull request improves the compatibility with Livewire Class Namespace configuration, allowing to create PowerGrid tables outside the app/Models directory.

To illustrate a use case, let's see an example where the application is organized in a Domain-Driven Design. Here is the directory structure:

|_app
|_bootstrap
|_config
|_database
|_Domain
   |____Client
   |_________Models
   |_________Tables
   |____User

By default, PowerGrid auto discovers Models living in the directory app/Models/. This PR, presents the new configuration key livewire-powergrid.auto_discover_models_paths, providing a simple way to register new directories to be scanned for Eloquent Models.

After publishing the configuration file by running php artisan vendor:publish --tag=livewire-powergrid-config, users can register directories as demonstrated below:

//config/livewire-powergrid.php

    /*
    |--------------------------------------------------------------------------
    | Auto-Discover Models
    |--------------------------------------------------------------------------
    |
    | PowerGrid will search for Models in the directories listed below.
    | These Models be listed as options when you run the
    | "artisan powergrid:create" command.
    |
    */

    'auto_discover_models_paths' => [
        app_path('Models'),
        base_path('Domain'),
    ],

Now, when running php artisan powergrid:create, the user will be presented with a list of Models found in the specified directories:

 Select a Model or enter its Fully qualified name
 › Domain\Client\Models\Client
   Domain\User\Models\User

To maintain consistency when saving files, PowerGrid will follow the Livewire Config Key livewire.class_namespace.

To adjust the configuration, run: php artisan livewire:publish --config.

In our example, Livewire Namespace can be adjusted as follows:

//config/livewire.php

    /*
    |---------------------------------------------------------------------------
    | Class Namespace
    |---------------------------------------------------------------------------
    |
    | This value sets the root class namespace for Livewire component classes in
    | your application. This value will change where component auto-discovery
    | finds components. It's also referenced by the file creation commands.
    |
    */

    'class_namespace' => 'Domain',

When running php artisan powergrid:create, the user can enter the component name following the desired path. For example:

Enter a name for your new PowerGrid Component:
Client\Tables\ClientList

This will result in:

 ⚡ ClientList was successfully created at [Domain/Client/Tables/ClientList.php].

 💡 include the ClientList component using the tag: <livewire:client.tables.client-list/>

 👍 Please consider ⭐ starring ⭐ our repository. Visit: https://github.com/Power-Components/livewire-powergrid

As a reminder, the Domain directory must be added to autoload in composer.json:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Domain\\": "Domain/"
        }
    },

Related Issue(s): https://github.com/Power-Components/livewire-powergrid/issues/1499

Documentation

This PR requires Documentation update?

luanfreitasdev commented 5 months ago

Thank you @dansysanalyst! Does this need documentation?

dansysanalyst commented 5 months ago

Thank you @dansysanalyst! Does this need documentation?

Doc update at: https://github.com/Power-Components/powergrid-doc/pull/97