Skip to content

Commit

Permalink
PHP 8 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Jun 8, 2022
1 parent 0402ab8 commit 9aa0c92
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 150 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions src/LaravelVersionable/CreatedVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/LaravelVersionable/CreatingVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 9 additions & 9 deletions src/LaravelVersionable/LaravelVersionableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/LaravelVersionable/Traits/HasAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

/**
Expand Down
41 changes: 20 additions & 21 deletions src/LaravelVersionable/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
});
}

/**
Expand All @@ -99,7 +99,7 @@ public function revert()
$dontVersionFields = $model->getDontVersionFields();

foreach ($dontVersionFields as $field) {
unset( $model->{$field} );
unset($model->{$field});
}

$model->save();
Expand All @@ -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]);
}
}

Expand Down
53 changes: 26 additions & 27 deletions src/LaravelVersionable/VersionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,38 @@ 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;
}

/**
* 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;
Expand All @@ -73,7 +72,7 @@ public function resetAction()
*/
public function hasAction()
{
return !is_null( $this->action );
return ! is_null($this->action);
}

/**
Expand All @@ -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();

Expand Down Expand Up @@ -177,6 +176,6 @@ private function getRequestUserAgent()
return null;
}

return Request::header( 'User-Agent' );
return Request::header('User-Agent');
}
}
22 changes: 8 additions & 14 deletions src/LaravelVersionable/Versionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* Interface Versionable
*
* @package RodrigoPedra\LaravelVersionable
*
* @mixin \Illuminate\Database\Eloquent\Model
*/
interface Versionable
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -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);

}
Loading

0 comments on commit 9aa0c92

Please sign in to comment.