Skip to content

Commit

Permalink
Purgeable is deprecated in 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Apr 17, 2021
1 parent cd94e27 commit 793e704
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function pluginDetails()

public function register()
{
/*
/*
* Load localized version of mail templates (akin to localized CMS content files)
*/
Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) {
Expand Down Expand Up @@ -75,8 +75,8 @@ public function register()
$model->addDynamicProperty('translatable', []);
}
$model->translatable = array_merge($model->translatable, ['title', 'description']);
if (!$model->isClassExtendedWith('October\Rain\Database\Behaviors\Purgeable')) {
$model->extendClassWith('October\Rain\Database\Behaviors\Purgeable');
if (!$model->isClassExtendedWith('RainLab\Translate\Behaviors\Purgeable')) {
$model->extendClassWith('RainLab\Translate\Behaviors\Purgeable');
}
if (!$model->isClassExtendedWith('RainLab\Translate\Behaviors\TranslatableModel')) {
$model->extendClassWith('RainLab\Translate\Behaviors\TranslatableModel');
Expand All @@ -91,8 +91,8 @@ public function register()
$model->addDynamicProperty('translatable', []);
}

if (!$model->isClassExtendedWith('October\Rain\Database\Behaviors\Purgeable')) {
$model->extendClassWith('October\Rain\Database\Behaviors\Purgeable');
if (!$model->isClassExtendedWith('RainLab\Translate\Behaviors\Purgeable')) {
$model->extendClassWith('RainLab\Translate\Behaviors\Purgeable');
}
if (!$model->isClassExtendedWith('RainLab\Translate\Behaviors\TranslatableModel')) {
$model->extendClassWith('RainLab\Translate\Behaviors\TranslatableModel');
Expand Down
131 changes: 131 additions & 0 deletions behaviors/Purgeable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php namespace RainLab\Translate\Behaviors;

/**
* Purgeable
*/
class Purgeable extends \October\Rain\Extension\ExtensionBase
{
/**
* @var array List of attribute names which should not be saved to the database.
*
* public $purgeable = [];
*/

protected $model;

protected static $purgableShownDeprecation = false;

public function __construct($parent)
{
$this->model = $parent;
$this->bootPurgeable();
}

/**
* @var array List of original attribute values before they were purged.
*/
protected $originalPurgeableValues = [];

/**
* Boot the purgeable trait for a model.
* @return void
*/
public function bootPurgeable()
{
if (!$this->model->propertyExists('purgeable')) {
$this->model->addDynamicProperty('purgeable', []);
}

$this->model->purgeable = array_merge($this->model->purgeable, ['purgeable']);
$dynPropNames = array_keys(array_diff_key($this->model->getDynamicProperties(), ['purgeable' => 0]));
$this->model->purgeable = array_merge($this->model->purgeable, $dynPropNames);

/*
* Remove any purge attributes from the data set
*/
$model = $this->model;
$model->bindEvent('model.saveInternal', function () use ($model) {
$model->purgeAttributes();
});

if (!static::$purgableShownDeprecation) {
traceLog('Class October\Rain\Database\Behaviors\Purgeable is deprecated. If you require this class, please copy it to your codebase locally.');
static::$purgableShownDeprecation = true;
}
}

/**
* Adds an attribute to the purgeable attributes list
* @param array|string|null $attributes
* @return $this
*/
public function addPurgeable($attributes = null)
{
$attributes = is_array($attributes) ? $attributes : func_get_args();

$this->model->purgeable = array_merge($this->model->purgeable, $attributes);

return $this->model;
}

/**
* Removes purged attributes from the dataset, used before saving.
* @param $attributes mixed Attribute(s) to purge, if unspecified, $purgable property is used
* @return array Current attribute set
*/
public function purgeAttributes($attributesToPurge = null)
{
if ($attributesToPurge !== null) {
$purgeable = is_array($attributesToPurge) ? $attributesToPurge : [$attributesToPurge];
}
else {
$purgeable = $this->getPurgeableAttributes();
}

$attributes = $this->model->getAttributes();
$cleanAttributes = array_diff_key($attributes, array_flip($purgeable));
$originalAttributes = array_diff_key($attributes, $cleanAttributes);

if (is_array($this->originalPurgeableValues)) {
$this->originalPurgeableValues = array_merge($this->originalPurgeableValues, $originalAttributes);
}
else {
$this->originalPurgeableValues = $originalAttributes;
}

return $this->model->attributes = $cleanAttributes;
}

/**
* Returns a collection of fields that will be hashed.
*/
public function getPurgeableAttributes()
{
return $this->model->purgeable;
}

/**
* Returns the original values of any purged attributes.
*/
public function getOriginalPurgeValues()
{
return $this->originalPurgeableValues;
}

/**
* Returns the original values of any purged attributes.
*/
public function getOriginalPurgeValue($attribute)
{
return $this->originalPurgeableValues[$attribute] ?? null;
}

/**
* Restores the original values of any purged attributes.
*/
public function restorePurgedValues()
{
$this->model->attributes = array_merge($this->model->getAttributes(), $this->originalPurgeableValues);
return $this->model;
}
}

0 comments on commit 793e704

Please sign in to comment.