Skip to content

Commit 6860c8d

Browse files
committed
Initial commit
0 parents  commit 6860c8d

19 files changed

+890
-0
lines changed

.coveralls.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage_clover: build/logs/clover.xml
2+
json_path: build/logs/coveralls-upload.json

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.lock
2+
/vendor/
3+
/build/logs/
4+
.php_cs.cache
5+
.phpunit.result.cache

.php_cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
return \PhpCsFixer\Config::create()
4+
->setRules([
5+
'@Symfony' => true,
6+
'align_multiline_comment' => ['comment_type' => 'phpdocs_like'],
7+
'array_syntax' => ['syntax' => 'short'],
8+
'blank_line_before_statement' => false,
9+
'cast_spaces' => ['space' => 'none'],
10+
'combine_consecutive_unsets' => true,
11+
'concat_space' => ['spacing' => 'one'],
12+
'heredoc_to_nowdoc' => true,
13+
'list_syntax' => null,
14+
'no_extra_consecutive_blank_lines' => ['tokens' => ['continue', 'parenthesis_brace_block', 'extra', 'return']],
15+
'no_multiline_whitespace_before_semicolons' => true,
16+
'no_null_property_initialization' => true,
17+
'no_short_echo_tag' => false,
18+
'no_superfluous_elseif' => true,
19+
'no_unneeded_curly_braces' => true,
20+
'no_unneeded_final_method' => true,
21+
'no_useless_else' => true,
22+
'no_useless_return' => true,
23+
'not_operator_with_space' => false,
24+
'not_operator_with_successor_space' => false,
25+
'phpdoc_add_missing_param_annotation' => true,
26+
'phpdoc_annotation_without_dot' => null,
27+
'phpdoc_no_alias_tag' => false,
28+
'phpdoc_order' => true,
29+
'phpdoc_separation' => false,
30+
'phpdoc_summary' => null,
31+
'phpdoc_types_order' => true,
32+
'return_type_declaration' => true,
33+
'void_return' => false,
34+
'yoda_style' => false,
35+
])
36+
->setFinder(\PhpCsFixer\Finder::create()->in(['src', 'tests']));

