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 array $objCache = [];
private $_cache_statusFlags = null;
public function __construct() public function __construct()
{ {
// no-op // 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 ----------------------------------------------------------------------------------------- // FIELD GETTERS & SETTERS -----------------------------------------------------------------------------------------
@ -677,4 +704,18 @@ class ModelData
{ {
return ModelDataDebugger::create($this); 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) * @inheritDoc
* Also clears any cached aggregate data.
* *
* @param boolean $persistent When true will also clear persistent data stored in the Cache system. * Also flush the cached results for all relations (has_one, has_many, many_many)
* When false will just clear session-local cached data
* @return static $this
*/ */
public function flushCache($persistent = true) public function flushCache(bool $persistent = true): static
{ {
if (static::class == DataObject::class) { if (static::class == DataObject::class) {
DataObject::$_cache_get_one = []; DataObject::$_cache_get_one = [];
@ -3532,11 +3529,9 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
} }
} }
$this->extend('onFlushCache');
$this->components = []; $this->components = [];
$this->eagerLoadedData = []; $this->eagerLoadedData = [];
return $this; return parent::flushCache($persistent);
} }
/** /**
@ -3563,7 +3558,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
*/ */
public static function reset() public static function reset()
{ {
DBEnum::flushCache(); DBEnum::clearStaticCache();
ClassInfo::reset_db_cache(); ClassInfo::reset_db_cache();
static::getSchema()->reset(); static::getSchema()->reset();
DataObject::$_cache_get_one = []; DataObject::$_cache_get_one = [];

View File

@ -38,7 +38,7 @@ class DBEnum extends DBString
/** /**
* Clear all cached enum values. * Clear all cached enum values.
*/ */
public static function flushCache(): void public static function clearStaticCache(): void
{ {
DBEnum::$enum_cache = []; 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, * 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. * 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 public function getEnumObsolete(): array
{ {

View File

@ -567,6 +567,16 @@ class Hierarchy extends Extension
return implode($separator ?? '', $crumbs); 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: * Flush all Hierarchy caches:
* - Children (instance) * - Children (instance)

View File

@ -494,16 +494,6 @@ class Group extends DataObject
->sort('"Sort"'); ->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. * Overloaded to ensure the code is always descent.
* *

View File

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

View File

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

View File

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