diff --git a/manager-bundle/src/Command/InstallWebDirCommand.php b/manager-bundle/src/Command/InstallWebDirCommand.php index 70669efe490..088c8847ef2 100644 --- a/manager-bundle/src/Command/InstallWebDirCommand.php +++ b/manager-bundle/src/Command/InstallWebDirCommand.php @@ -113,6 +113,7 @@ protected function executeLocked(InputInterface $input, OutputInterface $output) $webDir = $this->rootDir.'/'.rtrim($input->getArgument('target'), '/'); + $this->addHtaccess($webDir); $this->addFiles($webDir, !$input->getOption('no-dev')); $this->removeInstallPhp($webDir); $this->storeAppDevAccesskey($input, $this->rootDir); @@ -120,13 +121,36 @@ protected function executeLocked(InputInterface $input, OutputInterface $output) return 0; } + /** + * Adds the .htaccess file or merges it with an existing one. + */ + private function addHtaccess(string $webDir): void + { + $htaccess = __DIR__.'/../Resources/skeleton/web/.htaccess'; + + if (!file_exists($webDir.'/.htaccess')) { + $this->fs->copy($htaccess, $webDir.'/.htaccess', true); + + return; + } + + $existingContent = file_get_contents($webDir.'/.htaccess'); + + // Return if there already is a rewrite rule + if (preg_match('/^\s*RewriteRule\s/im', $existingContent)) { + return; + } + + $this->fs->dumpFile($webDir.'/.htaccess', $existingContent."\n\n".file_get_contents($htaccess)); + } + /** * Adds files from Resources/skeleton/web to the application's web directory. */ private function addFiles(string $webDir, bool $dev = true): void { /** @var SplFileInfo[] $finder */ - $finder = Finder::create()->files()->ignoreDotFiles(false)->in(__DIR__.'/../Resources/skeleton/web'); + $finder = Finder::create()->files()->in(__DIR__.'/../Resources/skeleton/web'); foreach ($finder as $file) { if ($this->isExistingOptionalFile($file, $webDir)) { @@ -218,7 +242,6 @@ private function addToDotEnv(string $projectDir, string $key, string $value): vo private function isExistingOptionalFile(SplFileInfo $file, string $webDir): bool { static $optional = [ - '.htaccess', 'favicon.ico', 'robots.txt', ]; diff --git a/manager-bundle/tests/Command/InstallWebDirCommandTest.php b/manager-bundle/tests/Command/InstallWebDirCommandTest.php index 5c3ce800924..62996fff973 100644 --- a/manager-bundle/tests/Command/InstallWebDirCommandTest.php +++ b/manager-bundle/tests/Command/InstallWebDirCommandTest.php @@ -105,7 +105,6 @@ public function testCommandDoesNotOverrideOptionals(): void } static $optional = [ - '.htaccess', 'favicon.ico', 'robots.txt', ]; @@ -122,6 +121,40 @@ public function testCommandDoesNotOverrideOptionals(): void } } + public function testHtaccessIsNotChangedIfRewriteRuleExists(): void + { + $existingHtaccess = <<<'EOT' + + RewriteRule ^ %{ENV:BASE}/app.php [L] + +EOT; + + $this->filesystem->dumpFile($this->getTempDir().'/web/.htaccess', $existingHtaccess); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([]); + + $this->assertStringEqualsFile($this->getTempDir().'/web/.htaccess', $existingHtaccess); + } + + public function testHtaccessIsChangedIfRewriteRuleDoesNotExists(): void + { + $existingHtaccess = <<<'EOT' +# Enable PHP 7.2 +AddHandler application/x-httpd-php72 .php +EOT; + + $this->filesystem->dumpFile($this->getTempDir().'/web/.htaccess', $existingHtaccess); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([]); + + $this->assertStringEqualsFile( + $this->getTempDir().'/web/.htaccess', + $existingHtaccess."\n\n".file_get_contents(__DIR__.'/../../src/Resources/skeleton/web/.htaccess') + ); + } + public function testCommandRemovesInstallPhp(): void { $this->filesystem->dumpFile($this->getTempDir().'/web/install.php', 'foobar-content');