Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Move logic from silverstripe/cms into central place #11439

Draft
wants to merge 1 commit into
base: 6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -677,4 +704,18 @@ 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;
}
}
15 changes: 5 additions & 10 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3511,14 +3511,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 @@ -3532,11 +3529,9 @@ public function flushCache($persistent = true)
}
}

$this->extend('onFlushCache');

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

/**
Expand All @@ -3563,7 +3558,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
10 changes: 10 additions & 0 deletions src/ORM/Hierarchy/Hierarchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,16 @@ public function getBreadcrumbs($separator = ' » ')
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)
Expand Down
10 changes: 0 additions & 10 deletions src/Security/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,6 @@ public function stageChildren()
->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.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/php/ORM/DBEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testObsoleteValues()
// Test values with a record
$obj->Colour = 'Red';
$obj->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();

$this->assertEquals(
['Red', 'Blue', 'Green'],
Expand All @@ -104,7 +104,7 @@ public function testObsoleteValues()

// 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
Expand Down Expand Up @@ -135,7 +135,7 @@ public function testObsoleteValues()
// If obsolete records are deleted, the extra values go away
$obj->delete();
$obj2->delete();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
['Blue', 'Green'],
$colourField->getEnumObsolete()
Expand Down
8 changes: 4 additions & 4 deletions tests/php/ORM/DataObjectSchemaGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testClassNameSpecGeneration()
$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
Expand All @@ -215,7 +215,7 @@ public function testClassNameSpecGeneration()
// Test with instance of subclass
$item1 = new TestIndexObject();
$item1->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
[
TestObject::class,
Expand All @@ -228,7 +228,7 @@ public function testClassNameSpecGeneration()
// Test with instance of main class
$item2 = new TestObject();
$item2->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
[
TestObject::class,
Expand All @@ -243,7 +243,7 @@ public function testClassNameSpecGeneration()
$item1->write();
$item2 = new TestObject();
$item2->write();
DBEnum::flushCache();
DBEnum::clearStaticCache();
$this->assertEquals(
[
TestObject::class,
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ public function testSuccessfulLoginAttempts()
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
Expand Down
Loading