Skip to content

Commit 783a3a7

Browse files
authored
Resolve compatibility issues with latest Valet (#44)
* fix sanitization of site_name * add ValetConfig * add Config facade * bind config to ValetConfig implementations * replace Valet::domain with Config * remove unused domain methods
1 parent ab3dedb commit 783a3a7

7 files changed

Lines changed: 60 additions & 30 deletions

File tree

src/Config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace WP_CLI_Valet;
4+
5+
/**
6+
* @method static get(string $key)
7+
*/
8+
class Config extends Facade
9+
{
10+
public static function getContainerKey()
11+
{
12+
return 'config';
13+
}
14+
}

src/Process/FakeValet.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44

55
class FakeValet implements ValetInterface
66
{
7-
8-
/**
9-
* Get the local domain served by Valet.
10-
*
11-
* @return string
12-
*/
13-
public function domain()
14-
{
15-
return 'dev';
16-
}
17-
187
/**
198
* Secure the installation with a self-signed TLS certificate.
209
*

src/Process/SystemValet.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ class SystemValet extends ShellCommand implements ValetInterface
66
{
77
protected $command = 'valet';
88

9-
/**
10-
* Get the local domain served by Valet.
11-
*
12-
* @return string
13-
*/
14-
public function domain()
15-
{
16-
return trim($this->run('domain')->stdout);
17-
}
18-
199
/**
2010
* Secure the installation with a self-signed TLS certificate.
2111
*

src/Process/ValetInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44

55
interface ValetInterface
66
{
7-
/**
8-
* Get the local domain served by Valet.
9-
*
10-
* @return string
11-
*/
12-
public function domain();
13-
147
/**
158
* Secure the installation with a self-signed TLS certificate.
169
*

src/Props.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function __construct(array $positional, array $options)
3333
*/
3434
public function populate()
3535
{
36-
$this->site_name = preg_replace('/^a-zA-Z/', '-', $this->positional[0]);
37-
$this->domain = sprintf('%s.%s', $this->site_name, Valet::domain());
36+
$this->site_name = preg_replace('/[^a-zA-Z0-9\.]/', '-', $this->positional[0]);
37+
$this->domain = sprintf('%s.%s', $this->site_name, Config::get('tld') ?: Config::get('domain'));
3838
}
3939

4040
/**

src/ValetCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public static function boot()
4848
$container->singleton('valet', getenv('BEHAT_RUN') ? FakeValet::class : SystemValet::class);
4949
$container->singleton('wp', SystemWp::class);
5050
$container->singleton('composer', SystemComposer::class);
51+
$container->singleton('config', function () {
52+
return getenv('BEHAT_RUN')
53+
? new ValetConfig(['tld' => 'dev'])
54+
: ValetConfig::loadSystem();
55+
});
5156

5257
$container->bind('wp-installer', WordPressInstaller::class);
5358
$container->bind('bedrock-installer', BedrockInstaller::class);

src/ValetConfig.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace WP_CLI_Valet;
4+
5+
class ValetConfig
6+
{
7+
protected $data;
8+
9+
/**
10+
* ValetConfig constructor.
11+
*
12+
* @param $data
13+
*/
14+
public function __construct($data)
15+
{
16+
$this->data = $data;
17+
}
18+
19+
/**
20+
* @throws \Exception
21+
*/
22+
public static function loadSystem()
23+
{
24+
$config_path = file_exists("{$_SERVER['HOME']}/.config/valet/config.json")
25+
? "{$_SERVER['HOME']}/.config/valet/config.json"
26+
: "{$_SERVER['HOME']}/.valet/config.json";
27+
28+
if (! file_exists($config_path)) {
29+
throw new \Exception('Valet configuration file not found.');
30+
}
31+
32+
return new static(json_decode(file_get_contents($config_path), true));
33+
}
34+
35+
public function get($key)
36+
{
37+
return isset($this->data[$key]) ? $this->data[$key] : null;
38+
}
39+
}

0 commit comments

Comments
 (0)