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
This logic is used by CMSMain but needs to be callable on any
hierarchical model.

In some cases this logic is generic enough it could be on any model or
any DataObject
  • Loading branch information
GuySartorelli committed Nov 14, 2024
1 parent eb8907f commit 43bdde5
Show file tree
Hide file tree
Showing 19 changed files with 928 additions and 66 deletions.
38 changes: 38 additions & 0 deletions src/Model/ModelData.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class ModelData

private array $objCache = [];

private $_cache_statusFlags = null;

public function __construct()
{
// no-op
Expand Down Expand Up @@ -487,6 +489,31 @@ public function hasValue(string $field, array $arguments = [], bool $cache = tru

// UTILITY METHODS -------------------------------------------------------------------------------------------------

/**
* 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;
}

/**
* Find appropriate templates for SSViewer to use to render this object
*/
Expand Down Expand Up @@ -545,6 +572,17 @@ public function Debug(): ModelData|string
return ModelDataDebugger::create($this);
}

/**
* Clears record-specific cached data.
*/
public function flushCache(): static
{
$this->objCacheClear();
$this->_cache_statusFlags = null;
$this->extend('onFlushCache');
return $this;
}

/**
* Generate the cache name for a field
*/
Expand Down
59 changes: 22 additions & 37 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,22 @@ 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.
* Unlike most configuration, this is usually used uninherited, meaning it should be defined
* on each subclass.
*
* Used in some areas of the CMS, e.g. when selecting what type of record to create.
*/
private static ?string $class_description = null;

/**
* Description of the class.
Expand All @@ -150,7 +155,6 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
* @var string
*/
private static $default_classname = null;

/**
* Whether this DataObject class must only use the primary database and not a read-only replica
* Note that this will be only be enforced when using DataQuery::execute() or
Expand Down Expand Up @@ -957,40 +961,23 @@ public function i18n_plural_name()

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

/**
* Get localised description for this class
* @return null|string
*/
public function i18n_classDescription()
public function i18n_classDescription(): ?string
{
$notDefined = 'NOT_DEFINED';
$baseDescription = $this->classDescription() ?? $notDefined;

// Check the new i18n key first
$description = _t(static::class . '.CLASS_DESCRIPTION', $baseDescription);
if ($description !== $baseDescription) {
return $description;
}

// Fall back on the deprecated localisation key
$legacyI18n = _t(static::class . '.DESCRIPTION', $baseDescription);
if ($legacyI18n !== $baseDescription) {
return $legacyI18n;
}

// If there was no description available in config nor in i18n, return null
if ($baseDescription === $notDefined) {
$description = _t(static::class.'.CLASS_DESCRIPTION', $this->classDescription() ?? $notDefined);
if ($description === $notDefined) {
return null;
}
// Return raw description
return $baseDescription;
return $description;
}

/**
Expand Down Expand Up @@ -3562,14 +3549,14 @@ 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
*
* Also flush the cached results for all relations (has_one, has_many, many_many)
*
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
* @param bool $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
*/
public function flushCache($persistent = true)
public function flushCache(bool $persistent = true): static
{
if (static::class == DataObject::class) {
DataObject::$_cache_get_one = [];
Expand All @@ -3583,11 +3570,9 @@ public function flushCache($persistent = true)
}
}

$this->extend('onFlushCache');

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

/**
Expand Down
11 changes: 0 additions & 11 deletions src/ORM/FieldType/DBEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Validation\FieldValidation\OptionFieldValidator;
use SilverStripe\Core\Resettable;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\SelectField;
Expand Down Expand Up @@ -43,16 +42,6 @@ class DBEnum extends DBString implements Resettable
*/
protected static array $enum_cache = [];

/**
* Clear all cached enum values.
* @deprecated 5.4.0 Use reset() instead.
*/
public static function flushCache(): void
{
Deprecation::notice('5.4.0', 'Use reset() instead.');
static::reset();
}

public static function reset(): void
{
DBEnum::$enum_cache = [];
Expand Down
Loading

0 comments on commit 43bdde5

Please sign in to comment.