Skip to content

Commit 9598139

Browse files
authored
Refactor: Move reconnector logic to a new class (#8)
* Refactor: Move reconnector logic to a new class * Refactor: Rename aliases * Doc: Fix typo * Doc: Fix wrong type
1 parent 6add43f commit 9598139

7 files changed

+60
-26
lines changed

src/ExpressionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface ExpressionInterface
2525
public function getType(): string;
2626

2727
/**
28-
* Return PDO::PARAM_* type.s
28+
* Return PDO::PARAM_* type.
2929
*
3030
* @return int
3131
*/

src/ManagesSystemVariables.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
trait ManagesSystemVariables
1111
{
12-
/**
13-
* Set the reconnect instance on the connection.
14-
*
15-
* @param callable $reconnector
16-
* @return $this
17-
*/
18-
abstract public function setReconnector(callable $reconnector);
19-
2012
/**
2113
* Set MySQL system variable for both read and write PDOs.
2214
* It is lazily executed for unresolved PDO instance.
@@ -41,14 +33,8 @@ public function setSystemVariable(string $key, $value, bool $memoizeForReconnect
4133
*/
4234
public function setSystemVariables(array $values, bool $memoizeForReconnect = true)
4335
{
44-
(new SystemVariableAssigner($this->readPdo, $this->pdo))->assign($values);
45-
46-
if (!$this->reconnector instanceof SystemVariableAwareReconnector) {
47-
$this->setReconnector(new SystemVariableAwareReconnector($this->reconnector));
48-
}
49-
if ($memoizeForReconnect) {
50-
$this->reconnector->memoizeSystemVariables($values);
51-
}
36+
(new SystemVariableMemoizedAssigner($this->reconnector, $this->readPdo, $this->pdo))
37+
->assign($values, $memoizeForReconnect);
5238

5339
return $this;
5440
}

src/Replacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static function str(callable $callback): StringReplacerInterface
6262
* Create new typed replacer for MySQL system variable.
6363
*
6464
* @param string $type
65-
* @param bool|float|int|string $callback
65+
* @param callable $callback
6666
* @return \Mpyw\LaravelMySqlSystemVariableManager\ExpressionInterface
6767
*/
6868
public static function as(string $type, $callback): ExpressionInterface

src/SystemVariableAssigner.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
use Mpyw\LaravelMySqlSystemVariableManager\Replacers\FloatReplacerInterface;
99
use Mpyw\LaravelMySqlSystemVariableManager\Replacers\IntegerReplacerInterface;
1010
use Mpyw\LaravelMySqlSystemVariableManager\Replacers\StringReplacerInterface;
11-
use Mpyw\LaravelMySqlSystemVariableManager\Value as BindingValue;
1211
use Mpyw\LaravelPdoEmulationControl\EmulationController;
13-
use Mpyw\Unclosure\Value;
12+
use Mpyw\Unclosure\Value as ValueEffector;
1413
use PDO;
1514
use Mpyw\LaravelMySqlSystemVariableManager\SystemVariableGrammar as Grammar;
1615
use PDOStatement;
@@ -55,7 +54,7 @@ public function assign(array $values)
5554
protected function withEmulatedStatement(string $query, array $values = [])
5655
{
5756
foreach ($this->pdos as &$pdo) {
58-
$pdo = Value::withCallback(
57+
$pdo = ValueEffector::withCallback(
5958
$pdo,
6059
Closure::fromCallable([$this, 'withEmulatedStatementFor']),
6160
$query,
@@ -91,7 +90,7 @@ protected static function withEmulatedStatementFor(PDO $pdo, string $query, arra
9190
*/
9291
protected static function withStatementFor(PDO $pdo, string $query, array $values): PDO
9392
{
94-
$expressions = array_map([BindingValue::class, 'wrap'], $values);
93+
$expressions = array_map([Value::class, 'wrap'], $values);
9594
$original = static::selectOriginalVariablesForReplacer($pdo, $expressions);
9695
$statement = $pdo->prepare($query);
9796

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Mpyw\LaravelMySqlSystemVariableManager;
4+
5+
class SystemVariableMemoizedAssigner
6+
{
7+
/**
8+
* @var \Mpyw\LaravelMySqlSystemVariableManager\SystemVariableAwareReconnector
9+
*/
10+
protected $reconnector;
11+
12+
/**
13+
* @var \Closure[]|\PDO[]
14+
*/
15+
protected $pdos;
16+
17+
/**
18+
* SystemVariableMemoizedAssigner constructor.
19+
*
20+
* @param null|callable|\Mpyw\LaravelMySqlSystemVariableManager\SystemVariableAwareReconnector &$reconnector
21+
* @param null|\Closure|\PDO &...$pdos
22+
*/
23+
public function __construct(&$reconnector, &...$pdos)
24+
{
25+
$this->reconnector = $reconnector = !$reconnector instanceof SystemVariableAwareReconnector
26+
? new SystemVariableAwareReconnector($reconnector)
27+
: $reconnector;
28+
29+
$this->pdos = array_filter($pdos);
30+
}
31+
32+
/**
33+
* Set MySQL system variables for PDO.
34+
*
35+
* @param array $values
36+
* @param bool $memoizeForReconnect
37+
* @return $this
38+
*/
39+
public function assign(array $values, bool $memoizeForReconnect = true)
40+
{
41+
(new SystemVariableAssigner(...$this->pdos))
42+
->assign($values);
43+
44+
if ($memoizeForReconnect) {
45+
$this->reconnector->memoizeSystemVariables($values);
46+
}
47+
48+
return $this;
49+
}
50+
}

src/SystemVariableSelector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Mpyw\LaravelMySqlSystemVariableManager;
44

5-
use Mpyw\LaravelMySqlSystemVariableManager\Value as BindingValue;
65
use PDO;
76

87
class SystemVariableSelector
@@ -25,7 +24,7 @@ public static function selectOriginalVariables(PDO $pdo, array $newValues): arra
2524
->fetch(PDO::FETCH_ASSOC);
2625

2726
foreach ($original as $key => $value) {
28-
$original[$key] = BindingValue::as(BindingValue::wrap($newValues[$key])->getType(), $value);
27+
$original[$key] = Value::as(Value::wrap($newValues[$key])->getType(), $value);
2928
}
3029

3130
return $original;

src/SystemVariableTemporaryAssigner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Mpyw\LaravelMySqlSystemVariableManager;
44

5-
use Mpyw\Unclosure\Value;
5+
use Mpyw\Unclosure\Value as ValueEffector;
66
use PDO;
77

88
class SystemVariableTemporaryAssigner
@@ -32,7 +32,7 @@ public function __construct(&...$pdos)
3232
*/
3333
public function using(array $using, callable $callback, ...$args)
3434
{
35-
return Value::withEffectForEach($this->pdos, function (PDO $pdo) use ($using) {
35+
return ValueEffector::withEffectForEach($this->pdos, function (PDO $pdo) use ($using) {
3636
$original = SystemVariableSelector::selectOriginalVariables($pdo, $using);
3737
(new SystemVariableAssigner($pdo))->assign($using);
3838

0 commit comments

Comments
 (0)