Skip to content

Commit da91400

Browse files
committed
API Move logic from silverstripe/cms into central place
1 parent 6b33b5a commit da91400

File tree

8 files changed

+331
-33
lines changed

8 files changed

+331
-33
lines changed

src/Model/ModelData.php

+41
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,38 @@ class ModelData
7474

7575
private array $objCache = [];
7676

77+
private $_cache_statusFlags = null;
78+
7779
public function __construct()
7880
{
7981
// no-op
8082
}
8183

84+
/**
85+
* Flags provides the user with additional data about the current page status.
86+
*
87+
* Mostly this is used for versioning, but can be used for other purposes (e.g. localisation).
88+
* Each page can have more than one status flag.
89+
*
90+
* Returns an associative array of a unique key to a (localized) title for the flag.
91+
* The unique key can be reused as a CSS class.
92+
*
93+
* Example (simple):
94+
* "deletedonlive" => "Deleted"
95+
*
96+
* Example (with optional title attribute):
97+
* "deletedonlive" => ['text' => "Deleted", 'title' => 'This page has been deleted']
98+
*/
99+
public function getStatusFlags(bool $cached = true): array
100+
{
101+
if (!$this->_cache_statusFlags || !$cached) {
102+
$flags = [];
103+
$this->extend('updateStatusFlags', $flags);
104+
$this->_cache_statusFlags = $flags;
105+
}
106+
return $this->_cache_statusFlags;
107+
}
108+
82109
// -----------------------------------------------------------------------------------------------------------------
83110

84111
// FIELD GETTERS & SETTERS -----------------------------------------------------------------------------------------
@@ -545,6 +572,20 @@ public function Debug(): ModelData|string
545572
return ModelDataDebugger::create($this);
546573
}
547574

575+
/**
576+
* Clears record-specific cached data.
577+
*
578+
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
579+
* When false will just clear session-local cached data
580+
*/
581+
public function flushCache(bool $persistent = true): static
582+
{
583+
$this->objCacheClear();
584+
$this->_cache_statusFlags = null;
585+
$this->extend('onFlushCache');
586+
return $this;
587+
}
588+
548589
/**
549590
* Generate the cache name for a field
550591
*/

src/ORM/DataObject.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -3509,14 +3509,11 @@ public static function get_one($callerClass = null, $filter = "", $cache = true,
35093509
}
35103510

35113511
/**
3512-
* Flush the cached results for all relations (has_one, has_many, many_many)
3513-
* Also clears any cached aggregate data.
3512+
* @inheritDoc
35143513
*
3515-
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
3516-
* When false will just clear session-local cached data
3517-
* @return static $this
3514+
* Also flush the cached results for all relations (has_one, has_many, many_many)
35183515
*/
3519-
public function flushCache($persistent = true)
3516+
public function flushCache(bool $persistent = true): static
35203517
{
35213518
if (static::class == DataObject::class) {
35223519
DataObject::$_cache_get_one = [];
@@ -3530,11 +3527,9 @@ public function flushCache($persistent = true)
35303527
}
35313528
}
35323529

3533-
$this->extend('onFlushCache');
3534-
35353530
$this->components = [];
35363531
$this->eagerLoadedData = [];
3537-
return $this;
3532+
return parent::flushCache($persistent);
35383533
}
35393534

35403535
/**
@@ -3561,7 +3556,7 @@ public static function flush_and_destroy_cache()
35613556
*/
35623557
public static function reset()
35633558
{
3564-
DBEnum::flushCache();
3559+
DBEnum::clearStaticCache();
35653560
ClassInfo::reset_db_cache();
35663561
static::getSchema()->reset();
35673562
DataObject::$_cache_get_one = [];

src/ORM/FieldType/DBEnum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DBEnum extends DBString
3838
/**
3939
* Clear all cached enum values.
4040
*/
41-
public static function flushCache(): void
41+
public static function clearStaticCache(): void
4242
{
4343
DBEnum::$enum_cache = [];
4444
}
@@ -176,7 +176,7 @@ public function getEnum(): array
176176
* If table or name are not set, or if it is not a valid field on the given table,
177177
* then only known enum values are returned.
178178
*
179-
* Values cached in this method can be cleared via `DBEnum::flushCache();`
179+
* Values cached in this method can be cleared via `DBEnum::clearStaticCache();`
180180
*/
181181
public function getEnumObsolete(): array
182182
{

0 commit comments

Comments
 (0)