Skip to content

Commit

Permalink
Implement debugging level
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Sep 23, 2024
1 parent 6c2694a commit 06c8902
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 85 deletions.
4 changes: 3 additions & 1 deletion config/edge-flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
/**
* Enable/disable the pacakge
*/
'debug' => env('EDGE_FLUSH_DEBUG', false),
'debug_level' => ($level = env('EDGE_FLUSH_DEBUG_LEVEL', 0)),

'debug' => env('EDGE_FLUSH_DEBUG', $level > 0),
];
14 changes: 7 additions & 7 deletions src/Behaviours/MakeTag.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace A17\EdgeFlush\Behaviours;

Expand Down Expand Up @@ -32,7 +34,7 @@ public function makeModelName(mixed $model, string $key = null, array $allowedKe
? $model->getCDNCacheTag($key, $type)
: $this->getCDNCacheTagFromModel($model, $key, $type);
} catch (\Exception $exception) {
Helpers::debug("Exception on makeModelName: ".$exception->getMessage());
Helpers::debug('MAKE-TAG:EXCEPTION: on makeModelName: ' . $exception->getMessage());

return null;
}
Expand Down Expand Up @@ -66,9 +68,7 @@ public function tagIsExcluded(string $tag): bool
/**
* @param callable(string $pattern): boolean $pattern
*/
return $this->excludedModels->contains(
fn(string $pattern) => EdgeFlush::match($pattern, $tag),
);
return $this->excludedModels->contains(fn(string $pattern) => EdgeFlush::match($pattern, $tag));
}

public function tagIsNotExcluded(string $tag): bool
Expand Down Expand Up @@ -147,9 +147,9 @@ protected function attributeMustBeIgnored(Model|string $model, string $attribute
{
$model = $model instanceof Model ? get_class($model) : $model;

$attributes = Helpers::configArray("edge-flush.invalidations.attributes.ignore", []);
$attributes = Helpers::configArray('edge-flush.invalidations.attributes.ignore', []);

$ignore = array_merge($attributes[$model] ?? [], ($attributes['*'] ?? []));
$ignore = array_merge($attributes[$model] ?? [], $attributes['*'] ?? []);

return in_array($attribute, $ignore);
}
Expand Down
15 changes: 9 additions & 6 deletions src/Services/Akamai/Service.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace A17\EdgeFlush\Services\Akamai;

Expand Down Expand Up @@ -89,7 +91,8 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul
$invalidation = (new Invalidation())->setUrls($invalidation);
}

$urls = $invalidation->urls()
$urls = $invalidation
->urls()
->map(function ($item) {
return $item instanceof Url ? $item->url_hash : $item;
})
Expand All @@ -104,7 +107,9 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul
'objects' => $urls->toArray(),
];

Helpers::debug('[AKAMAI] dispatching invalidations for ' . $urls->count() . ' urls');
Helpers::debug('AKAMAI: dispatching invalidations for ' . $urls->count() . ' urls');

Helpers::debug('AKAMAI: invalidating these URLs: ' . json_encode($body), 4);

// Create an instance of the Timer
$timer = new Timer();
Expand Down Expand Up @@ -143,9 +148,7 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul

public function isProperlyConfigured(): bool
{
return filled($this->getClientToken())
&& filled($this->getClientSecret())
&& filled($this->getAccessToken());
return filled($this->getClientToken()) && filled($this->getClientSecret()) && filled($this->getAccessToken());
}

public function getClientToken(): string|null
Expand Down
18 changes: 12 additions & 6 deletions src/Services/BaseService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace A17\EdgeFlush\Services;

Expand All @@ -14,7 +16,8 @@

abstract class BaseService implements ServiceContract
{
use ControlsInvalidations, CastObject;
use ControlsInvalidations;
use CastObject;

protected bool|null $enabled = null;

Expand All @@ -41,7 +44,10 @@ public function makeResponse(Response $response): Response
return $response;
}

Helpers::debug('CACHABLE-MATRIX: ' . json_encode(EdgeFlush::cacheControl()->getCachableMatrix($response)));
Helpers::debug(
'RESPONSE:CACHABLE-MATRIX: ' . json_encode(EdgeFlush::cacheControl()->getCachableMatrix($response)),
3,
);

