Skip to content

Commit

Permalink
Combine event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed May 7, 2024
1 parent 86fe631 commit cc23fb4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
39 changes: 1 addition & 38 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace craft\cloud;

use Craft;
use craft\base\Element;
use craft\base\Event;
use craft\base\Model;
use craft\cloud\fs\AssetsFs;
Expand All @@ -21,10 +20,8 @@
use craft\helpers\App;
use craft\imagetransforms\FallbackTransformer;
use craft\imagetransforms\ImageTransformer as CraftImageTransformer;
use craft\services\Elements;
use craft\services\Fs as FsService;
use craft\services\ImageTransforms;
use craft\utilities\ClearCaches;
use craft\web\Application as WebApplication;
use craft\web\twig\variables\CraftVariable;
use craft\web\View;
Expand Down Expand Up @@ -197,41 +194,7 @@ function(\yii\base\Event $e) {

protected function registerCloudEventHandlers(): void
{
Event::on(
View::class,
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
[$this->getStaticCache(), 'handleBeforeRenderPageTemplate'],
);

Event::on(
View::class,
View::EVENT_AFTER_RENDER_PAGE_TEMPLATE,
[$this->getStaticCache(), 'handleAfterRenderPageTemplate'],
);

Event::on(
Elements::class,
Elements::EVENT_INVALIDATE_CACHES,
[$this->getStaticCache(), 'handleInvalidateCaches'],
);

Event::on(
ClearCaches::class,
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
[$this->getStaticCache(), 'handleRegisterCacheOptions'],
);

Event::on(
Element::class,
Element::EVENT_AFTER_SAVE,
[$this->getStaticCache(), 'handleAfterUpdate'],
);

Event::on(
Element::class,
Element::EVENT_AFTER_DELETE,
[$this->getStaticCache(), 'handleAfterUpdate'],
);
$this->getStaticCache()->registerEventHandlers();

Event::on(
Asset::class,
Expand Down
89 changes: 73 additions & 16 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use craft\events\RegisterCacheOptionsEvent;
use craft\events\TemplateEvent;
use craft\helpers\ElementHelper;
use craft\services\Elements;
use craft\utilities\ClearCaches;
use craft\web\Application as WebApplication;
use craft\web\Response as WebResponse;
use craft\web\UrlManager;
use craft\web\View;
Expand All @@ -18,18 +21,65 @@

class StaticCache extends \yii\base\Component
{
public function handleBeforeRenderPageTemplate(TemplateEvent $event): void
public function registerEventHandlers(): void
{
// ignore CP and CLI requests
if (
$event->templateMode !== View::TEMPLATE_MODE_SITE ||
!(Craft::$app->getResponse() instanceof WebResponse)
) {
Event::on(
WebApplication::class,
WebApplication::EVENT_INIT,
[$this, 'handleWebApplicationInit'],
);

Event::on(
View::class,
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
[$this, 'handleBeforeRenderPageTemplate'],
);

Event::on(
View::class,
View::EVENT_AFTER_RENDER_PAGE_TEMPLATE,
[$this, 'handleAfterRenderPageTemplate'],
);

Event::on(
Elements::class,
Elements::EVENT_INVALIDATE_CACHES,
[$this, 'handleInvalidateElementCaches'],
);

Event::on(
ClearCaches::class,
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
[$this, 'handleRegisterCacheOptions'],
);

Event::on(
Element::class,
Element::EVENT_AFTER_SAVE,
[$this, 'handleAfterUpdate'],
);

Event::on(
Element::class,
Element::EVENT_AFTER_DELETE,
[$this, 'handleAfterUpdate'],
);
}

private function handleWebApplicationInit(Event $event): void
{
if (!$this->shouldCollectCacheInfo()) {
return;
}

// start collecting element cache info
Craft::$app->getElements()->startCollectingCacheInfo();
}

private function handleBeforeRenderPageTemplate(TemplateEvent $event): void
{
if (!$this->shouldCollectCacheInfo()) {
return;
}

// capture the matched element, if there was one
/** @var UrlManager $urlManager */
Expand All @@ -40,7 +90,7 @@ public function handleBeforeRenderPageTemplate(TemplateEvent $event): void
}
}

public function handleAfterRenderPageTemplate(TemplateEvent $event): void
private function handleAfterRenderPageTemplate(TemplateEvent $event): void
{
// ignore CP and CLI requests
if (
Expand All @@ -59,7 +109,7 @@ public function handleAfterRenderPageTemplate(TemplateEvent $event): void
}
}

public function handleAfterUpdate(Event $event): void
private function handleAfterUpdate(Event $event): void
{
/** @var Element $element */
$element = $event->sender;
Expand All @@ -77,7 +127,7 @@ public function handleAfterUpdate(Event $event): void
$this->purgePrefixes($url);
}

public function handleInvalidateCaches(InvalidateElementCachesEvent $event): void
private function handleInvalidateElementCaches(InvalidateElementCachesEvent $event): void
{
$tags = $event->tags ?? [];

Expand All @@ -88,7 +138,7 @@ public function handleInvalidateCaches(InvalidateElementCachesEvent $event): voi
$this->purgeTags(...$tags);
}

public function handleRegisterCacheOptions(RegisterCacheOptionsEvent $event): void
private function handleRegisterCacheOptions(RegisterCacheOptionsEvent $event): void
{
$event->options[] = [
'key' => 'cloud-static-caches',
Expand Down Expand Up @@ -169,7 +219,7 @@ public function purgeTags(string ...$tags): void
}
}

protected function prepareTags(iterable $tags): Collection
private function prepareTags(iterable $tags): Collection
{
Craft::info(new PsrMessage('Preparing tags', Collection::make($tags)->all()));

Expand All @@ -194,17 +244,17 @@ protected function prepareTags(iterable $tags): Collection
->values();
}

protected function removeNonPrintableChars(string $string): string
private function removeNonPrintableChars(string $string): string
{
return preg_replace('/[^[:print:]]/', '', $string);
}

protected function hash(string $string): ?string
private function hash(string $string): string
{
return sprintf('%x', crc32($string));
}

protected function addCacheTagsToResponse(array $tags, $duration = null): void
private function addCacheTagsToResponse(array $tags, $duration = null): void
{
$response = Craft::$app->getResponse();
$headers = $response->getHeaders();
Expand Down Expand Up @@ -238,10 +288,17 @@ protected function addCacheTagsToResponse(array $tags, $duration = null): void
$response->setCacheHeaders($duration, false);
}

protected function shouldIgnoreTags(iterable $tags): bool
private function shouldIgnoreTags(iterable $tags): bool
{
return Collection::make($tags)->contains(function(string $tag) {
return preg_match('/element::craft\\\\elements\\\\\S+::(drafts|revisions)/', $tag);
});
}

private function shouldCollectCacheInfo(): bool
{
return
Craft::$app->getView()->templateMode === View::TEMPLATE_MODE_SITE &&
Craft::$app->getResponse() instanceof WebResponse;
}
}

0 comments on commit cc23fb4

Please sign in to comment.