Skip to content
Open

Main #34

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ jobs:
coverage: ['none']
symfony-versions:
- '6.4.*'
- '7.0.*'
- '7.3.*'
- '7.4.*'
- '8.0.*'
exclude:
- php: '8.3'
symfony-versions: '8.0.*'
include:
- description: 'Log Code Coverage'
php: '8.3'
symfony-versions: '^7.0'
symfony-versions: '^6.4'
doctrine-orm-versions: '^3.0'
coverage: xdebug

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: composer install --no-progress --no-interaction --prefer-dist

- name: Run script
run: vendor/bin/phpstan analyse
run: vendor/bin/phpstan analyse --configuration=./phpstan.neon

composer-validate:
name: Composer validate
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
}
},
"require": {
"php": ">=8.3",
"symfony/messenger": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/config": "^6.4 || ^7.0",
"symfony/http-client": "^6.4 || ^7.0"
"php": ">=8.3 || >=8.4",
"symfony/messenger": "^6.4 || ^7.4 || ^8.0",
"symfony/http-kernel": "^6.4 || ^7.4 || ^8.0",
"symfony/dependency-injection": "^6.4 || ^7.4 || ^8.0",
"symfony/config": "^6.4 || ^7.4 || ^8.0",
"symfony/http-client": "^6.4 || ^7.4 || ^8.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^10.0",
"squizlabs/php_codesniffer": "3.7.*"
},
Expand All @@ -44,7 +44,7 @@
"vendor/bin/phpcbf"
],
"phpstan": [
"vendor/bin/phpstan analyse"
"vendor/bin/phpstan analyse --memory-limit=512M"
],
"phpunit": [
"vendor/bin/phpunit"
Expand Down
6 changes: 6 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
$environmentSchema: '%schema_context.environment_schema%'
$environmentName: '%schema_context.environment_name%'
$schemaOverridableEnvironments: '%schema_context.overridable_environments%'
$logger: '@Macpaw\SchemaContextBundle\Logger\DebugLogger'
public: true
shared: true

Expand All @@ -20,6 +21,7 @@ services:
arguments:
$baggageSchemaResolver: '@Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver'
$schemaRequestHeader: '%schema_context.header_name%'
$logger: '@Macpaw\SchemaContextBundle\Logger\DebugLogger'
tags:
- { name: kernel.event_subscriber }

Expand All @@ -28,3 +30,7 @@ services:
$sendersLocator: '@messenger.senders_locator'
tags:
- { name: messenger.middleware }

Macpaw\SchemaContextBundle\Logger\DebugLogger:
arguments:
$logger: null
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ parameters:
paths:
- src
- tests
ignoreErrors:
-
message: '~Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).~'
count: 1
path: ./src/DependencyInjection
32 changes: 27 additions & 5 deletions src/DependencyInjection/SchemaContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,44 @@

namespace Macpaw\SchemaContextBundle\DependencyInjection;

use LogicException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class SchemaContextExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('schema_context.header_name', $config['header_name']);
$container->setParameter('schema_context.environment_schema', $config['environment_schema']);
$container->setParameter('schema_context.environment_name', $config['environment_name']);
$container->setParameter('schema_context.overridable_environments', $config['overridable_environments']);
$headerName = $config['header_name'];
$environmentSchema = $config['environment_schema'];
$environmentName = $config['environment_name'];
$overridableEnvironments = $config['overridable_environments'];

if (!is_string($headerName)) {
throw new LogicException('Configuration "header_name" must be a string');
}

if (!is_string($environmentSchema)) {
throw new LogicException('Configuration "environment_schema" must be a string');
}

if (!is_string($environmentName)) {
throw new LogicException('Configuration "environment_name" must be a string');
}

if (!is_array($overridableEnvironments)) {
throw new LogicException('Configuration "overridable_environments" must be an array');
}

$container->setParameter('schema_context.header_name', $headerName);
$container->setParameter('schema_context.environment_schema', $environmentSchema);
$container->setParameter('schema_context.environment_name', $environmentName);
$container->setParameter('schema_context.overridable_environments', $overridableEnvironments);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));

