Skip to content

Commit

Permalink
Add command for clearing data
Browse files Browse the repository at this point in the history
  • Loading branch information
daun committed Mar 21, 2024
1 parent 261a41f commit 6429cf6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 9 deletions.
102 changes: 102 additions & 0 deletions src/Commands/Clear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Daun\StatamicPlaceholders\Commands;

use Daun\StatamicPlaceholders\Commands\Concerns\HasOutputStyles;
use Daun\StatamicPlaceholders\Services\PlaceholderService;
use Daun\StatamicPlaceholders\Support\PlaceholderField;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Statamic\Assets\Asset;
use Statamic\Assets\AssetContainer;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Asset as AssetFacade;
use Statamic\Facades\AssetContainer as AssetContainerFacade;

class Clear extends Command
{
use HasOutputStyles;
use RunsInPlease;

protected $signature = 'statamic:placeholders:clear
{--container= : Limit the command to a specific asset container}';

protected $description = 'Remove existing placeholder images';

protected $container;

public function handle(PlaceholderService $service)
{
$this->container = $this->option('container');

$containers = $this->getContainers();
if ($containers->count()) {
$containers->each(function ($container) use ($service) {
$this->clearPlaceholders($container, $service);
$this->newLine();
});

$this->components->info('All placeholders have been removed.');
}

return 0;
}

protected function clearPlaceholders(AssetContainer $container, PlaceholderService $service): void
{
$assets = AssetFacade::whereContainer($container->handle())
->filter(fn ($asset) => PlaceholderField::supportsAssetType($asset));

if (! $assets->count()) {
$this->components->info("No images found in container <info>{$container->title()}</info>");

return;
} else {
$this->components->info("Removing placeholders in container <info>{$container->title()}</info>");
}

$assets->each(function (Asset $asset) use ($service) {
$exists = $service->exists($asset);
$name = "<bold>{$asset->path()}</bold>";
if ($exists) {
$service->delete($asset);
$this->components->twoColumnDetail($name, '<success>✓ Removed</success>');
} else {
$this->components->twoColumnDetail($name, '<exists>✓ Empty</exists>');
}
});
}

protected function getContainers(): Collection
{
// Container argument passed in? Get the specified container

if ($this->container) {
$container = AssetContainerFacade::find($this->container);
if ($container && PlaceholderField::existsInBlueprint($container)) {
return collect($container);
} elseif ($container) {
$this->components->error("Asset container '{$this->container}' is not configured to generate placeholders.");
} else {
$this->components->error("Asset container '{$this->container}' not found");
}

return collect();
}

// Otherwise: get all containers with a placeholder field

$containers = AssetContainerFacade::all()
->filter(fn (AssetContainer $container) => PlaceholderField::existsInBlueprint($container))
->sortBy('title')
->keyBy->handle();

if ($containers->isEmpty()) {
$this->components->error('No containers are configured to generate placeholders.');
$this->newLine();
$this->line('Please add a `placeholder` field to at least one of your asset blueprints.');
}

return $containers;
}
}
8 changes: 0 additions & 8 deletions src/Commands/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ public function handle(PlaceholderService $service)
return 0;
}

protected function generate(PlaceholderService $service): void
{
$this->getContainers()->each(function ($container) use ($service) {
$this->generatePlaceholders($container, $service);
$this->newLine();
});
}

protected function generatePlaceholders(AssetContainer $container, PlaceholderService $service): void
{
$assets = AssetFacade::whereContainer($container->handle())
Expand Down
5 changes: 5 additions & 0 deletions src/Models/AssetPlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ protected function save(string $hash): void
PlaceholderData::addHash($this->asset, $hash, $this->provider()::$handle);
}

public function delete(): void
{
PlaceholderData::clear($this->asset);
}

public function contents(): ?string
{
return $this->asset->contents();
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ protected function save(string $hash): void
/* not implemented */
}

/**
* Delete the hash from cache or storage.
*/
public function delete(): void
{
/* not implemented */
}

/**
* Convert the blob contents to a placeholder hash.
*/
Expand Down
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ServiceProvider extends AddonServiceProvider
{
protected $commands = [
Commands\Generate::class,
Commands\Clear::class,
];

protected $listen = [
Expand Down
10 changes: 9 additions & 1 deletion src/Services/PlaceholderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function exists(mixed $input, ?string $provider = null): bool
}

/**
* Generate a placeholder for the given asset or url. Returns the generated hash.
* Generate a placeholder for the given asset. Returns the generated hash.
*/
public function generate(Asset $asset, bool $force = false): ?string
{
Expand All @@ -90,4 +90,12 @@ public function dispatch(Asset $asset, bool $force = false): void
{
GeneratePlaceholderJob::dispatch($asset, $force);
}

/**
* Remove a placeholder from the given asset.
*/
public function delete(Asset $asset): void
{
$this->make($asset)->delete();
}
}

0 comments on commit 6429cf6

Please sign in to comment.