{"id":13616598,"url":"https://github.com/Roave/psr-container-doctrine","last_synced_at":"2025-04-14T03:31:08.949Z","repository":{"id":40499374,"uuid":"238715361","full_name":"Roave/psr-container-doctrine","owner":"Roave","description":"Doctrine Factories for PSR-11 Containers","archived":false,"fork":false,"pushed_at":"2025-04-11T05:45:04.000Z","size":1257,"stargazers_count":96,"open_issues_count":11,"forks_count":35,"subscribers_count":13,"default_branch":"5.8.x","last_synced_at":"2025-04-11T06:31:10.134Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Roave.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-06T15:05:41.000Z","updated_at":"2025-04-11T05:45:07.000Z","dependencies_parsed_at":"2024-01-05T05:31:48.826Z","dependency_job_id":"a8ea1081-1080-4c13-9672-4a3507af0757","html_url":"https://github.com/Roave/psr-container-doctrine","commit_stats":{"total_commits":297,"total_committers":33,"mean_commits":9.0,"dds":0.8383838383838383,"last_synced_commit":"f17355e1e8896b68825d487dadfff61acfc046d0"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roave%2Fpsr-container-doctrine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roave%2Fpsr-container-doctrine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roave%2Fpsr-container-doctrine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roave%2Fpsr-container-doctrine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Roave","download_url":"https://codeload.github.com/Roave/psr-container-doctrine/tar.gz/refs/heads/5.8.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248815505,"owners_count":21165936,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T20:01:30.692Z","updated_at":"2025-04-14T03:31:08.937Z","avatar_url":"https://github.com/Roave.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# psr-container-doctrine: Doctrine Factories for PSR-11 Containers\n\n[![Latest Stable Version](https://poser.pugx.org/roave/psr-container-doctrine/v/stable)](https://packagist.org/packages/roave/psr-container-doctrine)\n[![Total Downloads](https://poser.pugx.org/roave/psr-container-doctrine/downloads)](https://packagist.org/packages/roave/psr-container-doctrine)\n[![Build Status](https://github.com/roave/psr-container-doctrine/workflows/main/badge.svg)](https://github.com/roave/psr-container-doctrine/actions)\n\n[Doctrine](https://github.com/doctrine) factories for [PSR-11 containers](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md).\n\nThis package provides a set of factories to be used with containers using the PSR-11 standard for an easy\nDoctrine integration in a project. This project was originally written by\n[@DASPRiD](https://github.com/DASPRiD/container-interop-doctrine) but maintenance has been taken over by Roave.\n\n## Installation\n\nThe easiest way to install this package is through composer:\n\n```bash\n$ composer require roave/psr-container-doctrine\n```\n\n## Configuration\n\nIn the general case where you are only using a single connection, it's enough to define the entity manager factory:\n\n```php\nreturn [\n    'dependencies' =\u003e [\n        'factories' =\u003e [\n            'doctrine.entity_manager.orm_default' =\u003e \\Roave\\PsrContainerDoctrine\\EntityManagerFactory::class,\n        ],\n    ],\n];\n```\n\nIf you want to add a second connection, or use another name than \"orm_default\", you can do so by using the static\nvariants of the factories:\n\n```php\nreturn [\n    'dependencies' =\u003e [\n        'factories' =\u003e [\n            'doctrine.entity_manager.orm_other' =\u003e static fn (ContainerInterface $container) =\u003e (new EntityManagerFactory('orm_custom_key'))($container),\n        ],\n    ],\n];\n```\n\nYou can also define an alias to retrieve an entity manager instance using `::class` capability:\n```php\nreturn [\n    'aliases' =\u003e [\n        'doctrine.entity_manager.orm_default' =\u003e Doctrine\\ORM\\EntityManagerInterface::class,\n    ],\n];\n```\n\nEach factory supplied by this package will by default look for a registered factory in the container. If it cannot find\none, it will automatically pull its dependencies from on-the-fly created factories. This saves you the hassle of\nregistering factories in your container which you may not need at all. Of course, you can always register those\nfactories when required. The following additional factories are available:\n\n- ```\\Roave\\PsrContainerDoctrine\\CacheFactory``` (doctrine.cache.*)\n- ```\\Roave\\PsrContainerDoctrine\\ConnectionFactory``` (doctrine.connection.*)\n- ```\\Roave\\PsrContainerDoctrine\\ConfigurationFactory``` (doctrine.configuration.*)\n- ```\\Roave\\PsrContainerDoctrine\\DriverFactory``` (doctrine.driver.*)\n- ```\\Roave\\PsrContainerDoctrine\\EventManagerFactory``` (doctrine.event_manager.*)\n\nEach of those factories supports the same static behavior as the entity manager factory. For container specific\nconfigurations, there are a few examples provided in the example directory:\n\n- [Aura.Di](example/aura-di.php)\n- [PimpleInterop](example/pimple-interop.php)\n- [Laminas\\ServiceManager](example/laminas-servicemanager.php)\n\n## Example configuration\n\nA complete example configuration can be found in [example/full-config.php](example/full-config.php). Please note that\nthe values in there are the defaults, and don't have to be supplied when you are not changing them. Keep your own\nconfiguration as minimal as possible. A minimal configuration can be found in\n[example/minimal-config.php](example/minimal-config.php)\n\n## Migrations\n\nIf you want to expose the migration commands, you have to map the command name to `CommandFactory`. This factory needs migrations config setup.\nFor `ExecuteCommand` example:\n\n```php\nreturn [\n    'dependencies' =\u003e [\n        'factories' =\u003e [\n            \\Doctrine\\Migrations\\Tools\\Console\\Command\\ExecuteCommand::class =\u003e \\Roave\\PsrContainerDoctrine\\Migrations\\CommandFactory::class,\n\n            // Optionally, you could make your container aware of additional factories as of migrations release v3.0:\n            \\Doctrine\\Migrations\\Configuration\\Migration\\ConfigurationLoader::class =\u003e \\Roave\\PsrContainerDoctrine\\Migrations\\ConfigurationLoaderFactory::class,\n            \\Doctrine\\Migrations\\DependencyFactory::class =\u003e \\Roave\\PsrContainerDoctrine\\Migrations\\DependencyFactoryFactory::class,\n        ],\n    ],\n];\n```\n\nYou can find a full list of available commands in [example/full-config.php](example/full-config.php).\n\n## Using the Doctrine CLI\n\nIn order to be able to use the CLI tool of Doctrine, you need to create a ```bin/doctrine``` file in your project\ndirectory. This sets up the command line application and enables you to add custom commands. It's nearly identical to \nthe file described in [Setting Up the Console](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/tools.html#setting-up-the-console)\nbut pulls `EntityManagerInterface` from your container:\n\n```php\n#!/usr/bin/env php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Doctrine\\ORM\\EntityManagerInterface;\nuse Doctrine\\ORM\\Tools\\Console\\ConsoleRunner;\nuse Doctrine\\ORM\\Tools\\Console\\EntityManagerProvider\\SingleManagerProvider;\nuse Psr\\Container\\ContainerInterface;\n\nrequire 'vendor/autoload.php';\n\n/** @var ContainerInterface $container */\n$container = require 'config/container.php';\n\n/** @var EntityManagerInterface $entityManager */\n$entityManager = $container-\u003eget('doctrine.entity_manager.orm_default');\n\n$commands = [\n    // If you want to add your own custom console commands,\n    // you can do so here.\n];\n\nConsoleRunner::run(\n    new SingleManagerProvider($entityManager),\n    $commands\n);\n\n```\n\nAfter that, invoke ```php bin/doctrine list``` to see the available commands. \n\n### Multiple connections\n\nIt gets a little trickier when you have multiple entity managers. Doctrine itself has no way to handle that itself, so\na possible way would be to have two copies of the command above, named after the manager they work with and each pulling \ndifferent entity managers from the container - for instance ```bin/doctrine-default``` and ```bin/doctrine-customer```.\n\nThe following code can be used for multiple connections, but it has a drawback: you won't see the `--em=...` option \nwithin the help section of each command.\n\n```php\n#!/usr/bin/env php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Doctrine\\ORM\\EntityManagerInterface;\nuse Doctrine\\ORM\\Tools\\Console\\ConsoleRunner;\nuse Doctrine\\ORM\\Tools\\Console\\EntityManagerProvider\\SingleManagerProvider;\nuse InvalidArgumentException;\nuse Psr\\Container\\ContainerInterface;\nuse Psr\\Container\\NotFoundExceptionInterface;\nuse Symfony\\Component\\Console\\Input\\ArgvInput;\n\nrequire 'vendor/autoload.php';\n\n/** @var ContainerInterface $container */\n$container = require 'config/container.php';\n\n$input = new ArgvInput();\n\n/** @var string $em */\n$em = $input-\u003egetParameterOption('--em', 'orm_default');\n\n// hack to remove the --em option, cause it's not supported by the original ConsoleRunner.\nforeach ($_SERVER['argv'] as $i =\u003e $arg) {\n    if (0 === strpos($arg, '--em=')) {\n        unset($_SERVER['argv'][$i]);\n    }\n}\n\ntry {\n    $entityManager = $this-\u003econtainer-\u003eget('doctrine.entity_manager.'.$em);\n} catch (NotFoundExceptionInterface $serviceNotFoundException) {\n    throw new InvalidArgumentException(sprintf('Missing entity manager with name \"%s\"', $em));\n}\n\n$commands = [\n    // If you want to add your own custom console commands,\n    // you can do so here.\n];\n\nConsoleRunner::run(\n    new SingleManagerProvider($entityManager),\n    $commands\n);\n\n```\n\n### Multiple connections and migrations\n\nThere may be a requirement for a migration table in each connection. Whether its a different database entirely,\nor other another schema (PostgreSQL for example). \n\nFor this to work you must first ensure the service `\\Doctrine\\Migrations\\Configuration\\Migration\\ConfigurationLoader`\nis **not** set in the container.\nThis allows for the factory `\\Roave\\PsrContainerDoctrine\\Migrations\\DependencyFactoryFactory` to create its\nown `ConfigurationLoader` by the key provided.\n\nYou first need to add additional config to the migrations in the doctrine config\n```php\nreturn [\n    'doctrine' =\u003e [\n        ...\n\n        'migrations' =\u003e [\n            'orm_default' =\u003e [\n                'table_storage' =\u003e [\n                    'table_name' =\u003e 'migrations_executed',\n                    'version_column_name' =\u003e 'version',\n                    'version_column_length' =\u003e 255,\n                    'executed_at_column_name' =\u003e 'executed_at',\n                    'execution_time_column_name' =\u003e 'execution_time',\n                ],\n                'migrations_paths' =\u003e ['My\\Migrations' =\u003e 'scripts/orm/migrations'],\n                'all_or_nothing' =\u003e true,\n                'check_database_platform' =\u003e true,\n            ],\n            'orm_default2' =\u003e [\n                'table_storage' =\u003e [\n                    'table_name' =\u003e 'migrations_executed',\n                    'version_column_name' =\u003e 'version',\n                    'version_column_length' =\u003e 255,\n                    'executed_at_column_name' =\u003e 'executed_at',\n                    'execution_time_column_name' =\u003e 'execution_time',\n                ],\n                'migrations_paths' =\u003e ['My\\Migrations2' =\u003e 'scripts/orm/migrations2'],\n                'all_or_nothing' =\u003e true,\n                'check_database_platform' =\u003e true,\n            ],\n        ],\n\n        ...\n    ],\n];\n```\n\nNow the example `bin/doctrine` script above can be extended to use the `$em` variable along with the `CommandFactory`.\n```php\n$commandFactory = new \\Roave\\PsrContainerDoctrine\\Migration\\CommandFactory($em);\n\n$commands = [\n    $commandFactory($container, Command\\GenerateCommand::class),\n];\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRoave%2Fpsr-container-doctrine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRoave%2Fpsr-container-doctrine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRoave%2Fpsr-container-doctrine/lists"}