1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 08:30:07 +01:00

Rewrite TLS Caddy files on install

Porting this from master branch for now, makes it a lot easier for
people to go from master to 1.1.* since the master branch has a
different Caddyfile structure.

Since Caddy 0.9.* still has the X-Accel-Redirect issue re:
Content-Type, we can't tag on master yet, so this will help people get
back to a tagged release without having to manually fix a bunch of
Caddyfiles.
This commit is contained in:
Adam Wathan
2016-09-23 09:50:09 -04:00
parent 5919e93c6c
commit 403e75d580
2 changed files with 33 additions and 6 deletions

View File

@@ -6,6 +6,8 @@ class Caddy
{
var $cli;
var $files;
var $configuration;
var $site;
var $daemonPath = '/Library/LaunchDaemons/com.laravel.valetServer.plist';
/**
@@ -14,10 +16,12 @@ class Caddy
* @param CommandLine $cli
* @param Filesystem $files
*/
function __construct(CommandLine $cli, Filesystem $files)
function __construct(CommandLine $cli, Filesystem $files, Configuration $configuration, Site $site)
{
$this->cli = $cli;
$this->files = $files;
$this->configuration = $configuration;
$this->site = $site;
}
/**
@@ -61,6 +65,21 @@ function installCaddyDirectory()
}
$this->files->putAsUser($caddyDirectory.'/.keep', "\n");
$this->rewriteSecureCaddyFiles();
}
/**
* Generate fresh Caddyfiles for existing secure sites.
*
* This simplifies upgrading when the Caddyfile structure changes.
*
* @return void
*/
function rewriteSecureCaddyFiles()
{
$domain = $this->configuration->read()['domain'];
$this->site->resecureForNewDomain($domain, $domain);
}
/**

View File

@@ -1,7 +1,9 @@
<?php
use Valet\Site;
use Valet\Caddy;
use Valet\Filesystem;
use Valet\Configuration;
use Illuminate\Container\Container;
class CaddyTest extends PHPUnit_Framework_TestCase
@@ -27,7 +29,7 @@ public function test_install_caddy_file_places_stub_in_valet_home_directory()
$files->shouldReceive('putAsUser')->andReturnUsing(function ($path, $contents) {
$this->assertSame(VALET_HOME_PATH.'/Caddyfile', $path);
$this->assertTrue(strpos($contents, 'import '.VALET_HOME_PATH.'/Caddy/*') !== false);
});
})->once();
swap(Filesystem::class, $files);
$caddy = resolve(Caddy::class);
@@ -39,10 +41,12 @@ public function test_install_caddy_directories_creates_location_for_site_specifi
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Caddy')->andReturn(false);
$files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH.'/Caddy');
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n");
$files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH.'/Caddy')->once();
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n")->once();
swap(Filesystem::class, $files);
swap(Configuration::class, Mockery::spy(Configuration::class));
swap(Site::class, Mockery::spy(Site::class));
$caddy = resolve(Caddy::class);
$caddy->installCaddyDirectory();
@@ -54,9 +58,11 @@ public function test_caddy_directory_is_never_created_if_it_already_exists()
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Caddy')->andReturn(true);
$files->shouldReceive('mkdirAsUser')->never();
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n");
$files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n")->once();
swap(Filesystem::class, $files);
swap(Configuration::class, Mockery::spy(Configuration::class));
swap(Site::class, Mockery::spy(Site::class));
$caddy = resolve(Caddy::class);
$caddy->installCaddyDirectory();
@@ -68,12 +74,14 @@ public function test_caddy_daemon_is_placed_in_correct_location()
$files = Mockery::mock(Filesystem::class.'[put]');
swap(Filesystem::class, $files);
swap(Configuration::class, Mockery::spy(Configuration::class));
swap(Site::class, Mockery::spy(Site::class));
$caddy = resolve(Caddy::class);
$files->shouldReceive('put')->andReturnUsing(function ($path, $contents) use ($caddy) {
$this->assertSame($caddy->daemonPath, $path);
$this->assertTrue(strpos($contents, VALET_HOME_PATH) !== false);
});
})->once();
$caddy->installCaddyDaemon();
}