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

Commit 2d28526

Browse files
committed
Initial commit
0 parents  commit 2d28526

17 files changed

Lines changed: 675 additions & 0 deletions

.github/workflows/style.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: styling
2+
3+
on: [push]
4+
5+
jobs:
6+
style:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout code
11+
uses: actions/checkout@v2
12+
13+
- name: Run PHP CS Fixer
14+
uses: docker://oskarstark/php-cs-fixer-ga
15+
with:
16+
args: --config=tools/.php-cs-fixer.php --allow-risky=yes
17+
18+
- name: Extract branch name
19+
shell: bash
20+
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
21+
id: extract_branch
22+
23+
- name: Commit changes
24+
uses: stefanzweifel/git-auto-commit-action@v2.3.0
25+
with:
26+
commit_message: Fix styling
27+
branch: ${{ steps.extract_branch.outputs.branch }}
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/tests.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
phpunit:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
os: [ubuntu-latest]
12+
php: [8.0]
13+
laravel: [8.*]
14+
dependency-version: [prefer-stable]
15+
include:
16+
- laravel: 8.*
17+
testbench: 6.*
18+
19+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
20+
21+
services:
22+
mysql:
23+
image: mysql:latest
24+
env:
25+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
26+
MYSQL_DATABASE: testing
27+
ports:
28+
- 3306
29+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
30+
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v2
34+
35+
- name: Cache composer dependencies
36+
uses: actions/cache@v1
37+
with:
38+
path: ~/.composer/cache/files
39+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
40+
41+
- name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php }}
45+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
46+
coverage: none
47+
48+
- name: Run composer
49+
run: |
50+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
51+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
52+
53+
- name: Run tests
54+
run: vendor/bin/phpunit
55+
env:
56+
DB_PORT: ${{ job.services.mysql.ports[3306] }}

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
build
2+
composer.lock
3+
vendor
4+
storage
5+
tests/World/database.sqlite
6+
.DS_Store
7+
coverage
8+
.phpunit.result.cache
9+
.idea
10+
.php_cs.cache

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright © Matt Kingshott and contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!-- Screenshot -->
2+
<p align="center">
3+
<img src="resources/wallpaper.jpg" alt="Wallpaper">
4+
</p>
5+
6+
<!-- Badges -->
7+
<p align="center">
8+
<img src="resources/version.svg" alt="Version">
9+
<img src="resources/license.svg" alt="License">
10+
</p>
11+
12+
# Triggers
13+
14+
This package enables the use of database triggers within Laravel applications. Note that your chosen database must support triggers for the package to work.
15+
16+
## Installation
17+
18+
Pull in the package using Composer:
19+
20+
```bash
21+
composer require mattkingshott/triggers
22+
```
23+
24+
## Usage
25+
26+
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.
27+
28+
### Table
29+
30+
To create a trigger, simply call the `table` method on the `Triggers\Trigger` class:
31+
32+
```php
33+
use Triggers\Trigger;
34+
35+
Trigger::table('posts');
36+
```
37+
38+
### Key
39+
40+
By default, the class will generate a name for the trigger using the following convention:
41+
42+
> trigger_{TABLE}_{TIME}_{EVENT}
43+
44+
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.
45+
46+
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:
47+
48+
```php
49+
Trigger::table('posts')->key('custom');
50+
```
51+
52+
When a key is specified, the trigger name is derived from the following convention:
53+
54+
> trigger_{TABLE}_{KEY}_{TIME}_{EVENT}
55+
56+
### Event and time
57+
58+
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:
59+
60+
```php
61+
Trigger::table('posts')->beforeDelete();
62+
Trigger::table('posts')->beforeInsert();
63+
Trigger::table('posts')->beforeUpdate();
64+
65+
Trigger::table('posts')->afterDelete();
66+
Trigger::table('posts')->afterInsert();
67+
Trigger::table('posts')->afterUpdate();
68+
```
69+
70+
### Statement
71+
72+
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.
73+
74+
```php
75+
Trigger::table('posts')->afterInsert(function() {
76+
return "UPDATE `users` SET `posts` = 1 WHERE `id` = NEW.user_id;";
77+
});
78+
```
79+
80+
### Example
81+
82+
The following example shows a migration that creates a `posts` table and then assigns the trigger to it.
83+
84+
```php
85+
use Triggers\Trigger;
86+
87+
class CreatePostsTable extends Migration
88+
{
89+
public function up() : void
90+
{
91+
Schema::create('posts', function(Blueprint $table) {
92+
$table->unsignedTinyInteger('id');
93+
$table->string('title');
94+
});
95+
96+
Trigger::table('posts')->key('count')->afterInsert(function() {
97+
return "UPDATE `users` SET `posts` = 1 WHERE `id` = NEW.user_id;";
98+
});
99+
}
100+
}
101+
```
102+
103+
## Contributing
104+
105+
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.
106+
107+
## Support the project
108+
109+
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!
110+
111+
## License
112+
113+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "mattkingshott/triggers",
3+
"description": "A package to add database trigger support to Laravel",
4+
"keywords": [
5+
"triggers",
6+
"php",
7+
"laravel",
8+
"database"
9+
],
10+
"type": "library",
11+
"license": "MIT",
12+
"homepage": "https://github.com/mattkingshott/triggers",
13+
"autoload": {
14+
"psr-4": {
15+
"Triggers\\": "src"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Triggers\\Tests\\": "tests"
21+
}
22+
},
23+
"require": {
24+
"php": "^8.0"
25+
},
26+
"require-dev": {
27+
"orchestra/testbench": "^6.0",
28+
"phpunit/phpunit": "^9.0"
29+
},
30+
"scripts": {
31+
"test": "vendor/bin/phpunit"
32+
},
33+
"minimum-stability": "stable"
34+
}

phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
<php>
14+
<env name="APP_NAME" value="waterfall"/>
15+
</php>
16+
</phpunit>

resources/license.svg

Lines changed: 20 additions & 0 deletions
Loading

resources/version.svg

Lines changed: 20 additions & 0 deletions
Loading

resources/wallpaper.jpg

252 KB
Loading

0 commit comments

Comments
 (0)