diff --git a/php/src/db/CapacitorStorage.php b/php/src/db/CapacitorStorage.php index 2801fca..1773198 100644 --- a/php/src/db/CapacitorStorage.php +++ b/php/src/db/CapacitorStorage.php @@ -388,7 +388,7 @@ EOT; if ($func !== null) { $updates = func::with($func) ->prependArgs([$item, $values, $pvalues]) - ->bind($channel, true) + ->bind($channel) ->invoke(); if ($updates === [false]) return 0; if (is_array($updates) && $updates) { @@ -603,7 +603,7 @@ EOT; function _each(CapacitorChannel $channel, $filter, $func, ?array $args, ?array $mergeQuery=null, ?int &$nbUpdated=null): int { $this->_create($channel); if ($func === null) $func = CapacitorChannel::onEach; - $onEach = func::with($func)->bind($channel, true); + $onEach = func::with($func)->bind($channel); $db = $this->db(); # si on est déjà dans une transaction, désactiver la gestion des transactions $manageTransactions = $channel->isManageTransactions() && !$db->inTransaction(); @@ -671,7 +671,7 @@ EOT; function _delete(CapacitorChannel $channel, $filter, $func, ?array $args): int { $this->_create($channel); if ($func === null) $func = CapacitorChannel::onDelete; - $onEach = func::with($func)->bind($channel, true); + $onEach = func::with($func)->bind($channel); $db = $this->db(); # si on est déjà dans une transaction, désactiver la gestion des transactions $manageTransactions = $channel->isManageTransactions() && !$db->inTransaction(); diff --git a/php/src/db/_private/_config.php b/php/src/db/_private/_config.php index e72b526..4a9ab06 100644 --- a/php/src/db/_private/_config.php +++ b/php/src/db/_private/_config.php @@ -29,7 +29,7 @@ class _config { if (is_string($config) && !func::is_method($config)) { $db->exec($config); } else { - func::with($config)->bind($this, true)->invoke([$db, $key]); + func::with($config)->bind($this)->invoke([$db, $key]); } } } diff --git a/php/src/db/_private/_migration.php b/php/src/db/_private/_migration.php index 761addf..f11cbb8 100644 --- a/php/src/db/_private/_migration.php +++ b/php/src/db/_private/_migration.php @@ -64,7 +64,7 @@ abstract class _migration { if (is_string($migration) || !func::is_callable($migration)) { $db->exec($migration); } else { - func::with($migration)->bind($this, true)->invoke([$db, $name]); + func::with($migration)->bind($this)->invoke([$db, $name]); } $this->setMigrated($name, true); } diff --git a/php/src/db/pdo/Pdo.php b/php/src/db/pdo/Pdo.php index 2a970fa..78e38db 100644 --- a/php/src/db/pdo/Pdo.php +++ b/php/src/db/pdo/Pdo.php @@ -120,7 +120,7 @@ class Pdo implements IDatabase { $dbconn = $this->dbconn; $options = $this->options; if (is_callable($options)) { - $options = func::with($options)->bind($this, true)->invoke(); + $options = func::with($options)->bind($this)->invoke(); } $this->db = new \PDO($dbconn["name"], $dbconn["user"], $dbconn["pass"], $options); _config::with($this->config)->configure($this); diff --git a/php/src/db/pgsql/Pgsql.php b/php/src/db/pgsql/Pgsql.php index b5bf9d8..34d82dc 100644 --- a/php/src/db/pgsql/Pgsql.php +++ b/php/src/db/pgsql/Pgsql.php @@ -136,7 +136,7 @@ class Pgsql implements IDatabase { $connection_string = implode(" ", array_filter($connection_string)); $options = $this->options; if (is_callable($options)) { - $options = func::with($options)->bind($this, true)->invoke(); + $options = func::with($options)->bind($this)->invoke(); } $forceNew = $options["force_new"] ?? false; $flags = $forceNew? PGSQL_CONNECT_FORCE_NEW: 0; diff --git a/php/src/file/tab/AbstractBuilder.php b/php/src/file/tab/AbstractBuilder.php index 2affcc8..3ec767d 100644 --- a/php/src/file/tab/AbstractBuilder.php +++ b/php/src/file/tab/AbstractBuilder.php @@ -35,7 +35,7 @@ abstract class AbstractBuilder extends TempStream implements IBuilder { $this->rows = $rows; $this->index = 0; $cookFunc = $params["cook_func"] ?? null; - if ($cookFunc !== null) $cookFunc = func::with($cookFunc)->bind($this, true); + if ($cookFunc !== null) $cookFunc = func::with($cookFunc)->bind($this); $this->cookFunc = $cookFunc; $this->output = $params["output"] ?? static::OUTPUT; $maxMemory = $params["max_memory"] ?? null; diff --git a/php/src/php/content/c.php b/php/src/php/content/c.php index 4105846..ebf4c52 100644 --- a/php/src/php/content/c.php +++ b/php/src/php/content/c.php @@ -82,7 +82,7 @@ class c { $arg = self::resolve($arg, $object_or_class, false); if (!$array) $arg = $arg[0]; }; unset($arg); - $value = func::with($func, $args)->bind($object_or_class, true)->invoke(); + $value = func::with($func, $args)->bind($object_or_class)->invoke(); } } if ($seq) $dest[] = $value; diff --git a/php/src/php/func.php b/php/src/php/func.php index 26361f7..90a1cff 100644 --- a/php/src/php/func.php +++ b/php/src/php/func.php @@ -649,9 +649,9 @@ class func { else return $this->bound && $this->object !== null; } - function bind($object, bool $unlessAlreadyBound=false, bool $replace=false): self { + function bind($object, bool $rebind=false, bool $replace=false): self { if ($this->type !== self::TYPE_METHOD) return $this; - if ($this->bound && $unlessAlreadyBound) return $this; + if (!$rebind && $this->isBound()) return $this; [$c, $f] = $this->func; if ($replace) { diff --git a/php/tests/php/funcTest.php b/php/tests/php/funcTest.php index 45a0581..22fa882 100644 --- a/php/tests/php/funcTest.php +++ b/php/tests/php/funcTest.php @@ -1102,15 +1102,25 @@ namespace nulib\php { } function testRebind() { + # bind if not already bound + $func = func::with([C1::class, "tmethod"]); + // bind + self::assertSame(11, $func->bind(new C1(0))->invoke()); + // pas de bind, puis que déjà bound + self::assertSame(11, $func->bind(new C1(1))->invoke()); + // même si l'objet est de type différent, pas de bind + self::assertSame(11, $func->bind(new C0())->invoke()); + + # force rebind $func = func::with([C1::class, "tmethod"]); // objets du même type - self::assertSame(11, $func->bind(new C1(0))->invoke()); - self::assertSame(12, $func->bind(new C1(1))->invoke()); + self::assertSame(11, $func->bind(new C1(0), true)->invoke()); + self::assertSame(12, $func->bind(new C1(1), true)->invoke()); // objets de type différent self::assertException(ValueException::class, function() use ($func) { - $func->bind(new C0())->invoke(); + $func->bind(new C0(), true)->invoke(); }); - self::assertSame(11, $func->bind(new C0(), false, true)->invoke()); + self::assertSame(11, $func->bind(new C0(), true, true)->invoke()); } function testModifyArgs() {