Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

dansysanalyst
Copy link
Member

@dansysanalyst dansysanalyst commented Apr 8, 2024

⚡ 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

  • Bug fix
  • Enhancement
  • New feature
  • Breaking change

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): #1499

Documentation

This PR requires Documentation update?

  • Yes
  • No
  • I have already submitted a Documentation pull request.

@dansysanalyst dansysanalyst marked this pull request as draft April 8, 2024 22:07
@luanfreitasdev
Copy link
Collaborator

Thank you @dansysanalyst! Does this need documentation?

@dansysanalyst dansysanalyst marked this pull request as ready for review April 11, 2024 10:13
@dansysanalyst dansysanalyst changed the title Create components following a custom Livewire namespace Create PowerGrid Components in different directories following Livewire Class Namespace Apr 11, 2024
@dansysanalyst
Copy link
Member Author

dansysanalyst commented Apr 11, 2024

Thank you @dansysanalyst! Does this need documentation?

Doc update at: Power-Components/powergrid-doc#97

@luanfreitasdev luanfreitasdev merged commit 1581c97 into Power-Components:5.x Apr 13, 2024
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants