commit vendor

This commit is contained in:
2025-11-11 14:49:30 +01:00
parent f33121a308
commit 6d03080c00
2436 changed files with 483781 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
abstract class AbstractAdapter implements AdapterInterface
{
/**
* @var AdapterOptions
*/
protected $options = null;
/**
* Constructor
*
* @param array|\Traversable|AdapterOptions $options
*/
public function __construct($options = null)
{
if ($options !== null) {
$this->setOptions($options);
}
}
/**
* Set adapter options
*
* @param array|\Traversable|AdapterOptions $options
* @return AbstractAdapter
*/
public function setOptions($options)
{
if (! $options instanceof AdapterOptions) {
$options = new AdapterOptions($options);
}
$this->options = $options;
return $this;
}
/**
* Get adapter options
*
* @return AdapterOptions
*/
public function getOptions()
{
if ($this->options === null) {
$this->options = new AdapterOptions();
}
return $this->options;
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
interface AdapterInterface
{
/**
* Generates a storable representation of a value.
*
* @param mixed $value Data to serialize
* @return string
* @throws \Laminas\Serializer\Exception\ExceptionInterface
*/
public function serialize($value);
/**
* Creates a PHP value from a stored representation.
*
* @param string $serialized Serialized string
* @return mixed
* @throws \Laminas\Serializer\Exception\ExceptionInterface
*/
public function unserialize($serialized);
}

View File

@ -0,0 +1,15 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Stdlib\AbstractOptions;
class AdapterOptions extends AbstractOptions
{
}

View File

@ -0,0 +1,84 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Stdlib\ErrorHandler;
class IgBinary extends AbstractAdapter
{
/**
* @var string Serialized null value
*/
private static $serializedNull = null;
/**
* Constructor
*
* @throws Exception\ExtensionNotLoadedException If igbinary extension is not present
*/
public function __construct($options = null)
{
if (! extension_loaded('igbinary')) {
throw new Exception\ExtensionNotLoadedException(
'PHP extension "igbinary" is required for this adapter'
);
}
if (static::$serializedNull === null) {
static::$serializedNull = igbinary_serialize(null);
}
parent::__construct($options);
}
/**
* Serialize PHP value to igbinary
*
* @param mixed $value
* @return string
* @throws Exception\RuntimeException on igbinary error
*/
public function serialize($value)
{
ErrorHandler::start();
$ret = igbinary_serialize($value);
$err = ErrorHandler::stop();
if ($ret === false) {
throw new Exception\RuntimeException('Serialization failed', 0, $err);
}
return $ret;
}
/**
* Deserialize igbinary string to PHP value
*
* @param string $serialized
* @return mixed
* @throws Exception\RuntimeException on igbinary error
*/
public function unserialize($serialized)
{
if ($serialized === static::$serializedNull) {
return;
}
ErrorHandler::start();
$ret = igbinary_unserialize($serialized);
$err = ErrorHandler::stop();
if ($ret === null) {
throw new Exception\RuntimeException('Unserialization failed', 0, $err);
}
return $ret;
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Json\Json as LaminasJson;
use Laminas\Serializer\Exception;
class Json extends AbstractAdapter
{
/**
* @var JsonOptions
*/
protected $options = null;
/**
* Set options
*
* @param array|\Traversable|JsonOptions $options
* @return Json
*/
public function setOptions($options)
{
if (! $options instanceof JsonOptions) {
$options = new JsonOptions($options);
}
$this->options = $options;
return $this;
}
/**
* Get options
*
* @return JsonOptions
*/
public function getOptions()
{
if ($this->options === null) {
$this->options = new JsonOptions();
}
return $this->options;
}
/**
* Serialize PHP value to JSON
*
* @param mixed $value
* @return string
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function serialize($value)
{
$options = $this->getOptions();
$cycleCheck = $options->getCycleCheck();
$opts = [
'enableJsonExprFinder' => $options->getEnableJsonExprFinder(),
'objectDecodeType' => $options->getObjectDecodeType(),
];
try {
return LaminasJson::encode($value, $cycleCheck, $opts);
} catch (\InvalidArgumentException $e) {
throw new Exception\InvalidArgumentException('Serialization failed: ' . $e->getMessage(), 0, $e);
} catch (\Exception $e) {
throw new Exception\RuntimeException('Serialization failed: ' . $e->getMessage(), 0, $e);
}
}
/**
* Deserialize JSON to PHP value
*
* @param string $json
* @return mixed
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function unserialize($json)
{
try {
$ret = LaminasJson::decode($json, $this->getOptions()->getObjectDecodeType());
} catch (\InvalidArgumentException $e) {
throw new Exception\InvalidArgumentException('Unserialization failed: ' . $e->getMessage(), 0, $e);
} catch (\Exception $e) {
throw new Exception\RuntimeException('Unserialization failed: ' . $e->getMessage(), 0, $e);
}
return $ret;
}
}

View File

@ -0,0 +1,86 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Json\Json as LaminasJson;
use Laminas\Serializer\Exception;
class JsonOptions extends AdapterOptions
{
/** @var bool */
protected $cycleCheck = false;
/** @var bool */
protected $enableJsonExprFinder = false;
/** @var int */
protected $objectDecodeType = LaminasJson::TYPE_ARRAY;
/**
* @param bool $flag
* @return JsonOptions
*/
public function setCycleCheck($flag)
{
$this->cycleCheck = (bool) $flag;
return $this;
}
/**
* @return bool
*/
public function getCycleCheck()
{
return $this->cycleCheck;
}
/**
* @param bool $flag
* @return JsonOptions
*/
public function setEnableJsonExprFinder($flag)
{
$this->enableJsonExprFinder = (bool) $flag;
return $this;
}
/**
* @return bool
*/
public function getEnableJsonExprFinder()
{
return $this->enableJsonExprFinder;
}
/**
* @param int $type
* @return JsonOptions
* @throws Exception\InvalidArgumentException
*/
public function setObjectDecodeType($type)
{
if ($type != LaminasJson::TYPE_ARRAY && $type != LaminasJson::TYPE_OBJECT) {
throw new Exception\InvalidArgumentException(
'Unknown decode type: ' . $type
);
}
$this->objectDecodeType = (int) $type;
return $this;
}
/**
* @return int
*/
public function getObjectDecodeType()
{
return $this->objectDecodeType;
}
}

View File

@ -0,0 +1,84 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Stdlib\ErrorHandler;
class MsgPack extends AbstractAdapter
{
/**
* @var string Serialized 0 value
*/
private static $serialized0 = null;
/**
* Constructor
*
* @throws Exception\ExtensionNotLoadedException If msgpack extension is not present
*/
public function __construct($options = null)
{
if (! extension_loaded('msgpack')) {
throw new Exception\ExtensionNotLoadedException(
'PHP extension "msgpack" is required for this adapter'
);
}
if (static::$serialized0 === null) {
static::$serialized0 = msgpack_serialize(0);
}
parent::__construct($options);
}
/**
* Serialize PHP value to msgpack
*
* @param mixed $value
* @return string
* @throws Exception\RuntimeException on msgpack error
*/
public function serialize($value)
{
ErrorHandler::start();
$ret = msgpack_serialize($value);
$err = ErrorHandler::stop();
if ($ret === false) {
throw new Exception\RuntimeException('Serialization failed', 0, $err);
}
return $ret;
}
/**
* Deserialize msgpack string to PHP value
*
* @param string $serialized
* @return mixed
* @throws Exception\RuntimeException on msgpack error
*/
public function unserialize($serialized)
{
if ($serialized === static::$serialized0) {
return 0;
}
ErrorHandler::start();
$ret = msgpack_unserialize($serialized);
$err = ErrorHandler::stop();
if ($ret === 0) {
throw new Exception\RuntimeException('Unserialization failed', 0, $err);
}
return $ret;
}
}

View File

@ -0,0 +1,59 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Stdlib\ErrorHandler;
class PhpCode extends AbstractAdapter
{
/**
* Serialize PHP using var_export
*
* @param mixed $value
* @return string
*/
public function serialize($value)
{
return var_export($value, true);
}
/**
* Deserialize PHP string
*
* Warning: this uses eval(), and should likely be avoided.
*
* @param string $code
* @return mixed
* @throws Exception\RuntimeException on eval error
*/
public function unserialize($code)
{
ErrorHandler::start(E_ALL);
$ret = null;
// This suppression is due to the fact that the ErrorHandler cannot
// catch syntax errors, and is intentionally left in place.
$eval = @eval('$ret=' . $code . ';');
$err = ErrorHandler::stop();
if ($eval === false || $err) {
$msg = 'eval failed';
// Error handler doesn't catch syntax errors
if ($eval === false) {
$lastErr = error_get_last();
$msg .= ': ' . $lastErr['message'];
}
throw new Exception\RuntimeException($msg, 0, $err);
}
return $ret;
}
}

View File

@ -0,0 +1,138 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Stdlib\ErrorHandler;
use Traversable;
class PhpSerialize extends AbstractAdapter
{
/**
* Serialized boolean false value
*
* @var null|string
*/
private static $serializedFalse = null;
/**
* @var PhpSerializeOptions
*/
protected $options;
/**
* Constructor
*
* @param array|Traversable|PhpSerializeOptions|null $options
*/
public function __construct($options = null)
{
// needed to check if a returned false is based on a serialize false
// or based on failure (igbinary can overwrite [un]serialize functions)
if (static::$serializedFalse === null) {
static::$serializedFalse = serialize(false);
}
parent::__construct($options);
}
/**
* Set options
*
* @param array|Traversable|PhpSerializeOptions $options
* @return PhpSerialize
*/
public function setOptions($options)
{
if (! $options instanceof PhpSerializeOptions) {
$options = new PhpSerializeOptions($options);
}
$this->options = $options;
return $this;
}
/**
* Get options
*
* @return PhpSerializeOptions
*/
public function getOptions()
{
if ($this->options === null) {
$this->options = new PhpSerializeOptions();
}
return $this->options;
}
/**
* Serialize using serialize()
*
* @param mixed $value
* @return string
* @throws Exception\RuntimeException On serialize error
*/
public function serialize($value)
{
ErrorHandler::start();
$ret = serialize($value);
$err = ErrorHandler::stop();
if ($err) {
throw new Exception\RuntimeException('Serialization failed', 0, $err);
}
return $ret;
}
/**
* Unserialize
*
* @todo Allow integration with unserialize_callback_func
* @param string $serialized
* @return mixed
* @throws Exception\RuntimeException on unserialize error
*/
public function unserialize($serialized)
{
if (! is_string($serialized) || ! preg_match('/^((s|i|d|b|a|O|C):|N;)/', $serialized)) {
$value = $serialized;
if (is_object($value)) {
$value = get_class($value);
} elseif (! is_string($value)) {
$value = gettype($value);
}
throw new Exception\RuntimeException(sprintf(
'Serialized data must be a string containing serialized PHP code; received: %s',
$value
));
}
// If we have a serialized boolean false value, just return false;
// prevents the unserialize handler from creating an error.
if ($serialized === static::$serializedFalse) {
return false;
}
ErrorHandler::start(E_NOTICE);
// The second parameter to unserialize() is only available on PHP 7.0 or higher
$ret = PHP_MAJOR_VERSION >= 7
? unserialize($serialized, ['allowed_classes' => $this->getOptions()->getUnserializeClassWhitelist()])
: unserialize($serialized);
$err = ErrorHandler::stop();
if ($ret === false) {
throw new Exception\RuntimeException('Unserialization failed', 0, $err);
}
return $ret;
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Json\Json as LaminasJson;
use Laminas\Serializer\Exception;
class PhpSerializeOptions extends AdapterOptions
{
/**
* The list of allowed classes for unserialization (PHP 7.0+).
*
* Possible values:
*
* - `array` of class names that are allowed to be unserialized
* - `true` if all classes should be allowed (behavior pre-PHP 7.0)
* - `false` if no classes should be allowed
*
* @var string[]|bool
*/
protected $unserializeClassWhitelist = true;
/**
* @param string[]|bool $unserializeClassWhitelist
* @return void
*/
public function setUnserializeClassWhitelist($unserializeClassWhitelist)
{
if ($unserializeClassWhitelist !== true && PHP_MAJOR_VERSION < 7) {
throw new Exception\InvalidArgumentException(
'Class whitelist for unserialize() is only available on PHP versions 7.0 or higher.'
);
}
$this->unserializeClassWhitelist = $unserializeClassWhitelist;
}
/**
* @return string[]|bool
*/
public function getUnserializeClassWhitelist()
{
return $this->unserializeClassWhitelist;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,54 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
class PythonPickleOptions extends AdapterOptions
{
/**
* Pickle protocol version to serialize data
*
* @var int
*/
protected $protocol = 0;
/**
* Set pickle protocol version to serialize data
*
* Supported versions are 0, 1, 2 and 3
*
* @param int $protocol
* @return PythonPickleOptions
* @throws Exception\InvalidArgumentException
*/
public function setProtocol($protocol)
{
$protocol = (int) $protocol;
if ($protocol < 0 || $protocol > 3) {
throw new Exception\InvalidArgumentException(
"Invalid or unknown protocol version '{$protocol}'"
);
}
$this->protocol = $protocol;
return $this;
}
/**
* Get pickle protocol version to serialize data
*
* @return int
*/
public function getProtocol()
{
return $this->protocol;
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
use Laminas\Serializer\Exception;
use Laminas\Stdlib\ErrorHandler;
/**
* @link http://www.infoloom.com/gcaconfs/WEB/chicago98/simeonov.HTM
* @link http://en.wikipedia.org/wiki/WDDX
*/
class Wddx extends AbstractAdapter
{
/**
* @var WddxOptions
*/
protected $options = null;
/**
* Constructor
*
* @param array|\Traversable|WddxOptions $options
* @throws Exception\ExtensionNotLoadedException if wddx extension not found
*/
public function __construct($options = null)
{
if (! extension_loaded('wddx')) {
throw new Exception\ExtensionNotLoadedException(
'PHP extension "wddx" is required for this adapter'
);
}
parent::__construct($options);
}
/**
* Set options
*
* @param array|\Traversable|WddxOptions $options
* @return Wddx
*/
public function setOptions($options)
{
if (! $options instanceof WddxOptions) {
$options = new WddxOptions($options);
}
$this->options = $options;
return $this;
}
/**
* Get options
*
* @return WddxOptions
*/
public function getOptions()
{
if ($this->options === null) {
$this->options = new WddxOptions();
}
return $this->options;
}
/**
* Serialize PHP to WDDX
*
* @param mixed $value
* @return string
* @throws Exception\RuntimeException on wddx error
*/
public function serialize($value)
{
$comment = $this->getOptions()->getComment();
ErrorHandler::start();
if ($comment !== '') {
$wddx = wddx_serialize_value($value, $comment);
} else {
$wddx = wddx_serialize_value($value);
}
$error = ErrorHandler::stop();
if ($wddx === false) {
throw new Exception\RuntimeException('Serialization failed', 0, $error);
}
return $wddx;
}
/**
* Unserialize from WDDX to PHP
*
* @param string $wddx
* @return mixed
* @throws Exception\RuntimeException on wddx error
* @throws Exception\InvalidArgumentException if invalid xml
*/
public function unserialize($wddx)
{
$ret = wddx_deserialize($wddx);
if ($ret === null && class_exists('SimpleXMLElement', false)) {
// check if the returned NULL is valid
// or based on an invalid wddx string
try {
$oldLibxmlDisableEntityLoader = libxml_disable_entity_loader(true);
$dom = new \DOMDocument;
$dom->loadXML($wddx);
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new Exception\InvalidArgumentException(
'Invalid XML: Detected use of illegal DOCTYPE'
);
}
}
$simpleXml = simplexml_import_dom($dom);
//$simpleXml = new \SimpleXMLElement($wddx);
libxml_disable_entity_loader($oldLibxmlDisableEntityLoader);
if (isset($simpleXml->data[0]->null[0])) {
return; // valid null
}
throw new Exception\RuntimeException('Unserialization failed: Invalid wddx packet');
} catch (\Exception $e) {
throw new Exception\RuntimeException('Unserialization failed: ' . $e->getMessage(), 0, $e);
}
}
return $ret;
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Adapter;
class WddxOptions extends AdapterOptions
{
/**
* Wddx packet header comment
*
* @var string
*/
protected $comment = '';
/**
* Set WDDX header comment
*
* @param string $comment
* @return WddxOptions
*/
public function setComment($comment)
{
$this->comment = (string) $comment;
return $this;
}
/**
* Get WDDX header comment
*
* @return string
*/
public function getComment()
{
return $this->comment;
}
}

View File

@ -0,0 +1,122 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer;
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
/**
* Plugin manager implementation for serializer adapters.
*
* Enforces that adapters retrieved are instances of
* Adapter\AdapterInterface. Additionally, it registers a number of default
* adapters available.
*/
class AdapterPluginManager extends AbstractPluginManager
{
protected $aliases = [
'igbinary' => Adapter\IgBinary::class,
'igBinary' => Adapter\IgBinary::class,
'IgBinary' => Adapter\IgBinary::class,
'json' => Adapter\Json::class,
'Json' => Adapter\Json::class,
'msgpack' => Adapter\MsgPack::class,
'msgPack' => Adapter\MsgPack::class,
'MsgPack' => Adapter\MsgPack::class,
'phpcode' => Adapter\PhpCode::class,
'phpCode' => Adapter\PhpCode::class,
'PhpCode' => Adapter\PhpCode::class,
'phpserialize' => Adapter\PhpSerialize::class,
'phpSerialize' => Adapter\PhpSerialize::class,
'PhpSerialize' => Adapter\PhpSerialize::class,
'pythonpickle' => Adapter\PythonPickle::class,
'pythonPickle' => Adapter\PythonPickle::class,
'PythonPickle' => Adapter\PythonPickle::class,
'wddx' => Adapter\Wddx::class,
'Wddx' => Adapter\Wddx::class,
// Legacy Zend Framework aliases
\Zend\Serializer\Adapter\IgBinary::class => Adapter\IgBinary::class,
\Zend\Serializer\Adapter\Json::class => Adapter\Json::class,
\Zend\Serializer\Adapter\MsgPack::class => Adapter\MsgPack::class,
\Zend\Serializer\Adapter\PhpCode::class => Adapter\PhpCode::class,
\Zend\Serializer\Adapter\PhpSerialize::class => Adapter\PhpSerialize::class,
\Zend\Serializer\Adapter\PythonPickle::class => Adapter\PythonPickle::class,
\Zend\Serializer\Adapter\Wddx::class => Adapter\Wddx::class,
// v2 normalized FQCNs
'zendserializeradapterigbinary' => Adapter\IgBinary::class,
'zendserializeradapterjson' => Adapter\Json::class,
'zendserializeradaptermsgpack' => Adapter\MsgPack::class,
'zendserializeradapterphpcode' => Adapter\PhpCode::class,
'zendserializeradapterphpserialize' => Adapter\PhpSerialize::class,
'zendserializeradapterpythonpickle' => Adapter\PythonPickle::class,
'zendserializeradapterwddx' => Adapter\Wddx::class,
];
protected $factories = [
Adapter\IgBinary::class => InvokableFactory::class,
Adapter\Json::class => InvokableFactory::class,
Adapter\MsgPack::class => InvokableFactory::class,
Adapter\PhpCode::class => InvokableFactory::class,
Adapter\PhpSerialize::class => InvokableFactory::class,
Adapter\PythonPickle::class => InvokableFactory::class,
Adapter\Wddx::class => InvokableFactory::class,
// Legacy (v2) due to alias resolution; canonical form of resolved
// alias is used to look up the factory, while the non-normalized
// resolved alias is used as the requested name passed to the factory.
'laminasserializeradapterigbinary' => InvokableFactory::class,
'laminasserializeradapterjson' => InvokableFactory::class,
'laminasserializeradaptermsgpack' => InvokableFactory::class,
'laminasserializeradapterphpcode' => InvokableFactory::class,
'laminasserializeradapterphpserialize' => InvokableFactory::class,
'laminasserializeradapterpythonpickle' => InvokableFactory::class,
'laminasserializeradapterwddx' => InvokableFactory::class,
];
protected $instanceOf = Adapter\AdapterInterface::class;
/**
* Validate the plugin is of the expected type (v3).
*
* Validates against `$instanceOf`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validate($instance)
{
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'%s can only create instances of %s; %s is invalid',
get_class($this),
$this->instanceOf,
(is_object($instance) ? get_class($instance) : gettype($instance))
));
}
}
/**
* Validate the plugin is of the expected type (v2).
*
* Proxies to `validate()`.
*
* @param mixed $instance
* @throws Exception\RuntimeException
*/
public function validatePlugin($instance)
{
try {
$this->validate($instance);
} catch (InvalidServiceException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}

View File

@ -0,0 +1,54 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
class AdapterPluginManagerFactory implements FactoryInterface
{
/**
* laminas-servicemanager v2 support for invocation options.
*
* @param array
*/
protected $creationOptions;
/**
* {@inheritDoc}
*
* @return AdapterPluginManager
*/
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
return new AdapterPluginManager($container, $options ?: []);
}
/**
* {@inheritDoc}
*
* @return AdapterPluginManager
*/
public function createService(ServiceLocatorInterface $container, $name = null, $requestedName = null)
{
return $this($container, $requestedName ?: AdapterPluginManager::class, $this->creationOptions);
}
/**
* laminas-servicemanager v2 support for invocation options.
*
* @param array $options
* @return void
*/
public function setCreationOptions(array $options)
{
$this->creationOptions = $options;
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer;
class ConfigProvider
{
/**
* Return configuration for this component.
*
* @return array
*/
public function __invoke()
{
return [
'dependencies' => $this->getDependencyConfig(),
];
}
/**
* Return dependency mappings for this component.
*
* @return array
*/
public function getDependencyConfig()
{
return [
// Legacy Zend Framework aliases
'aliases' => [
],
'factories' => [
'SerializerAdapterManager' => AdapterPluginManagerFactory::class,
],
];
}
}

View File

@ -0,0 +1,13 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Exception;
interface ExceptionInterface
{
}

View File

@ -0,0 +1,13 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Exception;
class ExtensionNotLoadedException extends RuntimeException
{
}

View File

@ -0,0 +1,17 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Exception;
/**
* @used InvalidArgumentException
*/
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{
}

View File

@ -0,0 +1,16 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer\Exception;
/**
* @used RuntimeException
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}

View File

@ -0,0 +1,44 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer;
class Module
{
/**
* Return default laminas-serializer configuration for laminas-mvc applications.
*/
public function getConfig()
{
$provider = new ConfigProvider();
return [
'service_manager' => $provider->getDependencyConfig(),
];
}
/**
* Register a specification for the SerializerAdapterManager with the ServiceListener.
*
* @param \Laminas\ModuleManager\ModuleManager $moduleManager
* @return void
*/
public function init($moduleManager)
{
$event = $moduleManager->getEvent();
$container = $event->getParam('ServiceManager');
$serviceListener = $container->get('ServiceListener');
$serviceListener->addServiceManager(
'SerializerAdapterManager',
'serializers',
'Laminas\ModuleManager\Feature\SerializerProviderInterface',
'getSerializerConfig'
);
}
}

View File

@ -0,0 +1,145 @@
<?php
/**
* @see https://github.com/laminas/laminas-serializer for the canonical source repository
* @copyright https://github.com/laminas/laminas-serializer/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-serializer/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Serializer;
use Laminas\Serializer\Adapter\AdapterInterface as Adapter;
use Laminas\ServiceManager\ServiceManager;
abstract class Serializer
{
/**
* Plugin manager for loading adapters
*
* @var null|AdapterPluginManager
*/
protected static $adapters;
/**
* The default adapter.
*
* @var string|Adapter
*/
protected static $defaultAdapter = 'PhpSerialize';
/**
* Create a serializer adapter instance.
*
* @param string|Adapter $adapterName Name of the adapter class
* @param array |\Traversable|null $adapterOptions Serializer options
* @return Adapter
*/
public static function factory($adapterName, $adapterOptions = null)
{
if ($adapterName instanceof Adapter) {
return $adapterName; // $adapterName is already an adapter object
}
return static::getAdapterPluginManager()->get($adapterName, $adapterOptions);
}
/**
* Change the adapter plugin manager
*
* @param AdapterPluginManager $adapters
* @return void
*/
public static function setAdapterPluginManager(AdapterPluginManager $adapters)
{
static::$adapters = $adapters;
}
/**
* Get the adapter plugin manager
*
* @return AdapterPluginManager
*/
public static function getAdapterPluginManager()
{
if (static::$adapters === null) {
static::$adapters = new AdapterPluginManager(new ServiceManager);
}
return static::$adapters;
}
/**
* Resets the internal adapter plugin manager
*
* @return AdapterPluginManager
*/
public static function resetAdapterPluginManager()
{
static::$adapters = new AdapterPluginManager(new ServiceManager);
return static::$adapters;
}
/**
* Change the default adapter.
*
* @param string|Adapter $adapter
* @param array|\Traversable|null $adapterOptions
*/
public static function setDefaultAdapter($adapter, $adapterOptions = null)
{
static::$defaultAdapter = static::factory($adapter, $adapterOptions);
}
/**
* Get the default adapter.
*
* @return Adapter
*/
public static function getDefaultAdapter()
{
if (! static::$defaultAdapter instanceof Adapter) {
static::setDefaultAdapter(static::$defaultAdapter);
}
return static::$defaultAdapter;
}
/**
* Generates a storable representation of a value using the default adapter.
* Optionally different adapter could be provided as second argument
*
* @param mixed $value
* @param string|Adapter $adapter
* @param array|\Traversable|null $adapterOptions Adapter constructor options
* only used to create adapter instance
* @return string
*/
public static function serialize($value, $adapter = null, $adapterOptions = null)
{
if ($adapter !== null) {
$adapter = static::factory($adapter, $adapterOptions);
} else {
$adapter = static::getDefaultAdapter();
}
return $adapter->serialize($value);
}
/**
* Creates a PHP value from a stored representation using the default adapter.
* Optionally different adapter could be provided as second argument
*
* @param string $serialized
* @param string|Adapter $adapter
* @param array|\Traversable|null $adapterOptions Adapter constructor options
* only used to create adapter instance
* @return mixed
*/
public static function unserialize($serialized, $adapter = null, $adapterOptions = null)
{
if ($adapter !== null) {
$adapter = static::factory($adapter, $adapterOptions);
} else {
$adapter = static::getDefaultAdapter();
}
return $adapter->unserialize($serialized);
}
}