Skip to content

Commit

Permalink
feat: first stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
txsoura committed Mar 21, 2024
1 parent e0207ca commit 70a7fc5
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 22 deletions.
34 changes: 13 additions & 21 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
/vendor/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
Homestead.yaml
Homestead.json
/.vagrant
.env.backup
.phpunit.result.cache
docker-compose.override.yml
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
/.idea
/.vscode
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [unreleased]

## [0.0.0] - 2021-10-18

### Added

- Project created
- Initial code commit

## [0.0.1] - 2024-03-20

### Added

- First project release
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pull:
git pull origin master

git:
bash git-cli.sh

merge:
git checkout master
git pull origin master
git push
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
# activity-log
# Activity Log

This project, is a simple [Laravel](https://laravel.com) library, to save eloquent model logs.

## Installation

You can install the package via composer:

```bash
composer require txsoura/activity-log
```

Next, you should publish the Activity Log provider using Artisan command. The `activitlog` configuration file will be placed in your application's `config` directory:

```bash
php artisan vendor:publish --provider="Txsoura\ActivityLog\Providers\ActivityLogServiceProvider"
```

Finally, you should run your database migrations. Activity Log will create one database table called `activity_logs`:

```bash
php artisan migrate
```


## Documentation

See the [WIKI](https://github.com/txsoura/activity-log/wiki) for documentation.

## License

This project is under MIT license. Go to [LICENSE](https://opensource.org/licenses/MIT) for more details.
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "txsoura/activity-log",
"description": "Txsoura Activity Log - Simple lib to save eloquent model logs",
"type": "library",
"version": "0.0.1",
"keywords": [
"eloquent",
"activity-log",
"laravel"
],
"homepage": "https://github.com/txsoura/activity-log",
"license": "MIT",
"authors": [
{
"name": "Victor Tesoura Júnior",
"email": "[email protected]",
"role": "owner"
}
],
"require": {
"php": "^7.2|^8.0"
},
"autoload": {
"psr-4": {
"Txsoura\\ActivityLog\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Txsoura\\ActivityLog\\Providers\\ActivityLogServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
57 changes: 57 additions & 0 deletions config/activitylog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Activity Log Causer
|--------------------------------------------------------------------------
|
| When causer is enabled, all activities will be saved with
| an action causer morphy.
|
*/

'causer_enabled' => (bool)env('ACTIVITY_LOG_CAUSER_ENABLED', true),

/*
|--------------------------------------------------------------------------
| Activity Log Causer Identifier
|--------------------------------------------------------------------------
|
| This Identifier is used to save activity causer type and
| is required when causer is enabled.
|
*/

'causer_identifier' => env('ACTIVITY_LOG_CAUSER_IDENTIFIER', 'users'),

/*
|--------------------------------------------------------------------------
| Activity Log Causer Provider
|--------------------------------------------------------------------------
|
| This Provider is used to get activity causer id
| and is required when causer is enabled.
|
| Supported: "auth"
*/

'causer_provider' => env('ACTIVITY_LOG_CAUSER_PROVIDER', 'auth'),

/*
|--------------------------------------------------------------------------
| Activity Log Ignore Attributes
|--------------------------------------------------------------------------
|
| The Ignore Attributes indicates which model attributes name, should
| never be saved in activity changes properties (Eg: "password").
|
*/

'ignore_attributes' => [
'store'=> (array)explode(',', env('ACTIVITY_LOG_STORE_IGNORE_ATTRIBUTES', 'created_at,updated_at,password')),

'update'=> (array)explode(',', env('ACTIVITY_LOG_UPDATE_IGNORE_ATTRIBUTES', 'updated_at,password')),
],

];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateActivityLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activity_logs', function (Blueprint $table) {
$table->id();
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableMorphs('subject');
$table->nullableMorphs('causer');
$table->text('request')->nullable();
$table->json('properties')->nullable();
$table->timestamps();

$table->index('log_name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activity_logs');
}
}
18 changes: 18 additions & 0 deletions git-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

###############################################################################
# Git Script #
# #
# Author: Victor Tesoura Júnior <[email protected]> #
###############################################################################
# #
# This script, is to be used after a approved pull request in main repo #
# branch. #
# #
###############################################################################


git checkout master
git fetch -p
git pull origin master

79 changes: 79 additions & 0 deletions src/ActivityLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Txsoura\ActivityLog;

use Illuminate\Support\Arr;
use Txsoura\ActivityLog\Models\ActivityLog as ActivityLogModel;

class ActivityLog
{
// Create activity log
public static function create($log_name, $description, $subject, $subject_id, $request, $model = null, $modelChanges = null)
{
$causer_type = config('activitylog.causer_enabled') ? config('activitylog.causer_identifier') : null;
$causer_id = config('activitylog.causer_enabled') ? auth()->user() ? auth()->user()->id : null : null;

ActivityLogModel::create([
"log_name" => $log_name,
"description" => $description,
"subject_type" => $subject,
"subject_id" => $subject_id,
"causer_type" => $causer_type,
"causer_id" => $causer_id,
"request" => array(
'ip' => $request->ip(),
'method' => $request->url(),
'url' => $request->method(),
'inputs' => $request->all(),
'headers' => $request->header(),
),
"properties" => array('changes' => self::properties($model, $modelChanges)) //changes:[{key:email,value:abc,old:null,status:new},{key:cellphone,value:1234,old:123,status:update}]
]);
}

//Format activity properties
public static function properties($model, $modelChanges): array
{
$changes = [];

if ($modelChanges) {
$modelChanges = Arr::except($modelChanges,config('activitylog.ignore_attributes.update'));

$keys = array_keys($modelChanges);

for ($i = 0; $i < count($modelChanges); $i++) {
$change = array(
'key' => $keys[$i],
'value' => $modelChanges[$keys[$i]],
'old' => $model[$keys[$i]],
'status' => 'update'
);

array_push($changes, $change);
}

return $changes;
}

if ($model) {
$model = Arr::except($model, config('activitylog.ignore_attributes.store'));

$keys = array_keys($model);

for ($i = 0; $i < count($model); $i++) {
$change = array(
'key' => $keys[$i],
'value' => $model[$keys[$i]],
'old' => null,
'status' => 'new'
);

array_push($changes, $change);
}

return $changes;
}

return $changes;
}
}
31 changes: 31 additions & 0 deletions src/Models/ActivityLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Txsoura\ActivityLog\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Txsoura\ActivityLog\Models\ActivityLog
*
*/
class ActivityLog extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'log_name', 'description', 'subject_type', 'subject_id', 'causer_type', 'causer_id', 'request', 'properties'
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'request' => 'json',
'properties' => 'json'
];
}
11 changes: 11 additions & 0 deletions src/Observers/CoreObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Txsoura\ActivityLog\Observers;

abstract class CoreObserver
{
/**
* @var string
*/
protected string $identifier='model';
}
Loading

0 comments on commit 70a7fc5

Please sign in to comment.