PHPackages                             naucon/storage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. naucon/storage

ActiveLibrary[Caching](/categories/caching)

naucon/storage
==============

Storage Abstraction Layer .

3.0.0(1y ago)04.2k—5.6%1MITPHPPHP ^8.1

Since May 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/naucon/storage)[ Packagist](https://packagist.org/packages/naucon/storage)[ Docs](https://github.com/naucon/storage)[ RSS](/packages/naucon-storage/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (4)Used By (0)

naucon Storage Package
======================

[](#naucon-storage-package)

About
-----

[](#about)

This package is an abstraction layer for key/value Stores (storages). The goal is to decouple a concrete key/value store from the business logic.

### Features

[](#features)

- storage manager as abstraction layer for storages or stores with basic CRUD operations.
- storage factory and registry to manage multiple storages or stores.
- chained storages to save entities in multiple storages or stores.
- support composite identifiers / keys.

### Adapters

[](#adapters)

- array (in-memory)
- file system
- native session
- bridge to symfony session handler
- redis (predis) - requires redis 2.8 or higher
- doctrine ORM
- PSR-6 Cache
- PSR-16 Simple Cache
- null storage (stores nothing, no-memory)

### Compatibility

[](#compatibility)

- PHP 7.1 to 7.4

Installation
------------

[](#installation)

install the latest version via composer

```
    composer require naucon/storage
```

Usage
-----

[](#usage)

### Getting started

[](#getting-started)

To use the storage we need a model (plain-old php objects). This model will be serialized so it is a good idea that the model implements the `Serializable` interface - even if it is not mandatory.

```
    class Product implements \Serializable
    {
        protected $id;
        protected $sku;
        protected $description;

        public function getId()
        {
            return $this->id;
        }

        public function setId($id)
        {
            $this->id = $id;
        }

        public function getSku()
        {
            return $this->sku;
        }

        public function setSku($sku)
        {
            $this->sku = $sku;
        }

        public function getDescription()
        {
            return $this->description;
        }

        public function setDescription($description)
        {
            $this->description = $description;
        }
    }
```

With a defined model we are able to create a `StorageManager`. The `StorageManager` provides all the basic CRUD operations `create()`, `has()`, `find()`, `flush()`, `remove()`. To work it requires a storage provider. The provider implements a concrete storage or store. In this example we use the `ArrayStorage` but you can choose any other supported provider or build a custom storage to your needs.

```
    use Naucon\Storage\Provider\ArrayStorage;
    use Naucon\Storage\StorageManager;

    $adapter = new ArrayStorage(Product::class);
    $manager = new StorageManager($adapter);
```

To save an entry in the storage we call `flush($identifier, $model)`

```
    $model = new Product();
    $model->setId(1);
    $model->setSku('U123');
    $model->setDescription('Dragon fruit');

    $manager->flush(1, $model);
```

To create a new instance of the model you can call `StorageManager::create()`. Requires a storage that implements `CreateAwareInterface`. The created instance will not be in the storage until you `flush()` it.

```
    $model = $manager->create();
    $model->setId(1);
    $model->setSku('U123');
    $model->setDescription('Dragon fruit');

    $manager->flush(1, $model);
```

To retrieve an entry from the storage we call `StorageManager::find($identifier)`. If no entry was found you get `null` in return. To verify if a storage contains an entry you can call `StorageManager::has($identifier)`.

```
    if ($manager->has(1)) {
        $model = $manager->find(1);
    }
```

To always get a model instance you can call `StorageManager::findOrCreate($identifier)`. That will create a new model instance if no entry was found. Requires a storage that implements `CreateAwareInterface`. The created instance will not be in the storage until you `flush()` it.

```
    $model = $manager->findOrCreate(1);
```

To remove an entry from storage you can call `StorageManager::remove($identifier, $model)`.

```
    $manager->remove(1, $product);
```

Remove all entries from the storage with `StorageManager::removeAll()`.

```
    $models = $manager->removeAll();
```

With `StorageManager::findAll()` you can retrieve all entries from the storage.

```
    $models = $manager->findAll();
```

With `StorageManager::findMultiple(array $identifiers)` you can retrieve multiple entries from the storage.

```
    $models = $manager->findMultiple([1, 2]);
```

### Doctrine ORM

[](#doctrine-orm)

The identifier must be handled by the storage component. Therefor doctrine ORM must use "NONE" as a Identifier Generation Strategies

XML

```

```

YAML

```
    Product:
      type: entity
      id:
        id:
          type: integer
          generator:
            strategy: NONE
```

Annotation

```
