Skip to content

fix/feat: Refactor commands - #7

Merged
techmahedy merged 4 commits into
doppar:2.xfrom
rrr63:refactor-make-commands
Aug 12, 2025
Merged

fix/feat: Refactor commands#7
techmahedy merged 4 commits into
doppar:2.xfrom
rrr63:refactor-make-commands

Conversation

@rrr63

@rrr63 rrr63 commented Aug 12, 2025

Copy link
Copy Markdown
Member

Hello,
This is a PR that refactors commands to eliminate code duplication, standardize command execution patterns, and improve maintainability. Also added some commands.

Refactor

Why? There were several commands using directly the Symfony Command Class, and another using the Doppar Command Class; I migrated all Symfony to the Doppar one. To have the same way of doing commands.

I refactor also all the commands, to avoid duplicated code. To reduce the number of code lines, and to improve maintainability.
Commands are now smaller, and users could now use some of the command generic function like :

->displaySuccess()
->displayError()
->displayWarning()

This functions include a newLine().
here a concret example of ClearConfigCommand
Before

protected function handle(): int
    {
        $startTime = microtime(true);

        $this->newLine();

        app('config')->clearCache();

        $executionTime = microtime(true) - $startTime;

        $this->newLine();
        $this->line('<bg=green;options=bold> SUCCESS </> Configuration cache has been gracefully cleared.');
        $this->newLine();
        $this->line(sprintf(
            "<fg=yellow>⏱ Time:</> <fg=white>%.4fs</> <fg=#6C7280>(%d μs)</>",
            $executionTime,
            (int) ($executionTime * 1000000)
        ));
        $this->newLine();

        return 0;
    }

after

protected function handle(): int
    {
        return $this->withTiming(function() {
            app('config')->clearCache();
            return 0;
        }, 'Configuration cache has been gracefully cleared.');
    }

I added a more complexe function : executeWithTiming() to allow more complexe logique, example with the new MakePasswordCommand

protected function handle(): int
    {
        return $this->executeWithTiming(function() {
            $password = $this->argument('password');

            if (empty($password)) {
                $this->displayError('Password is required');
                return 1;
            }
            if (!is_string($password)) {
                $this->displayError('Password must be a string');
                return 1;
            }
            
            $hashedPassword = self::hash($password);

            $this->displaySuccess('Password hashed successfully');
            $this->line("<fg=yellow>🔑 Key:</> <fg=white>$hashedPassword</>");
            
            return 0;
        });
    }

Commands added

I added 3 commands :

-make:password that allow user to generate a password in command line. Very usefull for user that want to create a user in database quickly. It is a thing that is very usefull in symfony : https://symfony.com/doc/current/security/passwords.html#reset-password

-server:start {port?} {--background} {--bg} start a server on a port, can add : --bg or --background to start it in a background.
If you don't put a port, it will find an available one, starting with 8000.

-server:stop {port?} stop a local server on a port, or stop all php servers if port isnt specify

I am open for reviews, and ready to edit this PR.
Thanks for your work

@techmahedy

Copy link
Copy Markdown
Member

Hey, you did a good job, small things to change,

Just update this as per the documentation

  1. Phaseolies\Console\Commands\MakePolicyCommand.
  2. https://doppar.com/versions/3.x/doppar-guard.html#writing-authorizers

Just make:policy to make:authorizer and make sure it is working with the passing Model

And update the MakePasswordCommand
use this
Use Phaseolies\Support\Facades\Hash::make($password);

@rrr63

rrr63 commented Aug 12, 2025

Copy link
Copy Markdown
Member Author

Ok thanks, i changed it. Maybe it is a good idea to rename MakePolicyCommand to MakeAuthorizerCommand so ?

@techmahedy
techmahedy merged commit baed555 into doppar:2.x Aug 12, 2025
5 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.

2 participants