From a594c748ac627295ee7e9f53a9bb0a792abb547b Mon Sep 17 00:00:00 2001 From: mpyw Date: Sun, 1 Mar 2020 08:18:36 +0900 Subject: [PATCH 1/4] Refactor: Move reconnector logic to a new class --- src/ManagesSystemVariables.php | 18 ++-------- src/SystemVariableMemoizedAssigner.php | 50 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 src/SystemVariableMemoizedAssigner.php diff --git a/src/ManagesSystemVariables.php b/src/ManagesSystemVariables.php index 8f7662d..caebddb 100644 --- a/src/ManagesSystemVariables.php +++ b/src/ManagesSystemVariables.php @@ -9,14 +9,6 @@ */ trait ManagesSystemVariables { - /** - * Set the reconnect instance on the connection. - * - * @param callable $reconnector - * @return $this - */ - abstract public function setReconnector(callable $reconnector); - /** * Set MySQL system variable for both read and write PDOs. * It is lazily executed for unresolved PDO instance. @@ -41,14 +33,8 @@ public function setSystemVariable(string $key, $value, bool $memoizeForReconnect */ public function setSystemVariables(array $values, bool $memoizeForReconnect = true) { - (new SystemVariableAssigner($this->readPdo, $this->pdo))->assign($values); - - if (!$this->reconnector instanceof SystemVariableAwareReconnector) { - $this->setReconnector(new SystemVariableAwareReconnector($this->reconnector)); - } - if ($memoizeForReconnect) { - $this->reconnector->memoizeSystemVariables($values); - } + (new SystemVariableMemoizedAssigner($this->reconnector, $this->readPdo, $this->pdo)) + ->assign($values, $memoizeForReconnect); return $this; } diff --git a/src/SystemVariableMemoizedAssigner.php b/src/SystemVariableMemoizedAssigner.php new file mode 100644 index 0000000..fb70db6 --- /dev/null +++ b/src/SystemVariableMemoizedAssigner.php @@ -0,0 +1,50 @@ +reconnector = $reconnector = !$reconnector instanceof SystemVariableAwareReconnector + ? new SystemVariableAwareReconnector($reconnector) + : $reconnector; + + $this->pdos = array_filter($pdos); + } + + /** + * Set MySQL system variables for PDO. + * + * @param array $values + * @param bool $memoizeForReconnect + * @return $this + */ + public function assign(array $values, bool $memoizeForReconnect = true) + { + (new SystemVariableAssigner(...$this->pdos)) + ->assign($values); + + if ($memoizeForReconnect) { + $this->reconnector->memoizeSystemVariables($values); + } + + return $this; + } +} From 1cc0adc560f1bc740278ce2476382183245368e9 Mon Sep 17 00:00:00 2001 From: mpyw Date: Sun, 1 Mar 2020 08:24:54 +0900 Subject: [PATCH 2/4] Refactor: Rename aliases --- src/SystemVariableAssigner.php | 7 +++---- src/SystemVariableSelector.php | 3 +-- src/SystemVariableTemporaryAssigner.php | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/SystemVariableAssigner.php b/src/SystemVariableAssigner.php index 4ec0453..a90e239 100644 --- a/src/SystemVariableAssigner.php +++ b/src/SystemVariableAssigner.php @@ -8,9 +8,8 @@ use Mpyw\LaravelMySqlSystemVariableManager\Replacers\FloatReplacerInterface; use Mpyw\LaravelMySqlSystemVariableManager\Replacers\IntegerReplacerInterface; use Mpyw\LaravelMySqlSystemVariableManager\Replacers\StringReplacerInterface; -use Mpyw\LaravelMySqlSystemVariableManager\Value as BindingValue; use Mpyw\LaravelPdoEmulationControl\EmulationController; -use Mpyw\Unclosure\Value; +use Mpyw\Unclosure\Value as ValueEffector; use PDO; use Mpyw\LaravelMySqlSystemVariableManager\SystemVariableGrammar as Grammar; use PDOStatement; @@ -55,7 +54,7 @@ public function assign(array $values) protected function withEmulatedStatement(string $query, array $values = []) { foreach ($this->pdos as &$pdo) { - $pdo = Value::withCallback( + $pdo = ValueEffector::withCallback( $pdo, Closure::fromCallable([$this, 'withEmulatedStatementFor']), $query, @@ -91,7 +90,7 @@ protected static function withEmulatedStatementFor(PDO $pdo, string $query, arra */ protected static function withStatementFor(PDO $pdo, string $query, array $values): PDO { - $expressions = array_map([BindingValue::class, 'wrap'], $values); + $expressions = array_map([Value::class, 'wrap'], $values); $original = static::selectOriginalVariablesForReplacer($pdo, $expressions); $statement = $pdo->prepare($query); diff --git a/src/SystemVariableSelector.php b/src/SystemVariableSelector.php index 5850cb7..31aa07a 100644 --- a/src/SystemVariableSelector.php +++ b/src/SystemVariableSelector.php @@ -2,7 +2,6 @@ namespace Mpyw\LaravelMySqlSystemVariableManager; -use Mpyw\LaravelMySqlSystemVariableManager\Value as BindingValue; use PDO; class SystemVariableSelector @@ -25,7 +24,7 @@ public static function selectOriginalVariables(PDO $pdo, array $newValues): arra ->fetch(PDO::FETCH_ASSOC); foreach ($original as $key => $value) { - $original[$key] = BindingValue::as(BindingValue::wrap($newValues[$key])->getType(), $value); + $original[$key] = Value::as(Value::wrap($newValues[$key])->getType(), $value); } return $original; diff --git a/src/SystemVariableTemporaryAssigner.php b/src/SystemVariableTemporaryAssigner.php index 015a417..912bb10 100644 --- a/src/SystemVariableTemporaryAssigner.php +++ b/src/SystemVariableTemporaryAssigner.php @@ -2,7 +2,7 @@ namespace Mpyw\LaravelMySqlSystemVariableManager; -use Mpyw\Unclosure\Value; +use Mpyw\Unclosure\Value as ValueEffector; use PDO; class SystemVariableTemporaryAssigner @@ -32,7 +32,7 @@ public function __construct(&...$pdos) */ public function using(array $using, callable $callback, ...$args) { - return Value::withEffectForEach($this->pdos, function (PDO $pdo) use ($using) { + return ValueEffector::withEffectForEach($this->pdos, function (PDO $pdo) use ($using) { $original = SystemVariableSelector::selectOriginalVariables($pdo, $using); (new SystemVariableAssigner($pdo))->assign($using); From b0ce0de17134af589bf1517a8c9af8bae07552bd Mon Sep 17 00:00:00 2001 From: mpyw Date: Sun, 1 Mar 2020 08:29:35 +0900 Subject: [PATCH 3/4] Doc: Fix typo --- src/ExpressionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExpressionInterface.php b/src/ExpressionInterface.php index 0ea751a..aca42ab 100644 --- a/src/ExpressionInterface.php +++ b/src/ExpressionInterface.php @@ -25,7 +25,7 @@ interface ExpressionInterface public function getType(): string; /** - * Return PDO::PARAM_* type.s + * Return PDO::PARAM_* type. * * @return int */ From 5efe6b0ddcd33d2c4196af29065033629c3d72b1 Mon Sep 17 00:00:00 2001 From: mpyw Date: Sun, 1 Mar 2020 08:42:19 +0900 Subject: [PATCH 4/4] Doc: Fix wrong type --- src/Replacer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Replacer.php b/src/Replacer.php index f0d43fd..c01c0cf 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -62,7 +62,7 @@ public static function str(callable $callback): StringReplacerInterface * Create new typed replacer for MySQL system variable. * * @param string $type - * @param bool|float|int|string $callback + * @param callable $callback * @return \Mpyw\LaravelMySqlSystemVariableManager\ExpressionInterface */ public static function as(string $type, $callback): ExpressionInterface