return $this->addHeadersToResponse(
$response,
Expand Down Expand Up @@ -99,9 +105,9 @@ public function getInvalidationPathsForTags(Invalidation $invalidation): Collect

public function addHeadersFromRequest(Response $response): void
{
(new Collection(Helpers::configArray('edge-flush.strategies.headers.from-request')))->each(function (string $header) use (
$response
) {
(new Collection(Helpers::configArray('edge-flush.strategies.headers.from-request')))->each(function (
string $header
) use ($response) {
if (filled($value = request()->header($header))) {
$response->headers->set($header, $value);
}
Expand Down
24 changes: 13 additions & 11 deletions src/Services/CdnBaseService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace A17\EdgeFlush\Services;

Expand Down Expand Up @@ -33,7 +35,7 @@ public function invalidateAll(): Invalidation
}

return $this->createInvalidationRequest(
Helpers::configArray('edge-flush.services.'.static::$serviceName.'.invalidate_all_paths'),
Helpers::configArray('edge-flush.services.' . static::$serviceName . '.invalidate_all_paths'),
);
}

Expand Down Expand Up @@ -80,10 +82,10 @@ public function invalidatePaths(Invalidation $invalidation): Invalidation
{
return $this->createInvalidationRequest($invalidation);
}

public function maxUrls(): int
{
return Helpers::configInt('edge-flush.services.'.static::$serviceName.'.max_urls') ?? 300;
return Helpers::configInt('edge-flush.services.' . static::$serviceName . '.max_urls') ?? 300;
}

public function enabled(): bool
Expand All @@ -92,27 +94,27 @@ public function enabled(): bool
return $this->enabled;
}

$enabled = EdgeFlush::enabled() &&
$this->serviceIsEnabled() &&
$this->isProperlyConfigured();
$enabled = EdgeFlush::enabled() && $this->serviceIsEnabled() && $this->isProperlyConfigured();

if (!$enabled) {
Helpers::debug('Service is not enabled or not properly configured: '.static::$serviceName);
Helpers::debug('CDN-SERVICE: service is not enabled or not properly configured: ' . static::$serviceName);
}

return $this->enabled = $enabled;
}

public function serviceIsEnabled(): bool
{
return Helpers::configBool('edge-flush.services.'.static::$serviceName.'.enabled', true);
return Helpers::configBool('edge-flush.services.' . static::$serviceName . '.enabled', true);
}

public function canInvalidateAll(): bool
{
return collect(
Helpers::configString('edge-flush.services.'.static::$serviceName.'.invalidate_all_paths') ?? []
)->filter()->isNotEmpty();
Helpers::configString('edge-flush.services.' . static::$serviceName . '.invalidate_all_paths') ?? [],
)
->filter()
->isNotEmpty();
}

public function createInvalidationRequest(Invalidation|array $invalidation = null): Invalidation
Expand Down
42 changes: 20 additions & 22 deletions src/Services/CloudFront/Service.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace A17\EdgeFlush\Services\CloudFront;

Expand Down Expand Up @@ -36,19 +38,19 @@ protected function instantiate(): void

protected function getDistributionId(): string|null
{
return Helpers::configString('edge-flush.services.'.static::$serviceName.'.distribution_id');
return Helpers::configString('edge-flush.services.' . static::$serviceName . '.distribution_id');
}

public function getClient(): CloudFrontClient|null
{
$config = [
'region' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.region'),
'region' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.region'),

'version' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.sdk_version'),
'version' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.sdk_version'),

'credentials' => [
'key' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.key'),
'secret' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.secret'),
'key' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.key'),
'secret' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.secret'),
],
];

Expand All @@ -57,13 +59,13 @@ public function getClient(): CloudFrontClient|null
}

return new CloudFrontClient([
'region' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.region'),
'region' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.region'),

'version' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.sdk_version'),
'version' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.sdk_version'),

'credentials' => [
'key' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.key'),
'secret' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.secret'),
'key' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.key'),
'secret' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.secret'),
],
]);
}
Expand Down Expand Up @@ -96,20 +98,16 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul
$invalidation = $this->createInvalidation($invalidation);

if ($this->client() === null) {
Helpers::debug('[CLOUD FRONT]: Service is disabled.');
Helpers::debug('CLOUD-FRONT: Service is disabled.');

return $invalidation;
}

$paths = $invalidation->paths()->toArray();

Helpers::debug(
'[CLOUD FRONT]: Invalidating ' .
count($paths) .
' path(s): (' .
(new Collection($paths))->take(20)->implode(', ') .
')...',
);
Helpers::debug('CLOUD-FRONT:INVALIDATION: path count: ' . count($paths));

Helpers::debug('CLOUD-FRONT:INVALIDATION: paths: ' . (new Collection($paths))->take(20)->implode(', '), 5);

try {
$response = $this->client()->createInvalidation([
Expand All @@ -124,10 +122,10 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul
]);
} catch (\Exception $e) {
Log::error(
'[EDGE-FLUSH] [CLOUD FRONT] Invalidation request failed: ' .
$e->getMessage() .
' - PATHS: ' .
json_encode($paths),
'[EDGE-FLUSH] CLOUD-FRONT Invalidation request failed: ' .
$e->getMessage() .
' - PATHS: ' .
json_encode($paths),
);

return $invalidation;
Expand Down
12 changes: 8 additions & 4 deletions src/Services/Entity.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace A17\EdgeFlush\Services;

Expand Down Expand Up @@ -133,7 +135,7 @@ public function getDirtyModelNames(): Collection
foreach ($this->attributes as $key => $value) {
$castTypeNew = gettype($value);
$castTypeOld = gettype($this->original[$key]);

$original = $this->encodeValueForComparison($this->original[$key], $castTypeOld);
$updated = $this->encodeValueForComparison($value, $castTypeNew);

Expand All @@ -146,7 +148,9 @@ public function getDirtyModelNames(): Collection
}

if (!isset($this->modelNames[$modelName])) {
Helpers::debug("ATTRIBUTE CHANGED: {$modelName} - old: {$original}({$castTypeOld}) - new: {$updated}({$castTypeNew})");
Helpers::debug(
"ENTITY:ATTRIBUTE-CHANGED: {$modelName} - old: {$original}({$castTypeOld}) - new: {$updated}({$castTypeNew})",
);

$this->modelNames[$modelName] = $modelName;
}
Expand All @@ -164,7 +168,7 @@ public function getDirtyModelNames(): Collection
}

if (!isset($this->modelNames[$modelName])) {
Helpers::debug("RELATION CHANGED: {$modelName}");
Helpers::debug("ENTITY:RELATION-CHANGED: {$modelName}");

$this->modelNames[$modelName] = $modelName;
}
Expand Down
Loading

0 comments on commit 06c8902

Please sign in to comment.