diff options
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Connection/Strategy')
5 files changed, 167 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/CallbackStrategy.php b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/CallbackStrategy.php new file mode 100644 index 00000000..ccaeb6dd --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/CallbackStrategy.php @@ -0,0 +1,51 @@ +<?php +namespace Elastica\Connection\Strategy; + +use Elastica\Exception\InvalidException; + +/** + * Description of CallbackStrategy. + * + * @author chabior + */ +class CallbackStrategy implements StrategyInterface +{ + /** + * @var callable + */ + protected $_callback; + + /** + * @param callable $callback + * + * @throws \Elastica\Exception\InvalidException + */ + public function __construct($callback) + { + if (!self::isValid($callback)) { + throw new InvalidException(sprintf('Callback should be a callable, %s given!', gettype($callback))); + } + + $this->_callback = $callback; + } + + /** + * @param array|\Elastica\Connection[] $connections + * + * @return \Elastica\Connection + */ + public function getConnection($connections) + { + return call_user_func_array($this->_callback, array($connections)); + } + + /** + * @param callable $callback + * + * @return bool + */ + public static function isValid($callback) + { + return is_callable($callback); + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/RoundRobin.php b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/RoundRobin.php new file mode 100644 index 00000000..92cd570e --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/RoundRobin.php @@ -0,0 +1,24 @@ +<?php +namespace Elastica\Connection\Strategy; + +/** + * Description of RoundRobin. + * + * @author chabior + */ +class RoundRobin extends Simple +{ + /** + * @param array|\Elastica\Connection[] $connections + * + * @throws \Elastica\Exception\ClientException + * + * @return \Elastica\Connection + */ + public function getConnection($connections) + { + shuffle($connections); + + return parent::getConnection($connections); + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/Simple.php b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/Simple.php new file mode 100644 index 00000000..7c42dd3b --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/Simple.php @@ -0,0 +1,30 @@ +<?php +namespace Elastica\Connection\Strategy; + +use Elastica\Exception\ClientException; + +/** + * Description of SimpleStrategy. + * + * @author chabior + */ +class Simple implements StrategyInterface +{ + /** + * @param array|\Elastica\Connection[] $connections + * + * @throws \Elastica\Exception\ClientException + * + * @return \Elastica\Connection + */ + public function getConnection($connections) + { + foreach ($connections as $connection) { + if ($connection->isEnabled()) { + return $connection; + } + } + + throw new ClientException('No enabled connection'); + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/StrategyFactory.php b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/StrategyFactory.php new file mode 100644 index 00000000..7590ab11 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/StrategyFactory.php @@ -0,0 +1,45 @@ +<?php +namespace Elastica\Connection\Strategy; + +use Elastica\Exception\InvalidException; + +/** + * Description of StrategyFactory. + * + * @author chabior + */ +class StrategyFactory +{ + /** + * @param mixed|callable|string|StrategyInterface $strategyName + * + * @throws \Elastica\Exception\InvalidException + * + * @return \Elastica\Connection\Strategy\StrategyInterface + */ + public static function create($strategyName) + { + if ($strategyName instanceof StrategyInterface) { + return $strategyName; + } + + if (CallbackStrategy::isValid($strategyName)) { + return new CallbackStrategy($strategyName); + } + + if (is_string($strategyName)) { + $requiredInterface = '\\Elastica\\Connection\\Strategy\\StrategyInterface'; + $predefinedStrategy = '\\Elastica\\Connection\\Strategy\\'.$strategyName; + + if (class_exists($predefinedStrategy) && class_implements($predefinedStrategy, $requiredInterface)) { + return new $predefinedStrategy(); + } + + if (class_exists($strategyName) && class_implements($strategyName, $requiredInterface)) { + return new $strategyName(); + } + } + + throw new InvalidException('Can\'t create strategy instance by given argument'); + } +} diff --git a/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/StrategyInterface.php b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/StrategyInterface.php new file mode 100644 index 00000000..29bf7701 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Connection/Strategy/StrategyInterface.php @@ -0,0 +1,17 @@ +<?php +namespace Elastica\Connection\Strategy; + +/** + * Description of AbstractStrategy. + * + * @author chabior + */ +interface StrategyInterface +{ + /** + * @param array|\Elastica\Connection[] $connections + * + * @return \Elastica\Connection + */ + public function getConnection($connections); +} |