Skip to content

Commit

Permalink
Add compatibility with Laravel 5.5 (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Aug 30, 2017
1 parent 5e982c1 commit 5c3baa8
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 65 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,12 @@

All notable changes to `spatie/laravel-activitylog` will be documented in this file


## 2.0.0 - 2017-08-30
- added support for Laravel 5.5, dropped support for older laravel versions
- renamed config file from `laravel-activitylog` to `activitylog`
- rename `getChangesAttribute` function to `changes` so it doesn't conflict with Laravel's native functionality

## 1.16.0 - 2017-06-28
- added `enableLogging` and `disableLogging`

Expand Down
10 changes: 1 addition & 9 deletions README.md
Expand Up @@ -91,15 +91,7 @@ You can install the package via composer:
composer require spatie/laravel-activitylog
```

Next, you must install the service provider:

```php
// config/app.php
'providers' => [
...
Spatie\Activitylog\ActivitylogServiceProvider::class,
];
```
The package will automatically register itself.

You can publish the migration with:
```bash
Expand Down
13 changes: 6 additions & 7 deletions composer.json
Expand Up @@ -26,16 +26,16 @@
],
"require": {
"php" : "^7.0",
"illuminate/config": "~5.1.0|~5.2.0|~5.3.0|~5.4.0",
"illuminate/database": "~5.1.0|~5.2.0|~5.3.0|~5.4.0",
"illuminate/support": "~5.1.0|~5.2.0|~5.3.0|~5.4.0",
"illuminate/config": "~5.5.0",
"illuminate/database": "~5.5.0",
"illuminate/support": "~5.5.0",
"spatie/string": "^2.1"

},
"require-dev": {
"phpunit/phpunit": "^5.7",
"orchestra/testbench": "~3.3.0|~3.4.0",
"orchestra/database": "~3.3.0|~3.4.0"
"phpunit/phpunit": "~6.2",
"orchestra/testbench": "~3.5.0",
"orchestra/database": "~3.5.0"
},
"autoload": {
"psr-4": {
Expand All @@ -50,7 +50,6 @@
"Spatie\\Activitylog\\Test\\": "tests"
}
},

"scripts": {
"test": "vendor/bin/phpunit"
},
Expand Down
1 change: 1 addition & 0 deletions config/laravel-activitylog.php → config/activitylog.php
@@ -1,5 +1,6 @@
<?php


