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 7, 2024
1 parent 6b33b5a commit b52073a
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 39 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
50 changes: 34 additions & 16 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,19 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
{
/**
* Human-readable singular name.
* @var string
* @config
*/
private static $singular_name = null;
private static ?string $singular_name = null;

/**
* Human-readable plural name
* @var string
* @config
*/
private static $plural_name = null;
private static ?string $plural_name = null;

/**
* Description of the class.
* Used in some areas of the CMS, e.g. when selecting what type of record to create.
*/
private static ?string $class_description = null;

/**
* @config
Expand Down Expand Up @@ -946,6 +948,27 @@ public function i18n_plural_name()
return _t(static::class . '.PLURALNAME', $this->plural_name());
}

/**
* Get description for this class
*/
public function classDescription(): ?string
{
return static::config()->get('class_description');
}

/**
* Get localised description for this class
*/
public function i18nClassDescription(): ?string
{
$placeholder = 'PLACEHOLDER_DESCRIPTION';
$description = _t(static::class.'.CLASS_DESCRIPTION', $this->classDescription() ?? $placeholder);
if ($description === $placeholder) {
return null;
}
return $description;
}

/**
* Standard implementation of a title/label for a specific
* record. Tries to find properties 'Title' or 'Name',
Expand Down Expand Up @@ -3509,14 +3532,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 +3550,9 @@ public function flushCache($persistent = true)
}
}

$this->extend('onFlushCache');

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

/**
Expand All @@ -3561,7 +3579,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 b52073a

Please sign in to comment.