Expand Down
3 changes: 3 additions & 0 deletions src/EventListener/BaggageRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Macpaw\SchemaContextBundle\EventListener;

use Macpaw\SchemaContextBundle\Logger\DebugLogger;
use Macpaw\SchemaContextBundle\Service\BaggageCodec;
use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -16,6 +17,7 @@ public function __construct(
private BaggageSchemaResolver $baggageSchemaResolver,
private BaggageCodec $baggageCodec,
private string $schemaRequestHeader,
private DebugLogger $logger,
) {
}

Expand All @@ -37,6 +39,7 @@ public function onKernelRequest(RequestEvent $event): void
$schema = $baggage[$this->schemaRequestHeader] ?? null;
}

$this->logger->logInfoFromRequest($baggage, $schema);
$this->baggageSchemaResolver->setBaggage($baggage);
$this->baggageSchemaResolver->setSchema($schema);
}
Expand Down
8 changes: 6 additions & 2 deletions src/HttpClient/BaggageAwareHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Macpaw\SchemaContextBundle\HttpClient;

use Macpaw\SchemaContextBundle\Logger\DebugLogger;
use Macpaw\SchemaContextBundle\Service\BaggageCodec;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand All @@ -16,6 +17,7 @@ public function __construct(
private HttpClientInterface $inner,
private BaggageSchemaResolver $baggageSchemaResolver,
private BaggageCodec $baggageCodec,
private DebugLogger $logger,
) {
}

Expand All @@ -28,7 +30,7 @@ public function request(string $method, string $url, array $options = []): Respo
? $options['headers']
: [];

$baggage = isset($headers['baggage'])
$baggage = isset($headers['baggage']) && is_string($headers['baggage'])
? $this->baggageCodec->decode($headers['baggage'])
: [];

Expand All @@ -40,6 +42,8 @@ public function request(string $method, string $url, array $options = []): Respo
$headers['baggage'] = $this->baggageCodec->encode($baggage);
$options['headers'] = $headers;

$this->logger->logHttpRequest($baggage);

return $this->inner->request($method, $url, $options);
}

Expand All @@ -56,6 +60,6 @@ public function withOptions(array $options): static
$wrapped = $this->inner->withOptions($options);

/** @phpstan-ignore-next-line */
return new self($wrapped, $this->baggageSchemaResolver, $this->baggageCodec);
return new self($wrapped, $this->baggageSchemaResolver, $this->baggageCodec, $this->logger);
}
}
87 changes: 87 additions & 0 deletions src/Logger/DebugLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Macpaw\SchemaContextBundle\Logger;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class DebugLogger
{
private readonly LoggerInterface $logger;

public function __construct(
LoggerInterface|null $logger = null,
) {
$this->logger = $logger ?? new NullLogger();
}

/** @param array<string, string|null>|null $baggage */
public function log(string $message, ?array $baggage = null, ?string $schema = null): void
{
$this->logger->info(
'schema_context_' . $message,
[
'schema' => $schema,
'baggage' => $baggage,
]
);
}

/** @param array<string, string|null>|null $baggage */
public function logInfoFromRequest(array|null $baggage, string|null $schema): void
{
$this->log('info_from_request', $baggage, $schema);
}

/** @param array<string, string|null> $baggage */
public function logHttpRequest(array $baggage): void
{
$this->log('http_request', $baggage);
}

/** @param array<string, string|null>|null $baggage */
public function logSetBaggage(array|null $baggage): void
{
$this->log('set_baggage', $baggage);
}

public function logSetSchema(string|null $schema): void
{
$this->log('set_schema', null, $schema);
}

/** @param array<string, string|null>|null $baggage */
public function logInfoFromStamp(array|null $baggage, string|null $schema): void
{
$this->log('info_from_stamp', $baggage, $schema);
}

public function logResetWorkerAfterWorker(): void
{
$this->log('reset_worker_after_worker');
}

/** @param array<string, string|null>|null $baggage */
public function logCreateMessage(array|null $baggage, string|null $schema): void
{
$this->log('create_message', $baggage, $schema);
}

public function logApplySearchPath(string $schema, bool $isNewConnection): void
{
$this->logger->info('schema_context_apply_search_path', [
'schema' => $schema,
'is_new_connection' => $isNewConnection,
]);
}

public function logSkipSearchPath(string $schema, string|null $actualScheme): void
{
$this->logger->info('schema_context_skip_search_path', [
'schema' => $schema,
'actualScheme' => $actualScheme,
]);
}
}
26 changes: 19 additions & 7 deletions src/Messenger/Middleware/BaggageSchemaMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Macpaw\SchemaContextBundle\Messenger\Middleware;

