juliangut/slim-doctrine

Slim-Doctrine managers integration

2.3 2017-10-08 11:27 UTC

This package is auto-updated.

Last update: 2024-12-03 01:29:07 UTC


README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Slim integration with Doctrine managers

Easy Slim framework integration with Doctrine's Entity Manager, MongoDB Document Manager and CouchDB Document Manager.

Important note

The latest version of slim-doctrine is focused only on Slim framework and Doctrine integration, and thus using the manager builders as stand alone is not possible.

That same functionality can be achieved by using juliangut/doctrine-manager-builder which is not tied to Slim framework.

Installation

Best way to install is using Composer:

composer require juliangut/slim-doctrine

Then require_once the autoload file:

require_once './vendor/autoload.php';

Configuration

Each kind of manager has its configurations stored on a key in the settings array

  • ManagerBuilder::DEFAULT_RELATIONAL_MANAGER_KEY ("entity_manager") for ORM
  • ManagerBuilder::DEFAULT_MONGODB_MANAGER_KEY ("mongodb_document_manager") for MongoDB ODM
  • ManagerBuilder::DEFAULT_COUCHDB_MANAGER_KEY ("couchdb_document_manager") for CouchDB ODM
[
    ManagerBuilder::DEFAULT_RELATIONAL_MANAGER_KEY => [
        'manager1_name' => <relational_manager_builder_configuration>,
        'manager2_name' => <relational_manager_builder_configuration>,
        ...
    ],
    ManagerBuilder::DEFAULT_MONGODB_MANAGER_KEY => [
        'manager3_name' => <mongodb_manager_builder_configuration>,
        'manager4_name' => <mongodb_manager_builder_configuration>,
        ...
    ],
    ManagerBuilder::DEFAULT_COUCHDB_MANAGER_KEY => [
        'manager5_name' => <couchdb_manager_builder_configuration>,
        'manager6_name' => <couchdb_manager_builder_configuration>,
        ...
    ],
]

If a manager is not given a name then a default one will be used:

  • ManagerBuilder::DEFAULT_RELATIONAL_MANAGER_NAME ("entityManager) for ORM
  • ManagerBuilder::DEFAULT_MONGODB_MANAGER_NAME ("mongoDocumentManager") for MongoDB ODM
  • ManagerBuilder::DEFAULT_COUCHDB_MANAGER_NAME ("couchDocumentManager") for CouchDB ODM

Options

ManagerBuilder's default keys and manager names can be modified providing constructor with an options array to change any of them

$options = [
    ManagerBuilder::RELATIONAL_MANAGER_KEY => 'entity_manager',
    ManagerBuilder::MONGODB_MANAGER_KEY => 'mongodb_document_manager',
    ManagerBuilder::COUCHDB_MANAGER_KEY => 'couchdb_document_manager',
    ManagerBuilder::RELATIONAL_MANAGER_NAME => 'entityManager',
    ManagerBuilder::MONGODB_MANAGER_NAME => 'mongoDocumentManager',
    ManagerBuilder::COUCHDB_MANAGER_NAME => 'couchDocumentManager',
];
$managerBuilder = new ManagerBuilder($options);

Manager builders

In order to configure the different Doctrine manager builders head to juliangut/doctrine-manager-builder which is used in this package.

Usage

Register managers in the DI container as any other service.

use Jgut\Slim\Doctrine\ManagerBuilder;
use Slim\App;

// Loaded from a file
$settings = [
    'my_custom_key' => [
        'annotation_autoloaders' => ['class_exists'],
        'connection' => [
            'driver' => 'pdo_sqlite',
            'memory' => true,
        ],
        'metadata_mapping' => [
            [
                'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION,
                'path' => 'path/to/annotation/mappings',
            ],
        ],
    ],
];

$managerBuilder = new ManagerBuilder([ManagerBuilder::RELATIONAL_MANAGER_KEY => 'my_custom_key']);
$managerBuilder->loadSettings($settings);

// Create Slim app and fetch DI Container
$app = new App();
$container = $app->getContainer();

// Register every manager in the container
foreach ($managerBuilder->getManagers() as $name => $manager) {
    $container[$name] = $manager;
}

// Use managers
$app->get('/', function () {
    $this->entityManager->persist(new \Entity);
    $this->entityManager->flush();
});

Register manager builder in the DI container to delegate managers creation.

use Jgut\Slim\Doctrine\ManagerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;

// Probably loaded from a file...
$settings = [
    'settings.doctrineManagers' => [
        ManagerBuilder::DEFAULT_RELATIONAL_MANAGER_KEY => [
            'mainDocumentManager' => [
                'connection' => [
                    'server' => 'mongodb://localhost:27017',
                ],
                'metadata_mapping' => [
                    [
                        'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION,
                        'path' => 'path/to/annotation/mappings',
                    ],
                ],
            ],
            'secondaryDocumentManager' => [
                'annotation_autoloaders' => ['class_exists'],
                'connection' => [
                    'server' => 'mongodb://localhost:27017',
                ],
                'metadata_mapping' => [
                    [
                        'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION,
                        'path' => 'path/to/annotation/mappings',
                    ],
                ],
            ],
        ],
    ],
];

// Create Slim app and fetch DI Container
$app = new App($settings);
$container = $app->getContainer();

// Register manager builder fetching settings from container
$container['manager_builder'] => function (ContainerInterface $container) {
    return (new ManagerBuilder())->loadSettings($container->get('settings.doctrineManagers'));
};

// Register managers by pulling them from the builder
$container['mainDocumentManager'] => function (ContainerInterface $container) {
    return $container->get('manager_builder')->getManager('mainDocumentManager');
};
$container['secondaryDocumentManager'] => function (ContainerInterface $container) {
    return $container->get('manager_builder')->getManager('secondaryDocumentManager');
};

// Use managers
$app->get('/', function () {
    $this->mainDocumentManager->persist(new \Document);
    $this->mainDocumentManager->flush();
});

CLI Application builder

doctrine-manager is a CLI tool that is installed with this package. It provides the same functionality that Doctrine's ORM doctrine CLI tool does but it does not need ORM to be installed. Additionally doctrine-manager allows you to have numerous managers configured thanks to prepending manager name.

The way to using doctrine-manager is the same as with doctrine by creating a cli-config.php file returning a Symfony\Component\Console\Application

require __DIR__ . '/vendor/autoload.php';

use Jgut\Slim\Doctrine\ManagerBuilder;

$settings = require 'configurations.php';

$managerBuilder = (new ManagerBuilder())->loadSettings($settings);

return $managerBuilder->getCLIApplication();

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before

See file CONTRIBUTING.md

Contributors

License

See file LICENSE included with the source code for a copy of the license terms