added predis and eseye back in.

This commit is contained in:
2020-12-25 11:28:41 +00:00
parent 0ddd298350
commit 017f72b42e
670 changed files with 60992 additions and 10 deletions

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Encryption\Compression;
interface CompressionMethod
{
/**
* Returns the name of the method.
*/
public function name(): string;
/**
* Compress the data.
* Throws an exception in case of failure.
*
* @param string $data The data to compress
*/
public function compress(string $data): string;
/**
* Uncompress the data.
* Throws an exception in case of failure.
*
* @param string $data The data to uncompress
*/
public function uncompress(string $data): string;
}

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Encryption\Compression;
use function array_key_exists;
use InvalidArgumentException;
class CompressionMethodManager
{
/**
* @var CompressionMethod[]
*/
private $compressionMethods = [];
public function __construct(array $methods = [])
{
foreach ($methods as $method) {
$this->add($method);
}
}
/**
* Returns true if the givn compression method is supported.
*/
public function has(string $name): bool
{
return array_key_exists($name, $this->compressionMethods);
}
/**
* This method returns the compression method with the given name.
* Throws an exception if the method is not supported.
*
* @param string $name The name of the compression method
*
* @throws InvalidArgumentException if the compression method is not supported
*/
public function get(string $name): CompressionMethod
{
if (!$this->has($name)) {
throw new InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name));
}
return $this->compressionMethods[$name];
}
/**
* Returns the list of compression method names supported by the manager.
*
* @return string[]
*/
public function list(): array
{
return array_keys($this->compressionMethods);
}
/**
* Add the given compression method to the manager.
*/
protected function add(CompressionMethod $compressionMethod): void
{
$name = $compressionMethod->name();
$this->compressionMethods[$name] = $compressionMethod;
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Encryption\Compression;
use InvalidArgumentException;
class CompressionMethodManagerFactory
{
/**
* @var CompressionMethod[]
*/
private $compressionMethods = [];
/**
* This method adds a compression method to this factory.
* The method is uniquely identified by an alias. This allows the same method to be added twice (or more)
* using several configuration options.
*/
public function add(string $alias, CompressionMethod $compressionMethod): void
{
$this->compressionMethods[$alias] = $compressionMethod;
}
/**
* Returns the list of compression method aliases supported by the factory.
*
* @return string[]
*/
public function aliases(): array
{
return array_keys($this->compressionMethods);
}
/**
* Returns all compression methods supported by this factory.
*
* @return CompressionMethod[]
*/
public function all(): array
{
return $this->compressionMethods;
}
/**
* Creates a compression method manager using the compression methods identified by the given aliases.
* If one of the aliases does not exist, an exception is thrown.
*
* @param string[] $aliases
*
* @throws InvalidArgumentException if the compression method alias is not supported
*/
public function create(array $aliases): CompressionMethodManager
{
$compressionMethods = [];
foreach ($aliases as $alias) {
if (!isset($this->compressionMethods[$alias])) {
throw new InvalidArgumentException(sprintf('The compression method with the alias "%s" is not supported.', $alias));
}
$compressionMethods[] = $this->compressionMethods[$alias];
}
return new CompressionMethodManager($compressionMethods);
}
}

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Component\Encryption\Compression;
use InvalidArgumentException;
use Throwable;
final class Deflate implements CompressionMethod
{
/**
* @var int
*/
private $compressionLevel = -1;
/**
* Deflate constructor.
*
* @throws InvalidArgumentException if the compression level is invalid
*/
public function __construct(int $compressionLevel = -1)
{
if ($compressionLevel < -1 || $compressionLevel > 9) {
throw new InvalidArgumentException('The compression level can be given as 0 for no compression up to 9 for maximum compression. If -1 given, the default compression level will be the default compression level of the zlib library.');
}
$this->compressionLevel = $compressionLevel;
}
public function name(): string
{
return 'DEF';
}
/**
* @throws InvalidArgumentException if the compression failed
*/
public function compress(string $data): string
{
try {
return gzdeflate($data, $this->getCompressionLevel());
} catch (Throwable $throwable) {
throw new InvalidArgumentException('Unable to compress data.', $throwable->getCode(), $throwable);
}
}
/**
* @throws InvalidArgumentException if the decompression failed
*/
public function uncompress(string $data): string
{
try {
return gzinflate($data);
} catch (Throwable $throwable) {
throw new InvalidArgumentException('Unable to uncompress data.', $throwable->getCode(), $throwable);
}
}
private function getCompressionLevel(): int
{
return $this->compressionLevel;
}
}