Skip to content

Commit 70a7fc5

Browse files
committed
feat: first stable version
1 parent e0207ca commit 70a7fc5

14 files changed

+455
-22
lines changed

.gitignore

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
/vendor/
2-
node_modules/
3-
npm-debug.log
4-
yarn-error.log
5-
6-
# Laravel 4 specific
7-
bootstrap/compiled.php
8-
app/storage/
9-
10-
# Laravel 5 & Lumen specific
11-
public/storage
12-
public/hot
13-
14-
# Laravel 5 & Lumen specific with changed public path
15-
public_html/storage
16-
public_html/hot
17-
18-
storage/*.key
1+
/node_modules
2+
/public/hot
3+
/public/storage
4+
/storage/*.key
5+
/vendor
196
.env
20-
Homestead.yaml
21-
Homestead.json
22-
/.vagrant
7+
.env.backup
238
.phpunit.result.cache
9+
docker-compose.override.yml
10+
Homestead.json
11+
Homestead.yaml
12+
npm-debug.log
13+
yarn-error.log
14+
/.idea
15+
/.vscode

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
6+
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [unreleased]
9+
10+
## [0.0.0] - 2021-10-18
11+
12+
### Added
13+
14+
- Project created
15+
- Initial code commit
16+
17+
## [0.0.1] - 2024-03-20
18+
19+
### Added
20+
21+
- First project release

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pull:
2+
git pull origin master
3+
4+
git:
5+
bash git-cli.sh
6+
7+
merge:
8+
git checkout master
9+
git pull origin master
10+
git push

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
# activity-log
1+
# Activity Log
2+
3+
This project, is a simple [Laravel](https://laravel.com) library, to save eloquent model logs.
4+
5+
## Installation
6+
7+
You can install the package via composer:
8+
9+
```bash
10+
composer require txsoura/activity-log
11+
```
12+
13+
Next, you should publish the Activity Log provider using Artisan command. The `activitlog` configuration file will be placed in your application's `config` directory:
14+
15+
```bash
16+
php artisan vendor:publish --provider="Txsoura\ActivityLog\Providers\ActivityLogServiceProvider"
17+
```
18+
19+
Finally, you should run your database migrations. Activity Log will create one database table called `activity_logs`:
20+
21+
```bash
22+
php artisan migrate
23+
```
24+
25+
26+
## Documentation
27+
28+
See the [WIKI](https://github.com/txsoura/activity-log/wiki) for documentation.
29+
30+
## License
31+
32+
This project is under MIT license. Go to [LICENSE](https://opensource.org/licenses/MIT) for more details.

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "txsoura/activity-log",
3+
"description": "Txsoura Activity Log - Simple lib to save eloquent model logs",
4+
"type": "library",
5+
"version": "0.0.1",
6+
"keywords": [
7+
"eloquent",
8+
"activity-log",
9+
"laravel"
10+
],
11+
"homepage": "https://github.com/txsoura/activity-log",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Victor Tesoura Júnior",
16+
"email": "[email protected]",
17+
"role": "owner"
18+
}
19+
],
20+
"require": {
21+
"php": "^7.2|^8.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Txsoura\\ActivityLog\\": "src/"
26+
}
27+
},
28+
"extra": {
29+
"laravel": {
30+
"providers": [
31+
"Txsoura\\ActivityLog\\Providers\\ActivityLogServiceProvider"
32+
]
33+
}
34+
},
35+
"config": {
36+
"sort-packages": true
37+
},
38+
"minimum-stability": "dev",
39+
"prefer-stable": true
40+
}

config/activitylog.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Activity Log Causer
7+
|--------------------------------------------------------------------------
8+
|
9+
| When causer is enabled, all activities will be saved with
10+
| an action causer morphy.
11+
|
12+
*/
13+
14+
'causer_enabled' => (bool)env('ACTIVITY_LOG_CAUSER_ENABLED', true),
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Activity Log Causer Identifier
19+
|--------------------------------------------------------------------------
20+
|
21+
| This Identifier is used to save activity causer type and
22+
| is required when causer is enabled.
23+
|
24+
*/
25+
26+
'causer_identifier' => env('ACTIVITY_LOG_CAUSER_IDENTIFIER', 'users'),
27+
28+
/*
29+
|--------------------------------------------------------------------------
30+
| Activity Log Causer Provider
31+
|--------------------------------------------------------------------------
32+
|
33+
| This Provider is used to get activity causer id
34+
| and is required when causer is enabled.
35+
|
36+
| Supported: "auth"
37+
*/
38+
39+
'causer_provider' => env('ACTIVITY_LOG_CAUSER_PROVIDER', 'auth'),
40+
41+
/*
42+
|--------------------------------------------------------------------------
43+
| Activity Log Ignore Attributes
44+
|--------------------------------------------------------------------------
45+
|
46+
| The Ignore Attributes indicates which model attributes name, should
47+
| never be saved in activity changes properties (Eg: "password").
48+
|
49+
*/
50+
51+
'ignore_attributes' => [
52+
'store'=> (array)explode(',', env('ACTIVITY_LOG_STORE_IGNORE_ATTRIBUTES', 'created_at,updated_at,password')),
53+
54+
'update'=> (array)explode(',', env('ACTIVITY_LOG_UPDATE_IGNORE_ATTRIBUTES', 'updated_at,password')),
55+
],
56+
57+
];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateActivityLogsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('activity_logs', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('log_name')->nullable();
19+
$table->text('description');
20+
$table->nullableMorphs('subject');
21+
$table->nullableMorphs('causer');
22+
$table->text('request')->nullable();
23+
$table->json('properties')->nullable();
24+
$table->timestamps();
25+
26+
$table->index('log_name');
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*
33+
* @return void
34+
*/
35+
public function down()
36+
{
37+
Schema::dropIfExists('activity_logs');
38+
}
39+
}

