Skip to content

Commit

Permalink
codesniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvb91 committed Nov 1, 2023
1 parent 753c693 commit 7fbda25
Show file tree
Hide file tree
Showing 62 changed files with 503 additions and 504 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ jobs:
run: |
docker network create -d bridge caddy-network
docker run --entrypoint caddy --name caddy -d --network="caddy-network" --hostname=caddy -v "$(pwd)/docker/caddy/autosave.json:/config/caddy/autosave.json" -v "$(pwd)/docker/caddy/files:/var/files" -p 80:80 -p 2019:2019 docker.pkg.github.com/mattvb91/caddy-php/caddy:head run --resume
docker run -v $(pwd):/app composer composer install
docker run -v $(pwd):/app composer composer phpstan
docker run --network="caddy-network" -e XDEBUG_MODE=coverage -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head php ./vendor/bin/phpunit --testdox --coverage-clover=coverage.xml
docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer install
docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer phpstan
docker run -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer codesniffer
docker run --network="caddy-network" -e XDEBUG_MODE=coverage -v $(pwd):/app docker.pkg.github.com/mattvb91/${{ env.REPO_NAME }}/composer:head composer phpunit
env:
REPO_NAME: ${{ github.event.repository.name }}
- uses: codecov/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion codesniffer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<description>PSR-12 with array indenting</description>
<rule ref="PSR12"/>
<rule ref="Generic.Arrays.ArrayIndent">
<exclude name="Generic.Arrays.ArrayIndent.CloseBRaceNotNewLine"/>
<exclude name="Generic.Arrays.ArrayIndent.CloseBraceNotNewLine"/>
</rule>
</ruleset>
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"type": "package",
"license": "MIT",
"scripts": {
"phpunit": "phpunit --testdox --coverage-clover=coverage.xml",
"phpstan": "phpstan analyse",
"codesniffer": "phpcs ./src ./tests/**/*.php --standard=./codesniffer.xml -p",
"codefixer": "phpcf ./src ./tests/**/*.php --standard=./codesniffer.xml"
"codefixer": "phpcbf ./src ./tests/**/*.php --standard=./codesniffer.xml"
},
"autoload": {
"psr-4": {
Expand Down
123 changes: 64 additions & 59 deletions src/Caddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,52 @@ class Caddy implements Arrayable
* host: Host
* }>
*/
private array $_hostsCache = [];
private array $hostsCache = [];

private Client $_client;
private Client $client;

private Admin $_admin;
private Admin $admin;

private ?Logging $_logging;
private ?Logging $logging;

/** @var App[] */
private array $_apps = [];
private array $apps = [];

private string $_hostname;
private string $hostname;

private string $_cacheHostnameHeader;
private string $cacheHostnameHeader;

public function __construct(
string $hostname = 'caddy',
Admin $admin = new Admin(),
Admin $admin = new Admin(),
Client $client = null,
string $cacheHostnameHeader = 'localhost')
{
string $cacheHostnameHeader = 'localhost'
) {
$this->setAdmin($admin);
$this->_hostname = $hostname;
$this->_cacheHostnameHeader = $cacheHostnameHeader;

$this->_client = $client ?? new Client([
'base_uri' => $hostname . $this->getAdmin()->getListen() . '/config',
'headers' => [
'Content-Type' => 'application/json',
],
]
);
$this->hostname = $hostname;
$this->cacheHostnameHeader = $cacheHostnameHeader;

$this->client = $client ?? new Client([
'base_uri' => $hostname . $this->getAdmin()->getListen() . '/config',
'headers' => [
'Content-Type' => 'application/json',
],
]);
}

/**
* If you are managing your hosts from an external source (for example db) and not directly in
* your config you should sync your hosts from the caddy config before making any changes for example trying to remove
* hosts
* your config you should sync your hosts from the caddy config before
* making any changes for example trying to remove hosts
*/
public function syncHosts(string $hostIdentifier): void
{
$this->buildHostsCache($hostIdentifier);

/** @var string[] $hosts */
$hosts = json_decode($this->_client->get($this->_hostsCache[$hostIdentifier]['path'])->getBody(), true);
$hosts = json_decode($this->client->get($this->hostsCache[$hostIdentifier]['path'])->getBody(), true);

$this->_hostsCache[$hostIdentifier]['host']->setHosts($hosts);
$this->hostsCache[$hostIdentifier]['host']->setHosts($hosts);
}

/**
Expand All @@ -85,10 +84,12 @@ public function addHostname(string $hostIdentifier, string $hostname): bool
{
$this->buildHostsCache($hostIdentifier);

if ($this->_client->put($this->_hostsCache[$hostIdentifier]['path'] . '/0', [
if (
$this->client->put($this->hostsCache[$hostIdentifier]['path'] . '/0', [
'json' => $hostname,
])->getStatusCode() === 200) {
$this->_hostsCache[$hostIdentifier]['host']->addHost($hostname);
])->getStatusCode() === 200
) {
$this->hostsCache[$hostIdentifier]['host']->addHost($hostname);
return true;
}

Expand All @@ -102,13 +103,15 @@ public function removeHostname(string $hostIdentifier, string $hostname): bool
{
$this->buildHostsCache($hostIdentifier);

$path = $this->_hostsCache[$hostIdentifier]['path'];
$path = $path . '/' . array_search($hostname, $this->_hostsCache[$hostIdentifier]['host']->getHosts());
$path = $this->hostsCache[$hostIdentifier]['path'];
$path = $path . '/' . array_search($hostname, $this->hostsCache[$hostIdentifier]['host']->getHosts());

if ($this->_client->delete($path, [
if (
$this->client->delete($path, [
'json' => $hostname,
])->getStatusCode() === 200) {
$this->_hostsCache[$hostIdentifier]['host']->syncRemoveHost($hostname);
])->getStatusCode() === 200
) {
$this->hostsCache[$hostIdentifier]['host']->syncRemoveHost($hostname);
return true;
}

Expand All @@ -126,25 +129,25 @@ public function removeHostname(string $hostIdentifier, string $hostname): bool
public function getRemoteConfig(): object
{
/** @var object */
return json_decode($this->_client->get('/config')->getBody(), false, 512, JSON_THROW_ON_ERROR);
return json_decode($this->client->get('/config')->getBody(), false, 512, JSON_THROW_ON_ERROR);
}

