Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates documentation for version 3 #51

Merged
merged 11 commits into from
Oct 4, 2023
38 changes: 16 additions & 22 deletions docs/book/v3/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ As an example, PHP objects will often be cast to arrays. If this fails, a
## The PhpSerialize Adapter

The `Laminas\Serializer\Adapter\PhpSerialize` adapter uses the built-in
[serialize()](http://php.net/serialize)/[unserialize()](http://php.net/unserialize)
[serialize()](https://php.net/serialize)/[unserialize()](https://php.net/unserialize)
functions, and is a good default adapter choice.

Available options include:

| Option | Data Type | Default Value | Description |
|-----------------------------|-------------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| unserialize_class_whitelist | `array` or `bool` | `true` | The allowed classes for unserialize(), see [unserialize()](http://php.net/unserialize) for more information. Only available on PHP 7.0 or higher. |
| Option | Data Type | Default Value | Description |
|-----------------------------|-------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| unserialize_class_whitelist | `array` or `bool` | `true` | The allowed classes for unserialize(), see [unserialize()](https://php.net/unserialize) for more information. Only available on PHP 7.0 or higher. |

## The IgBinary Adapter

[Igbinary](http://pecl.php.net/package/igbinary) was originally released by
Sulake Dynamoid Oy and since 2011-03-14 moved to [PECL](http://pecl.php.net) and
[Igbinary](https://pecl.php.net/package/igbinary) was originally released by
Sulake Dynamoid Oy and since 2011-03-14 moved to [PECL](https://pecl.php.net) and
maintained by Pierre Joye. It's a drop-in replacement for the standard PHP
serializer. Instead of using a costly textual representation, igbinary stores
PHP data structures in a compact binary form. Savings are significant when using
Expand All @@ -38,7 +38,7 @@ There are no configurable options for this adapter.

## The Json Adapter

The [JSON](http://wikipedia.org/wiki/JavaScript_Object_Notation) adapter provides a bridge to the
The [JSON](https://wikipedia.org/wiki/JavaScript_Object_Notation) adapter provides a bridge to the
[laminas-json](https://docs.laminas.dev/laminas-json) component.

Available options include:
Expand All @@ -52,21 +52,15 @@ Available options include:
## The PhpCode Adapter

The `Laminas\Serializer\Adapter\PhpCode` adapter generates a parsable PHP code
representation using [var_export()](http://php.net/var_export). To restore,
the data will be executed using [eval](http://php.net/eval).
representation using [var_export()](https://php.net/var_export). To restore,
the data will be executed using [eval](https://php.net/eval).

There are no configuration options for this adapter.

> ### Warning: Unserializing objects
>
> Objects will be serialized using the
> [__set_state](http://php.net/language.oop5.magic#language.oop5.magic.set-state) magic
> method. If the class doesn't implement this method, a fatal error will occur
> during execution.

> ### Warning: Uses eval()
>
> The `PhpCode` adapter utilizes `eval()` to unserialize. This introduces both a
> performance and potential security issue as a new process will be executed.
> Typically, you should use the `PhpSerialize` adapter unless you require
> human-readability of the serialized data.
WARNING: **Unserializing Objects**
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
Objects will be serialized using the [__set_state](https://php.net/language.oop5.magic#language.oop5.magic.set-state) magic method.
If the class doesn't implement this method, a fatal error will occur during execution.

WARNING: **Uses `eval()`**
The `PhpCode` adapter utilizes `eval()` to unserialize. This introduces both a performance and potential security issue as a new process will be executed.
Typically, you should use the `PhpSerialize` adapter unless you require human-readability of the serialized data.
94 changes: 94 additions & 0 deletions docs/book/v3/basic-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Basic Usage

Serializing adapters can either be created from the provided
`Laminas\Serializer\AdapterPluginManager#build` method, or by instantiating one of the
`Laminas\Serializer\Adapter\*` classes.

```php
use Laminas\Serializer\Adapter;
use Laminas\Serializer\AdapterPluginManager;
use Laminas\Serializer\Exception;
use Laminas\Serializer\Serializer;

$plugins = new AdapterPluginManager();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH: I would not encourage any1 in using the adapter plugin manager without fetching it from the container.
Main reason here is that some stuff might be registered to the plugin manager from container perspective.

Users are better off instantiating the adapter directly if they do not care about interchanging these.
IMHO, in 9/10 cases, its not even possible to swap serializers. We are using igbinary for almost 8+ years in combination with redis. There is no way of changing this without also changing namespace, etc.
I'd say once a project decided to go with one serializer, that will be for like forever and thus having a direct instantiation will be a bit better I guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only copied the content from the introduction, not changed it further: https://docs.laminas.dev/laminas-serializer/v3/intro/#quick-start


// Via plugin manager:
$serializer = $plugins->build(Adapter\PhpSerialize::class);

// Alternately:
$serializer = new Adapter\PhpSerialize();

// Now $serializer is an instance of Laminas\Serializer\Adapter\AdapterInterface,
// specifically Laminas\Serializer\Adapter\PhpSerialize

try {
$serialized = $serializer->serialize($data);
// now $serialized is a string

$unserialized = $serializer->unserialize($serialized);
// now $data == $unserialized
} catch (Exception\ExceptionInterface $e) {
echo $e;
}
```

The method `AdapterInterface#serialize` generates a storable string. To regenerate this
serialized data, call the method `AdapterInterface#unserialize`.

Any time an error is encountered serializing or unserializing, the adapter will
throw a `Laminas\Serializer\Exception\ExceptionInterface`.

## Configuration Options

To configure a serializer adapter, you can optionally use an instance of
`Laminas\Serializer\Adapter\AdapterOptions`, an instance of one of the adapter
specific options class, an `array`, or a `Traversable` object. The adapter
will convert it into the adapter specific options class instance (if present) or
into the basic `Laminas\Serializer\Adapter\AdapterOptions` class instance.

Options can be passed as the second argument to the provided via the
adapter's `setOptions` method, or as constructor arguments when directly
instantiating an adapter.

## Available Methods

Each serializer implements the interface `Laminas\Serializer\Adapter\AdapterInterface`.

This interface defines the following methods:

| Method signature | Description |
|--------------------------------------|---------------------------------------------------|
| `serialize(mixed $value) : string` | Generates a storable representation of a value. |
| `unserialize(string $value) : mixed` | Creates a PHP value from a stored representation. |

## Project Defaults

To configure a default serializer (other than `PhpSerializer`, which is already pre-configured), you can override the
dependency configuration in your project by implementing the following file `config/autoload/laminas-serializer.global.php`:

```php
use Laminas\Serializer\Adapter\AdapterInterface;
use Laminas\Serializer\Adapter\Json;
use Laminas\Serializer\GenericSerializerFactory;

// Adapter options can hold adapter specific options, please refer to the adapter configuration documentation section
$adapterOptions = ['cycle_check' => true];

return [
// mezzio projects
'dependencies' => [
'factories' => [
AdapterInterface::class => new GenericSerializerFactory(Json::class, $adapterOptions),
],
],
// laminas-mvc projects
'service_manager' => [
'factories' => [
AdapterInterface::class => new GenericSerializerFactory(Json::class, $adapterOptions),
],
],
];
```

INFO: **Defaults for PHP Serializer**
The PHP serializer does not have any defaults configured. If you want to modify the options of the `PhpSerializer` default, you will have to provide the config as shown above but with the `PhpSerializer` class and the options you want to use.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should encourage users to use allowed_classes for the PhpSerializer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but I have only copied the content from the introduction, not changed it further.

5 changes: 5 additions & 0 deletions docs/book/v3/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This Is Only a Placeholder

The content of this page can be found under:

https://github.com/laminas/documentation-theme/blob/master/theme/pages/installation.html
101 changes: 1 addition & 100 deletions docs/book/v3/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,4 @@ laminas-serialzier provides an adapter-based interface for serializing and
deserializing PHP types to and from different representations.

For more information what a serializer is read the wikipedia page of
[Serialization](http://en.wikipedia.org/wiki/Serialization).

## Quick Start

Serializing adapters can either be created from the provided
`Laminas\Serializer\AdapterPluginManager#build` method, or by instantiating one of the
`Laminas\Serializer\Adapter\*` classes.

```php
use Laminas\Serializer\Adapter;
use Laminas\Serializer\AdapterPluginManager;
use Laminas\Serializer\Exception;
use Laminas\Serializer\Serializer;

$plugins = new AdapterPluginManager();

// Via plugin manager:
$serializer = $plugins->build(Adapter\PhpSerialize::class);

// Alternately:
$serializer = new Adapter\PhpSerialize();

// Now $serializer is an instance of Laminas\Serializer\Adapter\AdapterInterface,
// specifically Laminas\Serializer\Adapter\PhpSerialize

try {
$serialized = $serializer->serialize($data);
// now $serialized is a string

$unserialized = $serializer->unserialize($serialized);
// now $data == $unserialized
} catch (Exception\ExceptionInterface $e) {
echo $e;
}
```

The method `AdapterInterface#serialize` generates a storable string. To regenerate this
serialized data, call the method `AdapterInterface#unserialize`.

Any time an error is encountered serializing or unserializing, the adapter will
throw a `Laminas\Serializer\Exception\ExceptionInterface`.

## Basic configuration Options

To configure a serializer adapter, you can optionally use an instance of
`Laminas\Serializer\Adapter\AdapterOptions`, an instance of one of the adapter
specific options class, an `array`, or a `Traversable` object. The adapter
will convert it into the adapter specific options class instance (if present) or
into the basic `Laminas\Serializer\Adapter\AdapterOptions` class instance.

Options can be passed as the second argument to the provided via the
adapter's `setOptions` method, or as constructor arguments when directly
instantiating an adapter.

## Available Methods

Each serializer implements the interface `Laminas\Serializer\Adapter\AdapterInterface`.

This interface defines the following methods:

| Method signature | Description |
|--------------------------------------|---------------------------------------------------|
| `serialize(mixed $value) : string` | Generates a storable representation of a value. |
| `unserialize(string $value) : mixed` | Creates a PHP value from a stored representation. |

## Project defaults

To configure a default serializer (other than `PhpSerializer`, which is already pre-configured), you can override the
dependency configuration in your project by implementing the following file `config/autoload/laminas-serializer.global.php`:

```php
<?php

use Laminas\Serializer\Adapter\AdapterInterface;
use Laminas\Serializer\Adapter\Json;
use Laminas\Serializer\GenericSerializerFactory;

// Adapter options can hold adapter specific options, please refer to the adapter configuration documentation section
$adapterOptions = ['cycle_check' => true];

return [
// mezzio projects
'dependencies' => [
'factories' => [
AdapterInterface::class => new GenericSerializerFactory(Json::class, $adapterOptions),
],
],
// laminas-mvc projects
'service_manager' => [
'factories' => [
AdapterInterface::class => new GenericSerializerFactory(Json::class, $adapterOptions),
],
],
];
```

> ### Defaults for PHP Serializer
>
> The PHP serializer does not have any defaults configured. If you want to modify the options of the `PhpSerializer` default, you
> will have to provide the config as shown above but with the `PhpSerializer` class and the options you want to use.
[Serialization](https://wikipedia.org/wiki/Serialization).
4 changes: 2 additions & 2 deletions docs/book/v3/plugin-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The `AdapterPluginManager` extends the laminas-servicemanager `AbstractPluginManager`, and has the following behaviors:

- It will only return `Laminas\Serializer\Adapter\AdapterInterface` instances.
- **Since 2.10.0**: It defines short-name aliases for all shipped serializers (the class name minus the namespace), in a variety of casing combinations.
- It defines short-name aliases for all shipped serializers (the class name minus the namespace), in a variety of casing combinations.
- All services are shared by default; a new instance will only be created once and shared each time you call `get()`.

## Factory
Expand All @@ -13,7 +13,7 @@ The `AdapterPluginManager` extends the laminas-servicemanager `AbstractPluginMan

The factory will be automatically registered when loading/installing the `Laminas\Serializer` module in `laminas-mvc` and/or loading/installing the `ConfigProvider` into a Mezzio application.

Since version 2.10.0, the factory will look for the `config` service, and use the `serializers` configuration key to seed it with additional services.
The factory will look for the `config` service, and use the `serializers` configuration key to seed it with additional services.
This configuration key should map to an array that follows [standard laminas-servicemanager configuration](https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/).

To add your own serializer you can add the following configuration:
Expand Down
15 changes: 15 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ nav:
- Home: index.md
- v3:
- Introduction: v3/intro.md
- Installation: v3/installation.md
- "Basic Usage": v3/basic-usage.md
- Adapters: v3/adapter.md
- "Plugin Manager": v3/plugin-manager.md
- "Migration Guide":
Expand All @@ -17,3 +19,16 @@ site_description: "Serialize and deserialize PHP structures to a variety of repr
repo_url: 'https://github.com/laminas/laminas-serializer'
extra:
project: Components
current_version: v3
versions:
- v3
- v2
installation:
config_provider_class: 'Laminas\Serializer\ConfigProvider'
module_class: 'Laminas\Serializer\Module'
plugins:
- redirects:
redirect_maps:
intro.md: v3/intro.md
adapter.md: v3/adapter.md
plugin-manager.md: v3/plugin-manager.md