Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: tests

on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Laravel Process Stamps

[![Latest Version on Packagist](https://img.shields.io/packagist/v/orisintel/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-process-stamps)
[![Build Status](https://img.shields.io/github/workflow/status/orisintel/laravel-process-stamps/tests?style=flat-square)](https://github.com/orisintel/laravel-process-stamps/actions?query=workflow%3Atests)
[![Total Downloads](https://img.shields.io/packagist/dt/orisintel/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-process-stamps)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/always-open/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/always-open/laravel-process-stamps)
[![Build Status](https://img.shields.io/github/workflow/status/always-open/laravel-process-stamps/tests?style=flat-square)](https://github.com/always-open/laravel-process-stamps/actions?query=workflow%3Atests)
[![Total Downloads](https://img.shields.io/packagist/dt/always-open/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/always-open/laravel-process-stamps)

It is sometimes very useful to know which process created or modified a particular record in your database. This package provides a trait to add to your Laravel models which automatically logs that for you.

Expand All @@ -11,13 +11,13 @@ It is sometimes very useful to know which process created or modified a particul
You can install the package via composer:

```bash
composer require orisintel/laravel-process-stamps
composer require always-open/laravel-process-stamps
```

## Configuration

``` php
php artisan vendor:publish --provider="\OrisIntel\ProcessStamps\ProcessStampsServiceProvider"
php artisan vendor:publish --provider="\AlwaysOpen\ProcessStamps\ProcessStampsServiceProvider"
```

Running the above command will publish both the migration and the config file.
Expand Down Expand Up @@ -84,7 +84,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.
If you discover any security related issues, please email @tomschlick or @qschmick directly instead of using the issue tracker.

## Credits

Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## Reporting a Vulnerability

If you encounter what you believe to be a security vulnerability, please report it immediately to [email protected]. Please include your name, your email address, the url of the package, and a description of the suspected issue.
If you encounter what you believe to be a security vulnerability, please report it immediately to @tomschlick or @qschmick. Please include your name, your email address, the url of the package, and a description of the suspected issue.
25 changes: 6 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
{
"name": "orisintel/laravel-process-stamps",
"name": "always-open/laravel-process-stamps",
"description": "Logs which process created or modified a record",
"keywords": [
"orisintel",
"always-open",
"laravel-process-stamps",
"laravel",
"logging"
],
"homepage": "https://github.com/orisintel/laravel-process-stamps",
"homepage": "https://github.com/always-open/laravel-process-stamps",
"license": "MIT",
"authors": [
{
"name": "Tom Schlick",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "ORIS Intelligence",
"email": "[email protected]",
"homepage": "https://orisintel.com",
"role": "Organization"
}
],
"require": {
"php": "^7.3|^8.0",
"laravel/framework": "^8.0"
Expand All @@ -35,12 +22,12 @@
},
"autoload": {
"psr-4": {
"OrisIntel\\ProcessStamps\\": "src"
"AlwaysOpen\\ProcessStamps\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"OrisIntel\\ProcessStamps\\Tests\\": "tests"
"AlwaysOpen\\ProcessStamps\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -54,7 +41,7 @@
"extra": {
"laravel": {
"providers": [
"OrisIntel\\ProcessStamps\\ProcessStampsServiceProvider"
"AlwaysOpen\\ProcessStamps\\ProcessStampsServiceProvider"
]
}
}
Expand Down
44 changes: 27 additions & 17 deletions src/ProcessStamp.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace OrisIntel\ProcessStamps;
namespace AlwaysOpen\ProcessStamps;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class ProcessStamp extends Model
{
Expand Down Expand Up @@ -42,6 +42,8 @@ public function getTable() : string
* @param null|string $hash
*
* @return ProcessStamp
*
* @throws ModelNotFoundException
*/
public static function firstOrCreateByProcess(array $process, ?string $hash = null) : self
{
Expand All @@ -59,23 +61,31 @@ public static function firstOrCreateByProcess(array $process, ?string $hash = nu
$parent = static::firstOrCreateByProcess(static::getProcessName($process['type'], $process['parent_name']));
}

$stamp = static::where('hash', $hash)->first();
return retry(4, function() use ($hash, $process, $parent) {
$stamp = static::firstWhere('hash', $hash);

/*
* If stamp does not exist in the database yet, go ahead and obtain a lock to create it.
* This specifically doesn't lock as the first step to avoid all calls obtaining a lock from the cache if
* the item already exists in the DB.
*/
if (! $stamp) {
Cache::lock('process-stamps-hash-create-' . $hash, 10)
->get(function () use (&$stamp, $hash, $process, $parent) {
$stamp = static::firstOrCreate(['hash' => $hash], [
'name' => trim($process['name']),
'type' => $process['type'],
'parent_id' => optional($parent)->getKey(),
]);
});
}

/*
* If stamp does not exist in the database yet, go ahead and obtain a lock to create it.
* This specifically doesn't lock as the first step to avoid all calls obtaining a lock from the cache if the item already exists in the DB.
*/
if (! $stamp) {
Cache::lock('process-stamps-hash-create-' . $hash, 10)->get(function () use (&$stamp, $hash, $process, $parent) {
$stamp = static::firstOrCreate(['hash' => $hash], [
'name' => trim($process['name']),
'type' => $process['type'],
'parent_id' => optional($parent)->getKey(),
]);
});
}
if (null === $stamp) {
throw new ModelNotFoundException();
}

return $stamp;
return $stamp;
}, 25);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ProcessStampable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace OrisIntel\ProcessStamps;
namespace AlwaysOpen\ProcessStamps;

use Illuminate\Database\Eloquent\Relations\BelongsTo;

Expand Down
2 changes: 1 addition & 1 deletion src/ProcessStampsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace OrisIntel\ProcessStamps;
namespace AlwaysOpen\ProcessStamps;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Arr;
Expand Down
4 changes: 2 additions & 2 deletions tests/Fakes/Post.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace OrisIntel\ProcessStamps\Tests\Fakes;
namespace AlwaysOpen\ProcessStamps\Tests\Fakes;

use Illuminate\Database\Eloquent\Model;
use OrisIntel\ProcessStamps\ProcessStampable;
use AlwaysOpen\ProcessStamps\ProcessStampable;

class Post extends Model
{
Expand Down
6 changes: 3 additions & 3 deletions tests/ParentTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace OrisIntel\ProcessStamps\Tests;
namespace AlwaysOpen\ProcessStamps\Tests;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use OrisIntel\ProcessStamps\ProcessStamp;
use OrisIntel\ProcessStamps\Tests\Fakes\Post;
use AlwaysOpen\ProcessStamps\ProcessStamp;
use AlwaysOpen\ProcessStamps\Tests\Fakes\Post;

class ParentTest extends TestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ProcessStampModelTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace OrisIntel\ProcessStamps\Tests;
namespace AlwaysOpen\ProcessStamps\Tests;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use OrisIntel\ProcessStamps\ProcessStamp;
use AlwaysOpen\ProcessStamps\ProcessStamp;

class ProcessStampModelTest extends TestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace OrisIntel\ProcessStamps\Tests;
namespace AlwaysOpen\ProcessStamps\Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use OrisIntel\ProcessStamps\ProcessStampsServiceProvider;
use AlwaysOpen\ProcessStamps\ProcessStampsServiceProvider;

abstract class TestCase extends Orchestra
{
Expand Down