use Macpaw\SchemaContextBundle\Logger\DebugLogger;
use Macpaw\SchemaContextBundle\Messenger\Stamp\BaggageSchemaStamp;
use Macpaw\SchemaContextBundle\Service\BaggageCodec;
use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;
Expand All @@ -20,6 +21,7 @@ public function __construct(
private SendersLocatorInterface $sendersLocator,
private BaggageSchemaResolver $baggageSchemaResolver,
private BaggageCodec $baggageCodec,
private DebugLogger $logger,
) {
}

Expand All @@ -28,15 +30,23 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$stamp = $envelope->last(BaggageSchemaStamp::class);

if ($this->isWorker($envelope) && !$this->isSyncTransport($envelope)) {
if ($stamp instanceof BaggageSchemaStamp) {
$this->baggageSchemaResolver
->setSchema($stamp->schema)
->setBaggage($stamp->baggage === null ? null : $this->baggageCodec->decode($stamp->baggage));
}
try {
if ($stamp instanceof BaggageSchemaStamp) {
$schema = $stamp->schema;
$baggage = $stamp->baggage === null ? null : $this->baggageCodec->decode($stamp->baggage);

$this->logger->logInfoFromStamp($baggage, $schema);

$result = $stack->next()->handle($envelope, $stack);
$this->baggageSchemaResolver
->setSchema($schema)
->setBaggage($baggage);
}

$this->baggageSchemaResolver->reset();
$result = $stack->next()->handle($envelope, $stack);
} finally {
$this->logger->logResetWorkerAfterWorker();
$this->baggageSchemaResolver->reset();
}

return $result;
}
Expand All @@ -46,6 +56,8 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
? null
: $this->baggageCodec->encode($this->baggageSchemaResolver->getBaggage());

$this->logger->logCreateMessage($this->baggageSchemaResolver->getBaggage(), $schema);

$envelope = $envelope->with(new BaggageSchemaStamp($schema, $baggage));

return $stack->next()->handle($envelope, $stack);
Expand Down
2 changes: 1 addition & 1 deletion src/SchemaContextBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class SchemaContextBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(
new SchemaContextCompilerPass(),
Expand Down
6 changes: 6 additions & 0 deletions src/Service/BaggageSchemaResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Macpaw\SchemaContextBundle\Service;

use Macpaw\SchemaContextBundle\Exception\EnvironmentSchemaMismatchException;
use Macpaw\SchemaContextBundle\Logger\DebugLogger;

class BaggageSchemaResolver
{
Expand All @@ -22,6 +23,7 @@ public function __construct(
private readonly string $environmentSchema,
private readonly string $environmentName,
private readonly array $schemaOverridableEnvironments,
private readonly DebugLogger $logger,
) {
$this->isSchemaOverridableEnvironment = in_array(
$this->environmentName,
Expand All @@ -47,6 +49,8 @@ public function setBaggage(?array $baggage): self
$baggage = null;
}

$this->logger->logSetBaggage($baggage);

$this->baggage = $baggage;

return $this;
Expand Down Expand Up @@ -74,6 +78,8 @@ public function setSchema(?string $schema): self
);
}

$this->logger->logSetSchema($schema);

$this->schema = $schema;

return $this;
Expand Down
Loading
Loading