Skip to content

Commit

Permalink
API Support new extend*() format for extension methods
Browse files Browse the repository at this point in the history
API Remove deprecated 5.0 methods
  • Loading branch information
Damian Mooyman authored and GuySartorelli committed Aug 23, 2022
1 parent 5eccc51 commit e106638
Showing 1 changed file with 52 additions and 138 deletions.
190 changes: 52 additions & 138 deletions src/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,6 @@ public function augmentDataQueryCreation(SQLSelect &$query, DataQuery &$dataQuer
*/
public function __construct($mode = self::STAGEDVERSIONED)
{
// Handle deprecated behaviour
if ($mode === 'Stage' && func_num_args() === 1) {
Deprecation::notice("5.0", "Versioned now takes a mode as a single parameter");
$mode = static::VERSIONED;
} elseif (is_array($mode) || func_num_args() > 1) {
Deprecation::notice("5.0", "Versioned now takes a mode as a single parameter");
$mode = func_num_args() > 1 || count($mode ?? []) > 1
? static::STAGEDVERSIONED
: static::VERSIONED;
}

if (!in_array($mode, [static::STAGEDVERSIONED, static::VERSIONED])) {
throw new InvalidArgumentException("Invalid mode: {$mode}");
}
Expand Down Expand Up @@ -1460,11 +1449,6 @@ public function onAfterSkippedWrite()
*/
public function canPublish($member = null)
{
// Skip if invoked by extendedCan()
if (func_num_args() > 4) {
return null;
}

if (!$member) {
$member = Security::getCurrentUser();
}
Expand All @@ -1484,6 +1468,12 @@ public function canPublish($member = null)
return $owner->canEdit($member);
}

protected function extendCanPublish()
{
// prevent canPublish() from extending itself
return null;
}

/**
* Check if the current user can delete this record from live
*
Expand All @@ -1492,11 +1482,6 @@ public function canPublish($member = null)
*/
public function canUnpublish($member = null)
{
// Skip if invoked by extendedCan()
if (func_num_args() > 4) {
return null;
}

if (!$member) {
$member = Security::getCurrentUser();
}
Expand All @@ -1516,6 +1501,12 @@ public function canUnpublish($member = null)
return $owner->canPublish($member);
}

protected function extendCanUnpublish()
{
// prevent canUnpublish() extending itself
return null;
}

/**
* Check if the current user is allowed to archive this record.
* If extended, ensure that both canDelete and canUnpublish are extended also
Expand All @@ -1525,11 +1516,6 @@ public function canUnpublish($member = null)
*/
public function canArchive($member = null)
{
// Skip if invoked by extendedCan()
if (func_num_args() > 4) {
return null;
}

if (!$member) {
$member = Security::getCurrentUser();
}
Expand Down Expand Up @@ -1559,6 +1545,12 @@ public function canArchive($member = null)
return true;
}

protected function extendCanArchive()
{
// Prevent canArchive() extending itself
return null;
}

/**
* Check if the user can revert this record to live
*
Expand All @@ -1569,11 +1561,6 @@ public function canRevertToLive($member = null)
{
$owner = $this->owner;

// Skip if invoked by extendedCan()
if (func_num_args() > 4) {
return null;
}

// Can't revert if not on live
if (!$owner->isPublished()) {
return false;
Expand All @@ -1597,6 +1584,12 @@ public function canRevertToLive($member = null)
return $owner->canEdit($member);
}

protected function extendCanRevertToLive()
{
// Prevent canRevertToLive() extending itself
return null;
}

/**
* Check if the user can restore this record to draft
*
Expand Down Expand Up @@ -1630,6 +1623,12 @@ public function canRestoreToDraft($member = null)
return $owner->canEdit($member);
}

protected function extendCanRevertToDraft()
{
// Prevent canRestoreToDraft() extending itself
return null;
}

/**
* Extend permissions to include additional security for objects that are not published to live.
*
Expand Down Expand Up @@ -1815,15 +1814,6 @@ public function latestPublished()
return $draftVersion === $liveVersion;
}

/**
* @deprecated 4.0..5.0
*/
public function doPublish()
{
Deprecation::notice('5.0', 'Use publishRecursive instead');
return $this->owner->publishRecursive();
}

/**
* Publishes this object to Live, but doesn't publish owned objects.
*
Expand Down Expand Up @@ -1963,22 +1953,6 @@ public function doRevertToLive()
return true;
}

/**
* @deprecated 1.2..2.0 This extension method is redundant and will be removed
*/
public function onAfterRevertToLive()
{
}

/**
* @deprecated 4.0..5.0
*/
public function publish($fromStage, $toStage, $createNewVersion = true)
{
Deprecation::notice('5.0', 'Use copyVersionToStage instead');
$this->owner->copyVersionToStage($fromStage, $toStage, true);
}

/**
* Move a database record from one stage to the other.
*
Expand Down Expand Up @@ -2019,16 +1993,6 @@ public function getMigratingVersion()
return $this->owner->getField(self::MIGRATING_VERSION);
}

/**
* @deprecated 4.0...5.0
* @param string $version The version.
*/
public function migrateVersion($version)
{
Deprecation::notice('5.0', 'use setMigratingVersion instead');
$this->setMigratingVersion($version);
}

/**
* Set the migrating version.
*
Expand Down Expand Up @@ -2378,7 +2342,9 @@ public static function current_archived_stage()
*/
public static function set_stage($stage)
{
ReadingMode::validateStage($stage);
if (!in_array($stage, [static::LIVE, static::DRAFT])) {
throw new \InvalidArgumentException("Invalid stage name \"{$stage}\"");
}
static::set_reading_mode('Stage.' . $stage);
}

Expand Down Expand Up @@ -2658,27 +2624,11 @@ public function writeToStage($stage, $forceInsert = false)
{
ReadingMode::validateStage($stage);
$owner = $this->owner;
return static::withVersionedMode(function () use ($stage, $forceInsert, $owner) {
$oldParams = $owner->getSourceQueryParams();
try {
// Lazy load and reset version in current stage prior to resetting write stage
$owner->forceChange();
$owner->Version = null;

// Migrate stage prior to write
Versioned::set_stage($stage);
$owner->setSourceQueryParam('Versioned.mode', 'stage');
$owner->setSourceQueryParam('Versioned.stage', $stage);

// Write
$owner->invokeWithExtensions('onBeforeWriteToStage', $stage, $forceInsert);
return $owner->write(false, $forceInsert);
} finally {
// Revert global state
$owner->invokeWithExtensions('onAfterWriteToStage', $stage, $forceInsert);
$owner->setSourceQueryParams($oldParams);
}
});
$owner->forceChange();
$result = $owner->write(false, $forceInsert);
Versioned::set_reading_mode($oldMode);

return $result;
}

/**
Expand All @@ -2699,58 +2649,22 @@ public function doRollbackTo($version)
$owner->extend('onAfterRollback', $version);
}

/**
* @deprecated 1.2..2.0 This extension method is redundant and will be removed
*/
public function onAfterRollback()
public function onAfterRollback($version)
{
}

/**
* Recursively rollback draft to the given version. This will also rollback any owned objects
* at that point in time to the same date. Objects which didn't exist (or weren't attached)
* to the record at the target point in time will be "unlinked", which dis-associates
* the record without requiring a hard deletion.
*
* @param int|string|null $version Version ID or Versioned::LIVE to rollback from live.
* Pass in null to rollback to the current object
* @return DataObject|Versioned The object rolled back
*/
public function rollbackRecursive($version = null)
{
$owner = $this->owner;
$owner->invokeWithExtensions('onBeforeRollbackRecursive', $version);
$owner->rollbackSingle($version);

// Rollback relations on this item (works on unversioned records too)
$rolledBackOwner = $this->getAtVersion($version);
if ($rolledBackOwner) {
$rolledBackOwner->rollbackRelations($version);
}

// Unlink any objects disowned as a result of this action
// I.e. objects which aren't owned anymore by this record, but are by the old draft record
$rolledBackOwner->unlinkDisownedObjects($rolledBackOwner, Versioned::DRAFT);
$rolledBackOwner->invokeWithExtensions('onAfterRollbackRecursive', $version);

// Get rolled back version on draft
return $this->getAtVersion(Versioned::DRAFT);
}
// Find record at this version
$baseClass = DataObject::getSchema()->baseDataClass($this->owner);
/** @var Versioned|RecursivePublishable|DataObject $recordVersion */
$recordVersion = static::get_version($baseClass, $this->owner->ID, $version);

/**
* Rollback draft to a given version
*
* @param int|string|null $version Version ID or Versioned::LIVE to rollback from live.
* Null to rollback current owner object.
*/
public function rollbackSingle($version)
{
// Validate $version and safely cast
if (isset($version) && !is_numeric($version) && $version !== self::LIVE) {
throw new InvalidArgumentException("Invalid rollback source version $version");
}
if (isset($version) && is_numeric($version)) {
$version = (int)$version;
// Note that unlike other publishing actions, rollback is NOT recursive;
// The owner collects all objects and writes them back using writeToStage();
foreach ($recordVersion->findOwned() as $object) {
// Skip unversioned owned objects
if (!$object->hasExtension(Versioned::class)) {
continue;
}
/** @var Versioned|DataObject $object */
$object->writeToStage(static::DRAFT);
}
// Copy version between stage
$owner = $this->owner;
Expand Down

0 comments on commit e106638

Please sign in to comment.