pas de rebind par défaut

This commit is contained in:
Jephté Clain 2025-04-28 05:43:34 +04:00
parent 299b90c85e
commit 9767028da6
9 changed files with 25 additions and 15 deletions

View File

@ -388,7 +388,7 @@ EOT;
if ($func !== null) { if ($func !== null) {
$updates = func::with($func) $updates = func::with($func)
->prependArgs([$item, $values, $pvalues]) ->prependArgs([$item, $values, $pvalues])
->bind($channel, true) ->bind($channel)
->invoke(); ->invoke();
if ($updates === [false]) return 0; if ($updates === [false]) return 0;
if (is_array($updates) && $updates) { 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 { function _each(CapacitorChannel $channel, $filter, $func, ?array $args, ?array $mergeQuery=null, ?int &$nbUpdated=null): int {
$this->_create($channel); $this->_create($channel);
if ($func === null) $func = CapacitorChannel::onEach; if ($func === null) $func = CapacitorChannel::onEach;
$onEach = func::with($func)->bind($channel, true); $onEach = func::with($func)->bind($channel);
$db = $this->db(); $db = $this->db();
# si on est déjà dans une transaction, désactiver la gestion des transactions # si on est déjà dans une transaction, désactiver la gestion des transactions
$manageTransactions = $channel->isManageTransactions() && !$db->inTransaction(); $manageTransactions = $channel->isManageTransactions() && !$db->inTransaction();
@ -671,7 +671,7 @@ EOT;
function _delete(CapacitorChannel $channel, $filter, $func, ?array $args): int { function _delete(CapacitorChannel $channel, $filter, $func, ?array $args): int {
$this->_create($channel); $this->_create($channel);
if ($func === null) $func = CapacitorChannel::onDelete; if ($func === null) $func = CapacitorChannel::onDelete;
$onEach = func::with($func)->bind($channel, true); $onEach = func::with($func)->bind($channel);
$db = $this->db(); $db = $this->db();
# si on est déjà dans une transaction, désactiver la gestion des transactions # si on est déjà dans une transaction, désactiver la gestion des transactions
$manageTransactions = $channel->isManageTransactions() && !$db->inTransaction(); $manageTransactions = $channel->isManageTransactions() && !$db->inTransaction();

View File

@ -29,7 +29,7 @@ class _config {
if (is_string($config) && !func::is_method($config)) { if (is_string($config) && !func::is_method($config)) {
$db->exec($config); $db->exec($config);
} else { } else {
func::with($config)->bind($this, true)->invoke([$db, $key]); func::with($config)->bind($this)->invoke([$db, $key]);
} }
} }
} }

View File

@ -64,7 +64,7 @@ abstract class _migration {
if (is_string($migration) || !func::is_callable($migration)) { if (is_string($migration) || !func::is_callable($migration)) {
$db->exec($migration); $db->exec($migration);
} else { } else {
func::with($migration)->bind($this, true)->invoke([$db, $name]); func::with($migration)->bind($this)->invoke([$db, $name]);
} }
$this->setMigrated($name, true); $this->setMigrated($name, true);
} }

View File

@ -120,7 +120,7 @@ class Pdo implements IDatabase {
$dbconn = $this->dbconn; $dbconn = $this->dbconn;
$options = $this->options; $options = $this->options;
if (is_callable($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); $this->db = new \PDO($dbconn["name"], $dbconn["user"], $dbconn["pass"], $options);
_config::with($this->config)->configure($this); _config::with($this->config)->configure($this);

View File

@ -136,7 +136,7 @@ class Pgsql implements IDatabase {
$connection_string = implode(" ", array_filter($connection_string)); $connection_string = implode(" ", array_filter($connection_string));
$options = $this->options; $options = $this->options;
if (is_callable($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; $forceNew = $options["force_new"] ?? false;
$flags = $forceNew? PGSQL_CONNECT_FORCE_NEW: 0; $flags = $forceNew? PGSQL_CONNECT_FORCE_NEW: 0;

View File

@ -35,7 +35,7 @@ abstract class AbstractBuilder extends TempStream implements IBuilder {
$this->rows = $rows; $this->rows = $rows;
$this->index = 0; $this->index = 0;
$cookFunc = $params["cook_func"] ?? null; $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->cookFunc = $cookFunc;
$this->output = $params["output"] ?? static::OUTPUT; $this->output = $params["output"] ?? static::OUTPUT;
$maxMemory = $params["max_memory"] ?? null; $maxMemory = $params["max_memory"] ?? null;

View File

@ -82,7 +82,7 @@ class c {
$arg = self::resolve($arg, $object_or_class, false); $arg = self::resolve($arg, $object_or_class, false);
if (!$array) $arg = $arg[0]; if (!$array) $arg = $arg[0];
}; unset($arg); }; 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; if ($seq) $dest[] = $value;

View File

@ -649,9 +649,9 @@ class func {
else return $this->bound && $this->object !== null; 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->type !== self::TYPE_METHOD) return $this;
if ($this->bound && $unlessAlreadyBound) return $this; if (!$rebind && $this->isBound()) return $this;
[$c, $f] = $this->func; [$c, $f] = $this->func;
if ($replace) { if ($replace) {

View File

@ -1102,15 +1102,25 @@ namespace nulib\php {
} }
function testRebind() { 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"]); $func = func::with([C1::class, "tmethod"]);
// objets du même type // objets du même type
self::assertSame(11, $func->bind(new C1(0))->invoke()); self::assertSame(11, $func->bind(new C1(0), true)->invoke());
self::assertSame(12, $func->bind(new C1(1))->invoke()); self::assertSame(12, $func->bind(new C1(1), true)->invoke());
// objets de type différent // objets de type différent
self::assertException(ValueException::class, function() use ($func) { 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() { function testModifyArgs() {