From 7c9f80bdcbfe9ce707bf07a33d1d76bbc7cc87c0 Mon Sep 17 00:00:00 2001 From: Mert Date: Sun, 10 Mar 2024 21:26:34 +0300 Subject: [PATCH 1/4] rename config file from cloudflare_cache to cloudflare-cache --- config/{cloudflare_cache.php => cloudflare-cache.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename config/{cloudflare_cache.php => cloudflare-cache.php} (100%) diff --git a/config/cloudflare_cache.php b/config/cloudflare-cache.php similarity index 100% rename from config/cloudflare_cache.php rename to config/cloudflare-cache.php From eb6ef0e320af5b468b88b18b6ae61a8535ea6bda Mon Sep 17 00:00:00 2001 From: Mert Date: Sun, 10 Mar 2024 21:29:14 +0300 Subject: [PATCH 2/4] rename package key --- src/CloudflareCache.php | 8 ++++---- src/CloudflareCacheServiceProvider.php | 14 +++++++------- src/CloudflarePagesMiddleware.php | 4 ++-- src/Commands/CloudflareCacheCommand.php | 2 +- src/Facades/CloudflareCache.php | 2 +- tests/TestCase.php | 8 ++++---- tests/Unit/CloudflarePagesMiddlewareTest.php | 10 +++++----- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/CloudflareCache.php b/src/CloudflareCache.php index 8ac24bb..6d108ef 100644 --- a/src/CloudflareCache.php +++ b/src/CloudflareCache.php @@ -9,9 +9,9 @@ class CloudflareCache implements CloudflareCacheInterface { - public const TAGS_ATTR = 'cloudflare_cache_tags'; + public const TAGS_ATTR = 'cloudflare-cache-tags'; - public const TTL_ATTR = 'cloudflare_cache_ttl'; + public const TTL_ATTR = 'cloudflare-cache-ttl'; public function __construct(private readonly CloudflareServiceInterface $service) { @@ -23,7 +23,7 @@ public function __construct(private readonly CloudflareServiceInterface $service */ public static function getIgnoredMiddlewares(): array { - return Arr::where(config('cloudflare_cache.ignored_middlewares', []), static fn ($middleware) => class_exists($middleware)); + return Arr::where(config('cloudflare-cache.ignored_middlewares', []), static fn ($middleware) => class_exists($middleware)); } /** @@ -95,7 +95,7 @@ public function purgeByUrls(array $urls): bool|string public function isEnabled(): bool { - if (config('cloudflare_cache.debug')) { + if (config('cloudflare-cache.debug')) { return true; } diff --git a/src/CloudflareCacheServiceProvider.php b/src/CloudflareCacheServiceProvider.php index 83d3d27..a0b32f0 100644 --- a/src/CloudflareCacheServiceProvider.php +++ b/src/CloudflareCacheServiceProvider.php @@ -18,7 +18,7 @@ class CloudflareCacheServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { - $package->name('cloudflare_cache') + $package->name('cloudflare-cache') ->hasConfigFile() ->hasCommand(CloudflareCacheCommand::class); } @@ -37,16 +37,16 @@ public function packageRegistered(): void public function registerClient(): static { - $this->app->bind('cloudflare_cache.client', function ($app): Factory { + $this->app->bind('cloudflare-cache.client', function ($app): Factory { return $app[Factory::class]; }); $this->app->singleton(CloudflareServiceInterface::class, function ($app): CloudflareService { return new CloudflareService( - $app->make('cloudflare_cache.client'), - config('cloudflare_cache.api_email'), - config('cloudflare_cache.api_key'), - config('cloudflare_cache.identifier'), + $app->make('cloudflare-cache.client'), + config('cloudflare-cache.api_email'), + config('cloudflare-cache.api_key'), + config('cloudflare-cache.identifier'), ); }); @@ -61,6 +61,6 @@ public function registerCloudflareCache(): void ); }); - $this->app->alias(CloudflareCacheInterface::class, 'cloudflare_cache'); + $this->app->alias(CloudflareCacheInterface::class, 'cloudflare-cache'); } } diff --git a/src/CloudflarePagesMiddleware.php b/src/CloudflarePagesMiddleware.php index 521d98e..eb83748 100644 --- a/src/CloudflarePagesMiddleware.php +++ b/src/CloudflarePagesMiddleware.php @@ -14,7 +14,7 @@ public function handle(Request $request, Closure $next): Response { /** @var Response $response */ $response = $next($request); - + if ($this->shouldCacheResponse($request, $response)) { $ttl = $this->getCacheTTL($request); $response->headers->add(['Cache-Control' => "max-age=$ttl, public"]); @@ -48,7 +48,7 @@ protected function getCacheTTL(Request $request): int return $request->attributes->get(CloudflareCache::TTL_ATTR); } - return config('cloudflare_cache.cache_ttl') ?? 600; + return config('cloudflare-cache.cache_ttl') ?? 600; } public function shouldCacheResponse(Request $request, Response $response): bool diff --git a/src/Commands/CloudflareCacheCommand.php b/src/Commands/CloudflareCacheCommand.php index 6639ca0..6614f15 100644 --- a/src/Commands/CloudflareCacheCommand.php +++ b/src/Commands/CloudflareCacheCommand.php @@ -7,7 +7,7 @@ class CloudflareCacheCommand extends Command { - public $signature = 'laravel-cloudflare-cache'; + public $signature = 'cloudflare-cache'; public $description = 'My command'; diff --git a/src/Facades/CloudflareCache.php b/src/Facades/CloudflareCache.php index 296c6b8..79cab29 100644 --- a/src/Facades/CloudflareCache.php +++ b/src/Facades/CloudflareCache.php @@ -19,6 +19,6 @@ class CloudflareCache extends Facade { protected static function getFacadeAccessor(): string { - return 'cloudflare_cache'; + return 'cloudflare-cache'; } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5724bef..38ec5ac 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -29,9 +29,9 @@ public function getEnvironmentSetUp($app): void config()->set('app.key', 'base64:' . base64_encode(Encrypter::generateKey(config()['app.cipher']))); config()->set('database.default', 'testing'); - config()->set('cloudflare_cache.api_email', ''); - config()->set('cloudflare_cache.api_key', ''); - config()->set('cloudflare_cache.identifier', ''); + config()->set('cloudflare-cache.api_email', ''); + config()->set('cloudflare-cache.api_key', ''); + config()->set('cloudflare-cache.identifier', ''); } protected function registerTestRoutes(): void @@ -67,7 +67,7 @@ protected function registerTestRoutes(): void }; Route::middleware('web') - ->name('cloudflare_cache.') + ->name('cloudflare-cache.') ->group(function () use ($routes) { $routes(); }); diff --git a/tests/Unit/CloudflarePagesMiddlewareTest.php b/tests/Unit/CloudflarePagesMiddlewareTest.php index 76ec963..5f0c5dd 100644 --- a/tests/Unit/CloudflarePagesMiddlewareTest.php +++ b/tests/Unit/CloudflarePagesMiddlewareTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); test('cache bulunmayan sayfalarda session cookie mevcut olmalıdır', function () { - $response = $this->get(route('cloudflare_cache.home')); + $response = $this->get(route('cloudflare-cache.home')); $response->assertCookie('XSRF-TOKEN'); $response->assertCookie('laravel_session'); @@ -16,8 +16,8 @@ $response->assertCookieMissing('XSRF-TOKEN'); $response->assertCookieMissing('laravel_session'); })->with([ - 'cloudflare_cache.content_without_tags', - 'cloudflare_cache.content_in_args', - 'cloudflare_cache.content_in_array', - 'cloudflare_cache.content_in_closure', + 'cloudflare-cache.content_without_tags', + 'cloudflare-cache.content_in_args', + 'cloudflare-cache.content_in_array', + 'cloudflare-cache.content_in_closure', ]); From 803fbf82e63954c700f95e14d4bb119b6cdcdd9b Mon Sep 17 00:00:00 2001 From: Mert Date: Sun, 10 Mar 2024 22:08:54 +0300 Subject: [PATCH 3/4] adds extra conditions --- src/CloudflareCache.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/CloudflareCache.php b/src/CloudflareCache.php index 6d108ef..dec8058 100644 --- a/src/CloudflareCache.php +++ b/src/CloudflareCache.php @@ -95,6 +95,17 @@ public function purgeByUrls(array $urls): bool|string public function isEnabled(): bool { + if (app()->runningUnitTests()) { + return true; + } + + if (! config('cloudflare-cache.api_email') + || ! config('cloudflare-cache.api_key') + || ! config('cloudflare-cache.identifier') + ) { + return false; + } + if (config('cloudflare-cache.debug')) { return true; } From 754a0e9d7c7b8bc1890917998120d824265e1b8a Mon Sep 17 00:00:00 2001 From: Mert Date: Sun, 10 Mar 2024 22:09:19 +0300 Subject: [PATCH 4/4] rename isEnabled to isActive --- src/CloudflareCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CloudflareCache.php b/src/CloudflareCache.php index dec8058..19d4dd2 100644 --- a/src/CloudflareCache.php +++ b/src/CloudflareCache.php @@ -93,7 +93,7 @@ public function purgeByUrls(array $urls): bool|string ]); } - public function isEnabled(): bool + public function isActive(): bool { if (app()->runningUnitTests()) { return true;