.scrutinizer.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
checks:
2+
php:
3+
code_rating: true
4+
5+
filter:
6+
excluded_paths:
7+
- tests/*
8+
- vendor/*
9+
10+
build:
11+
12+
environment:
13+
php: '7.3'
14+
docker: true
15+
16+
dependencies:
17+
before:
18+
- sudo rm /usr/local/bin/docker-compose
19+
- curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > docker-compose
20+
- chmod +x docker-compose
21+
- sudo mv docker-compose /usr/local/bin
22+
- curl https://bootstrap.pypa.io/get-pip.py | python
23+
- pip install docker-compose-wait
24+
- cp docker-compose-ci.yml docker-compose.yml
25+
- export PHP_VERSION=$(php -r 'preg_match("/^((\d+)[.](\d+))[.](\d+)$/", PHP_VERSION, $m); echo $m[1];')
26+
- docker-compose up -d
27+
- composer install
28+
- mkdir -p build/logs
29+
- docker-compose-wait
30+
31+
tests:
32+
override:
33+
-
34+
command: "docker-compose exec php sh -c 'cd /code; vendor/bin/phpunit --coverage-clover build/logs/clover.xml'"
35+
coverage:
36+
file: 'build/logs/clover.xml'
37+
format: 'clover'

.travis.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
language: php
2+
3+
services:
4+
- docker
5+
6+
matrix:
7+
exclude:
8+
- env: LARAVEL_VERSION=^6.0 TESTBENCH_VERSION=^4.0
9+
php: 7.1
10+
11+
php:
12+
- 7.3
13+
- 7.2
14+
- 7.1
15+
16+
cache:
17+
directories:
18+
- ./vendor
19+
- $HOME/.composer/cache
20+
21+
env:
22+
- LARAVEL_VERSION=^6.0 TESTBENCH_VERSION=^4.0
23+
- LARAVEL_VERSION=5.8.* TESTBENCH_VERSION=3.8.*
24+
- LARAVEL_VERSION=5.7.* TESTBENCH_VERSION=3.7.*
25+
- LARAVEL_VERSION=5.6.* TESTBENCH_VERSION=3.6.*
26+
27+
before_install:
28+
- sudo rm /usr/local/bin/docker-compose
29+
- curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > docker-compose
30+
- chmod +x docker-compose
31+
- sudo mv docker-compose /usr/local/bin
32+
- sudo pip install docker-compose-wait
33+
34+
before_script:
35+
- cp docker-compose-ci.yml docker-compose.yml
36+
- export PHP_VERSION=$(php -r 'preg_match("/^((\d+)[.](\d+))[.](\d+)$/", PHP_VERSION, $m); echo $m[1];')
37+
- docker-compose up -d
38+
- composer self-update
39+
- composer require "laravel/framework:${LARAVEL_VERSION}" "orchestra/testbench:${TESTBENCH_VERSION}" --no-update
40+
- if [ "$PHPUNIT_VERSION" != "" ]; then composer require "phpunit/phpunit:${PHPUNIT_VERSION}" --no-update; fi;
41+
- composer update
42+
- mkdir -p build/logs
43+
- docker-compose-wait
44+
45+
script:
46+
- docker-compose exec php sh -c 'cd /code; vendor/bin/phpunit'

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 mpyw
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 all
13+
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 THE
21+
SOFTWARE.

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Laravel MySQL System Variable Manager<br>[![Build Status](https://travis-ci.org/mpyw/laravel-mysql-system-variable-manager.svg?branch=master)](https://travis-ci.org/mpyw/laravel-mysql-system-variable-manager) [![Code Coverage](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/?branch=master)
2+
3+
A tiny extension of `MySqlConnection` that manages **session** system variables
4+
5+
## Requirements
6+
7+
- PHP: ^7.1
8+
- Laravel: ^5.6 || ^6.0
9+
10+
## Installing
11+
12+
```bash
13+
composer require mpyw/laravel-mysql-system-variable-manager
14+
```
15+
16+
The default implementation is provided by the automatically-discovered service provider.
17+
18+
## Basic Usage
19+
20+
```php
21+
<?php
22+
23+
use Illuminate\Support\Facades\DB;
24+
25+
// Assign an auto-recoverable system variable
26+
// The variable is reassigned on accidental disconnections
27+
DB::setSystemVariable('long_query_time', 10.0);
28+
29+
// Assign a system variable without auto-recovery
30+
DB::setSystemVariable('long_query_time', 10.0, false);
31+
32+
// Assign multiple variables
33+
DB::setSystemVariables(['long_query_time' => 10.0, 'tx_isolation' => 'read-committed']);
34+
35+
// Assign a variable on a different connection
36+
DB::connection('other_mysql_connection')->setSystemVariable('long_query_time', 10.0);
37+
```
38+
39+
**WARNING:**
40+
Don't use `DB::disconnect()` directly or auto-recovery won't be fired.
41+
Use **`DB::connection()->disconnect()`** instead.
42+
43+
## Advanced Usage
44+
45+
You can manually customize extended `MySqlConnection` using `ManagesSystemVariables` trait.
46+
47+
```php
48+
<?php
49+
50+
namespace App\Providers;
51+
52+
use App\Database\MySqlConnection;
53+
use Illuminate\Database\Connection;
54+
use Illuminate\Support\ServiceProvider;
55+
56+
class DatabaseServiceProvider extends ServiceProvider
57+
{
58+
public function boot(): void
59+
{
60+
Connection::resolverFor('mysql', function (...$parameters) {
61+
return new MySqlConnection(...$parameters);
62+
});
63+
}
64+
}
65+
```
66+
67+
```php
68+
<?php
69+
70+
namespace App\Database;
71+
72+
use Illuminate\Database\Connection as BaseMySqlConnection;
73+
use Mpyw\LaravelMysqlSystemVariableManager\ManagesSystemVariables;
74+
75+
class MySqlConnection extends BaseMySqlConnection
76+
{
77+
use ManagesSystemVariables;
78+
79+
public function withoutForeignKeyChecks(callable $callback, ...$args)
80+
{
81+
$this->setSystemVariable('foreign_key_checks', false);
82+
try {
83+
return $callback(...$args);
84+
} finally {
85+
$this->setSystemVariable('foreign_key_checks', true);
86+
}
87+
}
88+
}
89+
```
90+
91+
```php
92+
<?php
93+
94+
use App\Post;
95+
use Illuminate\Support\Facades\Auth;
96+
use Illuminate\Support\Facades\DB;
97+
98+
$post = new Post();
99+
$post->user()->associate(Auth::user());
100+
$post->save();
101+
102+
DB::withoutForeignKeyChecks(function () use ($post) {
103+
$post->user()->associate(null);
104+
$post->save();
105+
});
106+
```

composer.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "mpyw/laravel-mysql-system-variable-manager",
3+
"description": "A tiny extension of MySqlConnection that manages session system variables",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "mpyw",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"keywords": ["laravel", "illuminate", "mysql", "system", "variable"],
13+
"autoload": {
14+
"psr-4": {
15+
"Mpyw\\LaravelMysqlSystemVariableManager\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Lampager\\Laravel\\Tests\\": "tests/"
21+
}
22+
},
23+
"require": {
24+
"php": "^7.1",
25+
"ext-pdo": "*",
26+
"illuminate/support": "^5.6 || ^6.0 || ^7.0 || ^8.0",
27+
"illuminate/database": "^5.6 || ^6.0 || ^7.0 || ^8.0"
28+
},
29+
"require-dev": {
30+
"orchestra/testbench": "^4.0",
31+
"laravel/framework": "^6.0",
32+
"nilportugues/sql-query-formatter": "^1.2",
33+
"friendsofphp/php-cs-fixer": "^2.15"
34+
},
35+
"extra": {
36+
"laravel": {
37+
"providers": [
38+
"Mpyw\\LaravelMysqlSystemVariableManager\\LaravelMysqlSystemVariableManagerServiceProvider"
39+
]
40+
}
41+
}
42+
}

docker-compose-ci.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
3+
services:
4+
5+
php:
6+
image: circleci/php:${PHP_VERSION}
7+
volumes:
8+
- .:/code
9+
environment:
10+
- LARAVEL_VERSION
11+
- PHPUNIT_VERSION
12+
- TESTBENCH_VERSION
13+
command:
14+
sh -c '
15+
chown -R myuser:myuser;
16+
sudo docker-php-ext-install pdo_mysql;
17+
cd /code;
18+
tail -F /dev/null;
19+
'
20+
user: root
21+
22+
mysql:
23+
image: mysql:5.7
24+
environment:
25+
MYSQL_DATABASE: testing
26+
MYSQL_USER: user
27+
MYSQL_PASSWORD: password
28+
MYSQL_ALLOW_EMPTY_PASSWORD: 1

docker-compose.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: '3'
2+
3+
services:
4+
5+
php:
6+
image: circleci/php:7.3
7+
volumes:
8+
- .:/code:cached
9+
command:
10+
sh -c '
11+
sudo docker-php-ext-install pdo_mysql;
12+
cd /code;
13+
tail -F /dev/null;
14+
'
15+
16+
mysql:
17+
image: mysql:5.7
18+
environment:
19+
MYSQL_DATABASE: testing
20+
MYSQL_USER: user
21+
MYSQL_PASSWORD: password
22+
MYSQL_ALLOW_EMPTY_PASSWORD: 1

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
bootstrap="vendor/autoload.php"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
>
12+
<testsuites>
13+
<testsuite name="Package Test Suite">
14+
<directory suffix="Test.php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist processUncoveredFilesFromWhitelist="true">
19+
<directory suffix=".php">src</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Mpyw\LaravelMysqlSystemVariableManager;
4+
5+
use Illuminate\Database\Connection;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
/**
9+
* Class LaravelMysqlSystemVariableManagerServiceProvider
10+
*/
11+
class LaravelMysqlSystemVariableManagerServiceProvider extends ServiceProvider
12+
{
13+
public function boot(): void
14+
{
15+
Connection::resolverFor('mysql', function (...$parameters) {
16+
return new MySqlConnection(...$parameters);
17+
});
18+
}
19+
}

0 commit comments

Comments
 (0)