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) {
$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();

View File

@ -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]);
}
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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() {