Skip to content

Commit

Permalink
Refactored CloudflareCache class, added a new console command to purg… (
Browse files Browse the repository at this point in the history
#2)

* Refactored CloudflareCache class, added a new console command to purge everything, and updated configuration keys for the Cloudflare API.

* Fix styling

* Refactor config file to use more descriptive variable names and add comments for clarity.

---------

Co-authored-by: thejmitchener <thejmitchener@users.noreply.github.com>
  • Loading branch information
thejmitchener and thejmitchener authored Apr 2, 2024
1 parent c7287eb commit f7c7721
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,41 @@ php artisan vendor:publish --tag="cloudflare-cache-config"
This is the contents of the published config file:

```php
<?php

return [
/**
* Generate global api key.
* Generate zone or global api key.
*
* @see https://dash.cloudflare.com/profile/api-tokens
*/
'api_key' => env('CLOUDFLARE_CACHE_KEY'),
'api_key' => env('CLOUDFLARE_CACHE_API_KEY'),

/**
* zone_id of your site on cloudflare dashboard.
* The zone_id of your site on cloudflare dashboard.
*/
'identifier' => env('CLOUDFLARE_CACHE_IDENTIFIER'),
'identifier' => env('CLOUDFLARE_CACHE_ZONE_ID'),

'debug' => env('CLOUDFLARE_CACHE_DEBUG', false),
/**
* Debug mode.
*/
'debug' => env('CLOUDFLARE_CACHE_DEBUG', false),
];

```

## Usage

Purges everything
Purge everything function with:

```php
CloudflareCache::purgeEverything()
use Fuelviews\CloudflareCache\Facades\CloudflareCache;

CloudflareCache::purgeEverything();
```

Purge everything console command with:

```bash
php artisan cloudflare-cache:clear
```

## Testing
Expand Down
11 changes: 7 additions & 4 deletions config/cloudflare-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

return [
/**
* Generate global api key.
* Generate zone or global api key.
*
* @see https://dash.cloudflare.com/profile/api-tokens
*/
'api_key' => env('CLOUDFLARE_CACHE_KEY'),
'api_key' => env('CLOUDFLARE_CACHE_API_KEY'),

/**
* zone_id of your site on cloudflare dashboard.
* The zone_id of your site on cloudflare dashboard.
*/
'identifier' => env('CLOUDFLARE_CACHE_IDENTIFIER'),
'identifier' => env('CLOUDFLARE_CACHE_ZONE_ID'),

/**
* Debug mode.
*/
'debug' => env('CLOUDFLARE_CACHE_DEBUG', false),
];
4 changes: 2 additions & 2 deletions src/CloudflareCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
public function __construct(private CloudflareServiceInterface $service)
{
// .
//
}

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ public function purgeEverything(): bool|string
]);
}

public function isActive(): bool
public function ive(): bool
{
if (app()->runningUnitTests()) {
return true;
Expand Down
5 changes: 4 additions & 1 deletion src/CloudflareCacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fuelviews\CloudflareCache;

use Fuelviews\CloudflareCache\Commands\CloudflareCacheClearCommand;
use Fuelviews\CloudflareCache\Services\CloudflareService;
use Fuelviews\CloudflareCache\Services\CloudflareServiceInterface;
use Illuminate\Http\Client\Factory;
Expand All @@ -14,7 +15,9 @@ public function configurePackage(Package $package): void
{
$package
->name('laravel-cloudflare-cache')
->hasConfigFile();
->hasConfigFile()
->hasCommand(CloudflareCacheClearCommand::class,
);
}

public function packageRegistered(): void
Expand Down
29 changes: 29 additions & 0 deletions src/Commands/CloudflareCacheClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Fuelviews\CloudflareCache\Commands;

use Fuelviews\CloudflareCache\Facades\CloudflareCache;
use Illuminate\Console\Command;

class CloudflareCacheClearCommand extends Command
{
protected $signature = 'cloudflare-cache:clear';

protected $description = 'Cloudflare purge everything';

/**
* Execute the console command.
*
* This method handles the logic after the command is called. It decides
* whether to include an index in the sitemap based on configuration settings.
* Depending on those settings, it may generate individual sitemaps for pages
* and posts and then either create a sitemap index to include them or
* directly generate a single sitemap.
*/
public function handle(): bool
{
CloudflareCache::purgeEverything();

return true;
}
}
1 change: 0 additions & 1 deletion src/Facades/CloudflareCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/**
* @see \Fuelviews\CloudflareCache\CloudflareCache
*
* @method static bool isActive()
* @method static bool|string purgeEverything()
*/
class CloudflareCache extends Facade
Expand Down

0 comments on commit f7c7721

Please sign in to comment.