Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

small updates and tweaks for craft 4.3 and php 8.1 #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
}
],
"require": {
"craftcms/cms": "^3.2.0",
"guzzlehttp/guzzle": "^6.5.5|^7.2.0"
"craftcms/cms": "^4.3.0",
"guzzlehttp/guzzle": "^7.5.0",
"php": "^8.1.0"
},
"require-dev": {
"vimeo/psalm": "^4.4"
"vimeo/psalm": "^5.4.0"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 10 additions & 6 deletions src/CacheResponse.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?php namespace ostark\upper;
<?php declare(strict_types=1);

namespace ostark\upper;


use yii\base\Response;
use ostark\upper\behaviors\CacheControlBehavior;

class CacheResponse
{
/**
* @var \yii\base\Response|\ostark\upper\behaviors\CacheControlBehavior
* @var Response|CacheControlBehavior
*/
public $response;
public Response|CacheControlBehavior $response;

public function __construct(Response $response)
public function __construct(Response|CacheControlBehavior $response)
{
$this->response = $response;
}

public function never()
public function never(): void
{
if (!$this->isWebResponse()) {
return;
Expand All @@ -24,7 +28,7 @@ public function never()
$this->response->addCacheControlDirective('no-cache');
}

public function for(string $time)
public function for(string $time): void
{
if (!$this->isWebResponse()) {
return;
Expand Down
16 changes: 10 additions & 6 deletions src/EventRegistrar.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php namespace ostark\upper;
<?php declare(strict_types=1);

namespace ostark\upper;


use craft\base\Element;
use craft\elements\db\ElementQuery;
Expand Down Expand Up @@ -29,7 +32,7 @@
class EventRegistrar
{

public static function registerUpdateEvents()
public static function registerUpdateEvents(): void
{
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function ($event) {
static::handleUpdateEvent($event);
Expand All @@ -49,7 +52,7 @@ public static function registerUpdateEvents()

}

public static function registerFrontendEvents()
public static function registerFrontendEvents(): bool
{
// No need to continue when in cli mode
if (\Craft::$app instanceof \craft\console\Application) {
Expand Down Expand Up @@ -136,10 +139,11 @@ public static function registerFrontendEvents()
));
});

return true;
}


public static function registerCpEvents()
public static function registerCpEvents(): void
{
// Register cache purge checkbox
Event::on(
Expand All @@ -159,7 +163,7 @@ function (RegisterCacheOptionsEvent $event) {
}


public static function registerFallback()
public static function registerFallback(): void
{

Event::on(Plugin::class, Plugin::EVENT_AFTER_SET_TAG_HEADER, function (CacheResponseEvent $event) {
Expand Down Expand Up @@ -209,7 +213,7 @@ public static function registerFallback()
/**
* @param \yii\base\Event $event
*/
protected static function handleUpdateEvent(Event $event)
protected static function handleUpdateEvent(Event $event): void
{
$tags = [];

Expand Down
11 changes: 7 additions & 4 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php namespace ostark\upper;
<?php declare(strict_types=1);

namespace ostark\upper;



use Craft;
Expand Down Expand Up @@ -41,7 +44,7 @@ class Plugin extends BasePlugin
const INFO_HEADER_NAME = 'X-UPPER-CACHE';
const TRUNCATED_HEADER_NAME = 'X-UPPER-CACHE-TRUNCATED';

public $schemaVersion = '1.0.1';
public string $schemaVersion = '1.0.1';


/**
Expand Down Expand Up @@ -112,7 +115,7 @@ public function getTagCollection(): TagCollection
*
* @return \craft\base\Model|null
*/
protected function createSettingsModel()
protected function createSettingsModel(): \craft\base\Model|null
{
return new Settings();
}
Expand All @@ -122,7 +125,7 @@ protected function createSettingsModel()
* Is called after the plugin is installed.
* Copies example config to project's config folder
*/
protected function afterInstall()
protected function afterInstall(): void
{
$configSourceFile = __DIR__ . DIRECTORY_SEPARATOR . 'config.example.php';
$configTargetFile = \Craft::$app->getConfig()->configDir . DIRECTORY_SEPARATOR . $this->handle . '.php';
Expand Down
5 changes: 4 additions & 1 deletion src/PurgerFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php namespace ostark\upper;
<?php declare(strict_types=1);

namespace ostark\upper;


use Psr\Log\InvalidArgumentException;
use yii\base\Component;
Expand Down
5 changes: 4 additions & 1 deletion src/TagCollection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php namespace ostark\upper;
<?php declare(strict_types=1);

namespace ostark\upper;


/**
* Class TagCollection
Expand Down
6 changes: 4 additions & 2 deletions src/TwigExtension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace ostark\upper;
<?php declare(strict_types=1);

namespace ostark\upper;

use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
Expand All @@ -11,7 +13,7 @@ class TwigExtension extends AbstractExtension implements GlobalsInterface
*
* @return array An array of global variables
*/
public function getGlobals()
public function getGlobals(): array
{
return [
'upper' => [
Expand Down
20 changes: 10 additions & 10 deletions src/behaviors/CacheControlBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CacheControlBehavior extends Behavior
/**
* @var array
*/
protected $cacheControl = [];
protected array $cacheControl = [];


/**
Expand All @@ -23,7 +23,7 @@ class CacheControlBehavior extends Behavior
* @param string $key The Cache-Control directive name
* @param mixed $value The Cache-Control directive value
*/
public function addCacheControlDirective(string $key, $value = true)
public function addCacheControlDirective(string $key, $value = true): void
{
$this->cacheControl[$key] = $value;
$this->owner->getHeaders()->set('Cache-Control', $this->getCacheControlHeader());
Expand All @@ -34,7 +34,7 @@ public function addCacheControlDirective(string $key, $value = true)
*
* @param string $key The Cache-Control directive
*/
public function removeCacheControlDirective(string $key)
public function removeCacheControlDirective(string $key): void
{
unset($this->cacheControl[$key]);
$this->owner->getHeaders()->set('Cache-Control', $this->getCacheControlHeader());
Expand All @@ -48,7 +48,7 @@ public function removeCacheControlDirective(string $key)
*
* @return bool true if the directive exists, false otherwise
*/
public function hasCacheControlDirective(string $key)
public function hasCacheControlDirective(string $key): bool
{
return array_key_exists($key, $this->cacheControl);
}
Expand All @@ -60,7 +60,7 @@ public function hasCacheControlDirective(string $key)
*
* @return mixed|null The directive value if defined, null otherwise
*/
public function getCacheControlDirective($key)
public function getCacheControlDirective($key): string|null
{
return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
}
Expand All @@ -76,7 +76,7 @@ public function getCacheControlDirective($key)
* @return int|null Number of seconds
*
*/
public function getMaxAge()
public function getMaxAge(): int|null
{
if ($this->hasCacheControlDirective('s-maxage')) {
return (int)$this->getCacheControlDirective('s-maxage');
Expand All @@ -100,7 +100,7 @@ public function getMaxAge()
*
* @final since version 3.2
*/
public function setMaxAge($value)
public function setMaxAge($value): static
{
$this->addCacheControlDirective('max-age', $value);

Expand All @@ -119,7 +119,7 @@ public function setMaxAge($value)
*
* @final since version 3.2
*/
public function setSharedMaxAge($value)
public function setSharedMaxAge($value): static
{
$this->addCacheControlDirective('public');
$this->removeCacheControlDirective('private');
Expand All @@ -129,7 +129,7 @@ public function setSharedMaxAge($value)
return $this;
}

public function getCacheControl()
public function getCacheControl(): array
{
return $this->cacheControl;
}
Expand All @@ -145,7 +145,7 @@ public function setCacheControlDirectiveFromString(string $value = null)
}
}

protected function getCacheControlHeader()
protected function getCacheControlHeader(): string
{
$parts = array();
ksort($this->cacheControl);
Expand Down
2 changes: 1 addition & 1 deletion src/behaviors/TagHeaderBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TagHeaderBehavior extends Behavior
*
* @return bool
*/
public function setTagHeader(string $name, array $tags, string $delimiter = null)
public function setTagHeader(string $name, array $tags, string $delimiter = null): bool
{
$headers = $this->owner->getHeaders();

Expand Down
31 changes: 16 additions & 15 deletions src/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* Don't edit the config.example.php.
* Instead modify the projects/config/upper.php and use ENV VARS
*/
use craft\helpers\App;

return [

// Which driver?
'driver' => getenv('UPPER_DRIVER') ?: 'dummy',
'driver' => App::env('UPPER_DRIVER') ?: 'dummy',

// Default for Cache-control s-maxage
'defaultMaxAge' => 3600 * 24 * 7,
Expand All @@ -20,7 +21,7 @@
// same cache store for several Craft installations.
// Keep it nice and short for the sake of readability when debugging.
// 1-8 characters, special chars get removed
'keyPrefix' => getenv('UPPER_KEY_PREFIX') ?: '',
'keyPrefix' => App::env('UPPER_KEY_PREFIX') ?: '',

// Optional maximum length for the cache tag header. Setting this higher will
// allow Upper to return more tags in the header. However, it will also require
Expand All @@ -36,38 +37,38 @@
'varnish' => [
'tagHeaderName' => 'XKEY',
'purgeHeaderName' => 'XKEY-PURGE',
'purgeUrl' => getenv('VARNISH_URL') ?: 'http://127.0.0.1:80/',
'headers' => getenv('VARNISH_HOST') ? ['Host' => getenv('VARNISH_HOST')] : [],
'purgeUrl' => App::env('VARNISH_URL') ?: 'http://127.0.0.1:80/',
'headers' => App::env('VARNISH_HOST') ? ['Host' => App::env('VARNISH_HOST')] : [],
'softPurge' => false,
],

// Fastly config
'fastly' => [
'tagHeaderName' => 'Surrogate-Key',
'serviceId' => getenv('FASTLY_SERVICE_ID'),
'apiToken' => getenv('FASTLY_API_TOKEN'),
'domain' => getenv('FASTLY_DOMAIN'),
'serviceId' => App::env('FASTLY_SERVICE_ID'),
'apiToken' => App::env('FASTLY_API_TOKEN'),
'domain' => App::env('FASTLY_DOMAIN'),
'softPurge' => false
],

// KeyCDN config
'keycdn' => [
'tagHeaderName' => 'Cache-Tag',
'apiKey' => getenv('KEYCDN_API_KEY'),
'zoneId' => getenv('KEYCDN_ZONE_ID'),
'zoneUrl' => getenv('KEYCDN_ZONE_URL')
'apiKey' => App::env('KEYCDN_API_KEY'),
'zoneId' => App::env('KEYCDN_ZONE_ID'),
'zoneUrl' => App::env('KEYCDN_ZONE_URL')
],

// CloudFlare config
'cloudflare' => [
'tagHeaderName' => 'Cache-Tag',
'tagHeaderDelimiter' => ',',
'apiToken' => getenv('CLOUDFLARE_API_TOKEN'),
'zoneId' => getenv('CLOUDFLARE_ZONE_ID'),
'domain' => getenv('CLOUDFLARE_DOMAIN'),
'apiToken' => App::env('CLOUDFLARE_API_TOKEN'),
'zoneId' => App::env('CLOUDFLARE_ZONE_ID'),
'domain' => App::env('CLOUDFLARE_DOMAIN'),
// deprecated, do not use for new installs
'apiKey' => getenv('CLOUDFLARE_API_KEY'),
'apiEmail' => getenv('CLOUDFLARE_API_EMAIL'),
'apiKey' => App::env('CLOUDFLARE_API_KEY'),
'apiEmail' => App::env('CLOUDFLARE_API_EMAIL'),
],

// Dummy driver (default)
Expand Down
Loading