diff --git a/composer.json b/composer.json index ac264dd..d4cb087 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "rodrigopedra/laravel-versionable", "license": "MIT", - "description": "Allows to create Laravel 5.6 Model versioning and restoring", + "description": "Allows to create Laravel 5.6 and 6.0 Model versioning and restoring", "keywords": [ "model", "laravel", @@ -25,14 +25,14 @@ "source": "https://github.com/rodrigopedra/laravel-versionable" }, "require": { - "php": "^7.1.3", - "illuminate/support": "^5.6|^6.0", - "illuminate/database": "^5.6|^6.0", - "illuminate/events": "^5.6|^6.0" + "php": "^7.2.5|^8.0", + "illuminate/support": "^6.20.12|^7.30.4|^8.22.1|^9.0", + "illuminate/database": "^6.20.12|^7.30.4|^8.22.1|^9.0", + "illuminate/events": "^6.20.12|^7.30.4|^8.22.1|^9.0" }, "require-dev": { - "phpunit/phpunit": "^7.0", - "mockery/mockery": "dev-master" + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^9.3.3." }, "autoload": { "classmap": [ diff --git a/src/LaravelVersionable/CreatedVersion.php b/src/LaravelVersionable/CreatedVersion.php index cf58685..f3265b0 100644 --- a/src/LaravelVersionable/CreatedVersion.php +++ b/src/LaravelVersionable/CreatedVersion.php @@ -10,9 +10,9 @@ class CreatedVersion public $versionable; - public function __construct( Versionable $versionable, $action ) + public function __construct(Versionable $versionable, $action) { $this->versionable = $versionable; - $this->action = $action; + $this->action = $action; } } diff --git a/src/LaravelVersionable/CreatingVersion.php b/src/LaravelVersionable/CreatingVersion.php index c224929..42c5d89 100644 --- a/src/LaravelVersionable/CreatingVersion.php +++ b/src/LaravelVersionable/CreatingVersion.php @@ -10,9 +10,9 @@ class CreatingVersion public $versionable; - public function __construct( Versionable $versionable, $action ) + public function __construct(Versionable $versionable, $action) { $this->versionable = $versionable; - $this->action = $action; + $this->action = $action; } } diff --git a/src/LaravelVersionable/LaravelVersionableServiceProvider.php b/src/LaravelVersionable/LaravelVersionableServiceProvider.php index 955e775..deaca80 100644 --- a/src/LaravelVersionable/LaravelVersionableServiceProvider.php +++ b/src/LaravelVersionable/LaravelVersionableServiceProvider.php @@ -23,30 +23,30 @@ private function publishMigrations() { $this->requireMigrationFiles(); - if (class_exists( 'CreateVersionsTable', false )) { + if (class_exists('CreateVersionsTable', false)) { return; } - $timestamp = date( 'Y_m_d_His', time() ); + $timestamp = date('Y_m_d_His', time()); - $path = database_path( 'migrations/' . $timestamp . '_create_versions_table.php' ); + $path = database_path('migrations/' . $timestamp . '_create_versions_table.php'); - $this->publishes( [ + $this->publishes([ __DIR__ . '/../../database/migrations/create_versions_table.php.stub' => $path, - ], 'migrations' ); + ], 'migrations'); } private function requireMigrationFiles() { /** @var \Illuminate\Database\Migrations\Migrator $migrator */ - $migrator = App::make( 'migrator' ); + $migrator = App::make('migrator'); $paths = [ - database_path( 'migrations' ), + database_path('migrations'), ]; - $files = $migrator->getMigrationFiles( $paths ); + $files = $migrator->getMigrationFiles($paths); - $migrator->requireFiles( $files ); + $migrator->requireFiles($files); } } diff --git a/src/LaravelVersionable/Traits/HasAction.php b/src/LaravelVersionable/Traits/HasAction.php index c137b90..dda8d94 100644 --- a/src/LaravelVersionable/Traits/HasAction.php +++ b/src/LaravelVersionable/Traits/HasAction.php @@ -32,7 +32,7 @@ public function isUpdating() */ public function isDeleting() { - return in_array( $this->action, [ VersionFactory::ACTION_DELETE, VersionFactory::ACTION_SOFT_DELETE ] ); + return in_array($this->action, [VersionFactory::ACTION_DELETE, VersionFactory::ACTION_SOFT_DELETE]); } /** diff --git a/src/LaravelVersionable/Version.php b/src/LaravelVersionable/Version.php index 1ff6a42..fa87e2d 100644 --- a/src/LaravelVersionable/Version.php +++ b/src/LaravelVersionable/Version.php @@ -24,9 +24,9 @@ class Version extends Eloquent protected $primaryKey = 'version_id'; protected $casts = [ - 'version_id' => 'integer', + 'version_id' => 'integer', 'versionable_id' => 'integer', - 'user_id' => 'integer', + 'user_id' => 'integer', ]; protected $fillable = [ @@ -57,11 +57,11 @@ public function versionable() */ public function getResponsibleUserAttribute() { - return App::make( 'auth.driver' ) + return App::make('auth.driver') ->getProvider() ->createModel() ->withoutGlobalScopes() - ->find( $this->user_id ); + ->find($this->user_id); } /** @@ -71,20 +71,20 @@ public function getResponsibleUserAttribute() */ public function getModel() { - $modelData = is_resource( $this->model_data ) - ? stream_get_contents( $this->model_data ) + $modelData = is_resource($this->model_data) + ? stream_get_contents($this->model_data) : $this->model_data; - $additionalData = is_resource( $this->additional_data ) - ? stream_get_contents( $this->additional_data ) + $additionalData = is_resource($this->additional_data) + ? stream_get_contents($this->additional_data) : $this->additional_data; - $modelClass = Relation::getMorphedModel( $this->versionable_type ) ?: $this->versionable_type; + $modelClass = Relation::getMorphedModel($this->versionable_type) ?: $this->versionable_type; - return tap( new $modelClass, function ( Versionable $versionable ) use ( $modelData, $additionalData ) { - $versionable->unserializeAttributesFromVersoning( $modelData ); - $versionable->unserializeAdditionalDataFromVersoning( $additionalData ); - } ); + return tap(new $modelClass(), function (Versionable $versionable) use ($modelData, $additionalData) { + $versionable->unserializeAttributesFromVersoning($modelData); + $versionable->unserializeAdditionalDataFromVersoning($additionalData); + }); } /** @@ -99,7 +99,7 @@ public function revert() $dontVersionFields = $model->getDontVersionFields(); foreach ($dontVersionFields as $field) { - unset( $model->{$field} ); + unset($model->{$field}); } $model->save(); @@ -111,24 +111,23 @@ public function revert() * Diff the attributes of this version model against another version. * If no version is provided, it will be diffed against the current version. * - * @param Version|null $againstVersion - * + * @param Version|null $againstVersion * @return array */ - public function diff( Version $againstVersion = null ) + public function diff(Version $againstVersion = null) { - $model = $this->getModel(); + $model = $this->getModel(); $dontVersionFields = $model->getDontVersionFields(); $diff = $againstVersion ? $againstVersion->getModel() : $this->versionable()->withoutGlobalScopes()->first()->currentVersion()->getModel(); - $diffArray = array_diff_assoc( $diff->getAttributes(), $model->getAttributes() ); + $diffArray = array_diff_assoc($diff->getAttributes(), $model->getAttributes()); foreach ($dontVersionFields as $field) { - if (isset( $diffArray[ $field ] )) { - unset( $diffArray[ $field ] ); + if (isset($diffArray[$field])) { + unset($diffArray[$field]); } } diff --git a/src/LaravelVersionable/VersionFactory.php b/src/LaravelVersionable/VersionFactory.php index 0942e15..638e149 100644 --- a/src/LaravelVersionable/VersionFactory.php +++ b/src/LaravelVersionable/VersionFactory.php @@ -14,18 +14,18 @@ class VersionFactory { use HasAction; - const ACTION_CREATE = 'create'; - const ACTION_UPDATE = 'update'; - const ACTION_DELETE = 'delete'; + const ACTION_CREATE = 'create'; + const ACTION_UPDATE = 'update'; + const ACTION_DELETE = 'delete'; const ACTION_SOFT_DELETE = 'soft-delete'; - const ACTION_RESTORE = 'restore'; + const ACTION_RESTORE = 'restore'; /** * @var Versionable */ private $versionable; - public function __construct( Versionable $versionable ) + public function __construct(Versionable $versionable) { $this->versionable = $versionable; } @@ -33,20 +33,19 @@ public function __construct( Versionable $versionable ) /** * Set the action being performed * - * @param string $action - * + * @param string $action * @return $this */ - public function setAction( string $action ) + public function setAction(string $action) { - if (!in_array( $action, [ + if (! in_array($action, [ self::ACTION_CREATE, self::ACTION_UPDATE, self::ACTION_DELETE, self::ACTION_SOFT_DELETE, self::ACTION_RESTORE, - ] )) { - throw new InvalidActionForVersionException( 'The action for this version informed is invalid' ); + ])) { + throw new InvalidActionForVersionException('The action for this version informed is invalid'); } $this->action = $action; @@ -73,7 +72,7 @@ public function resetAction() */ public function hasAction() { - return !is_null( $this->action ); + return ! is_null($this->action); } /** @@ -83,35 +82,35 @@ public function hasAction() */ public function createNewVersion() { - if (!$this->hasAction()) { - throw new NoActionForVersionException( 'An action should be set before creating a new version' ); + if (! $this->hasAction()) { + throw new NoActionForVersionException('An action should be set before creating a new version'); } - if (Event::dispatch( new CreatingVersion( $this->versionable, $this->action ) ) === false) { + if (Event::dispatch(new CreatingVersion($this->versionable, $this->action)) === false) { $this->resetAction(); return null; } - if (!$this->versionable->shouldCreateNewVersion()) { + if (! $this->versionable->shouldCreateNewVersion()) { $this->resetAction(); return null; } /** @var Version $version */ - $version = $this->versionable->versions()->create( [ - 'user_id' => $this->getAuthUserId(), - 'action' => $this->action, - 'reason' => $this->versionable->getVersioningReason(), - 'url' => $this->getRequestUrl(), - 'ip_address' => $this->getRequestIp(), - 'user_agent' => $this->getRequestUserAgent(), - 'model_data' => $this->versionable->serializedAttributesForVersioning(), + $version = $this->versionable->versions()->create([ + 'user_id' => $this->getAuthUserId(), + 'action' => $this->action, + 'reason' => $this->versionable->getVersioningReason(), + 'url' => $this->getRequestUrl(), + 'ip_address' => $this->getRequestIp(), + 'user_agent' => $this->getRequestUserAgent(), + 'model_data' => $this->versionable->serializedAttributesForVersioning(), 'additional_data' => $this->versionable->serializedAdditionalDataForVersioning(), - ] ); + ]); - Event::dispatch( new CreatedVersion( $this->versionable, $this->action ) ); + Event::dispatch(new CreatedVersion($this->versionable, $this->action)); $this->resetAction(); @@ -177,6 +176,6 @@ private function getRequestUserAgent() return null; } - return Request::header( 'User-Agent' ); + return Request::header('User-Agent'); } } diff --git a/src/LaravelVersionable/Versionable.php b/src/LaravelVersionable/Versionable.php index 0808b6e..f3541cc 100644 --- a/src/LaravelVersionable/Versionable.php +++ b/src/LaravelVersionable/Versionable.php @@ -8,7 +8,6 @@ * Interface Versionable * * @package RodrigoPedra\LaravelVersionable - * * @mixin \Illuminate\Database\Eloquent\Model */ interface Versionable @@ -27,11 +26,10 @@ public function disableVersioning(); * Run a callback where a new version will be created on any save operation despite * any versioning criteria * - * @param callable $callback - * + * @param callable $callback * @return $this */ - public function forceVersioning( callable $callback ); + public function forceVersioning(callable $callback); /** * Check if it should create a new version @@ -72,11 +70,10 @@ public function serializedAttributesForVersioning(); /** * Unserialize the model's attributes from versioning * - * @param mixed $serializedAttributes - * + * @param mixed $serializedAttributes * @return $this */ - public function unserializeAttributesFromVersoning( $serializedAttributes ); + public function unserializeAttributesFromVersoning($serializedAttributes); /** * Get model's additional data serialized for versoning @@ -88,18 +85,16 @@ public function serializedAdditionalDataForVersioning(); /** * Unserialize the model's additional data from versioning * - * @param mixed $serializedData - * + * @param mixed $serializedData * @return $this */ - public function unserializeAdditionalDataFromVersoning( $serializedData ); + public function unserializeAdditionalDataFromVersoning($serializedData); /** * Get the attributes that have been changed since last sync. * - * @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::getDirty() - * * @return array + * @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::getDirty() */ public function getDirty(); @@ -128,9 +123,8 @@ public function previousVersion(); * Get a model based on the version id * * @param $versionId - * * @return $this|null */ - public function getVersionModel( $versionId ); + public function getVersionModel($versionId); } diff --git a/src/LaravelVersionable/VersionableObserver.php b/src/LaravelVersionable/VersionableObserver.php index 43e6b46..6b1b181 100644 --- a/src/LaravelVersionable/VersionableObserver.php +++ b/src/LaravelVersionable/VersionableObserver.php @@ -5,29 +5,29 @@ class VersionableObserver { /** - * @param Versionable $versionable + * @param Versionable $versionable */ - public function creating( Versionable $versionable ) + public function creating(Versionable $versionable) { - $versionable->getVersionFactory()->setAction( VersionFactory::ACTION_CREATE ); + $versionable->getVersionFactory()->setAction(VersionFactory::ACTION_CREATE); } /** - * @param Versionable $versionable + * @param Versionable $versionable */ - public function deleting( Versionable $versionable ) + public function deleting(Versionable $versionable) { - $action = $this->isForceDeleting( $versionable ) + $action = $this->isForceDeleting($versionable) ? VersionFactory::ACTION_DELETE : VersionFactory::ACTION_SOFT_DELETE; - $versionable->getVersionFactory()->setAction( $action ); + $versionable->getVersionFactory()->setAction($action); } /** - * @param Versionable $versionable + * @param Versionable $versionable */ - public function saving( Versionable $versionable ) + public function saving(Versionable $versionable) { $versionFactory = $versionable->getVersionFactory(); @@ -37,31 +37,31 @@ public function saving( Versionable $versionable ) return; } - $versionFactory->setAction( VersionFactory::ACTION_UPDATE ); + $versionFactory->setAction(VersionFactory::ACTION_UPDATE); } /** - * @param Versionable $versionable + * @param Versionable $versionable */ - public function restoring( Versionable $versionable ) + public function restoring(Versionable $versionable) { - $versionable->getVersionFactory()->setAction( VersionFactory::ACTION_RESTORE ); + $versionable->getVersionFactory()->setAction(VersionFactory::ACTION_RESTORE); } /** - * @param Versionable $versionable + * @param Versionable $versionable */ - public function saved( Versionable $versionable ) + public function saved(Versionable $versionable) { $versionable->getVersionFactory()->createNewVersion(); } /** - * @param Versionable $versionable + * @param Versionable $versionable */ - public function deleted( Versionable $versionable ) + public function deleted(Versionable $versionable) { - if ($this->isForceDeleting( $versionable ) && $versionable->shouldPurgeVersionsOnDelete()) { + if ($this->isForceDeleting($versionable) && $versionable->shouldPurgeVersionsOnDelete()) { $versionable->getVersionFactory()->purgeVersions(); return; @@ -70,8 +70,8 @@ public function deleted( Versionable $versionable ) $versionable->getVersionFactory()->createNewVersion(); } - protected function isForceDeleting( Versionable $versionable ) + protected function isForceDeleting(Versionable $versionable) { - return !method_exists( $versionable, 'isForceDeleting' ) || $versionable->isForceDeleting(); + return ! method_exists($versionable, 'isForceDeleting') || $versionable->isForceDeleting(); } } diff --git a/src/LaravelVersionable/VersionableTrait.php b/src/LaravelVersionable/VersionableTrait.php index f7fdd03..78c1ca9 100644 --- a/src/LaravelVersionable/VersionableTrait.php +++ b/src/LaravelVersionable/VersionableTrait.php @@ -8,7 +8,6 @@ * Class VersionableTrait * * @package RodrigoPedra\LaravelVersionable - * * @mixin \Illuminate\Database\Eloquent\Model */ trait VersionableTrait @@ -72,15 +71,14 @@ public function disableVersioning() * Run a callback where a new version will be created on any save operation despite * any versioning criteria * - * @param callable $callback - * + * @param callable $callback * @return $this */ - public function forceVersioning( callable $callback ) + public function forceVersioning(callable $callback) { $this->forceVersioning = true; - $callback( $this ); + $callback($this); $this->forceVersioning = false; @@ -98,17 +96,17 @@ public function shouldCreateNewVersion() return true; } - if (!$this->versioningEnabled) { + if (! $this->versioningEnabled) { return false; } - if (!$this->exists) { + if (! $this->exists) { return true; } $dontVersionFields = $this->getDontVersionFields(); - return count( array_diff_key( $this->getDirty(), array_flip( $dontVersionFields ) ) ) > 0; + return count(array_diff_key($this->getDirty(), array_flip($dontVersionFields))) > 0; } /** @@ -126,9 +124,9 @@ public function shouldPurgeVersionsOnDelete() */ public function getVersionFactory() { - if (is_null( $this->versionFactory )) { + if (is_null($this->versionFactory)) { /** @var Versionable $this */ - $this->versionFactory = new VersionFactory( $this ); + $this->versionFactory = new VersionFactory($this); } return $this->versionFactory; @@ -141,20 +139,20 @@ public function getDontVersionFields() { $dontVersionFields = $this->dontVersionFields ?? []; - return array_merge( $dontVersionFields, [ $this->getUpdatedAtColumn() ] ); + return array_merge($dontVersionFields, [$this->getUpdatedAtColumn()]); } /** * Attribute mutator for "versioning_reason" * Prevent "versioning_reason" to become a database attribute of model * - * @param string $value + * @param string $value */ - public function setVersioningReasonAttribute( $value ) + public function setVersioningReasonAttribute($value) { - $value = trim( $value ); + $value = trim($value); - $this->versioningReason = empty( $value ) ? null : $value; + $this->versioningReason = empty($value) ? null : $value; } /** @@ -172,11 +170,11 @@ public function getVersioningReasonAttribute() * Attribute mutator for "versioning_data" * Prevent "versioning_data" to become a database attribute of model * - * @param mixed $value + * @param mixed $value */ - public function setVersioningDataAttribute( $value ) + public function setVersioningDataAttribute($value) { - if (empty( $value )) { + if (empty($value)) { $this->versioningData = null; return; @@ -189,7 +187,7 @@ public function setVersioningDataAttribute( $value ) * Attribute accessor for "versioning_data" * Allows "versioning_data" to be accessed as a regular attribute * - * @return mixed + * @return string */ public function getVersioningDataAttribute() { @@ -207,35 +205,34 @@ public function getVersioningReason() /** * Get model's attributes serialized for versoning * - * @return mixed + * @return string */ public function serializedAttributesForVersioning() { $attributes = $this->getAttributes(); - return serialize( $attributes ); + return serialize($attributes); } /** * Unserialize the model's attributes from versioning * - * @param mixed $serializedAttributes - * + * @param mixed $serializedAttributes * @return $this */ - public function unserializeAttributesFromVersoning( $serializedAttributes ) + public function unserializeAttributesFromVersoning($serializedAttributes) { - $attributes = unserialize( $serializedAttributes ); + $attributes = unserialize($serializedAttributes); - if (array_key_exists( 'versioning_data', $attributes )) { - $this->versioningData = $attributes[ 'versioning_data' ]; + if (array_key_exists('versioning_data', $attributes)) { + $this->versioningData = $attributes['versioning_data']; } else { $this->versioningData = null; } - unset( $attributes[ 'versioning_data' ] ); + unset($attributes['versioning_data']); - $this->forceFill( $attributes ); + $this->forceFill($attributes); $this->exists = true; return $this; @@ -244,29 +241,28 @@ public function unserializeAttributesFromVersoning( $serializedAttributes ) /** * Get model's additional data serialized for versoning * - * @return mixed + * @return string|null */ public function serializedAdditionalDataForVersioning() { $additionalData = $this->versioningData; - if (is_null( $additionalData )) { + if (is_null($additionalData)) { return null; } - return serialize( $additionalData ); + return serialize($additionalData); } /** * Unserialize the model's additional data from versioning * - * @param mixed $serializedData - * + * @param mixed $serializedData * @return $this */ - public function unserializeAdditionalDataFromVersoning( $serializedData ) + public function unserializeAdditionalDataFromVersoning($serializedData) { - $additionalData = unserialize( $serializedData ); + $additionalData = unserialize($serializedData); $this->versioningData = $additionalData; @@ -280,7 +276,7 @@ public function unserializeAdditionalDataFromVersoning( $serializedData ) */ public function versions() { - return $this->morphMany( Version::class, 'versionable' ); + return $this->morphMany(Version::class, 'versionable'); } /** @@ -290,7 +286,7 @@ public function versions() */ public function currentVersion() { - return $this->versions()->orderBy( ( new Version )->getKeyName(), 'DESC' )->first(); + return $this->versions()->orderBy((new Version())->getKeyName(), 'DESC')->first(); } /** @@ -300,22 +296,21 @@ public function currentVersion() */ public function previousVersion() { - return $this->versions()->orderBy( ( new Version )->getKeyName(), 'DESC' )->limit( 1 )->offset( 1 )->first(); + return $this->versions()->orderBy((new Version())->getKeyName(), 'DESC')->limit(1)->offset(1)->first(); } /** * Get a model based on the version id * * @param $versionId - * * @return Versionable|null */ - public function getVersionModel( $versionId ) + public function getVersionModel($versionId) { /** @var Version $version */ - $version = $this->versions()->where( 'version_id', $versionId )->first(); + $version = $this->versions()->where('version_id', $versionId)->first(); - if (!is_null( $version )) { + if (! is_null($version)) { return $version->getModel(); } @@ -324,6 +319,6 @@ public function getVersionModel( $versionId ) public static function bootVersionableTrait() { - static::observe( VersionableObserver::class ); + static::observe(VersionableObserver::class); } } diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php index f011cff..b183031 100644 --- a/tests/VersionableTest.php +++ b/tests/VersionableTest.php @@ -12,7 +12,7 @@ class VersionableTest extends VersionableTestCase { - public function setUp() + public function setUp(): void { parent::setUp(); @@ -26,7 +26,7 @@ public function setUp() TestPartialVersionableUser::boot(); } - public function tearDown() + public function tearDown(): void { m::close(); Auth::clearResolvedInstances(); diff --git a/tests/VersionableTestCase.php b/tests/VersionableTestCase.php index e81f833..751a201 100644 --- a/tests/VersionableTestCase.php +++ b/tests/VersionableTestCase.php @@ -11,7 +11,7 @@ abstract class VersionableTestCase extends PHPUnitTestCase { const DB_CONNECTION_NAME = 'default'; - public function setUp() + public function setUp(): void { $this->configureDatabase(); $this->migrateUsersTable();