diff --git a/src/NewCommand.php b/src/NewCommand.php index 94278795..83700006 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -243,7 +243,7 @@ protected function defaultBranch() protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name, bool $migrate) { // MariaDB configuration only exists as of Laravel 11... - if ($database === 'mariadb' && ! $this->hasMariaDBConfig($directory)) { + if ($database === 'mariadb' && ! $this->usingLaravel11OrNewer($directory)) { $database = 'mysql'; } @@ -308,22 +308,18 @@ protected function configureDefaultDatabaseConnection(string $directory, string } /** - * Determine if the application has a MariaDB configuration entry. + * Determine if the application is using Laravel 11 or newer. * * @param string $directory * @return bool */ - protected function hasMariaDBConfig(string $directory): bool + public function usingLaravel11OrNewer(string $directory): bool { - // Laravel 11+ has moved the configuration files into the framework... - if (! file_exists($directory.'/config/database.php')) { - return true; - } + $version = json_decode(file_get_contents($directory.'/composer.json'), true)['require']['laravel/framework']; + $version = str_replace('^', '', $version); + $version = explode('.', $version)[0]; - return str_contains( - file_get_contents($directory.'/config/database.php'), - "'mariadb' =>" - ); + return $version >= 11; } /** @@ -450,7 +446,7 @@ protected function installJetstream(string $directory, InputInterface $input, Ou protected function promptForDatabaseOptions(string $directory, InputInterface $input) { // Laravel 11.x appliations use SQLite as default... - $defaultDatabase = $this->hasMariaDBConfig($directory) ? 'sqlite' : 'mysql'; + $defaultDatabase = $this->usingLaravel11OrNewer($directory) ? 'sqlite' : 'mysql'; if ($input->isInteractive()) { $database = select( @@ -465,7 +461,7 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i default: $defaultDatabase ); - if ($database !== $defaultDatabase) { + if ($this->usingLaravel11OrNewer($directory) && $database !== $defaultDatabase) { $migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true); } } diff --git a/tests/NewCommandTest.php b/tests/NewCommandTest.php index 4ed37aed..43ddf072 100644 --- a/tests/NewCommandTest.php +++ b/tests/NewCommandTest.php @@ -33,4 +33,17 @@ public function test_it_can_scaffold_a_new_laravel_app() $this->assertDirectoryExists($scaffoldDirectory.'/vendor'); $this->assertFileExists($scaffoldDirectory.'/.env'); } + + public function test_on_at_least_laravel_11() + { + $command = new NewCommand; + + $onLaravel10 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel10'); + $onLaravel11 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel11'); + $onLaravel12 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel12'); + + $this->assertFalse($onLaravel10); + $this->assertTrue($onLaravel11); + $this->assertTrue($onLaravel12); + } } diff --git a/tests/fixtures/laravel10/composer.json b/tests/fixtures/laravel10/composer.json new file mode 100644 index 00000000..8a3d72d4 --- /dev/null +++ b/tests/fixtures/laravel10/composer.json @@ -0,0 +1,66 @@ +{ + "name": "laravel/laravel", + "type": "project", + "description": "The skeleton application for the Laravel framework.", + "keywords": ["laravel", "framework"], + "license": "MIT", + "require": { + "php": "^8.1", + "guzzlehttp/guzzle": "^7.2", + "laravel/framework": "^10.10", + "laravel/sanctum": "^3.3", + "laravel/tinker": "^2.8" + }, + "require-dev": { + "fakerphp/faker": "^1.9.1", + "laravel/pint": "^1.0", + "laravel/sail": "^1.18", + "mockery/mockery": "^1.4.4", + "nunomaduro/collision": "^7.0", + "phpunit/phpunit": "^10.1", + "spatie/laravel-ignition": "^2.0" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" + ], + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi" + ] + }, + "extra": { + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "php-http/discovery": true + } + }, + "minimum-stability": "stable", + "prefer-stable": true +} diff --git a/tests/fixtures/laravel11/composer.json b/tests/fixtures/laravel11/composer.json new file mode 100644 index 00000000..3cdf16e3 --- /dev/null +++ b/tests/fixtures/laravel11/composer.json @@ -0,0 +1,69 @@ +{ + "name": "laravel/laravel", + "type": "project", + "description": "The skeleton application for the Laravel framework.", + "keywords": ["laravel", "framework"], + "license": "MIT", + "require": { + "php": "^8.2", + "laravel/framework": "^11.0", + "laravel/tinker": "^2.9" + }, + "require-dev": { + "fakerphp/faker": "^1.23", + "laravel/pint": "^1.13", + "laravel/sail": "^1.26", + "mockery/mockery": "^1.6", + "nunomaduro/collision": "^8.0", + "phpunit/phpunit": "^10.5", + "spatie/laravel-ignition": "^2.4" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" + ], + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi", + "@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"", + "@php artisan migrate --ansi" + ] + }, + "extra": { + "branch-alias": { + "dev-master": "11.x-dev" + }, + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "php-http/discovery": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/tests/fixtures/laravel12/composer.json b/tests/fixtures/laravel12/composer.json new file mode 100644 index 00000000..71811458 --- /dev/null +++ b/tests/fixtures/laravel12/composer.json @@ -0,0 +1,69 @@ +{ + "name": "laravel/laravel", + "type": "project", + "description": "The skeleton application for the Laravel framework.", + "keywords": ["laravel", "framework"], + "license": "MIT", + "require": { + "php": "^8.2", + "laravel/framework": "^12.0.1", + "laravel/tinker": "^2.9" + }, + "require-dev": { + "fakerphp/faker": "^1.23", + "laravel/pint": "^1.13", + "laravel/sail": "^1.26", + "mockery/mockery": "^1.6", + "nunomaduro/collision": "^8.0", + "phpunit/phpunit": "^10.5", + "spatie/laravel-ignition": "^2.4" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" + ], + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi", + "@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"", + "@php artisan migrate --ansi" + ] + }, + "extra": { + "branch-alias": { + "dev-master": "11.x-dev" + }, + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "php-http/discovery": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true +}