{"id":17001645,"url":"https://github.com/juliangut/slim-php-di","last_synced_at":"2025-03-17T09:30:53.527Z","repository":{"id":36423468,"uuid":"40728455","full_name":"juliangut/slim-php-di","owner":"juliangut","description":"Slim Framework PHP-DI container integration","archived":false,"fork":false,"pushed_at":"2025-01-13T12:46:44.000Z","size":313,"stargazers_count":15,"open_issues_count":1,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-27T08:00:36.631Z","etag":null,"topics":["container-interop","php-di","slim"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juliangut.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"github":"juliangut"}},"created_at":"2015-08-14T17:48:30.000Z","updated_at":"2024-12-24T11:20:35.000Z","dependencies_parsed_at":"2022-09-03T10:50:54.037Z","dependency_job_id":"66c72b75-e433-4db6-a475-630e1e3767ea","html_url":"https://github.com/juliangut/slim-php-di","commit_stats":{"total_commits":198,"total_committers":2,"mean_commits":99.0,"dds":0.005050505050505083,"last_synced_commit":"0d0836bc615c3bc41b865a8cf41ad2292fd0b7c1"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangut%2Fslim-php-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangut%2Fslim-php-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangut%2Fslim-php-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliangut%2Fslim-php-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliangut","download_url":"https://codeload.github.com/juliangut/slim-php-di/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243858929,"owners_count":20359260,"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":["container-interop","php-di","slim"],"created_at":"2024-10-14T04:25:39.250Z","updated_at":"2025-03-17T09:30:53.145Z","avatar_url":"https://github.com/juliangut.png","language":"PHP","funding_links":["https://github.com/sponsors/juliangut"],"categories":[],"sub_categories":[],"readme":"[![PHP version](https://img.shields.io/badge/PHP-%3E%3D8.0-8892BF.svg?style=flat-square)](http://php.net)\n[![Latest Version](https://img.shields.io/packagist/vpre/juliangut/slim-php-di.svg?style=flat-square)](https://packagist.org/packages/juliangut/slim-php-di)\n[![License](https://img.shields.io/github/license/juliangut/slim-php-di.svg?style=flat-square)](https://github.com/juliangut/slim-php-di/blob/master/LICENSE)\n\n[![Total Downloads](https://img.shields.io/packagist/dt/juliangut/slim-php-di.svg?style=flat-square)](https://packagist.org/packages/juliangut/slim-php-di/stats)\n[![Monthly Downloads](https://img.shields.io/packagist/dm/juliangut/slim-php-di.svg?style=flat-square)](https://packagist.org/packages/juliangut/slim-php-di/stats)\n\n# Slim Framework PHP-DI container integration\n\nPHP-DI dependency injection container integration for Slim framework.\n\n## Installation\n\nBest way to install is using [Composer](https://getcomposer.org/):\n\n```\ncomposer require juliangut/slim-php-di\n```\n\nThen require_once the autoload file:\n\n```php\nrequire_once './vendor/autoload.php';\n```\n\n## Usage\n\nUse `Jgut\\Slim\\PHPDI\\ContainerBuilder` to create PHP-DI container and extract Slim's App from it\n\n```php\nuse Jgut\\Slim\\PHPDI\\Configuration;\nuse Jgut\\Slim\\PHPDI\\ContainerBuilder;\nuse Psr\\Container\\ContainerInterface;\nuse Slim\\App;\n\n$container = ContainerBuilder::build(new Configuration());\n\n$app = $container-\u003eget(App::class);\n// same as $app = \\Slim\\Factory\\AppFactory::createFromContainer($container);\n\n// Register your services if not provided as definitions\n$container-\u003eset('service_one', function (ContainerInterface $container): ServiceOne {\n    return new ServiceOne($container-\u003eget('service_two'));\n});\n\n// Set your routes\n\n$app-\u003erun();\n```\n\n_In order to register services in the container it's way better to do it in definition files_\n\n### Configuration\n\n```php\nuse Jgut\\Slim\\PHPDI\\Configuration;\nuse Jgut\\Slim\\PHPDI\\ContainerBuilder;\n\n$settings = [\n    'useAttributes' =\u003e true,\n    'compilationPath' =\u003e '/path/to/compiled/container',\n];\n$configuration = new Configuration($settings);\n\n// Settings can be set after creation\n$configuration-\u003esetProxiesPath(sys_get_temp_dir());\n$configuration-\u003esetDefinitions('/path/to/definition/files');\n\n$container = ContainerBuilder::build($configuration);\n```\n\n#### PHP-DI settings\n\n* `useAutoWiring` whether to use auto wiring (true by default)\n* `useAttributes` whether to use attributes (false by default)\n* `useDefinitionCache`, whether to use definition cache (false by default)\n* `wrapContainer` wrapping container (none by default)\n* `proxiesPath` path where PHP-DI creates its proxy files (none by default)\n* `compilationPath` path where PHP-DI creates its compiled container (none by default)\n\nRefer to [PHP-DI documentation](http://php-di.org/doc/) to learn more about container configurations\n\n#### Additional settings\n\n* `definitions` an array of paths to definition files/directories or arrays of definitions. _Definitions are loaded in order of appearance_\n* `containerClass` container class used on the build. Must implement `\\Psr\\Container\\ContainerInterface`, `\\DI\\FactoryInterface` and `\\DI\\InvokerInterface` (`\\Jgut\\Slim\\PHPDI\\Container` by default)\n\n## Container array access shorthand\n\nDefault `\\Jgut\\Slim\\PHPDI\\Container` container allows shorthand array access by concatenating array keys with dots. If any key in the chain is not defined, normal `Psr\\Container\\NotFoundExceptionInterface` exception is thrown\n\n```php\nuse Jgut\\Slim\\PHPDI\\Configuration;\nuse Jgut\\Slim\\PHPDI\\ContainerBuilder;\n\n$container = ContainerBuilder::build(new Configuration([]));\n\n$configs = [\n    'database' =\u003e [\n        'dsn' =\u003e 'mysql://root:pass@localhost/my_ddbb',\n    ],\n];\n$container-\u003eset('configs', $configs);\n\n$container-\u003eget('configs')['database']['dsn'];\n$container-\u003eget('configs.database.dsn'); // same as above\n```\n\n#### Notice\n\nBe careful though not to shadow any array key by using dots in keys themselves\n\n```php\nuse Jgut\\Slim\\PHPDI\\Configuration;\nuse Jgut\\Slim\\PHPDI\\ContainerBuilder;\n\n$container = ContainerBuilder::build(new Configuration([]));\n\n$configs = [\n    'foo' =\u003e [\n        'bar' =\u003e [\n            'baz' =\u003e 'shadowed!', // \u003c== watch out!\n        ],\n    ],\n    'foo.bar' =\u003e 'bingo!',\n];\n$container-\u003eset('configs', $configs);\n\n$container-\u003eget('configs.foo.bar'); // bingo!\n$container-\u003eget('configs.foo.bar.baz'); // NotFoundExceptionInterface thrown\n```\n\n_The easiest way to avoid this from ever happening is by NOT using dots in array keys_\n\n## Invocation strategy\n\nBy default, slim-php-di sets a custom invocation strategy that employs PHP-DI's Invoker to fulfill callable parameters, it lets you do things like this\n\n```php\nuse Jgut\\Slim\\PHPDI\\Configuration;\nuse Jgut\\Slim\\PHPDI\\ContainerBuilder;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Slim\\App;\n\n$container = ContainerBuilder::build(new Configuration([]));\n\n$app = $container-\u003eget(App::class);\n\n$app-\u003eget('/hello/{name}', function (ResponseInterface $response, string $name, \\PDO $connection): ResponseInterface {\n    // $name will be injected from request arguments\n    // $connection will be injected directly from the container\n\n    $response-\u003egetBody()-\u003ewrite('Hello ' . $name);\n\n    return $response;\n});\n\n$app-\u003erun();\n```\n\nIf you prefer default Slim's `Slim\\Handlers\\Strategies\\RequestResponse` strategy or any other of your choosing you only have to set it in a definition file\n\n```php\nuse Slim\\Handlers\\Strategies\\RequestResponse;\nuse Slim\\Interfaces\\InvocationStrategyInterface;\n\nuse function DI\\create;\n\nreturn [\n    InvocationStrategyInterface::class =\u003e create(RequestResponse::class),\n];\n```\n\n## Console command\n\n```php\nuse Symfony\\Component\\Console\\Application;\nuse Jgut\\Slim\\PHPDI\\Command\\ListCommand;\n\n/** @var \\Slim\\App $app */\n$container = $app-\u003egetContainer();\n\n$cli = new Application('Slim CLI');\n$cli-\u003eadd(new ListCommand($container));\n\n$app-\u003erun();\n```\n\n### List container definitions\n\nList defined container definitions supporting searching\n\n```bash\nphp -f cli.php slim:container:list --help\n```\n\n## Migration from 3.x\n\n* PHP minimum required version is PHP 8.0\n* Moved to PHP-DI 7. Annotations have been removed, use Attributes\n\n## Contributing\n\nFound a bug or have a feature request? [Please open a new issue](https://github.com/juliangut/slim-php-di/issues). Have a look at existing issues before.\n\nSee file [CONTRIBUTING.md](https://github.com/juliangut/slim-php-di/blob/master/CONTRIBUTING.md)\n\n## License\n\nSee file [LICENSE](https://github.com/juliangut/slim-php-di/blob/master/LICENSE) included with the source code for a copy of the license terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliangut%2Fslim-php-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliangut%2Fslim-php-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliangut%2Fslim-php-di/lists"}