Skip to content

Commit

Permalink
phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvb91 committed Nov 1, 2023
1 parent 5147c2d commit 511e39f
Show file tree
Hide file tree
Showing 30 changed files with 170 additions and 97 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ jobs:
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
env:
REPO_NAME: ${{ github.event.repository.name }}
Expand Down
8 changes: 8 additions & 0 deletions codesniffer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" ?>
<ruleset name="PHP-Caddy">
<description>PSR-12 with array indenting</description>
<rule ref="PSR12"/>
<rule ref="Generic.Arrays.ArrayIndent">
<exclude name="Generic.Arrays.ArrayIndent.CloseBRaceNotNewLine"/>
</rule>
</ruleset>
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"description": "Control your Caddy instance through PHP",
"type": "package",
"license": "MIT",
"scripts": {
"phpstan": "phpstan analyse",
"codesniffer": "phpcs ./src ./tests/**/*.php --standard=./codesniffer.xml -p",
"codefixer": "phpcf ./src ./tests/**/*.php --standard=./codesniffer.xml"
},
"autoload": {
"psr-4": {
"mattvb91\\CaddyPhp\\": "src/"
Expand All @@ -17,11 +22,13 @@
}
},
"require": {
"php": "^8.0",
"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"
}
}
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 9
paths:
- src
excludePaths:
- vendor
- tests
65 changes: 40 additions & 25 deletions src/Caddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,10 @@ 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<string, array{
* path: string,
* host: Host
* }>
*/
private array $_hostsCache = [];

Expand All @@ -32,41 +38,41 @@ class Caddy implements Arrayable
private ?Logging $_logging;

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

private string $_hostname;

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',
],
]
);
'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
*/
public function syncHosts(string $hostIdentifier)
public function syncHosts(string $hostIdentifier): void
{
$this->buildHostsCache($hostIdentifier);

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

$this->_hostsCache[$hostIdentifier]['host']->setHosts($hosts);
Expand Down Expand Up @@ -114,14 +120,20 @@ 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
{
/** @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.
*
* @throws GuzzleException
* @param string[] $surrogates
*/
public function flushSurrogates(array $surrogates): bool
{
Expand Down Expand Up @@ -158,7 +170,7 @@ public function getClient(): Client
return $this->_client;
}

public function getAdmin(): ?Admin
public function getAdmin(): Admin
{
return $this->_admin;
}
Expand All @@ -170,7 +182,7 @@ protected function setAdmin(Admin $admin): static
return $this;
}

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

Expand All @@ -179,13 +191,11 @@ public function setLogging(Logging $logging)

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;
}
Expand All @@ -202,7 +212,7 @@ public function toArray(): array
$config['logging'] = $this->_logging->toArray();
}

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

array_map(static function (App $app, string $appNamespace) use (&$apps) {
Expand All @@ -215,12 +225,17 @@ 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 = '';
$hostPath = null;
foreach ($this->_apps as $app) {
if ($found = findHost($app, $hostIdentifier)) {
$hostPath = $found;
Expand Down
23 changes: 0 additions & 23 deletions src/Config/Apps.php

This file was deleted.

11 changes: 10 additions & 1 deletion src/Config/Apps/Http/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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;

Expand All @@ -13,6 +13,7 @@ class Server implements Arrayable
{
use IterableProps;

/** @var string[] */
private array $_listen = [':80'];

/** @var Route[] */
Expand All @@ -30,6 +31,10 @@ class Server implements Arrayable

private bool $_strictSniHost;

/**
* @param string[] $listen
* @return $this
*/
public function setListen(array $listen): static
{
$this->_listen = $listen;
Expand All @@ -44,6 +49,10 @@ public function addRoute(Route $route): static
return $this;
}

/**
* @param Route[] $routes
* @return $this
*/
public function setRoutes(array $routes): static
{
$this->_routes = $routes;
Expand Down
3 changes: 2 additions & 1 deletion src/Config/Apps/Http/Server/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +24,7 @@ class Route implements Arrayable
/** @var HandlerInterface[] */
private array $_handle = [];

/** @var MatcherInterface[]|null */
private ?array $_match;

private ?bool $_terminal;
Expand Down
11 changes: 4 additions & 7 deletions src/Config/Apps/Http/Server/Routes/Handle/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -29,7 +26,7 @@ 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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@

class HttpBasic implements ProviderInterface
{
private ?array $_accounts;
/** @var Account[] */
private array $_accounts = [];

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;
}

Expand All @@ -33,7 +31,7 @@ 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)];
Expand Down
7 changes: 7 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Cache implements HandlerInterface
{
private ?LogLevel $_logLevel;

/**
* @var string[]|null
*/
private ?array $_allowedHttpVerbs;

private ?string $_defaultCacheControl;
Expand All @@ -20,6 +23,10 @@ public function setLogLevel(?LogLevel $logLevel): static
return $this;
}

/**
* @param string[] $allowedHttpVerbs
* @return $this
*/
public function setAllowedHttpVerbs(array $allowedHttpVerbs): static
{
$this->_allowedHttpVerbs = $allowedHttpVerbs;
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Apps/Http/Server/Routes/Handle/FileServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class FileServer implements HandlerInterface
{
private $_root = "";
private string $_root = "";

public function setRoot(string $root): static
{
Expand Down
Loading

0 comments on commit 511e39f

Please sign in to comment.