zjkiza / tmp-storage-bundle
The bundle for temporary storage data which needs to be preserved between stateless requests.
Package info
github.com/zjkiza/tmp-storage-bundle
Type:symfony-bundle
pkg:composer/zjkiza/tmp-storage-bundle
Requires
- php: >=8.2
- doctrine/dbal: ^3.7|^4.0
- doctrine/orm: ^2.17|^3.0
- ramsey/uuid: ^3.0|^4.0
- symfony/config: ^5.4|^6.0|^7.0|^8.0
- symfony/console: ^5.4|^6.0|^7.0|^8.0
- symfony/dependency-injection: ^5.4|^6.0|^7.0|^8.0
- symfony/http-foundation: ^5.4|^6.0|^7.0|^8.0
- symfony/http-kernel: ^5.4|^6.0|^7.0|^8.0
- symfony/yaml: ^5.4|^6.0|^7.0|^8.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.13|^3.0
- friendsofphp/php-cs-fixer: ^3.52
- icanhazstring/composer-unused: ^0.9.5
- matthiasnoback/symfony-dependency-injection-test: ^6.1
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: ^11.5|^12.4
- psalm/plugin-phpunit: ^0.19
- psalm/plugin-symfony: ^5.2
- ramsey/uuid-doctrine: ^2.0
- rector/rector: ^2.0
- symfony/framework-bundle: ^5.4|^6.0|^7.0|^8.0
- symfony/property-info: ^5.4|^6.0|^7.0|^8.0
- vimeo/psalm: ^6.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The TMP Storage Bundle is used to store temporary state between two stateless requests. Common use cases include:
- SSO Login: During single sign-on, user data can be temporarily stored as an object in TMP Storage. A generated ID links the data, ensuring sensitive user information is not exposed in the URL. The ID is verified upon login, and the stored object is retrieved to complete the process.
- Password Reset: User data can be saved as an object in TMP Storage, and a reset link is generated with a unique ID. When the user clicks the link, they are directed to a page where the ID is verified, and the stored object is retrieved to facilitate password reset.
- Unsubscribe Links: User information is stored temporarily in TMP Storage, and an unsubscribe link is generated with a unique ID. When the user clicks the link, they are directed to the unsubscribe page, where the ID is verified, and the object is retrieved to process the request.
This bundle allows you to securely store objects in a temporary storage database, retrievable using randomly generated keys.
About the Bundle
The bundle defines the interface Zjk\TmpStorage\Contract\TmpStorageInterface with the following methods:
-
public function storage(object $tmp, int $ttl = 604800): string
Stores an object in the database. The$ttlparameter specifies the object's time-to-live in seconds (default: one week). -
public function fetch(string $id, bool $remove = true): object
Retrieves an object from the database by its ID. The$removeparameter determines whether the object is deleted immediately after retrieval. Set$removetofalseto keep the object. -
public function remove(string $id): void
Removes an object from the database if$removewas previously set tofalse. -
public function clearGarbage(): void
Performs maintenance to remove invalid or expired records, ensuring database cleanliness.
Maintenance Command
A terminal command is available for manual or scheduled garbage collection. It invokes the clearGarbage method:
bin/console zjkiza:tmp-storage:maintenance
Installation
Add the bundle to your project using Composer:
composer require zjkiza/tmp-storage-bundle
Requirements and supported versions
The CI pipeline (.github/workflows/ci.yml) runs the test suite against the whole matrix below.
| Component | Supported versions |
|---|---|
| PHP | 8.2, 8.3, 8.4, 8.5 |
| Symfony | 5.4, 6.x, 7.x, 8.x (Symfony 8 itself requires PHP 8.4 or newer) |
| Doctrine DBAL | 3.7+, 4.x (Symfony 5.4 itself is only compatible with DBAL 3) |
| Doctrine ORM | 2.17+, 3.x |
| Database | MySQL 8.0, MariaDB 11, PostgreSQL 16, SQLite (any other DBAL platform should work, it is just not tested) |
Symfony integration
The bundle integrates seamlessly with Symfony, wiring up all necessary classes. Follow these steps to set it up:
- Register the Bundle
<?php declare(strict_types=1); return [ // other bundles Zjk\TmpStorage\Bridge\Symfony\ZJKizaTmpStorageBundle::class => ['all' => true], ];
- Configure the Bundle
By default, the bundle uses a database table named zjkiza_tmp_storage and the doctrine.dbal.default_connection. To customize these settings, create a zjkiza_tmp_storage.yaml configuration file in the config/packages directory with the following parameters:
zjkiza_tmp_storage: dbal: table_name: your_table_name connection: ~
Replace your_table_name with your desired table name and configure the database connection as needed.
The bundle only uses portable SQL, so the table can live in MySQL, MariaDB, PostgreSQL or SQLite. On MySQL/MariaDB the table uses the MyISAM engine, so
the records survive a rollback of the application's transaction. Other databases have no such engine: if the data must outlive a rollback, point connection
to a dedicated connection.
Restricting the classes which can be restored
Objects are stored serialized, and fetch() unserializes them. By default any class may be restored. When somebody other than your application can write to
the table (shared database, SQL access for other teams, ...) restrict the classes, so a manipulated record cannot instantiate arbitrary classes:
zjkiza_tmp_storage: unserialize: allowed_classes: # true (default) = any class, false = none, or a list of classes - App\Dto\RegisterDto
A record that cannot be restored (corrupted, or its class is not allowed) throws Zjk\TmpStorage\Exception\UnserializeException and stays in the table.
- Generating the Required Table
To create the necessary table in the database, run the following command in your terminal:
bin/console doctrine:schema:update
This command instructs Doctrine to generate the table based on the bundle's configuration.
Working with the bundle
use Zjk\TmpStorage\Contract\TmpStorageInterface class MyService { private TmpStorageInterface $storage; public function __construct(TmpStorageInterface $storage) { $this->storage = $storage; } public function register(): string { $dto = new RegisterDto('test@text.com', 'lorem123'); return $this->storage->storage($dto); } public function login(string $id): void { $dto = $this->storage->fetch($id); ...... } }