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 11, 2024
1 parent bbd8bb9 commit 2a11c1e
Show file tree
Hide file tree
Showing 23 changed files with 1,055 additions and 69 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
55 changes: 39 additions & 16 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;

/**
* @config
Expand All @@ -141,7 +146,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 @@ -946,6 +950,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', Config::UNINHERITED);
}

/**
* Get localised description for this class
*/
public function i18n_classDescription(): ?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 @@ -3515,14 +3540,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 @@ -3536,11 +3561,9 @@ public function flushCache($persistent = true)
}
}

$this->extend('onFlushCache');

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

/**
Expand All @@ -3567,7 +3590,7 @@ public static function flush_and_destroy_cache()
*/
public static function reset()
{
DBEnum::flushCache();
DBEnum::reset();
ClassInfo::reset_db_cache();
static::getSchema()->reset();
DataObject::$_cache_get_one = [];
Expand Down
7 changes: 4 additions & 3 deletions src/ORM/FieldType/DBEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\SelectField;
use SilverStripe\Core\ArrayLib;
use SilverStripe\Core\Resettable;
use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\ORM\DB;
use SilverStripe\Model\ModelData;
Expand All @@ -17,7 +18,7 @@
*
* See {@link DropdownField} for a {@link FormField} to select enum values.
*/
class DBEnum extends DBString
class DBEnum extends DBString implements Resettable
{
private static array $field_validators = [
OptionFieldValidator::class => ['getEnum'],
Expand All @@ -44,7 +45,7 @@ class DBEnum extends DBString
/**
* Clear all cached enum values.
*/
public static function flushCache(): void
public static function reset(): void
{
DBEnum::$enum_cache = [];
}
Expand Down Expand Up @@ -182,7 +183,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::reset();`
*/
public function getEnumObsolete(): array
{
Expand Down
Loading

0 comments on commit 2a11c1e

Please sign in to comment.