-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added compiler extension `AmpClientExtension` for Nette DIC - added cache storage implementation `NetteCacheStorage` - fixed errors in codebase - added more tests - added PHP extension `uopz` for images in Dockerfile (for testing purposes only) - added dev dependencies on `nette/bootstrap`, `nette/caching`, `nette/di`, `psr/log` and `slope-it/clock-mock` - added GH action for code coverage
- Loading branch information
Showing
36 changed files
with
2,095 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Coverage | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
tags: | ||
- v* | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
coverage: | ||
name: Coverage | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.1 | ||
coverage: pcov | ||
extensions: tokenizer, uopz | ||
tools: composer:v2 | ||
|
||
- name: Install dependencies | ||
run: composer update --no-progress --prefer-dist --prefer-stable --optimize-autoloader | ||
|
||
- name: Generate the coverage report | ||
run: vendor/bin/tester -C -s --coverage ./coverage.xml --coverage-src ./src ./tests | ||
|
||
- name: Upload the coverage report | ||
env: | ||
COVERALLS_REPO_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | ||
run: | | ||
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.5.3/php-coveralls.phar | ||
php php-coveralls.phar --verbose --config tests/.coveralls.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Bridge\Nette\DI; | ||
|
||
use Nette\DI\CompilerExtension; | ||
use Nette\DI\Definitions\ServiceDefinition; | ||
use Nette\DI\Definitions\Statement; | ||
use Nette\Schema\Expect; | ||
use Nette\Schema\Schema; | ||
use SixtyEightPublishers\AmpClient\AmpClient; | ||
use SixtyEightPublishers\AmpClient\AmpClientInterface; | ||
use SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config\AmpClientConfig; | ||
use SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config\CacheConfig; | ||
use SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config\HttpConfig; | ||
use SixtyEightPublishers\AmpClient\Bridge\Nette\NetteCacheStorage; | ||
use SixtyEightPublishers\AmpClient\ClientConfig; | ||
use SixtyEightPublishers\AmpClient\Http\Cache\CacheStorageInterface; | ||
use SixtyEightPublishers\AmpClient\Http\Cache\NoCacheStorage; | ||
use SixtyEightPublishers\AmpClient\Http\HttpClientFactory; | ||
use SixtyEightPublishers\AmpClient\Http\HttpClientFactoryInterface; | ||
use SixtyEightPublishers\AmpClient\Request\ValueObject\BannerResource; | ||
use SixtyEightPublishers\AmpClient\Response\Hydrator\BannersResponseHydratorHandler; | ||
use SixtyEightPublishers\AmpClient\Response\Hydrator\ResponseHydrator; | ||
use SixtyEightPublishers\AmpClient\Response\Hydrator\ResponseHydratorHandlerInterface; | ||
use SixtyEightPublishers\AmpClient\Response\Hydrator\ResponseHydratorInterface; | ||
use function array_values; | ||
use function assert; | ||
use function count; | ||
|
||
final class AmpClientExtension extends CompilerExtension | ||
{ | ||
public function getConfigSchema(): Schema | ||
{ | ||
return Expect::structure([ | ||
'method' => Expect::anyOf(...ClientConfig::Methods) | ||
->dynamic(), | ||
'url' => Expect::string() | ||
->required() | ||
->dynamic(), | ||
'channel' => Expect::string() | ||
->required() | ||
->dynamic(), | ||
'version' => Expect::anyOf(...ClientConfig::Versions) | ||
->dynamic(), | ||
'locale' => Expect::string() | ||
->nullable() | ||
->dynamic(), | ||
'default_resources' => Expect::arrayOf( | ||
Expect::anyOf(Expect::string(), Expect::listOf('string')), | ||
Expect::string(), | ||
), | ||
'origin' => Expect::string() | ||
->nullable() | ||
->dynamic(), | ||
'cache' => Expect::structure([ | ||
'storage' => Expect::anyOf(Expect::string(), Expect::type(Statement::class)) | ||
->nullable() | ||
->before(static function ($factory): Statement { | ||
return $factory instanceof Statement ? $factory : new Statement($factory); | ||
}), | ||
'expiration' => Expect::anyOf(Expect::string(), Expect::int())->dynamic(), | ||
'cache_control_header_override' => Expect::string() | ||
->nullable(), | ||
])->castTo(CacheConfig::class), | ||
'http' => Expect::structure([ | ||
'guzzle_config' => Expect::array(), | ||
])->castTo(HttpConfig::class), | ||
])->castTo(AmpClientConfig::class); | ||
} | ||
|
||
public function loadConfiguration(): void | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
$config = $this->getConfig(); | ||
assert($config instanceof AmpClientConfig); | ||
|
||
$builder->addDefinition($this->prefix('config')) | ||
->setAutowired(false) | ||
->setType(ClientConfig::class) | ||
->setCreator($this->createClientConfigCreator($config)); | ||
|
||
$cacheStorageCreator = null === $config->cache->storage | ||
? new Statement(NoCacheStorage::class) | ||
: new Statement(NetteCacheStorage::class, [ | ||
'storage' => $config->cache->storage, | ||
]); | ||
|
||
$builder->addDefinition($this->prefix('cacheStorage')) | ||
->setAutowired(false) | ||
->setType(CacheStorageInterface::class) | ||
->setCreator($cacheStorageCreator); | ||
|
||
$builder->addDefinition($this->prefix('responseHydrator')) | ||
->setAutowired(false) | ||
->setType(ResponseHydratorInterface::class) | ||
->setCreator(ResponseHydrator::class); | ||
|
||
$builder->addDefinition($this->prefix('responseHydrator.handler.bannersRequest')) | ||
->setAutowired(false) | ||
->setType(ResponseHydratorHandlerInterface::class) | ||
->setCreator(BannersResponseHydratorHandler::class); | ||
|
||
$builder->addDefinition($this->prefix('httpClientFactory')) | ||
->setAutowired(false) | ||
->setType(HttpClientFactoryInterface::class) | ||
->setCreator(HttpClientFactory::class, [ | ||
'responseHydrator' => $this->prefix('@responseHydrator'), | ||
'guzzleClientConfig' => $config->http->guzzle_config, | ||
]); | ||
|
||
$builder->addDefinition($this->prefix('ampClient')) | ||
->setType(AmpClientInterface::class) | ||
->setCreator(AmpClient::class, [ | ||
'config' => $this->prefix('@config'), | ||
'httpClientFactory' => $this->prefix('@httpClientFactory'), | ||
'cacheStorage' => $this->prefix('@cacheStorage'), | ||
]); | ||
} | ||
|
||
public function beforeCompile(): void | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$responseHydratorHandlers = $builder->findByType(ResponseHydratorHandlerInterface::class); | ||
$responseHydratorService = $builder->getDefinition($this->prefix('responseHydrator')); | ||
assert($responseHydratorService instanceof ServiceDefinition); | ||
|
||
$responseHydratorService->setArgument('handlers', array_values($responseHydratorHandlers)); | ||
} | ||
|
||
private function createClientConfigCreator(AmpClientConfig $config): Statement | ||
{ | ||
$clientConfigFactory = new Statement([ClientConfig::class, 'create'], [ | ||
'url' => $config->url, | ||
'channel' => $config->channel, | ||
]); | ||
|
||
if (null !== $config->method) { | ||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withMethod'], [ | ||
'method' => $config->method, | ||
]); | ||
} | ||
|
||
if (null !== $config->version) { | ||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withVersion'], [ | ||
'version' => $config->version, | ||
]); | ||
} | ||
|
||
if (null !== $config->locale) { | ||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withLocale'], [ | ||
'locale' => $config->locale, | ||
]); | ||
} | ||
|
||
if (0 < count($config->default_resources)) { | ||
$defaultResources = []; | ||
|
||
foreach ($config->default_resources as $resourceCode => $resourceValues) { | ||
$defaultResources[] = new Statement(BannerResource::class, [$resourceCode, $resourceValues]); | ||
} | ||
|
||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withDefaultResources'], [ | ||
'resources' => $defaultResources, | ||
]); | ||
} | ||
|
||
if (null !== $config->origin) { | ||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withOrigin'], [ | ||
'origin' => $config->origin, | ||
]); | ||
} | ||
|
||
if (null !== $config->cache->expiration) { | ||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withCacheExpiration'], [ | ||
'cacheExpiration' => $config->cache->expiration, | ||
]); | ||
} | ||
|
||
if (null !== $config->cache->cache_control_header_override) { | ||
$clientConfigFactory = new Statement([$clientConfigFactory, 'withCacheControlHeaderOverride'], [ | ||
'cacheControlHeaderOverride' => $config->cache->cache_control_header_override, | ||
]); | ||
} | ||
|
||
return $clientConfigFactory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config; | ||
|
||
use Nette\DI\Definitions\Statement; | ||
|
||
final class AmpClientConfig | ||
{ | ||
public ?string $method = null; | ||
|
||
public string $url; | ||
|
||
public string $channel; | ||
|
||
public ?int $version = null; | ||
|
||
public ?string $locale = null; | ||
|
||
/** @var array<int, Statement> */ | ||
public array $default_resources = []; | ||
|
||
public ?string $origin = null; | ||
|
||
public CacheConfig $cache; | ||
|
||
public HttpConfig $http; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config; | ||
|
||
use Nette\DI\Definitions\Statement; | ||
|
||
final class CacheConfig | ||
{ | ||
public ?Statement $storage; | ||
|
||
/** @var string|int|null */ | ||
public $expiration = null; | ||
|
||
public ?string $cache_control_header_override = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config; | ||
|
||
final class HttpConfig | ||
{ | ||
/** @var array<string, mixed> */ | ||
public array $guzzle_config = []; | ||
} |
Oops, something went wrong.