git-cli.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
###############################################################################
4+
# Git Script #
5+
# #
6+
# Author: Victor Tesoura Júnior <[email protected]> #
7+
###############################################################################
8+
# #
9+
# This script, is to be used after a approved pull request in main repo #
10+
# branch. #
11+
# #
12+
###############################################################################
13+
14+
15+
git checkout master
16+
git fetch -p
17+
git pull origin master
18+

src/ActivityLog.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Txsoura\ActivityLog;
4+
5+
use Illuminate\Support\Arr;
6+
use Txsoura\ActivityLog\Models\ActivityLog as ActivityLogModel;
7+
8+
class ActivityLog
9+
{
10+
// Create activity log
11+
public static function create($log_name, $description, $subject, $subject_id, $request, $model = null, $modelChanges = null)
12+
{
13+
$causer_type = config('activitylog.causer_enabled') ? config('activitylog.causer_identifier') : null;
14+
$causer_id = config('activitylog.causer_enabled') ? auth()->user() ? auth()->user()->id : null : null;
15+
16+
ActivityLogModel::create([
17+
"log_name" => $log_name,
18+
"description" => $description,
19+
"subject_type" => $subject,
20+
"subject_id" => $subject_id,
21+
"causer_type" => $causer_type,
22+
"causer_id" => $causer_id,
23+
"request" => array(
24+
'ip' => $request->ip(),
25+
'method' => $request->url(),
26+
'url' => $request->method(),
27+
'inputs' => $request->all(),
28+
'headers' => $request->header(),
29+
),
30+
"properties" => array('changes' => self::properties($model, $modelChanges)) //changes:[{key:email,value:abc,old:null,status:new},{key:cellphone,value:1234,old:123,status:update}]
31+
]);
32+
}
33+
34+
//Format activity properties
35+
public static function properties($model, $modelChanges): array
36+
{
37+
$changes = [];
38+
39+
if ($modelChanges) {
40+
$modelChanges = Arr::except($modelChanges,config('activitylog.ignore_attributes.update'));
41+
42+
$keys = array_keys($modelChanges);
43+
44+
for ($i = 0; $i < count($modelChanges); $i++) {
45+
$change = array(
46+
'key' => $keys[$i],
47+
'value' => $modelChanges[$keys[$i]],
48+
'old' => $model[$keys[$i]],
49+
'status' => 'update'
50+
);
51+
52+
array_push($changes, $change);
53+
}
54+
55+
return $changes;
56+
}
57+
58+
if ($model) {
59+
$model = Arr::except($model, config('activitylog.ignore_attributes.store'));
60+
61+
$keys = array_keys($model);
62+
63+
for ($i = 0; $i < count($model); $i++) {
64+
$change = array(
65+
'key' => $keys[$i],
66+
'value' => $model[$keys[$i]],
67+
'old' => null,
68+
'status' => 'new'
69+
);
70+
71+
array_push($changes, $change);
72+
}
73+
74+
return $changes;
75+
}
76+
77+
return $changes;
78+
}
79+
}

src/Models/ActivityLog.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Txsoura\ActivityLog\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Txsoura\ActivityLog\Models\ActivityLog
9+
*
10+
*/
11+
class ActivityLog extends Model
12+
{
13+
/**
14+
* The attributes that are mass assignable.
15+
*
16+
* @var array
17+
*/
18+
protected $fillable = [
19+
'log_name', 'description', 'subject_type', 'subject_id', 'causer_type', 'causer_id', 'request', 'properties'
20+
];
21+
22+
/**
23+
* The attributes that should be cast to native types.
24+
*
25+
* @var array
26+
*/
27+
protected $casts = [
28+
'request' => 'json',
29+
'properties' => 'json'
30+
];
31+
}

0 commit comments

Comments
 (0)