forked from predis/predis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for container commands (predis#1049)
* Added support for container commands FUNCTION LOAD, FUNCTION DELETE and FCALL * Changed ContainerInterface and AbstractContainer * Re-implement logic of abstract methods --------- Co-authored-by: Vladyslav Vildanov <[email protected]>
- Loading branch information
1 parent
38e955b
commit 98222f4
Showing
22 changed files
with
1,051 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Redis\Container; | ||
|
||
use Predis\ClientInterface; | ||
|
||
abstract class AbstractContainer implements ContainerInterface | ||
{ | ||
/** | ||
* @var ClientInterface | ||
*/ | ||
protected $client; | ||
|
||
public function __construct(ClientInterface $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __call($subcommandID, $arguments) | ||
{ | ||
array_unshift($arguments, strtoupper($subcommandID)); | ||
|
||
return $this->client->executeCommand( | ||
$this->client->createCommand($this->getContainerCommandId(), $arguments) | ||
); | ||
} | ||
|
||
abstract public function getContainerCommandId(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Redis\Container; | ||
|
||
use Predis\ClientInterface; | ||
use UnexpectedValueException; | ||
|
||
class ContainerFactory | ||
{ | ||
private const CONTAINER_NAMESPACE = "Predis\Command\Redis\Container"; | ||
|
||
/** | ||
* Mappings for class names that corresponds to PHP reserved words. | ||
* | ||
* @var array | ||
*/ | ||
private static $specialMappings = [ | ||
'FUNCTION' => FunctionContainer::class, | ||
]; | ||
|
||
/** | ||
* Creates container command. | ||
* | ||
* @param ClientInterface $client | ||
* @param string $containerCommandID | ||
* @return ContainerInterface | ||
*/ | ||
public static function create(ClientInterface $client, string $containerCommandID): ContainerInterface | ||
{ | ||
$containerCommandID = strtoupper($containerCommandID); | ||
|
||
if (class_exists($containerClass = self::CONTAINER_NAMESPACE . '\\' . $containerCommandID)) { | ||
return new $containerClass($client); | ||
} | ||
|
||
if (array_key_exists($containerCommandID, self::$specialMappings)) { | ||
$containerClass = self::$specialMappings[$containerCommandID]; | ||
|
||
return new $containerClass($client); | ||
} | ||
|
||
throw new UnexpectedValueException('Given command is not supported.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Redis\Container; | ||
|
||
interface ContainerInterface | ||
{ | ||
/** | ||
* Creates Redis container command with subcommand as virtual method name | ||
* and sends a request to the server. | ||
* | ||
* @param $subcommandID | ||
* @param $arguments | ||
* @return mixed | ||
*/ | ||
public function __call($subcommandID, $arguments); | ||
|
||
/** | ||
* Returns containerCommandId of specific container command. | ||
* | ||
* @return string | ||
*/ | ||
public function getContainerCommandId(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Redis\Container; | ||
|
||
use Predis\Response\Status; | ||
|
||
/** | ||
* @method string load(string $functionCode, bool $replace = 'false') | ||
* @method Status delete(string $libraryName) | ||
*/ | ||
class FunctionContainer extends AbstractContainer | ||
{ | ||
public function getContainerCommandId(): string | ||
{ | ||
return 'functions'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Redis; | ||
|
||
use Predis\Command\Command as RedisCommand; | ||
use Predis\Command\Traits\Keys; | ||
|
||
/** | ||
* @see https://redis.io/commands/fcall/ | ||
* | ||
* Invoke a function. | ||
*/ | ||
class FCALL extends RedisCommand | ||
{ | ||
use Keys; | ||
|
||
protected static $keysArgumentPositionOffset = 1; | ||
|
||
public function getId() | ||
{ | ||
return 'FCALL'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Redis; | ||
|
||
use Predis\Command\Command as RedisCommand; | ||
use Predis\Command\Strategy\StrategyResolverInterface; | ||
use Predis\Command\Strategy\SubcommandStrategyResolver; | ||
|
||
/** | ||
* @see https://redis.io/commands/?name=function | ||
* | ||
* Container command corresponds to any FUNCTION *. | ||
* Represents any FUNCTION command with subcommand as first argument. | ||
*/ | ||
class FUNCTIONS extends RedisCommand | ||
{ | ||
/** | ||
* @var StrategyResolverInterface | ||
*/ | ||
private $strategyResolver; | ||
|
||
public function __construct() | ||
{ | ||
$this->strategyResolver = new SubcommandStrategyResolver(); | ||
} | ||
|
||
public function getId() | ||
{ | ||
return 'FUNCTION'; | ||
} | ||
|
||
public function setArguments(array $arguments) | ||
{ | ||
$strategy = $this->strategyResolver->resolve('functions', $arguments[0]); | ||
$arguments = $strategy->processArguments($arguments); | ||
|
||
parent::setArguments($arguments); | ||
$this->filterArguments(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Command/Strategy/ContainerCommands/Functions/DeleteStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Predis package. | ||
* | ||
* (c) 2009-2020 Daniele Alessandri | ||
* (c) 2021-2023 Till Krüss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Predis\Command\Strategy\ContainerCommands\Functions; | ||
|
||
use Predis\Command\Strategy\SubcommandStrategyInterface; | ||
|
||
class DeleteStrategy implements SubcommandStrategyInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function processArguments(array $arguments): array | ||
{ | ||
return $arguments; | ||
} | ||
} |
Oops, something went wrong.