diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index 0a3d486..7151e22 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -90,8 +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 --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/codecov-action@v3.1.0 diff --git a/codesniffer.xml b/codesniffer.xml new file mode 100644 index 0000000..1f58a26 --- /dev/null +++ b/codesniffer.xml @@ -0,0 +1,8 @@ + + + PSR-12 with array indenting + + + + + \ No newline at end of file diff --git a/composer.json b/composer.json index 0727e08..dc89303 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,12 @@ "description": "Control your Caddy instance through PHP", "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": "phpcbf ./src ./tests/**/*.php --standard=./codesniffer.xml" + }, "autoload": { "psr-4": { "mattvb91\\CaddyPhp\\": "src/" @@ -17,10 +23,13 @@ } }, "require": { + "php": "^8.1", "guzzlehttp/guzzle": "^7.0" }, "require-dev": { "phpunit/phpunit": "^9.5", - "dms/phpunit-arraysubset-asserts": "^0.4.0" + "dms/phpunit-arraysubset-asserts": "^0.4.0", + "phpstan/phpstan": "^1.10", + "squizlabs/php_codesniffer": "^3.7" } } diff --git a/docker/composer/Dockerfile b/docker/composer/Dockerfile index 25e1800..3f7586d 100644 --- a/docker/composer/Dockerfile +++ b/docker/composer/Dockerfile @@ -1,9 +1,9 @@ -FROM composer:2.3 +FROM composer:2.6.5 -RUN apk --update --no-cache add autoconf g++ make \ - && pecl install -f xdebug-3.1.4 \ +RUN apk --update --no-cache add autoconf g++ make linux-headers \ + && pecl install -f xdebug-3.2.2 \ && docker-php-ext-enable xdebug \ - && apk del --purge autoconf g++ make + && apk del --purge autoconf g++ make linux-headers EXPOSE 9000 \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..67ef3fe --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +parameters: + level: 9 + paths: + - src + excludePaths: + - vendor + - tests \ No newline at end of file diff --git a/src/Caddy.php b/src/Caddy.php index fafe375..69299cc 100644 --- a/src/Caddy.php +++ b/src/Caddy.php @@ -4,10 +4,12 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\GuzzleException; use mattvb91\CaddyPhp\Config\Admin; +use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Match\Host; use mattvb91\CaddyPhp\Config\Logging; use mattvb91\CaddyPhp\Exceptions\CaddyClientException; -use mattvb91\caddyPhp\Interfaces\App; +use mattvb91\CaddyPhp\Interfaces\App; use mattvb91\CaddyPhp\Interfaces\Arrayable; class Caddy implements Arrayable @@ -22,54 +24,57 @@ class Caddy implements Arrayable * * We need to build that path once based on the config and then cache it here. The format is * [ host_identifier => ['path' => '/anything', 'host' => &$host] + * @var array */ - 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(), - ?Client $client = null, - ?string $cacheHostnameHeader = 'localhost') - { + string $hostname = 'caddy', + Admin $admin = new Admin(), + Client $client = null, + 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) + public function syncHosts(string $hostIdentifier): void { $this->buildHostsCache($hostIdentifier); - $hosts = json_decode($this->_client->get($this->_hostsCache[$hostIdentifier]['path'])->getBody(), true); + /** @var string[] $hosts */ + $hosts = json_decode($this->client->get($this->hostsCache[$hostIdentifier]['path'])->getBody(), true); - $this->_hostsCache[$hostIdentifier]['host']->setHosts($hosts); + $this->hostsCache[$hostIdentifier]['host']->setHosts($hosts); } /** @@ -79,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; } @@ -96,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; } @@ -114,25 +123,31 @@ public function removeHostname(string $hostIdentifier, string $hostname): bool * * TODO we should be able to build our $caddy object back up from this. * So instead of toArray we should be able to do fromArray() or something + * + * @throws \JsonException|\GuzzleHttp\Exception\GuzzleException */ public function getRemoteConfig(): object { - return json_decode($this->_client->get('/config')->getBody(), false, 512, JSON_THROW_ON_ERROR); + /** @var object */ + 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. + * + * @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; } /** @@ -142,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) + ); } } @@ -155,37 +172,35 @@ public function load(): bool */ public function getClient(): Client { - return $this->_client; + return $this->client; } - public function getAdmin(): ?Admin + 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) + public function setLogging(Logging $logging): static { - $this->_logging = $logging; + $this->logging = $logging; return $this; } public function addApp(App $app): static { - $namespace = strtolower(substr(strrchr(get_class($app), '\\'), 1)); + /** @var string $name */ + $name = strrchr(get_class($app), '\\'); + $namespace = strtolower(substr($name, 1)); - if (!isset($this->_apps)) { - $this->_apps = [$namespace => $app]; - } else { - $this->_apps[$namespace] = $app; - } + $this->apps[$namespace] = $app; return $this; } @@ -194,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 (isset($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; } @@ -215,13 +230,18 @@ public function toArray(): array return $config; } + /** + * @param string $hostIdentifier + * @return void + * @throws \Exception + */ protected function buildHostsCache(string $hostIdentifier): void { - if (!in_array($hostIdentifier, $this->_hostsCache, true)) { + if (!key_exists($hostIdentifier, $this->hostsCache)) { //Find the host so we can get its path - $hostPath = ''; - foreach ($this->_apps as $app) { + $hostPath = null; + foreach ($this->apps as $app) { if ($found = findHost($app, $hostIdentifier)) { $hostPath = $found; break; @@ -232,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; } } -} \ No newline at end of file +} diff --git a/src/Config/Admin.php b/src/Config/Admin.php index f11bf88..e80518b 100644 --- a/src/Config/Admin.php +++ b/src/Config/Admin.php @@ -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; @@ -51,4 +52,4 @@ public function toArray(): array 'listen' => $this->getListen(), ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps.php b/src/Config/Apps.php deleted file mode 100644 index b03576c..0000000 --- a/src/Config/Apps.php +++ /dev/null @@ -1,23 +0,0 @@ -_api = $_api; - $this->_cdn = $_cdn; + Cdn $_cdn = new Cdn() + ) { + $this->api = $_api; + $this->cdn = $_cdn; } public function setApi(Api $api): static { - $this->_api = $api; + $this->api = $api; return $this; } public function setCdn(Cdn $cdn): static { - $this->_cdn = $cdn; + $this->cdn = $cdn; return $this; } public function setLogLevel(LogLevel $logLevel): static { - $this->_logLevel = $logLevel; + $this->logLevel = $logLevel; return $this; } public function setStale(string $stale): static { - $this->_stale = $stale; + $this->stale = $stale; return $this; } public function setTtl(string $ttl): static { - $this->_ttl = $ttl; + $this->ttl = $ttl; return $this; } public function setNuts(Nuts $nuts): static { - $this->_nuts = $nuts; + $this->nuts = $nuts; return $this; } public function setRedis(?Redis $redis): static { - $this->_redis = $redis; + $this->redis = $redis; return $this; } public function addCacheKey(Key $key): static { - if (!isset($this->_cacheKeys)) { - $this->_cacheKeys = [$key]; + if (!isset($this->cacheKeys)) { + $this->cacheKeys = [$key]; } else { - $this->_cacheKeys[] = $key; + $this->cacheKeys[] = $key; } return $this; @@ -100,27 +100,27 @@ public function addCacheKey(Key $key): static public function toArray(): array { $array = [ - 'api' => $this->_api->toArray(), - 'cdn' => $this->_cdn->toArray(), - 'log_level' => $this->_logLevel->value, - 'stale' => $this->_stale, - 'ttl' => $this->_ttl, + 'api' => $this->api->toArray(), + 'cdn' => $this->cdn->toArray(), + 'log_level' => $this->logLevel->value, + 'stale' => $this->stale, + 'ttl' => $this->ttl, ]; - if (isset($this->_nuts)) { - $array['nuts'] = $this->_nuts->toArray(); + if (isset($this->nuts)) { + $array['nuts'] = $this->nuts->toArray(); } - if (isset($this->_redis)) { - $array['redis'] = $this->_redis->toArray(); + if (isset($this->redis)) { + $array['redis'] = $this->redis->toArray(); } - if (isset($this->_cacheKeys)) { + if (isset($this->cacheKeys)) { $array['cache_keys'] = array_map(static function (Key $key) { return [$key->getPattern() => $key->toArray()]; - }, $this->_cacheKeys)[0]; //TODO there has to be a better way than [0] access to get this level + }, $this->cacheKeys)[0]; //TODO there has to be a better way than [0] access to get this level } return $array; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Cache/Api.php b/src/Config/Apps/Cache/Api.php index 64c81b0..0db86fe 100644 --- a/src/Config/Apps/Cache/Api.php +++ b/src/Config/Apps/Cache/Api.php @@ -7,18 +7,18 @@ class Api implements Arrayable { - private string $_basePath = '/cache'; + private string $basePath = '/cache'; - private Souin $_souin; + private Souin $souin; public function __construct(Souin $souin = new Souin()) { - $this->_souin = $souin; + $this->souin = $souin; } public function setBasePath(string $basePath): static { - $this->_basePath = $basePath; + $this->basePath = $basePath; return $this; } @@ -26,8 +26,8 @@ public function setBasePath(string $basePath): static public function toArray(): array { return [ - 'basepath' => $this->_basePath, - 'souin' => $this->_souin->toArray(), + 'basepath' => $this->basePath, + 'souin' => $this->souin->toArray(), ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Cache/Api/Souin.php b/src/Config/Apps/Cache/Api/Souin.php index db48d71..7970564 100644 --- a/src/Config/Apps/Cache/Api/Souin.php +++ b/src/Config/Apps/Cache/Api/Souin.php @@ -6,20 +6,20 @@ class Souin implements Arrayable { - private string $_basePath = '/souin'; + private string $basePath = '/souin'; - private bool $_enable = true; + private bool $enable = true; public function setBasePath(string $basePath): static { - $this->_basePath = $basePath; + $this->basePath = $basePath; return $this; } public function setEnable(bool $enable): static { - $this->_enable = $enable; + $this->enable = $enable; return $this; } @@ -27,8 +27,8 @@ public function setEnable(bool $enable): static public function toArray(): array { return [ - 'basepath' => $this->_basePath, - 'enable' => $this->_enable + 'basepath' => $this->basePath, + 'enable' => $this->enable ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Cache/Cdn.php b/src/Config/Apps/Cache/Cdn.php index f8ae80d..3a90a95 100644 --- a/src/Config/Apps/Cache/Cdn.php +++ b/src/Config/Apps/Cache/Cdn.php @@ -6,8 +6,8 @@ class Cdn implements Arrayable { - private bool $_dynamic; - private string $_strategy; + private bool $dynamic; + private string $strategy; /** * @param bool $_dynamic @@ -15,8 +15,8 @@ class Cdn implements Arrayable */ public function __construct(bool $_dynamic = true, string $_strategy = 'hard') { - $this->_dynamic = $_dynamic; - $this->_strategy = $_strategy; + $this->dynamic = $_dynamic; + $this->strategy = $_strategy; } /** @@ -24,7 +24,7 @@ public function __construct(bool $_dynamic = true, string $_strategy = 'hard') */ public function setDynamic(bool $dynamic): void { - $this->_dynamic = $dynamic; + $this->dynamic = $dynamic; } /** @@ -32,15 +32,15 @@ public function setDynamic(bool $dynamic): void */ public function setStrategy(string $strategy): void { - $this->_strategy = $strategy; + $this->strategy = $strategy; } public function toArray(): array { return [ - 'dynamic' => $this->_dynamic, - 'strategy' => $this->_strategy + 'dynamic' => $this->dynamic, + 'strategy' => $this->strategy ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Cache/Key.php b/src/Config/Apps/Cache/Key.php index b65906a..3f2b8a3 100644 --- a/src/Config/Apps/Cache/Key.php +++ b/src/Config/Apps/Cache/Key.php @@ -6,48 +6,48 @@ class Key implements Arrayable { - private string $_pattern; + private string $pattern; - private bool $_disable_host; + private bool $disable_host; - private bool $_disable_body; + private bool $disable_body; - private bool $_disable_method; + private bool $disable_method; public function __construct( string $pattern, - bool $disable_host = false, - bool $disable_body = false, - bool $disable_method = false) - { - $this->_pattern = $pattern; - $this->_disable_host = $disable_host; - $this->_disable_body = $disable_body; - $this->_disable_method = $disable_method; + bool $disable_host = false, + bool $disable_body = false, + bool $disable_method = false + ) { + $this->pattern = $pattern; + $this->disable_host = $disable_host; + $this->disable_body = $disable_body; + $this->disable_method = $disable_method; } public function getPattern(): string { - return $this->_pattern; + return $this->pattern; } public function setDisableHost(bool $disable_host): static { - $this->_disable_host = $disable_host; + $this->disable_host = $disable_host; return $this; } public function setDisableBody(bool $disable_body): static { - $this->_disable_body = $disable_body; + $this->disable_body = $disable_body; return $this; } public function setDisableMethod(bool $disable_domain): static { - $this->_disable_method = $disable_domain; + $this->disable_method = $disable_domain; return $this; } @@ -55,9 +55,9 @@ public function setDisableMethod(bool $disable_domain): static public function toArray(): array { return [ - 'disable_host' => $this->_disable_host, - 'disable_body' => $this->_disable_body, - 'disable_method' => $this->_disable_method, + 'disable_host' => $this->disable_host, + 'disable_body' => $this->disable_body, + 'disable_method' => $this->disable_method, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Cache/Nuts.php b/src/Config/Apps/Cache/Nuts.php index 70e2fec..a208365 100644 --- a/src/Config/Apps/Cache/Nuts.php +++ b/src/Config/Apps/Cache/Nuts.php @@ -6,17 +6,17 @@ class Nuts implements Arrayable { - private string $_path; + private string $path; public function __construct(string $_path = '/tmp/nuts-souin') { - $this->_path = $_path; + $this->path = $_path; } public function toArray(): array { return [ - 'path' => $this->_path + 'path' => $this->path ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Cache/Redis.php b/src/Config/Apps/Cache/Redis.php index 864dff1..fdacb8e 100644 --- a/src/Config/Apps/Cache/Redis.php +++ b/src/Config/Apps/Cache/Redis.php @@ -6,17 +6,17 @@ class Redis implements Arrayable { - private string $_url; + private string $url; public function __construct(string $_url) { - $this->_url = $_url; + $this->url = $_url; } public function toArray(): array { return [ - 'url' => $this->_url, + 'url' => $this->url, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http.php b/src/Config/Apps/Http.php index 09f04d2..8943d1a 100644 --- a/src/Config/Apps/Http.php +++ b/src/Config/Apps/Http.php @@ -10,39 +10,39 @@ class Http implements App { use IterableProps; - private int $_httpPort; + private int $httpPort; - private int $_httpsPort; + private int $httpsPort; - private int $_gracePeriod; + private int $gracePeriod; /** @var Server[] */ - private array $_servers = []; + private array $servers = []; public function addServer(string $key, Server $server): static { - $this->_servers[$key] = $server; + $this->servers[$key] = $server; return $this; } public function setHttpPort(int $httpPort): static { - $this->_httpPort = $httpPort; + $this->httpPort = $httpPort; return $this; } public function setHttpsPort(int $httpsPort): static { - $this->_httpsPort = $httpsPort; + $this->httpsPort = $httpsPort; return $this; } public function setGracePeriod(int $gracePeriod): static { - $this->_gracePeriod = $gracePeriod; + $this->gracePeriod = $gracePeriod; return $this; } @@ -50,25 +50,25 @@ public function toArray(): array { $config = []; - if (isset($this->_httpPort)) { - $config['http_port'] = $this->_httpPort; + if (isset($this->httpPort)) { + $config['http_port'] = $this->httpPort; } - if (isset($this->_httpsPort)) { - $config['https_port'] = $this->_httpsPort; + if (isset($this->httpsPort)) { + $config['https_port'] = $this->httpsPort; } - if (isset($this->_gracePeriod)) { - $config['grace_period'] = $this->_gracePeriod; + if (isset($this->gracePeriod)) { + $config['grace_period'] = $this->gracePeriod; } $servers = []; array_map(static function (Server $server, string $key) use (&$servers) { $servers[$key] = $server->toArray(); - }, $this->_servers, array_keys($this->_servers)); + }, $this->servers, array_keys($this->servers)); $config['servers'] = $servers; return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server.php b/src/Config/Apps/Http/Server.php index a63f97e..9ea1f2b 100644 --- a/src/Config/Apps/Http/Server.php +++ b/src/Config/Apps/Http/Server.php @@ -2,93 +2,103 @@ namespace mattvb91\CaddyPhp\Config\Apps\Http; -use mattvb91\caddyPhp\Config\Apps\Http\Server\Route; +use mattvb91\CaddyPhp\Config\Apps\Http\Server\Route; use mattvb91\CaddyPhp\Interfaces\Arrayable; use mattvb91\CaddyPhp\Traits\IterableProps; /** - * Servers is the list of servers, keyed by arbitrary names chosen at your discretion for your own convenience; the keys do not affect functionality. + * Servers is the list of servers, keyed by arbitrary names chosen at your discretion for your own convenience; + * the keys do not affect functionality. */ class Server implements Arrayable { use IterableProps; - private array $_listen = [':80']; + /** @var string[] */ + private array $listen = [':80']; /** @var Route[] */ - private array $_routes = []; + private array $routes = []; - private int $_readTimeout; + private int $readTimeout; - private int $_readHeaderTimeout; + private int $readHeaderTimeout; - private int $_writeTimeout; + private int $writeTimeout; - private int $_idleTimeout; + private int $idleTimeout; - private int $_maxHeaderBytes; + private int $maxHeaderBytes; - private bool $_strictSniHost; + private bool $strictSniHost; + /** + * @param string[] $listen + * @return $this + */ public function setListen(array $listen): static { - $this->_listen = $listen; + $this->listen = $listen; return $this; } public function addRoute(Route $route): static { - $this->_routes[] = $route; + $this->routes[] = $route; return $this; } + /** + * @param Route[] $routes + * @return $this + */ public function setRoutes(array $routes): static { - $this->_routes = $routes; + $this->routes = $routes; return $this; } public function setReadTimeout(int $readTimeout): static { - $this->_readTimeout = $readTimeout; + $this->readTimeout = $readTimeout; return $this; } public function setReadHeaderTimeout(int $readHeaderTimeout): static { - $this->_readHeaderTimeout = $readHeaderTimeout; + $this->readHeaderTimeout = $readHeaderTimeout; return $this; } public function setWriteTimeout(int $writeTimeout): static { - $this->_writeTimeout = $writeTimeout; + $this->writeTimeout = $writeTimeout; return $this; } public function setIdleTimeout(int $idleTimeout): static { - $this->_idleTimeout = $idleTimeout; + $this->idleTimeout = $idleTimeout; return $this; } public function setMaxHeaderBytes(int $maxHeaderBytes): static { - $this->_maxHeaderBytes = $maxHeaderBytes; + $this->maxHeaderBytes = $maxHeaderBytes; return $this; } public function setStrictSniHost(bool $strictSniHost): static { - $this->_strictSniHost = $strictSniHost; + $this->strictSniHost = $strictSniHost; return $this; } @@ -96,36 +106,37 @@ public function setStrictSniHost(bool $strictSniHost): static public function toArray(): array { $config = [ - 'listen' => $this->_listen, + 'listen' => $this->listen, 'routes' => [...array_map(static function (Route $route) { return $route->toArray(); - }, $this->_routes)], + }, $this->routes) + ], ]; - if (isset($this->_readTimeout)) { - $config['read_timeout'] = $this->_readTimeout; + if (isset($this->readTimeout)) { + $config['read_timeout'] = $this->readTimeout; } - if (isset($this->_readHeaderTimeout)) { - $config['read_header_timeout'] = $this->_readHeaderTimeout; + if (isset($this->readHeaderTimeout)) { + $config['read_header_timeout'] = $this->readHeaderTimeout; } - if (isset($this->_writeTimeout)) { - $config['write_timeout'] = $this->_writeTimeout; + if (isset($this->writeTimeout)) { + $config['write_timeout'] = $this->writeTimeout; } - if (isset($this->_idleTimeout)) { - $config['idle_timeout'] = $this->_idleTimeout; + if (isset($this->idleTimeout)) { + $config['idle_timeout'] = $this->idleTimeout; } - if (isset($this->_maxHeaderBytes)) { - $config['max_header_bytes'] = $this->_maxHeaderBytes; + if (isset($this->maxHeaderBytes)) { + $config['max_header_bytes'] = $this->maxHeaderBytes; } - if (isset($this->_strictSniHost)) { - $config['strict_sni_host'] = $this->_strictSniHost; + if (isset($this->strictSniHost)) { + $config['strict_sni_host'] = $this->strictSniHost; } return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Route.php b/src/Config/Apps/Http/Server/Route.php index d0692e6..48c6250 100644 --- a/src/Config/Apps/Http/Server/Route.php +++ b/src/Config/Apps/Http/Server/Route.php @@ -2,7 +2,7 @@ namespace mattvb91\CaddyPhp\Config\Apps\Http\Server; -use mattvb91\caddyPhp\Interfaces\Apps\Servers\Routes\Handle\HandlerInterface; +use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Handle\HandlerInterface; use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Match\MatcherInterface; use mattvb91\CaddyPhp\Interfaces\Arrayable; use mattvb91\CaddyPhp\Traits\IterableProps; @@ -11,7 +11,8 @@ * Routes describes how this server will handle requests. * Routes are executed sequentially. First a route's matchers are evaluated, then its grouping. * If it matches and has not been mutually-excluded by its grouping, then its handlers are executed sequentially. - * The sequence of invoked handlers comprises a compiled middleware chain that flows from each matching route and its handlers to the next. + * The sequence of invoked handlers comprises a compiled middleware chain that flows from each matching + * route and its handlers to the next. * * https://caddyserver.com/docs/json/apps/http/servers/routes/ */ @@ -19,28 +20,29 @@ class Route implements Arrayable { use IterableProps; - private ?string $_group; + private ?string $group; /** @var HandlerInterface[] */ - private array $_handle = []; + private array $handle = []; - private ?array $_match; + /** @var MatcherInterface[]|null */ + private ?array $match; - private ?bool $_terminal; + private ?bool $terminal; public function setGroup(string $group): static { - $this->_group = $group; + $this->group = $group; return $this; } public function addMatch(MatcherInterface $match): static { - if (!isset($this->_match)) { - $this->_match = [$match]; + if (!isset($this->match)) { + $this->match = [$match]; } else { - $this->_match[] = $match; + $this->match[] = $match; } return $this; @@ -48,14 +50,14 @@ public function addMatch(MatcherInterface $match): static public function addHandle(HandlerInterface $handler): static { - $this->_handle[] = $handler; + $this->handle[] = $handler; return $this; } public function setTerminal(bool $terminal): static { - $this->_terminal = $terminal; + $this->terminal = $terminal; return $this; } @@ -64,19 +66,19 @@ public function toArray(): array { $config = []; - if (isset($this->_group)) { - $config['group'] = $this->_group; + if (isset($this->group)) { + $config['group'] = $this->group; } $config['handle'] = [...array_map(static function (HandlerInterface $handler) { return $handler->toArray(); - }, $this->_handle)]; - - if (isset($this->_match)) { + }, $this->handle) + ]; + if (isset($this->match)) { $config['match'] = array_map(static function (MatcherInterface $matcher) { return $matcher->toArray(); - }, $this->_match); + }, $this->match); /** * TODO this is a brutal hack, check / test why we need to do this. @@ -91,10 +93,10 @@ public function toArray(): array } } - if (isset($this->_terminal)) { - $config['terminal'] = $this->_terminal; + if (isset($this->terminal)) { + $config['terminal'] = $this->terminal; } return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php b/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php index 33d3ecb..88b8c43 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Authentication.php @@ -10,15 +10,12 @@ */ class Authentication implements HandlerInterface { - private ?array $_providers; + /** @var ProviderInterface[] */ + private array $providers; public function addProvider(ProviderInterface $provider): static { - if (!isset($this->_providers)) { - $this->_providers = [$provider]; - } else { - $this->_providers[] = $provider; - } + $this->providers[] = $provider; return $this; } @@ -29,10 +26,10 @@ public function toArray(): array 'handler' => $this->getHandler(), ]; - if (isset($this->_providers)) { + if (count($this->providers)) { $config['providers'] = array_map(static function (ProviderInterface $provider) { return [$provider->getModuleName() => $provider->toArray()]; - }, $this->_providers)[0];//TODO there has to be a better way than [0] + }, $this->providers)[0];//TODO there has to be a better way than [0] } return $config; @@ -43,4 +40,4 @@ public function getHandler(): string { return 'authentication'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php index 2b2caf7..50399e2 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic.php @@ -8,23 +8,21 @@ class HttpBasic implements ProviderInterface { - private ?array $_accounts; + /** @var Account[] */ + private array $accounts = []; - private ?HashInterface $_hash; + private ?HashInterface $hash; public function addAccount(Account $account): static { - if (!isset($this->_accounts)) { - $this->_accounts = [$account]; - } else { - $this->_accounts[] = $account; - } + $this->accounts[] = $account; + return $this; } public function setHash(HashInterface $hash): static { - $this->_hash = $hash; + $this->hash = $hash; return $this; } @@ -33,14 +31,15 @@ public function toArray(): array { $config = []; - if (isset($this->_accounts)) { + if (count($this->accounts)) { $config['accounts'] = [...array_map(function (Account $account) { return $account->toArray(); - }, $this->_accounts)]; + }, $this->accounts) + ]; } - if (isset($this->_hash)) { - $config['hash'] = $this->_hash->toArray(); + if (isset($this->hash)) { + $config['hash'] = $this->hash->toArray(); } return $config; @@ -50,5 +49,4 @@ public function getModuleName(): string { return 'http_basic'; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Account.php b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Account.php index ac1ad93..634b7f3 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Account.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Account.php @@ -6,31 +6,30 @@ class Account implements Arrayable { + private string $username; - private string $_username; + private string $password; - private string $_password; - - private ?string $_salt; + private ?string $salt; public function __construct(string $username, string $password, ?string $salt = null) { - $this->_username = $username; - $this->_password = $password; - $this->_salt = $salt; + $this->username = $username; + $this->password = $password; + $this->salt = $salt; } public function toArray(): array { $config = [ - 'username' => $this->_username, - 'password' => base64_encode(password_hash($this->_password, PASSWORD_BCRYPT)), + 'username' => $this->username, + 'password' => base64_encode(password_hash($this->password, PASSWORD_BCRYPT)), ]; - if ($this->_salt) { - $config['salt'] = $this->_salt; + if ($this->salt) { + $config['salt'] = $this->salt; } return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Hash/Bcrypt.php b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Hash/Bcrypt.php index 5f3dd7f..e0533e4 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Hash/Bcrypt.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Authentication/Providers/HttpBasic/Hash/Bcrypt.php @@ -17,4 +17,4 @@ public function getAlgorithm(): string { return 'bcrypt'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Cache.php b/src/Config/Apps/Http/Server/Routes/Handle/Cache.php index e696ae7..6fce2ca 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Cache.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Cache.php @@ -7,29 +7,36 @@ class Cache implements HandlerInterface { - private ?LogLevel $_logLevel; + private ?LogLevel $logLevel; - private ?array $_allowedHttpVerbs; + /** + * @var string[]|null + */ + private ?array $allowedHttpVerbs; - private ?string $_defaultCacheControl; + private ?string $defaultCacheControl; public function setLogLevel(?LogLevel $logLevel): static { - $this->_logLevel = $logLevel; + $this->logLevel = $logLevel; return $this; } + /** + * @param string[] $allowedHttpVerbs + * @return $this + */ public function setAllowedHttpVerbs(array $allowedHttpVerbs): static { - $this->_allowedHttpVerbs = $allowedHttpVerbs; + $this->allowedHttpVerbs = $allowedHttpVerbs; return $this; } public function setDefaultCacheControl(string $defaultCacheControl): static { - $this->_defaultCacheControl = $defaultCacheControl; + $this->defaultCacheControl = $defaultCacheControl; return $this; } @@ -40,16 +47,16 @@ public function toArray(): array 'handler' => $this->getHandler(), ]; - if (isset($this->_logLevel)) { - $array['log_level'] = $this->_logLevel->value; + if (isset($this->logLevel)) { + $array['log_level'] = $this->logLevel->value; } - if (isset($this->_allowedHttpVerbs)) { - $array['allowed_http_verbs'] = $this->_allowedHttpVerbs; + if (isset($this->allowedHttpVerbs)) { + $array['allowed_http_verbs'] = $this->allowedHttpVerbs; } - if (isset($this->_defaultCacheControl)) { - $array['default_cache_control'] = $this->_defaultCacheControl; + if (isset($this->defaultCacheControl)) { + $array['default_cache_control'] = $this->defaultCacheControl; } return $array; @@ -59,5 +66,4 @@ public function getHandler(): string { return 'cache'; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Error.php b/src/Config/Apps/Http/Server/Routes/Handle/Error.php index 4317472..62b4930 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Error.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Error.php @@ -6,20 +6,20 @@ class Error implements HandlerInterface { - private string $_error; + private string $error; - private string|int $_statusCode; + private string|int $statusCode; public function setError(string $error): static { - $this->_error = $error; + $this->error = $error; return $this; } public function setStatusCode(int|string $statusCode): static { - $this->_statusCode = $statusCode; + $this->statusCode = $statusCode; return $this; } @@ -31,12 +31,12 @@ public function toArray(): array 'handler' => $this->getHandler() ]; - if(isset($this->_error)) { - $config['error'] = $this->_error; + if (isset($this->error)) { + $config['error'] = $this->error; } - if(isset($this->_statusCode)) { - $config['status_code'] = $this->_statusCode; + if (isset($this->statusCode)) { + $config['status_code'] = $this->statusCode; } return $config; @@ -46,4 +46,4 @@ public function getHandler(): string { return 'error'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/FileServer.php b/src/Config/Apps/Http/Server/Routes/Handle/FileServer.php index 22c9c81..6f3aa24 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/FileServer.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/FileServer.php @@ -6,11 +6,11 @@ class FileServer implements HandlerInterface { - private $_root = ""; + private string $root = ""; public function setRoot(string $root): static { - $this->_root = $root; + $this->root = $root; return $this; } @@ -19,7 +19,7 @@ public function toArray(): array { return [ 'handler' => $this->getHandler(), - 'root' => $this->_root, + 'root' => $this->root, ]; } @@ -27,4 +27,4 @@ public function getHandler(): string { return 'file_server'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Headers.php b/src/Config/Apps/Http/Server/Routes/Handle/Headers.php index 84c048a..e0547f0 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Headers.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Headers.php @@ -11,20 +11,20 @@ */ class Headers implements HandlerInterface { - private ?Request $_request; + private ?Request $request; - private ?Response $_response; + private ?Response $response; public function setRequest(?Request $request): static { - $this->_request = $request; + $this->request = $request; return $this; } public function setResponse(?Response $response): static { - $this->_response = $response; + $this->response = $response; return $this; } @@ -35,12 +35,12 @@ public function toArray(): array 'handler' => $this->getHandler(), ]; - if (isset($this->_response)) { - $array['response'] = $this->_response->toArray(); + if (isset($this->response)) { + $array['response'] = $this->response->toArray(); } - if (isset($this->_request)) { - $array['request'] = $this->_request->toArray(); + if (isset($this->request)) { + $array['request'] = $this->request->toArray(); } return $array; @@ -50,4 +50,4 @@ public function getHandler(): string { return 'headers'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php index 7938098..6d0cddb 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Request.php @@ -11,16 +11,13 @@ class Request implements Arrayable { /** * Names of HTTP header fields to delete. + * @var string[] */ - private ?array $_delete; + private array $delete = []; public function addDeleteHeader(string $header): static { - if (!isset($this->_delete)) { - $this->_delete = [$header]; - } else { - $this->_delete[] = $header; - } + $this->delete[] = $header; return $this; } @@ -29,10 +26,10 @@ public function toArray(): array { $array = []; - if (isset($this->_delete)) { - $array['delete'] = $this->_delete; + if (count($this->delete)) { + $array['delete'] = $this->delete; } return $array; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php index 23659c7..28b2879 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php @@ -8,19 +8,23 @@ class Response implements Arrayable { /** * Names of HTTP header fields to delete. + * @var string[] */ - private ?array $_delete; + private array $delete = []; - private ?array $_add; + /** + * @var array + */ + private array $add = []; - private bool $_deferred = false; + private bool $deferred = false; public function addDeleteHeader(string $header): static { - if (!isset($this->_delete)) { - $this->_delete = [$header]; + if (!isset($this->delete)) { + $this->delete = [$header]; } else { - $this->_delete[] = $header; + $this->delete[] = $header; } return $this; @@ -28,14 +32,14 @@ public function addDeleteHeader(string $header): static public function addHeader(string $name, string $value): static { - $this->_add[$name] = [$value]; + $this->add[$name] = [$value]; return $this; } public function setDeferred(bool $deferred): static { - $this->_deferred = $deferred; + $this->deferred = $deferred; return $this; } @@ -44,16 +48,16 @@ public function toArray(): array { $array = []; - if (isset($this->_delete)) { - $array['delete'] = $this->_delete; + if (count($this->delete)) { + $array['delete'] = $this->delete; } - if (isset($this->_add)) { - $array['add'] = $this->_add; + if (count($this->add)) { + $array['add'] = $this->add; } - $array['deferred'] = $this->_deferred; + $array['deferred'] = $this->deferred; return $array; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php index 77b7814..07a6f95 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy.php @@ -11,16 +11,18 @@ */ class ReverseProxy implements HandlerInterface { - private ?array $_upstreams; + /** @var Upstream[]|null */ + private ?array $upstreams; - private ?array $_transport; + /** @var TransportInterface[]|null */ + private ?array $transport; public function addUpstream(Upstream $upstream): static { - if (!isset($this->_upstreams)) { - $this->_upstreams = [$upstream]; + if (!isset($this->upstreams)) { + $this->upstreams = [$upstream]; } else { - $this->_upstreams[] = $upstream; + $this->upstreams[] = $upstream; } return $this; @@ -28,10 +30,10 @@ public function addUpstream(Upstream $upstream): static public function addTransport(TransportInterface $transport): static { - if (!isset($this->_transport)) { - $this->_transport = [$transport]; + if (!isset($this->transport)) { + $this->transport = [$transport]; } else { - $this->_transport[] = $transport; + $this->transport[] = $transport; } return $this; @@ -43,16 +45,17 @@ public function toArray(): array 'handler' => $this->getHandler(), ]; - if (isset($this->_transport)) { + if (isset($this->transport)) { $array['transport'] = array_map(static function (TransportInterface $transport) { return $transport->toArray(); - }, $this->_transport)[0]; //TODO there has to be a better way than [0] access to get this level + }, $this->transport)[0]; //TODO there has to be a better way than [0] access to get this level } - if (isset($this->_upstreams)) { + if (isset($this->upstreams)) { $array['upstreams'] = [...array_map(static function (Upstream $upstream) { return $upstream->toArray(); - }, $this->_upstreams)]; + }, $this->upstreams) + ]; } return $array; @@ -62,4 +65,4 @@ public function getHandler(): string { return 'reverse_proxy'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Transport/FastCGI.php b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Transport/FastCGI.php index f8799df..d4ed513 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Transport/FastCGI.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Transport/FastCGI.php @@ -9,20 +9,25 @@ */ class FastCGI implements TransportInterface { - private ?string $_root; + private ?string $root; - private ?array $_splitPath; + /** @var string[]|null */ + private ?array $splitPath; public function setRoot(string $root): static { - $this->_root = $root; + $this->root = $root; return $this; } + /** + * @param string[] $splitPath + * @return $this + */ public function setSplitPath(array $splitPath): static { - $this->_splitPath = $splitPath; + $this->splitPath = $splitPath; return $this; } @@ -33,12 +38,12 @@ public function toArray(): array 'protocol' => $this->getProtocol(), ]; - if (isset($this->_splitPath)) { - $array['split_path'] = $this->_splitPath; + if (isset($this->splitPath)) { + $array['split_path'] = $this->splitPath; } - if (isset($this->_root)) { - $array['root'] = $this->_root; + if (isset($this->root)) { + $array['root'] = $this->root; } return $array; @@ -48,4 +53,4 @@ public function getProtocol(): string { return 'fastcgi'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Upstream.php b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Upstream.php index 9b9a8fa..c8d0338 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Upstream.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/ReverseProxy/Upstream.php @@ -9,11 +9,11 @@ */ class Upstream implements Arrayable { - private ?string $_dial; + private ?string $dial; public function setDial(string $dial): static { - $this->_dial = $dial; + $this->dial = $dial; return $this; } @@ -22,11 +22,10 @@ public function toArray(): array { $array = []; - if (isset($this->_dial)) { - $array['dial'] = $this->_dial; + if (isset($this->dial)) { + $array['dial'] = $this->dial; } return $array; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Rewrite.php b/src/Config/Apps/Http/Server/Routes/Handle/Rewrite.php index 7c3bc6a..fdf5d5d 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Rewrite.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Rewrite.php @@ -9,20 +9,20 @@ */ class Rewrite implements HandlerInterface { - private ?string $_stripPathPrefix; + private ?string $stripPathPrefix; - private ?string $_uri; + private ?string $uri; public function setStripPathPrefix(?string $stripPathPrefix): static { - $this->_stripPathPrefix = $stripPathPrefix; + $this->stripPathPrefix = $stripPathPrefix; return $this; } public function setUri(string $uri): static { - $this->_uri = $uri; + $this->uri = $uri; return $this; } @@ -33,12 +33,12 @@ public function toArray(): array 'handler' => $this->getHandler(), ]; - if (isset($this->_stripPathPrefix)) { - $array['strip_path_prefix'] = $this->_stripPathPrefix; + if (isset($this->stripPathPrefix)) { + $array['strip_path_prefix'] = $this->stripPathPrefix; } - if (isset($this->_uri)) { - $array['uri'] = $this->_uri; + if (isset($this->uri)) { + $array['uri'] = $this->uri; } return $array; @@ -48,5 +48,4 @@ public function getHandler(): string { return 'rewrite'; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php b/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php index bebaec6..b495fae 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/StaticResponse.php @@ -11,22 +11,26 @@ */ class StaticResponse implements HandlerInterface { + private ?string $body; - private ?string $_body; + private int $statusCode; - private int $_statusCode; - - private array $_headers; + /** @var string[] */ + private array $headers; public function __construct(?string $body = null, int $statusCode = 200) { - $body ? $this->_body = $body : null; - $this->_statusCode = $statusCode; + $body ? $this->body = $body : null; + $this->statusCode = $statusCode; } + /** + * @param string[] $headers + * @return $this + */ public function setHeaders(array $headers): static { - $this->_headers = $headers; + $this->headers = $headers; return $this; } @@ -35,15 +39,15 @@ public function toArray(): array { $config = [ 'handler' => $this->getHandler(), - 'status_code' => $this->_statusCode, + 'status_code' => $this->statusCode, ]; - if (isset($this->_body)) { - $config['body'] = $this->_body; + if (isset($this->body)) { + $config['body'] = $this->body; } - if (isset($this->_headers)) { - $config['headers'] = $this->_headers; + if (isset($this->headers)) { + $config['headers'] = $this->headers; } return $config; @@ -53,4 +57,4 @@ public function getHandler(): string { return 'static_response'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php b/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php index 1596301..6e58572 100644 --- a/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php +++ b/src/Config/Apps/Http/Server/Routes/Handle/Subroute.php @@ -10,11 +10,12 @@ */ class Subroute implements HandlerInterface { - private array $_routes = []; + /** @var Route[] */ + private array $routes = []; public function addRoute(Route $route): static { - $this->_routes[] = $route; + $this->routes[] = $route; return $this; } @@ -25,7 +26,8 @@ public function toArray(): array 'handler' => $this->getHandler(), 'routes' => [...array_map(static function (Route $route) { return $route->toArray(); - }, $this->_routes)], + }, $this->routes) + ], ]; } @@ -33,4 +35,4 @@ public function getHandler(): string { return 'subroute'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Match/File.php b/src/Config/Apps/Http/Server/Routes/Match/File.php index fed99aa..6d26005 100644 --- a/src/Config/Apps/Http/Server/Routes/Match/File.php +++ b/src/Config/Apps/Http/Server/Routes/Match/File.php @@ -6,11 +6,16 @@ class File implements MatcherInterface { - private array $_tryFiles; + /** @var string[] */ + private array $tryFiles; + /** + * @param string[] $paths + * @return $this + */ public function setTryFiles(array $paths): static { - $this->_tryFiles = $paths; + $this->tryFiles = $paths; return $this; } @@ -19,12 +24,12 @@ public function toArray(): array { $config = []; - if (isset($this->_tryFiles)) { - $config['try_files'] = $this->_tryFiles; + if (isset($this->tryFiles)) { + $config['try_files'] = $this->tryFiles; } return [ 'file' => $config, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Match/Host.php b/src/Config/Apps/Http/Server/Routes/Match/Host.php index 26bc2f5..00110a6 100644 --- a/src/Config/Apps/Http/Server/Routes/Match/Host.php +++ b/src/Config/Apps/Http/Server/Routes/Match/Host.php @@ -10,50 +10,60 @@ class Host implements MatcherInterface * We need unique identifiers for all our hosts * so we can later retrieve them to attach domains to the correct paths */ - private string $_identifier; + private string $identifier; - private array $_hosts = []; + /** + * @var array + */ + private array $hosts = []; public function __construct(string $identifier) { - $this->_identifier = $identifier; + $this->identifier = $identifier; } public function getIdentifier(): string { - return $this->_identifier; + return $this->identifier; } + /** + * @param array $hosts + * @return $this + */ public function setHosts(array $hosts): static { - $this->_hosts = $hosts; + $this->hosts = $hosts; return $this; } - public function addHost(string $host) + public function addHost(string $host): void { - $this->_hosts = [$host, ...$this->_hosts]; + $this->hosts = [$host, ...$this->hosts]; } /** * DO NOT CALL MANUALLY * This is used for when caddy->removeHostname() is called to keep this object in sync */ - public function syncRemoveHost(string $hostname) + public function syncRemoveHost(string $hostname): void { - unset($this->_hosts[array_search($hostname, $this->_hosts)]); + unset($this->hosts[array_search($hostname, $this->hosts)]); } - public function getHosts() + /** + * @return string[] + */ + public function getHosts(): array { - return $this->_hosts; + return $this->hosts; } public function toArray(): array { return [ - 'host' => $this->_hosts, + 'host' => $this->hosts, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Match/MatchProtocol.php b/src/Config/Apps/Http/Server/Routes/Match/MatchProtocol.php new file mode 100644 index 0000000..2d5c768 --- /dev/null +++ b/src/Config/Apps/Http/Server/Routes/Match/MatchProtocol.php @@ -0,0 +1,10 @@ + */ + private array $not = []; public function addNotMatcher(MatcherInterface $matcher): static { - if (!isset($this->_not)) { - $this->_not = [$matcher]; - } else { - $this->_not[] = $matcher; - } + $this->not[] = $matcher; return $this; } @@ -24,7 +21,7 @@ public function toArray(): array return [ 'not' => array_map(static function (MatcherInterface $matcher) { return $matcher->toArray(); - }, $this->_not), + }, $this->not), ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Match/Path.php b/src/Config/Apps/Http/Server/Routes/Match/Path.php index 25a245d..f4ee808 100644 --- a/src/Config/Apps/Http/Server/Routes/Match/Path.php +++ b/src/Config/Apps/Http/Server/Routes/Match/Path.php @@ -6,11 +6,16 @@ class Path implements MatcherInterface { - private array $_paths = []; + /** @var array */ + private array $paths = []; + /** + * @param array $paths + * @return $this + */ public function setPaths(array $paths): static { - $this->_paths = $paths; + $this->paths = $paths; return $this; } @@ -18,7 +23,7 @@ public function setPaths(array $paths): static public function toArray(): array { return [ - 'path' => $this->_paths, + 'path' => $this->paths, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Http/Server/Routes/Match/Protocol.php b/src/Config/Apps/Http/Server/Routes/Match/Protocol.php index 1a2d08b..8bc0849 100644 --- a/src/Config/Apps/Http/Server/Routes/Match/Protocol.php +++ b/src/Config/Apps/Http/Server/Routes/Match/Protocol.php @@ -4,26 +4,19 @@ use mattvb91\CaddyPhp\Interfaces\Apps\Servers\Routes\Match\MatcherInterface; -enum MatchProtocol: string -{ - public const HTTP = 'http'; - public const HTTPS = 'https'; - public const GRPC = 'grpc'; -} - class Protocol implements MatcherInterface { - private ?string $_protocol; + private ?string $protocol; public function __construct(string $protocol) { - $this->_protocol = $protocol; + $this->protocol = $protocol; } public function toArray(): array { return [ - 'protocol' => $this->_protocol, + 'protocol' => $this->protocol, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls.php b/src/Config/Apps/Tls.php index 74f170b..696380d 100644 --- a/src/Config/Apps/Tls.php +++ b/src/Config/Apps/Tls.php @@ -10,11 +10,11 @@ */ class Tls implements App { - private ?Automation $_automation; + private ?Automation $automation; public function setAutomation(Automation $automation): static { - $this->_automation = $automation; + $this->automation = $automation; return $this; } @@ -23,10 +23,10 @@ public function toArray(): array { $config = []; - if (isset($this->_automation)) { - $config['automation'] = $this->_automation->toArray(); + if (isset($this->automation)) { + $config['automation'] = $this->automation->toArray(); } return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation.php b/src/Config/Apps/Tls/Automation.php index 35c1e04..1e1709b 100644 --- a/src/Config/Apps/Tls/Automation.php +++ b/src/Config/Apps/Tls/Automation.php @@ -8,23 +8,24 @@ class Automation implements Arrayable { - private ?OnDemand $_onDemand; + private ?OnDemand $onDemand; - private ?array $_policies; + /** @var array|null */ + private ?array $policies; public function setOnDemand(OnDemand $onDemand): static { - $this->_onDemand = $onDemand; + $this->onDemand = $onDemand; return $this; } public function addPolicies(Policies $policies): static { - if (!isset($this->_policies)) { - $this->_policies = [$policies]; + if (!isset($this->policies)) { + $this->policies = [$policies]; } else { - $this->_policies[] = $policies; + $this->policies[] = $policies; } return $this; @@ -34,18 +35,16 @@ public function toArray(): array { $config = []; - if (isset($this->_onDemand)) { - $config['on_demand'] = $this->_onDemand->toArray(); + if (isset($this->onDemand)) { + $config['on_demand'] = $this->onDemand->toArray(); } - if (isset($this->_policies)) { + if (isset($this->policies)) { $config['policies'] = array_map(function (Policies $policies) { return $policies->toArray(); - }, $this->_policies); + }, $this->policies); } return $config; } - - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/OnDemand.php b/src/Config/Apps/Tls/Automation/OnDemand.php index 6994d41..2a3ed20 100644 --- a/src/Config/Apps/Tls/Automation/OnDemand.php +++ b/src/Config/Apps/Tls/Automation/OnDemand.php @@ -7,20 +7,20 @@ class OnDemand implements Arrayable { - private ?RateLimit $_rateLimit; + private ?RateLimit $rateLimit; - private ?string $_ask; + private ?string $ask; public function setRateLimit(RateLimit $rateLimit): static { - $this->_rateLimit = $rateLimit; + $this->rateLimit = $rateLimit; return $this; } public function setAsk(string $ask): static { - $this->_ask = $ask; + $this->ask = $ask; return $this; } @@ -29,15 +29,14 @@ public function toArray(): array { $config = []; - if (isset($this->_rateLimit)) { - $config['rate_limit'] = $this->_rateLimit->toArray(); + if (isset($this->rateLimit)) { + $config['rate_limit'] = $this->rateLimit->toArray(); } - if (isset($this->_ask)) { - $config['ask'] = $this->_ask; + if (isset($this->ask)) { + $config['ask'] = $this->ask; } return $config; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/OnDemand/RateLimit.php b/src/Config/Apps/Tls/Automation/OnDemand/RateLimit.php index 627604a..04e1968 100644 --- a/src/Config/Apps/Tls/Automation/OnDemand/RateLimit.php +++ b/src/Config/Apps/Tls/Automation/OnDemand/RateLimit.php @@ -6,20 +6,20 @@ class RateLimit implements Arrayable { - private ?string $_interval; + private ?string $interval; - private ?int $_burst; + private ?int $burst; public function setInterval(string $interval): static { - $this->_interval = $interval; + $this->interval = $interval; return $this; } - public function setBurst(string $burst): static + public function setBurst(int $burst): static { - $this->_burst = $burst; + $this->burst = $burst; return $this; } @@ -29,14 +29,14 @@ public function toArray(): array { $config = []; - if (isset($this->_interval)) { - $config['interval'] = $this->_interval; + if (isset($this->interval)) { + $config['interval'] = $this->interval; } - if (isset($this->_interval)) { - $config['burst'] = $this->_burst; + if (isset($this->interval)) { + $config['burst'] = $this->burst; } return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/Policies.php b/src/Config/Apps/Tls/Automation/Policies.php index 66326d9..fbdab3e 100644 --- a/src/Config/Apps/Tls/Automation/Policies.php +++ b/src/Config/Apps/Tls/Automation/Policies.php @@ -7,16 +7,18 @@ class Policies implements Arrayable { - private ?array $_subjects; + /** @var array|null */ + private ?array $subjects; - private ?array $_issuers; + /** @var array|null */ + private ?array $issuers; public function addSubjects(string $subject): static { - if (!isset($this->_subjects)) { - $this->_subjects = [$subject]; + if (!isset($this->subjects)) { + $this->subjects = [$subject]; } else { - $this->_subjects[] = $subject; + $this->subjects[] = $subject; } return $this; @@ -24,10 +26,10 @@ public function addSubjects(string $subject): static public function addIssuer(IssuerInterface $issuer): static { - if (!isset($this->_subjects)) { - $this->_issuers = [$issuer]; + if (!isset($this->subjects)) { + $this->issuers = [$issuer]; } else { - $this->_issuers[] = $issuer; + $this->issuers[] = $issuer; } return $this; @@ -37,18 +39,16 @@ public function toArray(): array { $config = []; - if (isset($this->_subjects)) { - $config['subjects'] = $this->_subjects; + if (isset($this->subjects)) { + $config['subjects'] = $this->subjects; } - if (isset($this->_issuers)) { + if (isset($this->issuers)) { $config['issuers'] = array_map(function (IssuerInterface $issuer) { return $issuer->toArray(); - }, $this->_issuers); + }, $this->issuers); } return $config; } - - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme.php b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme.php index 9a4272d..52e6905 100644 --- a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme.php +++ b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme.php @@ -7,20 +7,20 @@ class Acme implements IssuerInterface { - private ?string $_email; + private ?string $email; - private ?Challenges $_challenges; + private ?Challenges $challenges; public function setEmail(string $email): static { - $this->_email = $email; + $this->email = $email; return $this; } public function setChallenges(Challenges $challenges): static { - $this->_challenges = $challenges; + $this->challenges = $challenges; return $this; } @@ -31,12 +31,12 @@ public function toArray(): array 'module' => $this->getModuleName(), ]; - if (isset($this->_email)) { - $config['email'] = $this->_email; + if (isset($this->email)) { + $config['email'] = $this->email; } - if (isset($this->_challenges)) { - $config['challenges'] = $this->_challenges->toArray(); + if (isset($this->challenges)) { + $config['challenges'] = $this->challenges->toArray(); } return $config; @@ -46,4 +46,4 @@ public function getModuleName(): string { return 'acme'; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges.php b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges.php index a3c1fa8..de9ecc0 100644 --- a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges.php +++ b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges.php @@ -7,12 +7,11 @@ class Challenges implements Arrayable { - - private ?Dns $_dns; + private ?Dns $dns; public function setDns(Dns $dns): static { - $this->_dns = $dns; + $this->dns = $dns; return $this; } @@ -21,10 +20,10 @@ public function toArray(): array { $config = []; - if (isset($this->_dns)) { - $config['dns'] = $this->_dns->toArray(); + if (isset($this->dns)) { + $config['dns'] = $this->dns->toArray(); } return $config; } -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns.php b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns.php index afaaa4e..624134e 100644 --- a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns.php +++ b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns.php @@ -7,11 +7,11 @@ class Dns implements Arrayable { - private ?ProviderInterface $_provider; + private ?ProviderInterface $provider; public function setProvider(ProviderInterface $provider): static { - $this->_provider = $provider; + $this->provider = $provider; return $this; } @@ -20,11 +20,10 @@ public function toArray(): array { $config = []; - if (isset($this->_provider)) { - $config['provider'] = $this->_provider->toArray(); + if (isset($this->provider)) { + $config['provider'] = $this->provider->toArray(); } return $config; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/Provider/Route53.php b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/Provider/Route53.php index 20f9147..8786820 100644 --- a/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/Provider/Route53.php +++ b/src/Config/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/Provider/Route53.php @@ -6,11 +6,11 @@ class Route53 implements ProviderInterface { - private ?int $_maxRetries; + private ?int $maxRetries; public function setMaxRetries(int $maxRetries): static { - $this->_maxRetries = $maxRetries; + $this->maxRetries = $maxRetries; return $this; } @@ -21,8 +21,8 @@ public function toArray(): array 'name' => $this->getProviderName(), ]; - if (isset($this->_maxRetries)) { - $config['max_retries'] = $this->_maxRetries; + if (isset($this->maxRetries)) { + $config['max_retries'] = $this->maxRetries; } return $config; @@ -32,5 +32,4 @@ public function getProviderName(): string { return 'route53'; } - -} \ No newline at end of file +} diff --git a/src/Config/Apps/Tls/Automation/Policies/Issuers/Internal.php b/src/Config/Apps/Tls/Automation/Policies/Issuers/Internal.php new file mode 100644 index 0000000..a083606 --- /dev/null +++ b/src/Config/Apps/Tls/Automation/Policies/Issuers/Internal.php @@ -0,0 +1,20 @@ + $this->getModuleName(), + ]; + } +} diff --git a/src/Config/Logging.php b/src/Config/Logging.php index 53b73ab..e32b12e 100644 --- a/src/Config/Logging.php +++ b/src/Config/Logging.php @@ -14,15 +14,15 @@ */ class Logging implements Arrayable { - /** @var Log[] $_logs */ - private $_logs = []; + /** @var Log[] $logs */ + private $logs = []; - public function addLog(Log $log, ?string $name = 'default') + public function addLog(Log $log, ?string $name = 'default'): static { - if (array_key_exists('', $this->_logs)) { + if (array_key_exists('', $this->logs)) { throw new \Exception('Log with this name alread exists'); } - $this->_logs[$name] = $log; + $this->logs[$name] = $log; return $this; } @@ -33,10 +33,10 @@ public function toArray(): array array_map(function (string $key, Log $log) use (&$logs) { $logs[$key] = $log->toArray(); - }, array_keys($this->_logs), $this->_logs); + }, array_keys($this->logs), $this->logs); return [ 'logs' => $logs, ]; } -} \ No newline at end of file +} diff --git a/src/Config/Logs/Log.php b/src/Config/Logs/Log.php index 3b158e4..6420623 100644 --- a/src/Config/Logs/Log.php +++ b/src/Config/Logs/Log.php @@ -13,17 +13,16 @@ */ class Log implements Arrayable { + private LogLevel $level; - private LogLevel $_level; - - public function __construct(?LogLevel $level = LogLevel::DEBUG) + public function __construct(LogLevel $level = LogLevel::DEBUG) { - $this->_level = $level; + $this->level = $level; } public function getLevel(): LogLevel { - return $this->_level; + return $this->level; } public function toArray(): array @@ -32,4 +31,4 @@ public function toArray(): array 'level' => $this->getLevel(), ]; } -} \ No newline at end of file +} diff --git a/src/Config/Logs/LogLevel.php b/src/Config/Logs/LogLevel.php index be0db16..c71c57b 100644 --- a/src/Config/Logs/LogLevel.php +++ b/src/Config/Logs/LogLevel.php @@ -10,4 +10,4 @@ enum LogLevel: string case ERROR = "ERROR"; case PANIC = "PANIC"; case FATAL = "FATAL"; -} \ No newline at end of file +} diff --git a/src/Exceptions/CaddyClientException.php b/src/Exceptions/CaddyClientException.php index 7cd6f63..587cf08 100644 --- a/src/Exceptions/CaddyClientException.php +++ b/src/Exceptions/CaddyClientException.php @@ -4,5 +4,4 @@ class CaddyClientException extends \Exception { - -} \ No newline at end of file +} diff --git a/src/Functions.php b/src/Functions.php index 95fb495..8e014f1 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -2,18 +2,27 @@ namespace mattvb91\CaddyPhp; +use mattvb91\CaddyPhp\Config\Apps\Http; use mattvb91\CaddyPhp\Config\Apps\Http\Server\Routes\Match\Host; use mattvb91\CaddyPhp\Traits\IterableProps; /** - * Walk the config objects to find the host we need - * - * TODO this is pretty inefficient there must be a better way to gather this. + * @param string|array|object|string>|object $objectToWalk + * @param string $hostToFind + * @param string $path + * @return array{ + * path: string, + * host: Host + * }|null */ -function findHost($objectToWalk, $hostToFind, $path = '') +function findHost(string|array|object $objectToWalk, string $hostToFind, string $path = ''): ?array { if ($objectToWalk instanceof Host) { - if ($objectToWalk->getIdentifier() === $hostToFind && str_contains($path, 'routes') && str_contains($path, 'match')) { + if ( + $objectToWalk->getIdentifier() === $hostToFind && + str_contains($path, 'routes') && + str_contains($path, 'match') + ) { return [ 'path' => '/config/apps/http' . str_replace('_', '', $path) . '/host', 'host' => &$objectToWalk, @@ -22,14 +31,11 @@ function findHost($objectToWalk, $hostToFind, $path = '') } if (is_object($objectToWalk)) { - $canIterate = array_key_exists(IterableProps::class, class_uses($objectToWalk)); - - if ($canIterate) { + if (method_exists($objectToWalk, 'iterateAllProperties')) { $props = $objectToWalk->iterateAllProperties(); if ($found = findHost($props, $hostToFind, $path)) { return $found; } - } } @@ -40,4 +46,6 @@ function findHost($objectToWalk, $hostToFind, $path = '') } } } -} \ No newline at end of file + + return null; +} diff --git a/src/Interfaces/App.php b/src/Interfaces/App.php index bdd75c2..e75a27c 100644 --- a/src/Interfaces/App.php +++ b/src/Interfaces/App.php @@ -7,5 +7,4 @@ */ interface App extends Arrayable { - -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/ProviderInterface.php b/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/ProviderInterface.php index a8b4eee..cd2e0c6 100644 --- a/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/ProviderInterface.php +++ b/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/ProviderInterface.php @@ -7,5 +7,4 @@ interface ProviderInterface extends Arrayable { public function getModuleName(): string; - -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/Providers/HttpBasic/HashInterface.php b/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/Providers/HttpBasic/HashInterface.php index 3d53a5e..b37c0b8 100644 --- a/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/Providers/HttpBasic/HashInterface.php +++ b/src/Interfaces/Apps/Servers/Routes/Handle/Authentication/Providers/HttpBasic/HashInterface.php @@ -7,4 +7,4 @@ interface HashInterface extends Arrayable { public function getAlgorithm(): string; -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Servers/Routes/Handle/HandlerInterface.php b/src/Interfaces/Apps/Servers/Routes/Handle/HandlerInterface.php index 5f5078c..7757de9 100644 --- a/src/Interfaces/Apps/Servers/Routes/Handle/HandlerInterface.php +++ b/src/Interfaces/Apps/Servers/Routes/Handle/HandlerInterface.php @@ -7,4 +7,4 @@ interface HandlerInterface extends Arrayable { public function getHandler(): string; -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Servers/Routes/Handle/ReverseProxy/TransportInterface.php b/src/Interfaces/Apps/Servers/Routes/Handle/ReverseProxy/TransportInterface.php index 54ecd26..5da40f6 100644 --- a/src/Interfaces/Apps/Servers/Routes/Handle/ReverseProxy/TransportInterface.php +++ b/src/Interfaces/Apps/Servers/Routes/Handle/ReverseProxy/TransportInterface.php @@ -7,4 +7,4 @@ interface TransportInterface extends Arrayable { public function getProtocol(): string; -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Servers/Routes/Match/MatcherInterface.php b/src/Interfaces/Apps/Servers/Routes/Match/MatcherInterface.php index 15f8cae..c7fc9fb 100644 --- a/src/Interfaces/Apps/Servers/Routes/Match/MatcherInterface.php +++ b/src/Interfaces/Apps/Servers/Routes/Match/MatcherInterface.php @@ -6,5 +6,4 @@ interface MatcherInterface extends Arrayable { - -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Tls/Automation/Policies/IssuerInterface.php b/src/Interfaces/Apps/Tls/Automation/Policies/IssuerInterface.php index fc78462..14ddd65 100644 --- a/src/Interfaces/Apps/Tls/Automation/Policies/IssuerInterface.php +++ b/src/Interfaces/Apps/Tls/Automation/Policies/IssuerInterface.php @@ -7,4 +7,4 @@ interface IssuerInterface extends Arrayable { public function getModuleName(): string; -} \ No newline at end of file +} diff --git a/src/Interfaces/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/ProviderInterface.php b/src/Interfaces/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/ProviderInterface.php index 482469c..2a54d79 100644 --- a/src/Interfaces/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/ProviderInterface.php +++ b/src/Interfaces/Apps/Tls/Automation/Policies/Issuers/Acme/Challenges/Dns/ProviderInterface.php @@ -7,4 +7,4 @@ interface ProviderInterface extends Arrayable { public function getProviderName(): string; -} \ No newline at end of file +} diff --git a/src/Interfaces/Arrayable.php b/src/Interfaces/Arrayable.php index e500d01..d770293 100644 --- a/src/Interfaces/Arrayable.php +++ b/src/Interfaces/Arrayable.php @@ -4,5 +4,8 @@ interface Arrayable { + /** + * @return array + */ public function toArray(): array; -} \ No newline at end of file +} diff --git a/src/Traits/IterableProps.php b/src/Traits/IterableProps.php index 45ed9c3..26e8383 100644 --- a/src/Traits/IterableProps.php +++ b/src/Traits/IterableProps.php @@ -4,9 +4,11 @@ trait IterableProps { + /** + * @return array + */ public function iterateAllProperties(): array { return get_object_vars($this); } - -} \ No newline at end of file +} diff --git a/tests/Integration/CaddyTest.php b/tests/Integration/CaddyTest.php index 070ec23..23a5bee 100644 --- a/tests/Integration/CaddyTest.php +++ b/tests/Integration/CaddyTest.php @@ -21,7 +21,7 @@ class CaddyTest extends TestCase /** * @covers \mattvb91\CaddyPhp\Caddy::load */ - public function test_can_load_config(): void + public function testCanLoadConfig(): void { $caddy = new Caddy(); @@ -31,11 +31,12 @@ public function test_can_load_config(): void /** * @coversNothing */ - public function test_can_load_with_logs(): void + public function testCanLoadWithLogs(): void { $caddy = new Caddy(); - $caddy->setLogging((new Logging()) - ->addLog(new Log()) + $caddy->setLogging( + (new Logging()) + ->addLog(new Log()) ); $this->assertTrue($caddy->load()); @@ -44,14 +45,16 @@ public function test_can_load_with_logs(): void /** * @coversNothing */ - public function test_can_load_with_http_app(): void + public function testCanLoadWithHttpApp(): void { $caddy = new Caddy(); $caddy->addApp( (new Http())->addServer( - 'server1', (new Http\Server())->addRoute( - (new Route()) - )) + 'server1', + (new Http\Server())->addRoute( + (new Route()) + ) + ) ); $this->assertTrue($caddy->load()); @@ -60,16 +63,18 @@ public function test_can_load_with_http_app(): void /** * @coversNothing */ - public function test_can_load_static_response_app(): void + public function testCanLoadStaticResponseApp(): void { $caddy = new Caddy(); $caddy->addApp( (new Http())->addServer( - 'server1', (new Http\Server())->addRoute( - (new Route())->addHandle( - new StaticResponse('phpunit', 200) + 'server1', + (new Http\Server())->addRoute( + (new Route())->addHandle( + new StaticResponse('phpunit', 200) + ) ) - )) + ) ); $this->assertTrue($caddy->load()); @@ -89,23 +94,28 @@ public function test_can_load_static_response_app(): void * @covers \mattvb91\CaddyPhp\Caddy::removeHostname * @covers \mattvb91\CaddyPhp\findHost */ - public function test_can_add_remove_hosts() + public function testCanAddRemoveHosts() { $caddy = new Caddy(); $caddy->addApp( (new Http())->addServer( - 'server1', (new Http\Server())->addRoute( - (new Route())->addHandle( - new StaticResponse('host test', 200) - )->addMatch((new Host('main')) - ->setHosts(['test.localhost']) - ) - )->addRoute((new Route()) - ->addHandle(new StaticResponse('Not found', 404)) - ->addMatch((new Host('notFound')) - ->setHosts(['*.localhost']) + 'server1', + (new Http\Server())->addRoute( + (new Route())->addHandle( + new StaticResponse('host test', 200) + )->addMatch( + (new Host('main')) + ->setHosts(['test.localhost']) + ) + )->addRoute( + (new Route()) + ->addHandle(new StaticResponse('Not found', 404)) + ->addMatch( + (new Host('notFound')) + ->setHosts(['*.localhost']) + ) ) - )) + ) ); $this->assertTrue($caddy->load()); @@ -159,22 +169,25 @@ public function test_can_add_remove_hosts() /** * @covers \mattvb91\CaddyPhp\Caddy::syncHosts */ - public function test_sync_hosts_works() + public function testSyncHostsWorks() { $caddy = new Caddy(); $caddy->addApp( (new Http())->addServer( - 'server1', (new Http\Server())->addRoute( - (new Route())->addHandle( - new StaticResponse('host test', 200) - )->addMatch((new Host('main')) - ->setHosts([ - 'test.localhost', - 'test2.localhost', - 'localhost', - ]) + 'server1', + (new Http\Server())->addRoute( + (new Route())->addHandle( + new StaticResponse('host test', 200) + )->addMatch( + (new Host('main')) + ->setHosts([ + 'test.localhost', + 'test2.localhost', + 'localhost', + ]) + ) ) - )) + ) ); $caddy->load(); @@ -183,11 +196,13 @@ public function test_sync_hosts_works() $mainHost = new Host('main'); $caddy->addApp( (new Http())->addServer( - 'server1', (new Http\Server())->addRoute( - (new Route())->addHandle( - new StaticResponse('host test', 200) - )->addMatch($mainHost) - )) + 'server1', + (new Http\Server())->addRoute( + (new Route())->addHandle( + new StaticResponse('host test', 200) + )->addMatch($mainHost) + ) + ) ); $caddy->syncHosts('main'); @@ -197,24 +212,31 @@ public function test_sync_hosts_works() /** * @coversNothing */ - public function test_http_basic_auth() + public function testHttpBasicAuth() { $caddy = new Caddy(); $caddy->addApp( (new Http())->addServer( - 'server1', (new Http\Server())->addRoute( - (new Route()) - ->addHandle((new Authentication()) - ->addProvider((new HttpBasic()) - ->addAccount(new Account('test', 'test123')))) - ->addHandle( - new StaticResponse('auth test', 200) - )->addMatch((new Host('main')) - ->setHosts([ - 'localhost', - ]) - ) - )) + 'server1', + (new Http\Server())->addRoute( + (new Route()) + ->addHandle( + (new Authentication()) + ->addProvider( + (new HttpBasic()) + ->addAccount(new Account('test', 'test123')) + ) + ) + ->addHandle( + new StaticResponse('auth test', 200) + )->addMatch( + (new Host('main')) + ->setHosts([ + 'localhost', + ]) + ) + ) + ) ); $caddy->load(); @@ -236,7 +258,6 @@ public function test_http_basic_auth() ], ]); $this->assertEquals(200, $request->getStatusCode()); - } /** @@ -278,16 +299,22 @@ public function test_http_basic_auth() /** * @covers \mattvb91\CaddyPhp\Caddy::getRemoteConfig */ - public function test_caddy_get_config() + public function testCaddyGetConfig() { $caddy = new Caddy(); - $caddy->addApp((new Http()) - ->addServer('test', (new Http\Server()) - ->addRoute((new Route()) - ->addHandle((new StaticResponse('test')))))); + $caddy->addApp( + (new Http()) + ->addServer( + 'test', + (new Http\Server()) + ->addRoute( + (new Route()) + ->addHandle((new StaticResponse('test'))) + ) + ) + ); $caddy->load(); $this->assertEquals(json_decode(json_encode($caddy->toArray())), $caddy->getRemoteConfig()); } - -} \ No newline at end of file +} diff --git a/tests/Unit/Apps/TlsTest.php b/tests/Unit/Apps/TlsTest.php index ddd2980..83f4f94 100644 --- a/tests/Unit/Apps/TlsTest.php +++ b/tests/Unit/Apps/TlsTest.php @@ -32,6 +32,9 @@ public function test_tls_app() ) ) ) + )->addPolicies((new Tls\Automation\Policies()) + ->addSubjects('test.local') + ->addIssuer(new Tls\Automation\Policies\Issuers\Internal()) ) ); @@ -63,6 +66,16 @@ public function test_tls_app() ], ], ], + [ + 'subjects' => [ + 'test.local', + ], + 'issuers' => [ + [ + 'module' => 'internal', + ], + ], + ], ], ], ], $tls->toArray()); diff --git a/tests/Unit/CacheTest.php b/tests/Unit/CacheTest.php index 34f625d..26659cf 100644 --- a/tests/Unit/CacheTest.php +++ b/tests/Unit/CacheTest.php @@ -18,19 +18,21 @@ class CacheTest extends TestCase * @covers \mattvb91\CaddyPhp\Config\Apps\Cache\Api\Souin::toArray * @covers \mattvb91\CaddyPhp\Config\Apps\Cache\Api\Souin::setEnable */ - public function test_api() + public function testApi() { - $api = new Api((new Api\Souin()) - ->setBasePath('/test2') - ->setEnable(false)); + $api = new Api( + (new Api\Souin()) + ->setBasePath('/test2') + ->setEnable(false) + ); $api->setBasePath('/test'); $this->assertEquals([ 'basepath' => '/test', 'souin' => [ 'basepath' => '/test2', - 'enable' => false - ] + 'enable' => false, + ], ], $api->toArray()); } -} \ No newline at end of file +} diff --git a/tests/Unit/CaddyTest.php b/tests/Unit/CaddyTest.php index f7685a1..3518ff1 100644 --- a/tests/Unit/CaddyTest.php +++ b/tests/Unit/CaddyTest.php @@ -26,7 +26,7 @@ class CaddyTest extends TestCase * @covers \mattvb91\CaddyPhp\Caddy::getClient * @covers \mattvb91\CaddyPhp\Caddy::__construct */ - public function test_can_instantiate(): void + public function testCanInstantiate(): void { $caddy = new Caddy(); $this->assertInstanceOf(Caddy::class, $caddy); @@ -49,13 +49,14 @@ public function test_can_instantiate(): void * @covers \mattvb91\CaddyPhp\Config\Apps\Http::setGracePeriod * @covers \mattvb91\CaddyPhp\Config\Apps\Http::toArray */ - public function test_can_add_app() + public function testCanAddApp() { $caddy = new Caddy(); - $caddy->addApp((new Http()) - ->setHttpPort(1) - ->setHttpsPort(2) - ->setGracePeriod(3) + $caddy->addApp( + (new Http()) + ->setHttpPort(1) + ->setHttpsPort(2) + ->setGracePeriod(3) )->addApp(new Tls()); self::assertArraySubset([ @@ -74,11 +75,17 @@ public function test_can_add_app() /** * @covers \mattvb91\CaddyPhp\Caddy::load */ - public function test_client_exception() + public function testClientException() { /** @var MockObject|Caddy $mockClient */ $mockClient = $this->createPartialMock(Client::class, ['post']); - $mockClient->method('post')->willThrowException(new ClientException('error', new Request('post', '/'), new Response(500))); + $mockClient->method('post')->willThrowException( + new ClientException( + 'error', + new Request('post', '/'), + new Response(500) + ) + ); $this->expectException(CaddyClientException::class); $caddy = new Caddy(client: $mockClient); @@ -91,7 +98,7 @@ public function test_client_exception() * @covers \mattvb91\CaddyPhp\Config\Admin::toArray * @covers \mattvb91\CaddyPhp\Config\Admin::getListen */ - public function test_admin() + public function testAdmin() { $admin = (new Admin()) ->setDisabled(true) @@ -108,7 +115,7 @@ public function test_admin() * @covers \mattvb91\CaddyPhp\Config\Apps\Http\Server::setListen * @covers \mattvb91\CaddyPhp\Config\Apps\Http\Server::addRoute */ - public function test_server() + public function testServer() { $server = (new Http\Server()) ->setListen([':122']) @@ -123,4 +130,4 @@ public function test_server() ], ], $server->toArray()); } -} \ No newline at end of file +} diff --git a/tests/Unit/LoggingTest.php b/tests/Unit/LoggingTest.php index 7a050af..0a09632 100644 --- a/tests/Unit/LoggingTest.php +++ b/tests/Unit/LoggingTest.php @@ -14,12 +14,11 @@ class LoggingTest extends TestCase * @covers \mattvb91\CaddyPhp\Caddy::setLogging * @covers \mattvb91\CaddyPhp\Caddy::toArray */ - public function test_adding_default_log() + public function testAddingDefaultLog() { $caddy = new Caddy(); $caddy->setLogging((new Logging()) - ->addLog(new Log()) - ); + ->addLog(new Log())); $this->assertEquals([ 'logs' => [ @@ -29,5 +28,4 @@ public function test_adding_default_log() ] ], $caddy->toArray()['logging']); } - -} \ No newline at end of file +} diff --git a/tests/Unit/MiscTest.php b/tests/Unit/MiscTest.php index 96c6de8..cb64dc6 100644 --- a/tests/Unit/MiscTest.php +++ b/tests/Unit/MiscTest.php @@ -4,6 +4,7 @@ use mattvb91\CaddyPhp\Config\Apps\Http; use PHPUnit\Framework\TestCase; + use function mattvb91\CaddyPhp\findHost; class MiscTest extends TestCase @@ -11,7 +12,7 @@ class MiscTest extends TestCase /** * @covers \mattvb91\CaddyPhp\findHost */ - public function test_finding_host() + public function testFindingHost() { $http = new Http(); $server = new Http\Server(); @@ -33,5 +34,4 @@ public function test_finding_host() $this->assertEquals($host, findHost($http, 'shops')['host']); } - -} \ No newline at end of file +}