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

Overriding console command constructor is bad practice #42454

Closed
wants to merge 11 commits into from
47 changes: 47 additions & 0 deletions src/Illuminate/Console/ProtectedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ProtectedCommand extends Command
{
/**
* Create a new console command instance.
* Not used for protected command.
*
* @return void
*/
final public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
* Not used for protected command.
*
* @return int
*/
final public function handle()
{
return 0;
}

/**
* Execute the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (method_exists($this, '__invoke')) {
return (int) $this->laravel->call([$this, '__invoke']);
}

return 1;
}
}
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'make:command')]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also revert this.

class ConsoleMakeCommand extends GeneratorCommand
{
use CreatesMatchingTest;
Expand Down Expand Up @@ -68,6 +66,10 @@ protected function getStub()
{
$relativePath = '/stubs/console.stub';

if ($this->confirm('Do you want to create a protected command?')) {
$relativePath = '/stubs/console-protected.stub';
}

return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/')))
? $customPath
: __DIR__.$relativePath;
Expand Down
43 changes: 43 additions & 0 deletions src/Illuminate/Foundation/Console/stubs/console-protected.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace {{ namespace }};

use Illuminate\Console\Command;
use Illuminate\Console\ProtectedCommand;

/**
* Use "__invoke()" method
* Inject dependencies "__invoke(Application $application)"
*
* This console command is protected.
* To make the current command unprotected change extend class to "Command"
*
* Example: class {{ class }} extends Command {}
* @see Command
*/
class {{ class }} extends ProtectedCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = '{{ command }}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*
* @return int
*/
public function __invoke()
{
return 0;
}
}