Skip to content

Commit

Permalink
Merged in master
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin.schmick committed Jun 29, 2022
2 parents dc4f4c9 + 41af928 commit fdb249b
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
/tests export-ignore
/.editorconfig export-ignore
/.github export-ignore
/docker export-ignore
6 changes: 4 additions & 2 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 All @@ -23,8 +24,9 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3, 7.4]
stability: [prefer-lowest, prefer-stable]
php: [7.3, 7.4, 8.0, 8.1]
stability:
- prefer-stable

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

Expand Down
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# 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)
[![Maintainability](https://api.codeclimate.com/v1/badges/f7dfbbd4462347976259/maintainability)](https://codeclimate.com/github/always-open/laravel-process-stamps/maintainability)

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 +12,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 @@ -59,13 +60,32 @@ That will generate the nessesary field names to track processes.
composer test
```

## Using Docker
All assets are set up under the docker-compose.yml file. The first time you run the docker image you must build it with
the following command:
```bash
docker-compose build
```

Then you can bring it up in the background using:
```bash
docker-compose up -d
```

And the image is aliased so you can access its command line via:
```bash
docker exec -it processes-stamp-app /bin/bash
```

From there you can run the tests within an isolated environment

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

If you discover any security related issues, please email [opensource@orisintel.com](mailto:opensource@orisintel.com) 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 opensource@orisintel.com. 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.
17 changes: 4 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,23 @@
],
"homepage": "https://github.com/always-open/laravel-process-stamps",
"license": "MIT",
"authors": [
{
"name": "Tom Schlick",
"email": "tschlick@orisintel.com",
"role": "Developer"
}
],
"require": {
"php": "^8.0.0|^8.1.0",
"laravel/framework": "^9.0"
},
"require-dev": {
"doctrine/dbal": "^3.3",
"larapack/dd": "^1.0",
"mockery/mockery": "~1.0",
"doctrine/dbal": "^2.9|^3.0",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"OrisIntel\\ProcessStamps\\": "src"
"AlwaysOpen\\ProcessStamps\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"OrisIntel\\ProcessStamps\\Tests\\": "tests"
"AlwaysOpen\\ProcessStamps\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -48,7 +39,7 @@
"extra": {
"laravel": {
"providers": [
"OrisIntel\\ProcessStamps\\ProcessStampsServiceProvider"
"AlwaysOpen\\ProcessStamps\\ProcessStampsServiceProvider"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
build:
context: ./
dockerfile: ./docker/Dockerfile
image: processstamps
container_name: processstamps-app
image: processes-stamp
container_name: processes-stamp-app
restart: unless-stopped
working_dir: /var/www/
volumes:
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

0 comments on commit fdb249b

Please sign in to comment.