luccpl/sonata

dev-main 2024-07-21 01:29 UTC

This package is auto-updated.

Last update: 2024-11-21 02:13:50 UTC


README

Sonata is the base package for the persistence layer in Orkestra, providing essential functionalities for authentication, session management, and optionally, database interactions through Doctrine ORM.

Installation

To install Sonata, use Composer:

composer require luccpl/sonata

Configuration

Session Management

Sonata provides a session interface that currently supports PHP's default session handling. To use it, register the Sonata\Sessions\SessionsProvider in your providers list.

Authentication Configuration

To configure authentication guards, add the sonata.auth_guards configuration. This configuration maps guard keys to their respective drivers and repositories:

$app->config()->set('sonata.auth_guards', [
    'guard_key' => [
        'driver' => 'session',  // the driver to use for authentication
        'repository' => YourRepository::class,  // An implementation of Sonata\Repositories\Interfaces\Partials\IdentifiableRepositoryInterface
    ],
]);

Set the default authentication guard using the sonata.default_guard configuration:

['sonata.default_guard', 'guard_key'];

Custom Session Interface

Optionally, you can change the session interface by setting the sonata.session configuration:

['sonata.session', YourCustomSessionInterface::class];

Added Middleware

With the session provider registered, you gain access to the auth middleware, which can be used to protect routes with the existing guards:

return function (RouterInterface $router): void {
    $router->get('/protected', function ($request, $response) {
        return ['message' => 'This is a protected route'];
    })->middleware('auth');
};

optionally you can specify a guard key to use a specific guard:

return function (RouterInterface $router): void {
    $router->get('/protected', function ($request, $response) {
        return ['message' => 'This is a protected route'];
    })->middleware('auth', ['guard' => 'web']);
};

Optional: Doctrine ORM Integration

If you wish to use Doctrine ORM for database interactions, install the necessary Doctrine packages:

composer require doctrine/orm doctrine/dbal doctrine/migrations symfony/cache

Then add the Sonata\Doctrine\DoctrineProvider to the Orkestra providers list.

Default Configuration

By default, Doctrine is configured to use SQLite. You can configure it to use other databases supported by Doctrine by setting the doctrine.connection configuration in Orkestra following the Doctrine configuration format.

[
	'doctrine.connection' => [
		'dbname' => 'mydb',
		'user' => 'user',
		'password' => 'secret',
		'host' => 'localhost',
		'driver' => 'pdo_mysql',
	],
];

Entities and Migrations

You can specify the directories for your entities and migration classes using the doctrine.entities and doctrine.migrations configurations:

[
	'doctrine.entities' => [
		'App\Entities' => '/path/to/entities'
	],
	'doctrine.migrations' => [
		'/path/to/migrations'
	],
];

By default, Sonata will use the App\Entities namespace for entities and the App\Migrations namespace for migrations in ./migrations directory.