Skip to content

Commit

Permalink
Support existing htaccess without RewriteRule (#160)
Browse files Browse the repository at this point in the history
Description
-----------

Implements contao/manager-bundle#70.

Commits
-------

6ea8dbf Added support for already existing htaccess without existing RewriteRule directive
426f9af Encapsulate the .htaccess logic in a separate method
  • Loading branch information
Toflar authored and leofeyer committed Nov 15, 2018
1 parent b1dfb30 commit df75a50
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
27 changes: 25 additions & 2 deletions manager-bundle/src/Command/InstallWebDirCommand.php
Expand Up @@ -113,20 +113,44 @@ 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);

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)) {
Expand Down Expand Up @@ -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',
];
Expand Down
35 changes: 34 additions & 1 deletion manager-bundle/tests/Command/InstallWebDirCommandTest.php
Expand Up @@ -105,7 +105,6 @@ public function testCommandDoesNotOverrideOptionals(): void
}

static $optional = [
'.htaccess',
'favicon.ico',
'robots.txt',
];
Expand All @@ -122,6 +121,40 @@ public function testCommandDoesNotOverrideOptionals(): void
}
}

public function testHtaccessIsNotChangedIfRewriteRuleExists(): void
{
$existingHtaccess = <<<'EOT'
<IfModule mod_headers.c>
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>
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');
Expand Down

0 comments on commit df75a50

Please sign in to comment.