/**
* This is responsible for flushing the individual caches of items on the caddy server.
*
* @throws GuzzleException
* @param string[] $surrogates
* @throws GuzzleException
*/
public function flushSurrogates(array $surrogates): bool
{
//TODO this is missing the fact that you could customize your cache paths.

return $this->_client->request('PURGE', 'http://' . $this->_hostname . '/cache/souin', [
'headers' => [
'Surrogate-Key' => implode(', ', $surrogates),
'Host' => $this->_cacheHostnameHeader,
],
])->getStatusCode() === 204;
return $this->client->request('PURGE', 'http://' . $this->hostname . '/cache/souin', [
'headers' => [
'Surrogate-Key' => implode(', ', $surrogates),
'Host' => $this->cacheHostnameHeader,
],
])->getStatusCode() === 204;
}

/**
Expand All @@ -154,11 +157,13 @@ public function flushSurrogates(array $surrogates): bool
public function load(): bool
{
try {
return $this->_client->post('/load', [
'json' => $this->toArray(),
])->getStatusCode() === 200;
return $this->client->post('/load', [
'json' => $this->toArray(),
])->getStatusCode() === 200;
} catch (ClientException $e) {
throw new CaddyClientException($e->getResponse()->getBody() . PHP_EOL . json_encode($this->toArray(), JSON_PRETTY_PRINT));
throw new CaddyClientException(
$e->getResponse()->getBody() . PHP_EOL . json_encode($this->toArray(), JSON_PRETTY_PRINT)
);
}
}

Expand All @@ -167,24 +172,24 @@ public function load(): bool
*/
public function getClient(): Client
{
return $this->_client;
return $this->client;
}

public function getAdmin(): Admin
{
return $this->_admin;
return $this->admin;
}

protected function setAdmin(Admin $admin): static
{
$this->_admin = $admin;
$this->admin = $admin;

return $this;
}

public function setLogging(Logging $logging): static
{
$this->_logging = $logging;
$this->logging = $logging;

return $this;
}
Expand All @@ -195,7 +200,7 @@ public function addApp(App $app): static
$name = strrchr(get_class($app), '\\');
$namespace = strtolower(substr($name, 1));

$this->_apps[$namespace] = $app;
$this->apps[$namespace] = $app;

return $this;
}
Expand All @@ -204,20 +209,20 @@ public function toArray(): array
{
$config = [];

if (isset($this->_admin)) {
$config['admin'] = $this->_admin->toArray();
if (isset($this->admin)) {
$config['admin'] = $this->admin->toArray();
}

if (isset($this->_logging)) {
$config['logging'] = $this->_logging->toArray();
if (isset($this->logging)) {
$config['logging'] = $this->logging->toArray();
}

if (count($this->_apps)) {
if (count($this->apps)) {
$apps = [];

array_map(static function (App $app, string $appNamespace) use (&$apps) {
$apps[$appNamespace] = $app->toArray();
}, $this->_apps, array_keys($this->_apps));
}, $this->apps, array_keys($this->apps));

$config['apps'] = $apps;
}
Expand All @@ -232,11 +237,11 @@ public function toArray(): array
*/
protected function buildHostsCache(string $hostIdentifier): void
{
if (!key_exists($hostIdentifier, $this->_hostsCache)) {
if (!key_exists($hostIdentifier, $this->hostsCache)) {
//Find the host so we can get its path

$hostPath = null;
foreach ($this->_apps as $app) {
foreach ($this->apps as $app) {
if ($found = findHost($app, $hostIdentifier)) {
$hostPath = $found;
break;
Expand All @@ -247,7 +252,7 @@ protected function buildHostsCache(string $hostIdentifier): void
throw new \Exception('Host does not exist. Check your host identified');
}

$this->_hostsCache[$hostIdentifier] = $hostPath;
$this->hostsCache[$hostIdentifier] = $hostPath;
}
}
}
}
5 changes: 3 additions & 2 deletions src/Config/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Admin implements Arrayable
{
/**
* If true, the admin endpoint will be completely disabled.
* Note that this makes any runtime changes to the config impossible, since the interface to do so is through the admin endpoint.
* Note that this makes any runtime changes to the config impossible,
* since the interface to do so is through the admin endpoint.
*/
private bool $disabled = false;

Expand Down Expand Up @@ -51,4 +52,4 @@ public function toArray(): array
'listen' => $this->getListen(),
];
}
}
}
Loading

0 comments on commit 7fbda25

Please sign in to comment.