Doctrine handler service for Slim3.
Frees you from the tedious work of configuring Doctrine's Entity Manager and Document Manager.
Best way to install is using Composer:
composer require juliangut/slim-doctrine
Then require_once the autoload file:
require_once './vendor/autoload.php';
Register in the DI container as any other service.
use Jgut\Slim\Doctrine\EntityManagerBuilder;
use Slim\App;
// Create Slim app and fetch DI Container
$app = new App();
$container = $app->getContainer();
// Register Entity Manager in the container
$container['entityManager'] = function () {
$doctrineORMSettings = [
'connection' => [
'driver' => 'pdo_sqlite',
'memory' => true,
],
'annotation_paths' => ['path_to_entities_files'],
];
return EntityManagerBuilder::build($doctrineORMSettings);
};
$app->get('/', function () {
$this->entityManager->persist(new \Entity);
$this->entityManager->flush();
});
You can use Slim settings service to store Doctrine configurations.
use Jgut\Slim\Doctrine\DocumentManagerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;
$settings = [
'settings' => [
'doctrine_odm' => [
'connection' => [
'server' => 'mongodb://localhost:27017',
],
'annotation_paths' => ['path_to_documents_files'],
],
],
];
// Create Slim app and fetch DI Container
$app = new App($settings);
$container = $app->getContainer();
// Register Document Manager in the container
$container['documentManager'] = function (ContainerInterface $container) {
return DocumentManagerBuilder::build($container->get('settings')['doctrine_odm']);
};
$app->get('/', function () {
$this->documentManager->persist(new \Document);
$this->documentManager->flush();
});
connection
array of PDO configurations or \Doctrine\DBAL\Connectioncache_driver
\Doctrine\Common\Cache\Cacheannotation_files
array of Doctrine annotations filesannotation_namespaces
array of Doctrine annotations namespacesannotation_autoloaders
array of Doctrine annotations autoloader callablesannotation_paths
array of paths where to find annotated entity filesxml_paths
array of paths where to find XML entity mapping filesyaml_paths
array of paths where to find YAML entity mapping filesphp_paths
array of paths where to find PHP entity mapping filesnaming_strategy
a\Doctrine\ORM\Mapping\NamingStrategy
, defaults toUnderscoreNamingStrategy
quote_strategy
a\Doctrine\ORM\Mapping\QuoteStrategy
, defaults toDefaultQuoteStrategy
proxy_path
path were Doctrine creates its proxy classes, defaults to /tmpproxies_namespace
string for proxies namespace, defaults to 'DoctrineORMProxy'auto_generate_proxies
integer indicating proxy auto generation behaviorsql_logger
a\Doctrine\DBAL\Logging\SQLLogger
event_manager
a configuredDoctrine\Common\EventManager
custom_types
array of'type_name' => '\Doctrine\DBAL\Types\Type'
string_functions
array of custom'function_name' => '\Doctrine\ORM\Query\AST\Functions\FunctionNode'
DQL functionsnumeric_functions
array of custom'function_name' => '\Doctrine\ORM\Query\AST\Functions\FunctionNode'
DQL functionsdatetime_functions
array of custom'function_name' => '\Doctrine\ORM\Query\AST\Functions\FunctionNode'
DQL functions
connection
array of MongoClient configurations or \Doctrine\MongoDB\Connectioncache_driver
\Doctrine\Common\Cache\Cacheannotation_files
array of Doctrine annotations filesannotation_namespaces
array of Doctrine annotations namespacesannotation_autoloaders
array of Doctrine annotations autoloader callablesannotation_paths
array of paths where to find annotated document filesxml_paths
array of paths where to find XML document mapping filesyaml_paths
array of paths where to find YAML document mapping filesdefault_database
default database to be used in case none specifiedproxy_path
path where Doctrine creates its proxy classes, defaults to /tmpproxies_namespace
string for proxies namespace, defaults to 'DoctrineODMProxy'auto_generate_proxies
integer indicating proxy auto generation behaviorhydrator_path
path where Doctrine creates its hydrator classes, defaults to /tmphydrators_namespace
string for hydrators namespace, defaults to 'DoctrineODMHydrator'logger_callable
valid callableevent_manager
a configuredDoctrine\Common\EventManager
These are general considerations when configuring both Entity and Document managers:
-
connection
configuration is mandatory:- For ORM as a PDO configurations array or as a proper Doctrine DBAL Connection.
- For ODM as a MongoClient configurations array or as a proper Doctrine MongoDB Connection.
-
One of 'paths' configurations is mandatory (
annotation_paths
,xml_paths
,yaml_paths
orphp_paths
) as it's needed by Doctrine to configure the Metadata Driver. They are checked in that order and the first to appear is the one that gets configured. Most commonly used is annotation_paths. -
auto_generate_proxies
configuration values areDoctrine\Common\Proxy\AbstractProxyFactory
constants, in both cases it defaults toDoctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_NEVER
(0). -
Managers are being configured ready for production and not for development, this mainly means proxies won't be automatically generated and, in case no
cache_driver
is provided, one will be auto-generated in the following order depending on availability:ApcCache
,XcacheCache
,MemcacheCache
,RedisCache
or finally fall back toArrayCache
which is always available. It is recommended you provide your cache always, for development you can useVoidCache
.
Find here an example of cli-config.php
file that can be used as a template:
require __DIR__ . '/vendor/autoload.php';
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Jgut\Slim\Doctrine\DocumentManagerBuilder;
use Jgut\Slim\Doctrine\EntityManagerBuilder;
$CLISettings = [
'cache_driver' => new VoidCache,
];
$settings = require 'configurations.php';
$entityManager = EntityManagerBuilder::build(array_merge($settings['entity_manager'], $CLISettings));
$documentManager = DocumentManagerBuilder::build(array_merge($settings['document_manager'], $CLISettings));
$helperSet = ConsoleRunner::createHelperSet($entityManager);
$helperSet->set(new DocumentManagerHelper($documentManager), 'dm');
$cli = ConsoleRunner::createApplication($helperSet, [
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand(),
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\UpdateCommand(),
]);
return $cli->run();
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before
See file CONTRIBUTING.md
See file LICENSE included with the source code for a copy of the license terms