Skip to content

Commit 53106bc

Browse files
committed
API Move logic from silverstripe/cms into central place
1 parent bbd8bb9 commit 53106bc

File tree

9 files changed

+412
-39
lines changed

9 files changed

+412
-39
lines changed

src/Model/ModelData.php

+41
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,38 @@ class ModelData
7474

7575
private array $objCache = [];
7676

77+
private $_cache_statusFlags = null;
78+
7779
public function __construct()
7880
{
7981
// no-op
8082
}
8183

84+
/**
85+
* Flags provides the user with additional data about the current page status.
86+
*
87+
* Mostly this is used for versioning, but can be used for other purposes (e.g. localisation).
88+
* Each page can have more than one status flag.
89+
*
90+
* Returns an associative array of a unique key to a (localized) title for the flag.
91+
* The unique key can be reused as a CSS class.
92+
*
93+
* Example (simple):
94+
* "deletedonlive" => "Deleted"
95+
*
96+
* Example (with optional title attribute):
97+
* "deletedonlive" => ['text' => "Deleted", 'title' => 'This page has been deleted']
98+
*/
99+
public function getStatusFlags(bool $cached = true): array
100+
{
101+
if (!$this->_cache_statusFlags || !$cached) {
102+
$flags = [];
103+
$this->extend('updateStatusFlags', $flags);
104+
$this->_cache_statusFlags = $flags;
105+
}
106+
return $this->_cache_statusFlags;
107+
}
108+
82109
// -----------------------------------------------------------------------------------------------------------------
83110

84111
// FIELD GETTERS & SETTERS -----------------------------------------------------------------------------------------
@@ -545,6 +572,20 @@ public function Debug(): ModelData|string
545572
return ModelDataDebugger::create($this);
546573
}
547574

575+
/**
576+
* Clears record-specific cached data.
577+
*
578+
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
579+
* When false will just clear session-local cached data
580+
*/
581+
public function flushCache(bool $persistent = true): static
582+
{
583+
$this->objCacheClear();
584+
$this->_cache_statusFlags = null;
585+
$this->extend('onFlushCache');
586+
return $this;
587+
}
588+
548589
/**
549590
* Generate the cache name for a field
550591
*/

src/ORM/DataObject.php

+37-16
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,22 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
116116
{
117117
/**
118118
* Human-readable singular name.
119-
* @var string
120-
* @config
121119
*/
122-
private static $singular_name = null;
120+
private static ?string $singular_name = null;
123121

124122
/**
125123
* Human-readable plural name
126-
* @var string
127-
* @config
128124
*/
129-
private static $plural_name = null;
125+
private static ?string $plural_name = null;
126+
127+
/**
128+
* Description of the class.
129+
* Unlike most configuration, this is usually used uninherited, meaning it should be defined
130+
* on each subclass.
131+
*
132+
* Used in some areas of the CMS, e.g. when selecting what type of record to create.
133+
*/
134+
private static ?string $class_description = null;
130135

131136
/**
132137
* @config
@@ -946,6 +951,27 @@ public function i18n_plural_name()
946951
return _t(static::class . '.PLURALNAME', $this->plural_name());
947952
}
948953

954+
/**
955+
* Get description for this class
956+
*/
957+
public function classDescription(): ?string
958+
{
959+
return static::config()->get('class_description', Config::UNINHERITED);
960+
}
961+
962+
/**
963+
* Get localised description for this class
964+
*/
965+
public function i18nClassDescription(): ?string
966+
{
967+
$placeholder = 'PLACEHOLDER_DESCRIPTION';
968+
$description = _t(static::class.'.CLASS_DESCRIPTION', $this->classDescription() ?? $placeholder);
969+
if ($description === $placeholder) {
970+
return null;
971+
}
972+
return $description;
973+
}
974+
949975
/**
950976
* Standard implementation of a title/label for a specific
951977
* record. Tries to find properties 'Title' or 'Name',
@@ -3515,14 +3541,11 @@ public static function get_one($callerClass = null, $filter = "", $cache = true,
35153541
}
35163542

35173543
/**
3518-
* Flush the cached results for all relations (has_one, has_many, many_many)
3519-
* Also clears any cached aggregate data.
3544+
* @inheritDoc
35203545
*
3521-
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
3522-
* When false will just clear session-local cached data
3523-
* @return static $this
3546+
* Also flush the cached results for all relations (has_one, has_many, many_many)
35243547
*/
3525-
public function flushCache($persistent = true)
3548+
public function flushCache(bool $persistent = true): static
35263549
{
35273550
if (static::class == DataObject::class) {
35283551
DataObject::$_cache_get_one = [];
@@ -3536,11 +3559,9 @@ public function flushCache($persistent = true)
35363559
}
35373560
}
35383561

3539-
$this->extend('onFlushCache');
3540-
35413562
$this->components = [];
35423563
$this->eagerLoadedData = [];
3543-
return $this;
3564+
return parent::flushCache($persistent);
35443565
}
35453566

35463567
/**
@@ -3567,7 +3588,7 @@ public static function flush_and_destroy_cache()
35673588
*/
35683589
public static function reset()
35693590
{
3570-
DBEnum::flushCache();
3591+
DBEnum::clearStaticCache();
35713592
ClassInfo::reset_db_cache();
35723593
static::getSchema()->reset();
35733594
DataObject::$_cache_get_one = [];

src/ORM/FieldType/DBEnum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DBEnum extends DBString
4444
/**
4545
* Clear all cached enum values.
4646
*/
47-
public static function flushCache(): void
47+
public static function clearStaticCache(): void
4848
{
4949
DBEnum::$enum_cache = [];
5050
}
@@ -182,7 +182,7 @@ public function getEnum(): array
182182
* If table or name are not set, or if it is not a valid field on the given table,
183183
* then only known enum values are returned.
184184
*
185-
* Values cached in this method can be cleared via `DBEnum::flushCache();`
185+
* Values cached in this method can be cleared via `DBEnum::clearStaticCache();`
186186
*/
187187
public function getEnumObsolete(): array
188188
{

0 commit comments

Comments
 (0)