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

NEW: Static methods replaced with non-static singleton pattern. #55

Merged
merged 1 commit into from
Mar 28, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/Handler/Elemental/ArchiveElementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function createSnapshot(EventContextInterface $context): ?Snapshot
return null;
}

$archivedBlock = SnapshotPublishable::get_at_last_snapshot(BaseElement::class, $blockID);
$archivedBlock = SnapshotPublishable::singleton()->getAtLastSnapshotByClassAndId(BaseElement::class, $blockID);

if (!$archivedBlock) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Elemental/ModifyElementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function createSnapshot(EventContextInterface $context): ?Snapshot
}

foreach ($snapshot->Items() as $item) {
if (!static::hashSnapshotCompare($item->getItem(), $block)) {
if (!$this->hashSnapshotCompare($item->getItem(), $block)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Form/UnpublishHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function createSnapshot(EventContextInterface $context): ?Snapshot
}

foreach ($snapshot->Items() as $item) {
if (!static::hashSnapshotCompare($item->getItem(), $record)) {
if (!$this->hashSnapshotCompare($item->getItem(), $record)) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function getIsLiveSnapshot(): bool
return false;
}

$liveVersionNumber = SnapshotPublishable::get_published_version_number(
$liveVersionNumber = SnapshotPublishable::singleton()->getPublishedVersionNumber(
$originVersion->baseClass(),
$originVersion->ID
);
Expand All @@ -217,7 +217,7 @@ public function getIsLiveSnapshot(): bool
->setFrom("\"$table\"")
->addWhere([
"\"$table\".\"Version\" = ?" => $liveVersionNumber,
"\"$table\".\"ObjectHash\" = ?" => static::hashObjectForSnapshot($originVersion),
"\"$table\".\"ObjectHash\" = ?" => $this->hashObjectForSnapshot($originVersion),
])
->execute()
->value();
Expand Down Expand Up @@ -283,7 +283,7 @@ public function onBeforeWrite(): void
{
parent::onBeforeWrite();

$this->OriginHash = static::hashForSnapshot($this->OriginClass, $this->OriginID);
$this->OriginHash = $this->hashForSnapshot($this->OriginClass, $this->OriginID);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/SnapshotHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait SnapshotHasher
* @param int|null $id
* @return string
*/
public static function hashForSnapshot(?string $class, ?int $id): string
public function hashForSnapshot(?string $class, ?int $id): string
{
return md5(sprintf('%s:%s', $class, $id));
}
Expand All @@ -27,18 +27,18 @@ public static function hashForSnapshot(?string $class, ?int $id): string
* @param DataObject $obj
* @return string
*/
public static function hashObjectForSnapshot(DataObject $obj): string
public function hashObjectForSnapshot(DataObject $obj): string
{
return static::hashForSnapshot($obj->baseClass(), $obj->ID);
return $this->hashForSnapshot($obj->baseClass(), $obj->ID);
}

/**
* @param DataObject $obj1
* @param DataObject $obj2
* @return bool
*/
public static function hashSnapshotCompare(DataObject $obj1, DataObject $obj2): bool
public function hashSnapshotCompare(DataObject $obj1, DataObject $obj2): bool
{
return static::hashObjectForSnapshot($obj1) === static::hashObjectForSnapshot($obj2);
return $this->hashObjectForSnapshot($obj1) === $this->hashObjectForSnapshot($obj2);
}
}
4 changes: 2 additions & 2 deletions src/SnapshotItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function onBeforeWrite()
{
parent::onBeforeWrite();

$this->ObjectHash = static::hashForSnapshot($this->ObjectClass, $this->ObjectID);
$this->ObjectHash = $this->hashForSnapshot($this->ObjectClass, $this->ObjectID);
}

/**
Expand Down Expand Up @@ -217,7 +217,7 @@ public function hydrateFromDataObject(DataObject $object): self
} else {
// Track publish state for non-versioned owners, they're always in a published state.
$exists = SnapshotItem::get()->filter([
'ObjectHash' => static::hashObjectForSnapshot($object)
'ObjectHash' => $this->hashObjectForSnapshot($object)
]);
$this->WasCreated = !$exists->exists();
$this->WasPublished = true;
Expand Down
82 changes: 56 additions & 26 deletions src/SnapshotPublishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Exception;
use InvalidArgumentException;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Resettable;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
Expand All @@ -18,9 +20,10 @@
*
* @property DataObject|SnapshotPublishable|Versioned $owner
*/
class SnapshotPublishable extends RecursivePublishable
class SnapshotPublishable extends RecursivePublishable implements Resettable
{

use Injectable;
use SnapshotHasher;

/**
Expand All @@ -32,7 +35,17 @@ class SnapshotPublishable extends RecursivePublishable
/**
* @var array
*/
private static $relationDiffs = [];
private $relationDiffs = [];

public function flushCachedData(): void
{
$this->relationDiffs = [];
}

public static function reset()
{
static::singleton()->flushCachedData();
}

/**
* A more resilient wrapper for the Versioned function that holds up against un-staged versioned
Expand All @@ -42,7 +55,7 @@ class SnapshotPublishable extends RecursivePublishable
* @param int $id
* @return int|null
*/
public static function get_published_version_number(string $class, int $id): ?int
public function getPublishedVersionNumber(string $class, int $id): ?int
{
$inst = DataObject::singleton($class);

Expand All @@ -68,7 +81,7 @@ public static function get_published_version_number(string $class, int $id): ?in
* @param string|int $snapshot A snapshot ID or a Y-m-d h:i:s date formatted string
* @return DataObject|null
*/
public static function get_at_snapshot(string $class, int $id, $snapshot): ?DataObject
public function getAtSnapshotByClassAndId(string $class, int $id, $snapshot): ?DataObject
{
$baseClass = DataObject::getSchema()->baseDataClass($class);

Expand All @@ -94,34 +107,34 @@ public static function get_at_snapshot(string $class, int $id, $snapshot): ?Data
return $list->byID($id);
}

public static function get_at_last_snapshot(string $class, int $id): ?DataObject
public function getAtLastSnapshotByClassAndId(string $class, int $id): ?DataObject
{
/** @var SnapshotItem $lastItem */
$lastItem = static::get_last_snapshot_item($class, $id);
$lastItem = $this->getLastSnapshotItemByClassAndId($class, $id);

if (!$lastItem) {
return null;
}

return static::get_at_snapshot($class, $id, $lastItem->SnapshotID);
return $this->getAtSnapshotByClassAndId($class, $id, $lastItem->SnapshotID);
}

/**
* @param string $class
* @param int $id
* @return DataObject|null
*/
public static function get_last_snapshot_item(string $class, int $id): ?DataObject
public function getLastSnapshotItemByClassAndId(string $class, int $id): ?DataObject
{
return SnapshotItem::get()
->sort('Created', 'DESC')
->find('ObjectHash', static::hashForSnapshot($class, $id));
->find('ObjectHash', $this->hashForSnapshot($class, $id));
}

/**
* @return DataList
*/
public static function getSnapshots(): DataList
public function getSnapshots(): DataList
{
$snapshotTable = DataObject::getSchema()->tableName(Snapshot::class);
$itemTable = DataObject::getSchema()->tableName(SnapshotItem::class);
Expand All @@ -138,7 +151,7 @@ public function getRelevantSnapshots(): DataList
$itemTable = DataObject::getSchema()->tableName(SnapshotItem::class);
$snapshots = $this->owner->getSnapshots()
->where([
["\"$itemTable\".\"ObjectHash\" = ?" => static::hashObjectForSnapshot($this->owner)]
["\"$itemTable\".\"ObjectHash\" = ?" => $this->hashObjectForSnapshot($this->owner)]
]);

$this->owner->extend('updateRelevantSnapshots', $snapshots);
Expand Down Expand Up @@ -177,7 +190,7 @@ public function getSnapshotsSinceVersion($sinceVersion): DataList
), 0)",
$itemTable
) => [
static::hashObjectForSnapshot($this->owner),
$this->hashObjectForSnapshot($this->owner),
$sinceVersion,
],
],
Expand All @@ -194,7 +207,7 @@ public function getSnapshotsSinceLastPublish(): DataList
{
$class = $this->owner->baseClass();
$id = $this->owner->ID;
$publishedVersion = static::get_published_version_number($class, $id);
$publishedVersion = $this->getPublishedVersionNumber($class, $id);

return $this->owner->getSnapshotsSinceVersion($publishedVersion);
}
Expand All @@ -212,7 +225,7 @@ protected function getSnapshotsBetweenVersionsFilters(int $min, ?int $max = null
{
$itemTable = DataObject::getSchema()->tableName(SnapshotItem::class);

$hash = static::hashObjectForSnapshot($this->owner);
$hash = $this->hashObjectForSnapshot($this->owner);
$minShot = SQLSelect::create(
"MIN(\"$itemTable\".\"SnapshotID\")",
"\"$itemTable\"",
Expand Down Expand Up @@ -270,7 +283,7 @@ public function hasOwnedModifications(): bool

$class = $this->owner->baseClass();
$id = $this->owner->ID;
$minVersion = static::get_published_version_number($class, $id);
$minVersion = $this->getPublishedVersionNumber($class, $id);

if (is_null($minVersion)) {
return false; //Draft page.
Expand Down Expand Up @@ -319,7 +332,7 @@ public function getPublishableObjects(): ArrayList
$id = $row['ObjectID'];
/** @var DataObject|SnapshotPublishable $obj */
$obj = DataObject::get_by_id($class, $id);
$map[static::hashObjectForSnapshot($obj)] = $obj;
$map[$this->hashObjectForSnapshot($obj)] = $obj;
}

return ArrayList::create(array_values($map));
Expand Down Expand Up @@ -352,7 +365,7 @@ public function getRelationTracking(): array
*/
public function getAtSnapshot($snapshot): ?DataObject
{
return static::get_at_snapshot($this->owner->baseClass(), $this->owner->ID, $snapshot);
return $this->getAtSnapshotByClassAndId($this->owner->baseClass(), $this->owner->ID, $snapshot);
}

/**
Expand All @@ -366,7 +379,7 @@ public function getAtLastSnapshot(): ?DataObject
return null;
}

return static::get_at_snapshot($this->owner->baseClass(), $this->owner->ID, $lastItem->SnapshotID);
return $this->getAtSnapshotByClassAndId($this->owner->baseClass(), $this->owner->ID, $lastItem->SnapshotID);
}

/**
Expand All @@ -377,7 +390,7 @@ public function onAfterRevertToLive(): void
{
$snapshots = $this->getSnapshotsSinceVersion($this->owner->Version)
->filter([
'OriginHash' => static::hashObjectForSnapshot($this->owner),
'OriginHash' => $this->hashObjectForSnapshot($this->owner),
]);

$snapshots->removeAll();
Expand All @@ -390,7 +403,7 @@ public function getPreviousSnapshotItem(): ?DataObject
{
return SnapshotItem::get()
->sort('Created', 'DESC')
->find('ObjectHash', static::hashObjectForSnapshot($this->owner));
->find('ObjectHash', $this->hashObjectForSnapshot($this->owner));
}

/**
Expand All @@ -403,7 +416,7 @@ public function atPreviousSnapshot(callable $callback)
// timestamp prior to now, because the Version may be unchanged.
$lastSnapshot = SnapshotItem::get()
->filter([
'ObjectHash' => static::hashObjectForSnapshot($this->owner),
'ObjectHash' => $this->hashObjectForSnapshot($this->owner),
])
->max('LastEdited');

Expand Down Expand Up @@ -440,12 +453,27 @@ public function isModifiedSinceLastSnapshot(): bool
}

/**
* @param bool $cache
* @return RelationDiffer[]
* @throws Exception
* TODO Memoise / cache / enable cache once it's confirmed that this feature is needed
*/
public function getRelationDiffs(): array
public function getRelationDiffs(bool $cache = true): array
{
$cacheKey = $this->owner->isInDB()
? sprintf(
'%s-%s',
$this->owner->getUniqueKey(),
$this->hashObjectForSnapshot($this->owner)
)
: '';
// TODO in-memory cache disabled until we can confirm that we need it
$cacheKey = '';

if ($cache && $cacheKey && array_key_exists($cacheKey, $this->relationDiffs)) {
return $this->relationDiffs[$cacheKey];
}

$diffs = [];
$previousTracking = $this->owner->atPreviousSnapshot(function ($date) {
if (!$date) {
Expand All @@ -471,7 +499,9 @@ public function getRelationDiffs(): array
$diffs[] = RelationDiffer::create($class, $type, $prevMap, $currentMap);
}

static::$relationDiffs[static::hashObjectForSnapshot($this->owner)] = $diffs;
if ($cacheKey) {
$this->relationDiffs[$cacheKey] = $diffs;
}

return $diffs;
}
Expand Down Expand Up @@ -624,7 +654,7 @@ public function reconcileOwnershipChanges(?DataObject $previous = null): void
$currentOwner = $spec['current'];
$currentOwners = array_merge([$currentOwner], $currentOwner->findOwners()->toArray());

$previousHashes = array_map([static::class, 'hashObjectForSnapshot'], $previousOwners);
$previousHashes = array_map([$this, 'hashObjectForSnapshot'], $previousOwners);

// Get the earliest snapshot where the previous owner was published.
$cutoff = $previousOwner->getSnapshotsSinceLastPublish()
Expand Down Expand Up @@ -684,7 +714,7 @@ public function getIntermediaryObjects(): array
$extraObjects = [];

foreach ($intermediaryObjects as $extra) {
$extraObjects[SnapshotHasher::hashObjectForSnapshot($extra)] = $extra;
$extraObjects[$this->hashObjectForSnapshot($extra)] = $extra;
}

return $extraObjects;
Expand Down Expand Up @@ -732,7 +762,7 @@ public function getActivityFeed(?int $minVersion = null, ?int $maxVersion = null
if (is_null($minVersion)) {
$class = $this->owner->baseClass();
$id = $this->owner->ID;
$minVersion = static::get_published_version_number($class, $id);
$minVersion = $this->getPublishedVersionNumber($class, $id);

if (is_null($minVersion)) {
$minVersion = 1;
Expand Down
12 changes: 6 additions & 6 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1489,11 +1489,11 @@ private function assertActivityContains(ArrayList $activity, array $objs = []):
}

$expectedHash = $obj->isInDB()
? SnapshotPublishable::hashObjectForSnapshot($obj)
: SnapshotPublishable::hashForSnapshot($obj->ClassName, $obj->OldID);
? SnapshotPublishable::singleton()->hashObjectForSnapshot($obj)
: SnapshotPublishable::singleton()->hashForSnapshot($obj->ClassName, $obj->OldID);
$this->assertEquals(
$expectedHash,
SnapshotPublishable::hashObjectForSnapshot($entry->Subject)
SnapshotPublishable::singleton()->hashObjectForSnapshot($entry->Subject)
);
$this->assertEquals($action, $entry->Action);
}
Expand All @@ -1512,11 +1512,11 @@ private function assertPublishableObjectsContains(ArrayList $items, array $objs

$obj= $objs[$i];
$expectedHash = $obj->isInDB()
? SnapshotPublishable::hashObjectForSnapshot($obj)
: SnapshotPublishable::hashForSnapshot($obj->ClassName, $obj->OldID);
? SnapshotPublishable::singleton()->hashObjectForSnapshot($obj)
: SnapshotPublishable::singleton()->hashForSnapshot($obj->ClassName, $obj->OldID);
$this->assertEquals(
$expectedHash,
SnapshotPublishable::hashObjectForSnapshot($dataObject)
SnapshotPublishable::singleton()->hashObjectForSnapshot($dataObject)
);
}
}
Expand Down
Loading