Skip to content

Commit e5a3ce8

Browse files
authored
[5.x] Implies only the new migrations behaviour on L11 (#305)
* Implies only the new migrations behaviour on L11 * Improves method `usingLaravel11OrNewer`
1 parent a269426 commit e5a3ce8

File tree

5 files changed

+226
-13
lines changed

5 files changed

+226
-13
lines changed

src/NewCommand.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ protected function defaultBranch()
243243
protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name, bool $migrate)
244244
{
245245
// MariaDB configuration only exists as of Laravel 11...
246-
if ($database === 'mariadb' && ! $this->hasMariaDBConfig($directory)) {
246+
if ($database === 'mariadb' && ! $this->usingLaravel11OrNewer($directory)) {
247247
$database = 'mysql';
248248
}
249249

@@ -308,22 +308,18 @@ protected function configureDefaultDatabaseConnection(string $directory, string
308308
}
309309

310310
/**
311-
* Determine if the application has a MariaDB configuration entry.
311+
* Determine if the application is using Laravel 11 or newer.
312312
*
313313
* @param string $directory
314314
* @return bool
315315
*/
316-
protected function hasMariaDBConfig(string $directory): bool
316+
public function usingLaravel11OrNewer(string $directory): bool
317317
{
318-
// Laravel 11+ has moved the configuration files into the framework...
319-
if (! file_exists($directory.'/config/database.php')) {
320-
return true;
321-
}
318+
$version = json_decode(file_get_contents($directory.'/composer.json'), true)['require']['laravel/framework'];
319+
$version = str_replace('^', '', $version);
320+
$version = explode('.', $version)[0];
322321

323-
return str_contains(
324-
file_get_contents($directory.'/config/database.php'),
325-
"'mariadb' =>"
326-
);
322+
return $version >= 11;
327323
}
328324

329325
/**
@@ -450,7 +446,7 @@ protected function installJetstream(string $directory, InputInterface $input, Ou
450446
protected function promptForDatabaseOptions(string $directory, InputInterface $input)
451447
{
452448
// Laravel 11.x appliations use SQLite as default...
453-
$defaultDatabase = $this->hasMariaDBConfig($directory) ? 'sqlite' : 'mysql';
449+
$defaultDatabase = $this->usingLaravel11OrNewer($directory) ? 'sqlite' : 'mysql';
454450

455451
if ($input->isInteractive()) {
456452
$database = select(
@@ -465,7 +461,7 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
465461
default: $defaultDatabase
466462
);
467463

468-
if ($database !== $defaultDatabase) {
464+
if ($this->usingLaravel11OrNewer($directory) && $database !== $defaultDatabase) {
469465
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
470466
}
471467
}

tests/NewCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,17 @@ public function test_it_can_scaffold_a_new_laravel_app()
3333
$this->assertDirectoryExists($scaffoldDirectory.'/vendor');
3434
$this->assertFileExists($scaffoldDirectory.'/.env');
3535
}
36+
37+
public function test_on_at_least_laravel_11()
38+
{
39+
$command = new NewCommand;
40+
41+
$onLaravel10 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel10');
42+
$onLaravel11 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel11');
43+
$onLaravel12 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel12');
44+
45+
$this->assertFalse($onLaravel10);
46+
$this->assertTrue($onLaravel11);
47+
$this->assertTrue($onLaravel12);
48+
}
3649
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"name": "laravel/laravel",
3+
"type": "project",
4+
"description": "The skeleton application for the Laravel framework.",
5+
"keywords": ["laravel", "framework"],
6+
"license": "MIT",
7+
"require": {
8+
"php": "^8.1",
9+
"guzzlehttp/guzzle": "^7.2",
10+
"laravel/framework": "^10.10",
11+
"laravel/sanctum": "^3.3",
12+
"laravel/tinker": "^2.8"
13+
},
14+
"require-dev": {
15+
"fakerphp/faker": "^1.9.1",
16+
"laravel/pint": "^1.0",
17+
"laravel/sail": "^1.18",
18+
"mockery/mockery": "^1.4.4",
19+
"nunomaduro/collision": "^7.0",
20+
"phpunit/phpunit": "^10.1",
21+
"spatie/laravel-ignition": "^2.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"App\\": "app/",
26+
"Database\\Factories\\": "database/factories/",
27+
"Database\\Seeders\\": "database/seeders/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"Tests\\": "tests/"
33+
}
34+
},
35+
"scripts": {
36+
"post-autoload-dump": [
37+
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
38+
"@php artisan package:discover --ansi"
39+
],
40+
"post-update-cmd": [
41+
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
42+
],
43+
"post-root-package-install": [
44+
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
45+
],
46+
"post-create-project-cmd": [
47+
"@php artisan key:generate --ansi"
48+
]
49+
},
50+
"extra": {
51+
"laravel": {
52+
"dont-discover": []
53+
}
54+
},
55+
"config": {
56+
"optimize-autoloader": true,
57+
"preferred-install": "dist",
58+
"sort-packages": true,
59+
"allow-plugins": {
60+
"pestphp/pest-plugin": true,
61+
"php-http/discovery": true
62+
}
63+
},
64+
"minimum-stability": "stable",
65+
"prefer-stable": true
66+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "laravel/laravel",
3+
"type": "project",
4+
"description": "The skeleton application for the Laravel framework.",
5+
"keywords": ["laravel", "framework"],
6+
"license": "MIT",
7+
"require": {
8+
"php": "^8.2",
9+
"laravel/framework": "^11.0",
10+
"laravel/tinker": "^2.9"
11+
},
12+
"require-dev": {
13+
"fakerphp/faker": "^1.23",
14+
"laravel/pint": "^1.13",
15+
"laravel/sail": "^1.26",
16+
"mockery/mockery": "^1.6",
17+
"nunomaduro/collision": "^8.0",
18+
"phpunit/phpunit": "^10.5",
19+
"spatie/laravel-ignition": "^2.4"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"App\\": "app/",
24+
"Database\\Factories\\": "database/factories/",
25+
"Database\\Seeders\\": "database/seeders/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Tests\\": "tests/"
31+
}
32+
},
33+
"scripts": {
34+
"post-autoload-dump": [
35+
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
36+
"@php artisan package:discover --ansi"
37+
],
38+
"post-update-cmd": [
39+
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
40+
],
41+
"post-root-package-install": [
42+
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
43+
],
44+
"post-create-project-cmd": [
45+
"@php artisan key:generate --ansi",
46+
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
47+
"@php artisan migrate --ansi"
48+
]
49+
},
50+
"extra": {
51+
"branch-alias": {
52+
"dev-master": "11.x-dev"
53+
},
54+
"laravel": {
55+
"dont-discover": []
56+
}
57+
},
58+
"config": {
59+
"optimize-autoloader": true,
60+
"preferred-install": "dist",
61+
"sort-packages": true,
62+
"allow-plugins": {
63+
"pestphp/pest-plugin": true,
64+
"php-http/discovery": true
65+
}
66+
},
67+
"minimum-stability": "dev",
68+
"prefer-stable": true
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "laravel/laravel",
3+
"type": "project",
4+
"description": "The skeleton application for the Laravel framework.",
5+
"keywords": ["laravel", "framework"],
6+
"license": "MIT",
7+
"require": {
8+
"php": "^8.2",
9+
"laravel/framework": "^12.0.1",
10+
"laravel/tinker": "^2.9"
11+
},
12+
"require-dev": {
13+
"fakerphp/faker": "^1.23",
14+
"laravel/pint": "^1.13",
15+
"laravel/sail": "^1.26",
16+
"mockery/mockery": "^1.6",
17+
"nunomaduro/collision": "^8.0",
18+
"phpunit/phpunit": "^10.5",
19+
"spatie/laravel-ignition": "^2.4"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"App\\": "app/",
24+
"Database\\Factories\\": "database/factories/",
25+
"Database\\Seeders\\": "database/seeders/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Tests\\": "tests/"
31+
}
32+
},
33+
"scripts": {
34+
"post-autoload-dump": [
35+
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
36+
"@php artisan package:discover --ansi"
37+
],
38+
"post-update-cmd": [
39+
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
40+
],
41+
"post-root-package-install": [
42+
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
43+
],
44+
"post-create-project-cmd": [
45+
"@php artisan key:generate --ansi",
46+
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
47+
"@php artisan migrate --ansi"
48+
]
49+
},
50+
"extra": {
51+
"branch-alias": {
52+
"dev-master": "11.x-dev"
53+
},
54+
"laravel": {
55+
"dont-discover": []
56+
}
57+
},
58+
"config": {
59+
"optimize-autoloader": true,
60+
"preferred-install": "dist",
61+
"sort-packages": true,
62+
"allow-plugins": {
63+
"pestphp/pest-plugin": true,
64+
"php-http/discovery": true
65+
}
66+
},
67+
"minimum-stability": "dev",
68+
"prefer-stable": true
69+
}

0 commit comments

Comments
 (0)