Skip to content

Commit 06ca66b

Browse files
authored
Upgrade to silverstripe 6 (#5)
* Upgrade to silverstripe 6 * 🐛 fix: don't scaffold MaxAge
1 parent bf99863 commit 06ca66b

File tree

6 files changed

+109
-44
lines changed

6 files changed

+109
-44
lines changed

composer.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@
77
],
88
"license": "BSD-3-Clause",
99
"require": {
10-
"silverstripe/framework": "^4.0 || ^5.0"
10+
"php": "^8.3",
11+
"silverstripe/framework": "^6.0"
1112
},
1213
"config": {
1314
"allow-plugins": {
1415
"composer/installers": true,
16+
"phpstan/extension-installer": true,
1517
"silverstripe/vendor-plugin": true
16-
}
18+
},
19+
"sort-packages": true
1720
},
1821
"autoload": {
1922
"psr-4": {
2023
"DNADesign\\HTTPCacheControl\\": "extensions"
2124
}
25+
},
26+
"require-dev": {
27+
"cambis/silverstan": "^2.1",
28+
"cambis/silverstripe-rector": "^2.1",
29+
"phpstan/extension-installer": "^1.4",
30+
"phpstan/phpstan": "^2.1",
31+
"phpstan/phpstan-deprecation-rules": "^2.0",
32+
"rector/rector": "^2.2",
33+
"symplify/easy-coding-standard": "^12.6"
2234
}
2335
}

ecs.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
6+
use Symplify\EasyCodingStandard\Config\ECSConfig;
7+
8+
return ECSConfig::configure()
9+
->withPaths([
10+
__DIR__ . '/extensions',
11+
])
12+
->withPreparedSets(common: true, psr12: true)
13+
->withSkip([
14+
NotOperatorWithSuccessorSpaceFixer::class,
15+
]);

extensions/ControllerExtension.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@
22

33
namespace DNADesign\HTTPCacheControl;
44

5+
use SilverStripe\Control\Controller;
56
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
6-
use SilverStripe\Core\Extension;
77
use SilverStripe\Core\Config\Config;
8+
use SilverStripe\Core\Extension;
89

910
/**
1011
* This extension uses the field data "max-age" and sets the max age.
1112
* This will be applied to the Controller::init function for the controller
1213
* you add this extension to.
14+
*
15+
* @extends Extension<Controller>
1316
*/
1417
class ControllerExtension extends Extension
1518
{
16-
1719
/**
18-
* @var int $cacheAge Max-age seconds to cache for.
20+
* Max-age seconds to cache for.
1921
*/
20-
private static $cacheAge_default = 120;
22+
private static int $cacheAge_default = 120;
2123

2224
/**
2325
* Called by ContentController::init();
2426
*/
25-
public function onBeforeInit()
27+
public function onBeforeInit(): void
2628
{
2729
$cacheControl = HTTPCacheControlMiddleware::singleton();
2830

2931
if ($this->getDisableCache()) {
30-
$cacheControl->disableCache($force = true);
32+
$cacheControl->disableCache(true);
3133
} else {
3234
$cacheControl
3335
->enableCache($this->getForceCache())
@@ -36,52 +38,52 @@ public function onBeforeInit()
3638
}
3739
}
3840

39-
public function getDisableCache()
41+
public function getDisableCache(): bool
4042
{
41-
if ($this->owner->Config()->get('http_cache_disable')) {
43+
if ($this->getOwner()->Config()->get('http_cache_disable')) {
4244
return true;
4345
}
4446

45-
if ($this->owner->failover->Config()->get('http_cache_disable')) {
47+
if ($this->getOwner()->getFailover()->Config()->get('http_cache_disable')) {
4648
return true;
4749
}
4850

49-
if ($this->owner->failover->MaxAge == '0') {
51+
if ($this->getOwner()->getFailover()->MaxAge == '0') {
5052
return true;
5153
}
54+
55+
return false;
5256
}
5357

54-
public function getForceCache()
58+
public function getForceCache(): bool
5559
{
56-
if ($this->owner->failover->Config()->get('http_cache_force')) {
57-
return true;
58-
}
60+
return $this->getOwner()->getFailover()->Config()->get('http_cache_force') === true;
5961
}
6062

61-
public function getCacheAge()
63+
public function getCacheAge(): int
6264
{
63-
if ($this->owner->Config()->get('http_cache_disable')) {
65+
if ($this->getOwner()->Config()->get('http_cache_disable')) {
6466
return 0;
6567
}
6668

6769
/* http_cache_disable can be used on subclasses to override the behaviour */
68-
if ($this->owner->failover->Config()->get('http_cache_disable')) {
70+
if ($this->getOwner()->getFailover()->Config()->get('http_cache_disable')) {
6971
return 0;
7072
}
7173

7274
//any page with forms in its elements shouldnt be cached
73-
if ($this->owner->failover->hasExtension('DNADesign\Elemental\Extensions\ElementalPageExtension')) {
74-
$area = $this->owner->ElementalArea();
75+
if ($this->getOwner()->getFailover()->hasExtension('DNADesign\Elemental\Extensions\ElementalPageExtension')) {
76+
$area = $this->getOwner()->ElementalArea(); // @phpstan-ignore-line method.notFound
7577
$elements = $area->Elements();
7678

7779
foreach ($elements as $element) {
78-
/* http_cache_disable can be used on Elements to override the behaviour
79-
this is particularly useful for elements containing forms which
80-
contain a security ID specific to the user
80+
// http_cache_disable can be used on Elements to override the behaviour
81+
// this is particularly useful for elements containing forms which
82+
// contain a security ID specific to the user
83+
84+
// Another way to approach this and still get decent caching on the page
85+
// is to lazy load those elements after the initial page request
8186

82-
Another way to approach this and still get decent caching on the page
83-
is to lazy load those elements after the initial page request
84-
*/
8587
if ($element->Config()->get('http_cache_disable')) {
8688
return 0;
8789
} elseif ($element->ClassName == 'DNADesign\ElementalVirtual\Model\ElementVirtual') {
@@ -92,9 +94,9 @@ public function getCacheAge()
9294
}
9395
}
9496

95-
if ($this->owner->failover->MaxAge != '' && $this->owner->failover->MaxAge != '0') {
96-
return (int) ($this->owner->failover->MaxAge * 60);
97+
if ($this->getOwner()->getFailover()->MaxAge != '' && $this->getOwner()->getFailover()->MaxAge != '0') {
98+
return (int) ($this->getOwner()->getFailover()->MaxAge * 60);
9799
}
98-
return (int) Config::inst()->get('DNADesign\HTTPCacheControl\ControllerExtension', 'cacheAge_default');
100+
return (int) Config::inst()->get(self::class, 'cacheAge_default');
99101
}
100102
}

extensions/PageExtension.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,42 @@
66
use SilverStripe\Core\Extension;
77
use SilverStripe\Forms\FieldList;
88
use SilverStripe\Forms\TextField;
9-
use SilverStripe\Security\Security;
9+
use SilverStripe\ORM\DataObject;
1010
use SilverStripe\Security\Permission;
11+
use SilverStripe\Security\Security;
1112

1213
/**
1314
* This extension adds the ability to control the max-age per originator.
1415
* The configuration option is surfaced to the CMS UI. The extension needs to be added
1516
* to the object related to the policed controller.
17+
*
18+
* @property ?string $MaxAge
19+
* @extends Extension<DataObject>
1620
*/
1721
class PageExtension extends Extension
1822
{
23+
private static array $db = [
24+
'MaxAge' => 'Varchar(10)',
25+
];
26+
27+
private static array $scaffold_cms_fields_settings = [
28+
'ignoreFields' => [
29+
'MaxAge',
30+
],
31+
];
1932

20-
/**
21-
* @var array
22-
*/
23-
private static $db = array(
24-
'MaxAge' => 'Varchar(10)'
25-
);
26-
27-
/**
28-
* @param FieldList $fields
29-
*/
30-
public function updateSettingsFields(FieldList $fields)
33+
public function updateSettingsFields(FieldList $fields): void
3134
{
3235
// Only admins are allowed to modify this.
3336
$member = Security::getCurrentUser();
3437
if (!$member || !Permission::checkMember($member, 'ADMIN')) {
3538
return;
3639
}
3740

38-
$default = Config::inst()->get('DNADesign\HTTPCacheControl\ControllerExtension', 'cacheAge_default');
41+
$default = Config::inst()->get(ControllerExtension::class, 'cacheAge_default');
3942

4043
/* http_cache_disable can be used on subclasses to override the behaviour */
41-
if ($this->owner->Config()->get('http_cache_disable')) {
44+
if ($this->getOwner()->Config()->get('http_cache_disable')) {
4245
$maxAge = TextField::create('MaxAgeOverriden', 'Custom cache timeout [minutes]', 0);
4346
$maxAge->setDisabled(true);
4447

phpstan.neon.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
includes:
2+
- vendor/cambis/silverstan/bleedingEdge.neon
3+
parameters:
4+
level: 4
5+
paths:
6+
- extensions
7+
treatPhpDocTypesAsCertain: false

rector.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeLevelSetList;
6+
use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeSetList;
7+
use Cambis\SilverstripeRector\Silverstripe52\Rector\Class_\AddExtendsAnnotationToExtensionRector;
8+
use Rector\Config\RectorConfig;
9+
10+
return RectorConfig::configure()
11+
->withImportNames()
12+
->withPaths([
13+
__DIR__ . '/extensions',
14+
])
15+
->withPreparedSets(
16+
deadCode: true,
17+
typeDeclarations: true
18+
)
19+
->withPhpSets()
20+
->withSets([
21+
SilverstripeLevelSetList::UP_TO_SILVERSTRIPE_60,
22+
SilverstripeSetList::CODE_QUALITY,
23+
])
24+
->withSkip([
25+
AddExtendsAnnotationToExtensionRector::class,
26+
]);

0 commit comments

Comments
 (0)