return [

/*
Expand Down
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Expand Up @@ -19,11 +19,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
6 changes: 3 additions & 3 deletions src/ActivityLogger.php
Expand Up @@ -39,17 +39,17 @@ public function __construct(AuthManager $auth, Repository $config)

$this->properties = collect();

$this->authDriver = $config['laravel-activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver();
$this->authDriver = $config['activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver();

if (starts_with(app()->version(), '5.1')) {
$this->causedBy = $auth->driver($this->authDriver)->user();
} else {
$this->causedBy = $auth->guard($this->authDriver)->user();
}

$this->logName = $config['laravel-activitylog']['default_log_name'];
$this->logName = $config['activitylog']['default_log_name'];

$this->logEnabled = $config['laravel-activitylog']['enabled'] ?? true;
$this->logEnabled = $config['activitylog']['enabled'] ?? true;
}

public function performedOn(Model $model)
Expand Down
6 changes: 3 additions & 3 deletions src/ActivitylogServiceProvider.php
Expand Up @@ -15,10 +15,10 @@ class ActivitylogServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__.'/../config/laravel-activitylog.php' => config_path('laravel-activitylog.php'),
__DIR__.'/../config/activitylog.php' => config_path('activitylog.php'),
], 'config');

$this->mergeConfigFrom(__DIR__.'/../config/laravel-activitylog.php', 'laravel-activitylog');
$this->mergeConfigFrom(__DIR__.'/../config/activitylog.php', 'activitylog');

if (! class_exists('CreateActivityLogTable')) {
$timestamp = date('Y_m_d_His', time());
Expand All @@ -43,7 +43,7 @@ public function register()

public static function determineActivityModel(): string
{
$activityModel = config('laravel-activitylog.activity_model') ?? Activity::class;
$activityModel = config('activitylog.activity_model') ?? Activity::class;

if (! is_a($activityModel, Activity::class, true)) {
throw InvalidConfiguration::modelIsNotValid($activityModel);
Expand Down
2 changes: 1 addition & 1 deletion src/CleanActivitylogCommand.php
Expand Up @@ -25,7 +25,7 @@ public function handle()
{
$this->comment('Cleaning activity log...');

$maxAgeInDays = config('laravel-activitylog.delete_records_older_than_days');
$maxAgeInDays = config('activitylog.delete_records_older_than_days');

$cutOffDate = Carbon::now()->subDays($maxAgeInDays)->format('Y-m-d H:i:s');

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Activity.php
Expand Up @@ -19,7 +19,7 @@ class Activity extends Model

public function subject(): MorphTo
{
if (config('laravel-activitylog.subject_returns_soft_deleted_models')) {
if (config('activitylog.subject_returns_soft_deleted_models')) {
return $this->morphTo()->withTrashed();
}

Expand All @@ -43,7 +43,7 @@ public function getExtraProperty(string $propertyName)
return array_get($this->properties->toArray(), $propertyName);
}

public function getChangesAttribute(): Collection
public function changes(): Collection
{
return collect(array_filter($this->properties->toArray(), function ($key) {
return in_array($key, ['attributes', 'old']);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/LogsActivity.php
Expand Up @@ -66,7 +66,7 @@ public function getDescriptionForEvent(string $eventName): string

public function getLogNameToUse(string $eventName = ''): string
{
return config('laravel-activitylog.default_log_name');
return config('activitylog.default_log_name');
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Expand Up @@ -5,7 +5,7 @@
if (! function_exists('activity')) {
function activity(string $logName = null): ActivityLogger
{
$defaultLogName = config('laravel-activitylog.default_log_name');
$defaultLogName = config('activitylog.default_log_name');

return app(ActivityLogger::class)->useLog($logName ?? $defaultLogName);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/ActivityloggerTest.php
Expand Up @@ -32,7 +32,7 @@ public function it_can_log_an_activity()
/** @test */
public function it_will_not_log_an_activity_when_the_log_is_not_enabled()
{
config(['laravel-activitylog.enabled' => false]);
config(['activitylog.enabled' => false]);

activity()->log($this->activityDescription);

Expand All @@ -42,7 +42,7 @@ public function it_will_not_log_an_activity_when_the_log_is_not_enabled()
/** @test */
public function it_will_log_an_activity_when_enabled_option_is_null()
{
config(['laravel-activitylog.enabled' => null]);
config(['activitylog.enabled' => null]);

activity()->log($this->activityDescription);

Expand All @@ -54,7 +54,7 @@ public function it_will_log_to_the_default_log_by_default()
{
activity()->log($this->activityDescription);

$this->assertEquals(config('laravel-activitylog.default_log_name'), $this->getLastActivity()->log_name);
$this->assertEquals(config('activitylog.default_log_name'), $this->getLastActivity()->log_name);
}

/** @test */
Expand Down Expand Up @@ -104,7 +104,7 @@ public function it_can_log_an_activity_with_a_causer_when_there_is_no_web_guard(
{
config(['auth.guards.web' => null]);
config(['auth.guards.foo' => ['driver' => 'session', 'provider' => 'users']]);
config(['laravel-activitylog.default_auth_driver' => 'foo']);
config(['activitylog.default_auth_driver' => 'foo']);

$user = User::first();

Expand Down Expand Up @@ -172,7 +172,7 @@ public function it_can_translate_a_given_causer_id_to_an_object()
*/
public function it_will_throw_an_exception_if_it_cannot_translate_a_causer_id()
{
$this->setExpectedException(CouldNotLogActivity::class);
$this->expectException(CouldNotLogActivity::class);

activity()->causedBy(999);
}
Expand Down Expand Up @@ -237,7 +237,7 @@ public function it_returns_an_instance_of_the_activity_log_after_logging_when_us

$activityClassName = get_class($activityClass);

$this->app['config']->set('laravel-activitylog.activity_model', $activityClassName);
$this->app['config']->set('activitylog.activity_model', $activityClassName);

$activityModel = activity()->log('test');

Expand Down
2 changes: 1 addition & 1 deletion tests/CleanActivitylogCommandTest.php
Expand Up @@ -14,7 +14,7 @@ public function setUp()

Carbon::setTestNow(Carbon::create(2016, 1, 1, 00, 00, 00));

$this->app['config']->set('laravel-activitylog.delete_records_older_than_days', 31);
$this->app['config']->set('activitylog.delete_records_older_than_days', 31);
}

/** @test */
Expand Down
26 changes: 23 additions & 3 deletions tests/CustomActivityModelTest.php
Expand Up @@ -25,7 +25,7 @@ public function setUp()
/** @test */
public function it_can_log_activity_using_a_custom_model()
{
$this->app['config']->set('laravel-activitylog.activity_model', CustomActivityModel::class);
$this->app['config']->set('activitylog.activity_model', CustomActivityModel::class);

$activity = activity()->log($this->activityDescription);

Expand All @@ -37,18 +37,38 @@ public function it_can_log_activity_using_a_custom_model()
/** @test */
public function it_does_not_throw_an_exception_when_model_config_is_null()
{
$this->app['config']->set('laravel-activitylog.activity_model', null);
$this->app['config']->set('activitylog.activity_model', null);

activity()->log($this->activityDescription);

$this->doNotMarkAsRisky();
}

/** @test */
public function it_throws_an_exception_when_model_doesnt_extend_package_model()
{
$this->app['config']->set('laravel-activitylog.activity_model', InvalidActivityModel::class);
$this->app['config']->set('activitylog.activity_model', InvalidActivityModel::class);

$this->expectException(InvalidConfiguration::class);

activity()->log($this->activityDescription);
}

/** @test */
public function it_doesnt_conlict_with_laravel_change_tracking()
{
$this->app['config']->set('activitylog.activity_model', CustomActivityModel::class);

$properties = [
'attributes' => [
'name' => 'my name',
'text' => null,
],
];

$activity = activity()->withProperties($properties)->log($this->activityDescription);

$this->assertEquals($properties, $activity->changes()->toArray());
$this->assertEquals($properties, $activity->custom_property->toArray());
}
}

0 comments on commit 5c3baa8

Please sign in to comment.