Skip to content

Commit

Permalink
[10.x] Move class from file logic in Console Kernel to dedicated meth…
Browse files Browse the repository at this point in the history
…od (#47665)

* Move class from file logic to dedicated method

* formatting
"

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
calebdw and taylorotwell authored Jul 8, 2023
1 parent 8eb6ae8 commit 33aa4a9
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Str;
use ReflectionClass;
use SplFileInfo;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
Expand Down Expand Up @@ -339,12 +340,8 @@ protected function load($paths)

$namespace = $this->app->getNamespace();

foreach ((new Finder)->in($paths)->files() as $command) {
$command = $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($command->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
);
foreach ((new Finder)->in($paths)->files() as $file) {
$command = $this->commandClassFromFile($file, $namespace);

if (is_subclass_of($command, Command::class) &&
! (new ReflectionClass($command))->isAbstract()) {
Expand All @@ -355,6 +352,22 @@ protected function load($paths)
}
}

/**
* Extract the command class name from the given file path.
*
* @param \SplFileInfo $file
* @param string $namespace
* @return string
*/
protected function commandClassFromFile(SplFileInfo $file, string $namespace): string
{
return $namespace.str_replace(
['/', '.php'],
['\\', ''],
Str::after($file->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR)
);
}

/**
* Register the given command with the console application.
*
Expand Down

0 comments on commit 33aa4a9

Please sign in to comment.