Predis\Replication\ReplicationStrategy::setCommandReadOnly PHP Method

setCommandReadOnly() public method

When the behavior of a command can be decided only at runtime depending on its arguments, a callable object can be provided to dynamically check if the specified command performs a read or a write operation.
public setCommandReadOnly ( string $commandID, mixed $readonly = true )
$commandID string Command ID.
$readonly mixed A boolean value or a callable object.
    public function setCommandReadOnly($commandID, $readonly = true)
    {
        $commandID = strtoupper($commandID);
        if ($readonly) {
            $this->readonly[$commandID] = $readonly;
        } else {
            unset($this->readonly[$commandID]);
        }
    }

Usage Example

 /**
  * @group disconnected
  */
 public function testCanUseCallableToCheckCommand()
 {
     $strategy = new ReplicationStrategy();
     $profile = ServerProfile::getDevelopment();
     $strategy->setCommandReadOnly('SET', function ($command) {
         return $command->getArgument(1) === true;
     });
     $command = $profile->createCommand('SET', array('trigger', false));
     $this->assertFalse($strategy->isReadOperation($command));
     $command = $profile->createCommand('SET', array('trigger', true));
     $this->assertTrue($strategy->isReadOperation($command));
 }