Skip to content

Commit

Permalink
API Move logic from silverstripe/cms into central place
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 5, 2024
1 parent 6b33b5a commit da91400
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 33 deletions.
41 changes: 41 additions & 0 deletions src/Model/ModelData.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,38 @@ class ModelData

private array $objCache = [];

private $_cache_statusFlags = null;

public function __construct()
{
// no-op
}

/**
* Flags provides the user with additional data about the current page status.
*
* Mostly this is used for versioning, but can be used for other purposes (e.g. localisation).
* Each page can have more than one status flag.
*
* Returns an associative array of a unique key to a (localized) title for the flag.
* The unique key can be reused as a CSS class.
*
* Example (simple):
* "deletedonlive" => "Deleted"
*
* Example (with optional title attribute):
* "deletedonlive" => ['text' => "Deleted", 'title' => 'This page has been deleted']
*/
public function getStatusFlags(bool $cached = true): array
{
if (!$this->_cache_statusFlags || !$cached) {
$flags = [];
$this->extend('updateStatusFlags', $flags);
$this->_cache_statusFlags = $flags;
}
return $this->_cache_statusFlags;
}

// -----------------------------------------------------------------------------------------------------------------

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

/**
* Clears record-specific cached data.
*
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
* When false will just clear session-local cached data
*/
public function flushCache(bool $persistent = true): static
{
$this->objCacheClear();
$this->_cache_statusFlags = null;
$this->extend('onFlushCache');
return $this;
}

/**
* Generate the cache name for a field
*/
Expand Down
15 changes: 5 additions & 10 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3509,14 +3509,11 @@ public static function get_one($callerClass = null, $filter = "", $cache = true,
}

/**
* Flush the cached results for all relations (has_one, has_many, many_many)
* Also clears any cached aggregate data.
* @inheritDoc
*
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
* When false will just clear session-local cached data
* @return static $this
* Also flush the cached results for all relations (has_one, has_many, many_many)
*/
public function flushCache($persistent = true)
public function flushCache(bool $persistent = true): static
{
if (static::class == DataObject::class) {
DataObject::$_cache_get_one = [];
Expand All @@ -3530,11 +3527,9 @@ public function flushCache($persistent = true)
}
}

$this->extend('onFlushCache');

$this->components = [];
$this->eagerLoadedData = [];
return $this;
return parent::flushCache($persistent);
}

/**
Expand All @@ -3561,7 +3556,7 @@ public static function flush_and_destroy_cache()
*/
public static function reset()
{
DBEnum::flushCache();
DBEnum::clearStaticCache();
ClassInfo::reset_db_cache();
static::getSchema()->reset();
DataObject::$_cache_get_one = [];
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/FieldType/DBEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DBEnum extends DBString
/**
* Clear all cached enum values.
*/
public static function flushCache(): void
public static function clearStaticCache(): void
{
DBEnum::$enum_cache = [];
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public function getEnum(): array
* If table or name are not set, or if it is not a valid field on the given table,
* then only known enum values are returned.
*
* Values cached in this method can be cleared via `DBEnum::flushCache();`
* Values cached in this method can be cleared via `DBEnum::clearStaticCache();`
*/
public function getEnumObsolete(): array
{
Expand Down
Loading

0 comments on commit da91400

Please sign in to comment.