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 module:make-class command #1802

Merged
merged 24 commits into from
May 12, 2024

Conversation

solomon-ochepa
Copy link
Contributor

@solomon-ochepa solomon-ochepa commented Apr 1, 2024

Issue: #1800 - Need to add two command "make:enum" and "make:class" for the modules

Description:
  Create a new class

Usage:
  module:make:class [options] [--] <name> <module>

Arguments:
  name                  The name of the class
  module                The targeted module

Options:
  -t, --type[=TYPE]     The type of class, e.g. class, service, repository, contract, etc. [default: "service"]
  -p, --plain           Create the class without suffix (type)
  -i, --invokable       Generate a single method, invokable class
  -f, --force           Create the class even if the class already exists
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Examples:

Create a service class:

php artisan module:make:class Example Example

Create a standard class

php artisan module:make:class Example Example -t class

Create a repository class

php artisan module:make:class Example Example -t repository

Create a repository class without a suffix

php artisan module:make:class Example Example -p -t repository

@dcblogdev
Copy link
Collaborator

This looks good the signature should be module:make-class to match the other commands.

This needs to be compatible with the already released config file please remove config changes that are dependant on your other PR.

Also, tests are required for the new command.

@Siam401
Copy link

Siam401 commented Apr 2, 2024

yes this command should look like module:make-class and module:make-enum

php artisan module:make-enum ExampleEnum Example

php artisan module:make-class Service/ExampleService Example

class command should create file in the module app directory like Example/{app}/Service/ExampleService

@dcblogdev
Copy link
Collaborator

I'm not sure about having a classes command, I think it is better to have specific make commands ie:

php artisan module:make-enum
php artisan module:make-service
php artisan module:make-repository

Thoughts?

@solomon-ochepa
Copy link
Contributor Author

I'm not sure about having a classes command, I think it is better to have specific make commands ie:

php artisan module:make-enum php artisan module:make-service php artisan module:make-repository

Thoughts?

Yes, you are correct.

This PR will enable the generation of various types of classes, including enums, services, repositories, and many other custom types.

I intend to establish this class as a foundation for easily generating dedicated class commands such as module:make-enum, module:make-service, and module:make-repository while also allowing for the creation of custom classes.

After this PR is merged, I'll proceed with the remaining commands immediately.

@solomon-ochepa solomon-ochepa changed the title Create make:class command Module make-class command Apr 26, 2024
@solomon-ochepa solomon-ochepa changed the title Module make-class command Create module:make-class command Apr 27, 2024
src/Commands/Make/ClassCommand.php Outdated Show resolved Hide resolved
src/Commands/Make/ClassCommand.php Outdated Show resolved Hide resolved
src/Commands/Make/ClassCommand.php Outdated Show resolved Hide resolved
@dcblogdev
Copy link
Collaborator

dcblogdev commented Apr 29, 2024

I think it may be better to generate classes into the folder root or the 'app_folder' folder that way you can set the path as wanted for example:

php module:make-class Concerns/HasDomain
php module:make-class Services/UserService
php module:make-class Actions/UserStoreAction
php module:make-class Helpers/DateFormat

These would be stored in app/src depends on the value of app_folder

Also the make-class command needs tests in order to be accepted.

@solomon-ochepa
Copy link
Contributor Author

I think it may be better to generate classes into the folder root or the 'app_folder' folder that way you can set the path as wanted for example:

php module:make-class Concerns/HasDomain
php module:make-class Services/UserService
php module:make-class Actions/UserStoreAction
php module:make-class Helpers/DateFormat

These would be stored in app/src depending on the value of app_folder

This is already the default behaviour. Regarding the paths.app, it will be solved with the #1802 (comment) review.

Also the make-class command needs tests to be accepted.

I can't run the test with my current Windows system. I'll appreciate it if you or anyone else can create the test.

@dcblogdev
Copy link
Collaborator

To handle cases there the config doesn't have the class path you can modify your code from:

$config = GenerateConfigReader::read('class');
$path .= $this->type_path($config->getPath()) . '/' . $this->getFileName() . '.php';

To

$filePath = GenerateConfigReader::read('class')->getPath() ?? config('modules.paths.app_folder') . 'Classes';
$path .= $this->type_path($filePath) . '/' . $this->getFileName() . '.php';

Can you change your method names to be camelCased rather then snake_case, also there's a few methods that don't have a return type.

Let me know when you feel this is ready and I can look at added the tests

@solomon-ochepa
Copy link
Contributor Author

To handle cases where the config doesn't have the class path you can modify your code from:

$config = GenerateConfigReader::read('class');
$path .= $this->type_path($config->getPath()) . '/' . $this->getFileName() . '.php';

To

$filePath = GenerateConfigReader::read('class')->getPath() ?? config('modules.paths.app_folder') . 'Classes';
$path .= $this->type_path($filePath) . '/' . $this->getFileName() . '.php';

I've fixed this already.

    public function getDestinationFilePath(): string
    {
        $app_path = GenerateConfigReader::read('class')->getPath() ?? $this->app_path('Classes');

        return $this->path($this->type_path($app_path) . '/' . $this->getFileName() . '.php');
    }

I've added a method to get the paths.app_folder by default or fallback to app/ if empty. To use the module base path, we should consider passing '/' as the value of the paths.app_folder

    /**
     * Get the app path basename.
     */
    public function app_path(?string $path = null): string
    {
        $config_path = config('modules.paths.app_folder');
        $app_path = strlen($config_path) ? trim($config_path, '/') : 'app';
        $app_path .= strlen($path) ? '/' . $path : '';

        return $this->clean_path($app_path);
    }

Can you change your method names to camelCased rather than snake_case, ...

The snake_case is easier for me to identify and read. I didn't change any existing identifiers to my preference case, as every developer has their preferences. However, the PHP convention supports the snake_case - https://www.php.net/manual/en/userlandnaming.rules.php.

..., also there are a few methods that don't have a return type.

I've fixed them too.

Let me know when you feel this is ready and I can look at adding the tests

Okay, sir.

I'm currently experiencing a minor difficulty with the namespace. I'll update you, once I rounded it up.

Thank you.

@solomon-ochepa
Copy link
Contributor Author

Hi, @dcblogdev.
I'm done.

solomon-ochepa and others added 13 commits May 12, 2024 09:00
Description:
  Create a new class

Usage:
  module:make:class [options] [--] <name> <module>

Arguments:
  name                  The name of the class
  module                The targeted module

Options:
  -t, --type[=TYPE]     The type of class, e.g. class, service, repository, contract, etc. [default: "service"]
  -p, --plain           Create the class without suffix (type)
  -i, --invokable       Generate a single method, invokable class
  -f, --force           Create the class even if the class already exists
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Examples:
```
php artisan module:make:class Example Example
```

```
php artisan module:make:class Example Example -t class
```

```
php artisan module:make:class Example Example -t repository
```

```
php artisan module:make:class Example Example -p -t repository
```
@dcblogdev dcblogdev merged commit 7c6bda0 into nWidart:master May 12, 2024
2 of 3 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.

3 participants