Skip to content

Commit

Permalink
Remove base controller if it doesn't exist (#49902)
Browse files Browse the repository at this point in the history
* remove base controller if it doesn't exist

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
taylorotwell and StyleCIBot authored Jan 29, 2024
1 parent b63a4e6 commit bc753cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/Illuminate/Routing/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ protected function getDefaultNamespace($rootNamespace)
*/
protected function buildClass($name)
{
$rootNamespace = $this->rootNamespace();
$controllerNamespace = $this->getNamespace($name);

$replace = [];
Expand All @@ -126,11 +127,20 @@ protected function buildClass($name)
$replace['abort(404);'] = '//';
}

$replace["use {$controllerNamespace}\Controller;\n"] = '';
$baseControllerExists = class_exists($rootNamespace.'Http\Controllers\Controller');

return str_replace(
if ($baseControllerExists) {
$replace["use {$controllerNamespace}\Controller;\n"] = '';
} else {
$replace[' extends Controller'] = '';
$replace["use {$rootNamespace}Http\Controllers\Controller;\n"] = '';
}

$class = str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);

return $class;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Integration/Generators/ControllerMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testItCanGenerateControllerFile()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class FooController extends Controller',
'class FooController',
], 'app/Http/Controllers/FooController.php');

$this->assertFileNotContains([
Expand All @@ -37,7 +37,7 @@ public function testItCanGenerateControllerFileWithInvokableTypeOption()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class FooController extends Controller',
'class FooController',
'public function __invoke(Request $request)',
], 'app/Http/Controllers/FooController.php');
}
Expand All @@ -50,7 +50,7 @@ public function testItCanGenerateControllerFileWithInvokableOption()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class FooController extends Controller',
'class FooController',
'public function __invoke(Request $request)',
], 'app/Http/Controllers/FooController.php');
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testItCanGenerateControllerFileWithApiOption()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class FooController extends Controller',
'class FooController',
'public function index()',
'public function store(Request $request)',
'public function update(Request $request, string $id)',
Expand All @@ -124,7 +124,7 @@ public function testItCanGenerateControllerFileWithInvokableIgnoresApiOption()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class FooController extends Controller',
'class FooController',
'public function __invoke(Request $request)',
], 'app/Http/Controllers/FooController.php');

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Generators/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testItCanGenerateModelFileWithControllerOption()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class FooController extends Controller',
'class FooController',
], 'app/Http/Controllers/FooController.php');

$this->assertFileNotContains([
Expand Down Expand Up @@ -156,7 +156,7 @@ public function testItCanGenerateNestedModelFileWithControllerOption()
$this->assertFileContains([
'namespace App\Http\Controllers;',
'use Illuminate\Http\Request;',
'class BarController extends Controller',
'class BarController',
], 'app/Http/Controllers/BarController.php');

$this->assertFilenameNotExists('database/factories/FooFactory.php');
Expand Down

0 comments on commit bc753cf

Please sign in to comment.