API Move logic from silverstripe/cms into central place

This commit is contained in:
Guy Sartorelli 2024-10-22 15:16:13 +13:00
parent 6bb9a0b33d
commit 72fe36f79e
No known key found for this signature in database
8 changed files with 66 additions and 30 deletions

View File

@ -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 -----------------------------------------------------------------------------------------
@ -677,4 +704,18 @@ class ModelData
{
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;
}
}

View File

@ -3511,14 +3511,11 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
}
/**
* 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 = [];
@ -3532,11 +3529,9 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
}
}
$this->extend('onFlushCache');
$this->components = [];
$this->eagerLoadedData = [];
return $this;
return parent::flushCache($persistent);
}
/**
@ -3563,7 +3558,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
*/
public static function reset()
{
DBEnum::flushCache();
DBEnum::clearStaticCache();
ClassInfo::reset_db_cache();
static::getSchema()->reset();
DataObject::$_cache_get_one = [];

View File

@ -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 = [];
}
@ -176,7 +176,7 @@ class DBEnum extends DBString
* 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
{

View File

@ -567,6 +567,16 @@ class Hierarchy extends Extension
return implode($separator ?? '', $crumbs);
}
/**
* Get the title that will be used in TreeDropdownField and other tree structures.
*/
public function getTreeTitle(): string
{
$title = $this->getOwner()->MenuTitle ?? $this->getOwner()->Title;
$this->getOwner()->extend('updateTreeTitle', $title);
return $title; // @TODO see if we need to escape this (it was escaped in Group and is in File too)
}
/**
* Flush all Hierarchy caches:
* - Children (instance)

View File

@ -494,16 +494,6 @@ class Group extends DataObject
->sort('"Sort"');
}
/**
* @return string
*/
public function getTreeTitle()
{
$title = htmlspecialchars($this->Title ?? '', ENT_QUOTES);
$this->extend('updateTreeTitle', $title);
return $title;
}
/**
* Overloaded to ensure the code is always descent.
*

View File

@ -95,7 +95,7 @@ class DBEnumTest extends SapphireTest
// Test values with a record
$obj->Colour = 'Red';
$obj->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
['Red', 'Blue', 'Green'],
@ -104,7 +104,7 @@ class DBEnumTest extends SapphireTest
// If the value is removed from the enum, obsolete content is still retained
$colourField->setEnum(['Blue', 'Green', 'Purple']);
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
['Blue', 'Green', 'Purple', 'Red'], // Red on the end now, because it's obsolete
@ -135,7 +135,7 @@ class DBEnumTest extends SapphireTest
// If obsolete records are deleted, the extra values go away
$obj->delete();
$obj2->delete();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
['Blue', 'Green'],
$colourField->getEnumObsolete()

View File

@ -197,7 +197,7 @@ class DataObjectSchemaGenerationTest extends SapphireTest
$schema = DataObject::getSchema();
// Test with blank entries
DBEnum::flushCache();
DBEnum::clearStaticCache();
$do1 = new TestObject();
$fields = $schema->databaseFields(TestObject::class, false);
// May be overridden from DBClassName to DBClassNameVarchar by config
@ -215,7 +215,7 @@ class DataObjectSchemaGenerationTest extends SapphireTest
// Test with instance of subclass
$item1 = new TestIndexObject();
$item1->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
[
TestObject::class,
@ -228,7 +228,7 @@ class DataObjectSchemaGenerationTest extends SapphireTest
// Test with instance of main class
$item2 = new TestObject();
$item2->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
[
TestObject::class,
@ -243,7 +243,7 @@ class DataObjectSchemaGenerationTest extends SapphireTest
$item1->write();
$item2 = new TestObject();
$item2->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
[
TestObject::class,

View File

@ -681,7 +681,7 @@ class SecurityTest extends FunctionalTest
public function testDatabaseIsReadyWithInsufficientMemberColumns()
{
Security::clear_database_is_ready();
DBEnum::flushCache();
DBEnum::clearStaticCache();
// Assumption: The database has been built correctly by the test runner,
// and has all columns present in the ORM