Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkingshott committed Feb 6, 2022
0 parents commit 2d28526
Show file tree
Hide file tree
Showing 17 changed files with 675 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: styling

on: [push]

jobs:
style:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=tools/.php-cs-fixer.php --allow-risky=yes

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Commit changes
uses: stefanzweifel/[email protected]
with:
commit_message: Fix styling
branch: ${{ steps.extract_branch.outputs.branch }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56 changes: 56 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: tests

on: [push, pull_request]

jobs:
phpunit:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.0]
laravel: [8.*]
dependency-version: [prefer-stable]
include:
- laravel: 8.*
testbench: 6.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

services:
mysql:
image: mysql:latest
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: testing
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Run composer
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Run tests
run: vendor/bin/phpunit
env:
DB_PORT: ${{ job.services.mysql.ports[3306] }}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
build
composer.lock
vendor
storage
tests/World/database.sqlite
.DS_Store
coverage
.phpunit.result.cache
.idea
.php_cs.cache
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © Matt Kingshott and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!-- Screenshot -->
<p align="center">
<img src="resources/wallpaper.jpg" alt="Wallpaper">
</p>

<!-- Badges -->
<p align="center">
<img src="resources/version.svg" alt="Version">
<img src="resources/license.svg" alt="License">
</p>

# Triggers

This package enables the use of database triggers within Laravel applications. Note that your chosen database must support triggers for the package to work.

## Installation

Pull in the package using Composer:

```bash
composer require mattkingshott/triggers
```

## Usage

Triggers can only be added to existing tables. Therefore, when creating triggers in your migration files, make sure you add them after the `Schema::create` method.

### Table

To create a trigger, simply call the `table` method on the `Triggers\Trigger` class:

```php
use Triggers\Trigger;

Trigger::table('posts');
```

### Key

By default, the class will generate a name for the trigger using the following convention:

> trigger_{TABLE}_{TIME}_{EVENT}
However, since trigger names must be unique across the database, if you were to create two triggers that used the same event and time (these concepts are covered in the next section), then you'd get an error.

To address this problem, the class offers a `key` method that allows you to add your own custom text to the trigger's name, thereby ensuring that the trigger name can be made unique:

```php
Trigger::table('posts')->key('custom');
```

When a key is specified, the trigger name is derived from the following convention:

> trigger_{TABLE}_{KEY}_{TIME}_{EVENT}
### Event and time

Next, you need to specify whether the trigger should be fired for an `INSERT`, `UPDATE` or `DELETE` event. You will also need to specify whether the trigger should run `BEFORE` or `AFTER` the event has taken place:

```php
Trigger::table('posts')->beforeDelete();
Trigger::table('posts')->beforeInsert();
Trigger::table('posts')->beforeUpdate();

Trigger::table('posts')->afterDelete();
Trigger::table('posts')->afterInsert();
Trigger::table('posts')->afterUpdate();
```

### Statement

The final step, is to specify the SQL statement(s) that should be executed by the trigger when it is fired. To do this, supply a `Closure` to the event / time method. Note that the `Closure` must return a SQL `string` e.g.

```php
Trigger::table('posts')->afterInsert(function() {
return "UPDATE `users` SET `posts` = 1 WHERE `id` = NEW.user_id;";
});
```

### Example

The following example shows a migration that creates a `posts` table and then assigns the trigger to it.

```php
use Triggers\Trigger;

class CreatePostsTable extends Migration
{
public function up() : void
{
Schema::create('posts', function(Blueprint $table) {
$table->unsignedTinyInteger('id');
$table->string('title');
});

Trigger::table('posts')->key('count')->afterInsert(function() {
return "UPDATE `users` SET `posts` = 1 WHERE `id` = NEW.user_id;";
});
}
}
```

## Contributing

Thank you for considering a contribution to the project. You are welcome to submit a PR containing improvements, however if they are substantial in nature, please also be sure to include a test or tests.

## Support the project

If you'd like to support the development of the project, then please consider [sponsoring me](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YBEHLHPF3GUVY&source=url). Thanks so much!

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "mattkingshott/triggers",
"description": "A package to add database trigger support to Laravel",
"keywords": [
"triggers",
"php",
"laravel",
"database"
],
"type": "library",
"license": "MIT",
"homepage": "https://github.com/mattkingshott/triggers",
"autoload": {
"psr-4": {
"Triggers\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Triggers\\Tests\\": "tests"
}
},
"require": {
"php": "^8.0"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0"
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"minimum-stability": "stable"
}
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_NAME" value="waterfall"/>
</php>
</phpunit>
20 changes: 20 additions & 0 deletions resources/license.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions resources/version.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/wallpaper.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2d28526

Please sign in to comment.