{"id":18929498,"url":"https://github.com/thecodingmachine/service-provider-registry","last_synced_at":"2025-04-15T15:31:04.002Z","repository":{"id":57067904,"uuid":"56168981","full_name":"thecodingmachine/service-provider-registry","owner":"thecodingmachine","description":"This package provides a service provider registry than can lazily instantiate the service providers it contains.","archived":false,"fork":false,"pushed_at":"2020-04-10T07:44:54.000Z","size":27,"stargazers_count":1,"open_issues_count":0,"forks_count":3,"subscribers_count":8,"default_branch":"3.0","last_synced_at":"2025-04-11T18:59:50.893Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-13T16:39:45.000Z","updated_at":"2023-11-11T00:19:20.000Z","dependencies_parsed_at":"2022-08-24T14:54:08.327Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/service-provider-registry","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fservice-provider-registry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fservice-provider-registry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fservice-provider-registry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fservice-provider-registry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/service-provider-registry/tar.gz/refs/heads/3.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249097860,"owners_count":21212362,"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-11-08T11:33:00.853Z","updated_at":"2025-04-15T15:31:03.754Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/service-provider-registry/badges/quality-score.png?b=3.0)](https://scrutinizer-ci.com/g/thecodingmachine/service-provider-registry/?branch=3.0)\n[![Build Status](https://travis-ci.org/thecodingmachine/service-provider-registry.svg?branch=3.0)](https://travis-ci.org/thecodingmachine/service-provider-registry)\n[![Coverage Status](https://coveralls.io/repos/thecodingmachine/service-provider-registry/badge.svg?branch=3.0\u0026service=github)](https://coveralls.io/github/thecodingmachine/service-provider-registry?branch=3.0)\n\n\nWhat is it?\n===========\n\nThis project contains a registry that stores service providers. The registry implements `\\ArrayAccess` and behaves like an array.\nHowever, service providers in this object can be instantiated only when the key in the array is fetched, so you don't have to create the instance right away. This is useful for performance considerations, especially for compiled or cached containers.\n\nThis class is meant to be used by compiled/cached containers that want to implement [container-interop service providers](http://github.com/container-interop/service-provider). It is not meant for the mere mortals.\n\nHow does it work?\n=================\n\nEasy, you create a new `Registry` object, and then, you push objects in it.\n\n```php\n$registry = new Registry();\n\n$key = $registry-\u003epush(MyServiceProvider::class);\n\n// This will trigger the creation of the MyServiceProvider object and return it.\n$serviceProvider = $registry[$key];\n```\n\nYou can also pass parameters to the constructor of the object:\n\n```php\n$registry = new Registry();\n\n$key = $registry-\u003epush(MyServiceProvider::class, \"param1\", \"param2\");\n```\n\nAnd because we are kind, you can also push into the lazy array an already instantiated object:\n\n```php\n$registry = new Registry();\n\n// This is possible, even if we loose the interest of the Registry.\n$key = $registry-\u003epush(new MyServiceProvider());\n```\n\n\nFinally, if you are performance oriented (and I'm sure you are, otherwise you wouldn't be looking at this package), you can create the whole registry in one call:\n\n```php\n$registry = new Registry([\n    MyServiceProvider::class, // Is you simply want to create an instance without passing parameters\n    [ MyServiceProvider2::class, [ \"param1\", \"param2 ] ],  // Is you simply want to create an instance and pass parameters to the constructor\n    new MyServiceProvider4('foo') // If you directly want to push the constructed instance.\n]);\n```\n\nIterating the registry\n======================\n\nThe registry implements the `\\Traversable` interface, so iterating it is as simple as a `foreach`:\n\n```php\nforeach ($registry as $serviceProvider) {\n    // Do stuff for each service provider.\n    // Service providers will be instantiated on the fly if needed.\n}\n```\n\nDiscovery with thecodingmachine/discovery\n=========================================\n\nThe registry supports a discovery mechanism based on [thecodingmachine/discovery](https://thecodingmachine.github.io/discovery/) (to automatically find and attach service providers to your application).\n\nAs a second parameter, the `Registry` accepts the `Discovery` object from [thecodingmachine/discovery](https://github.com/thecodingmachine/discovery). Pass this object and thecodingmachine/discovery will be used to fetch service providers from your packages.\n\n```php\n$registry = new Registry([], TheCodingMachine\\Discovery::getInstance());\n\n// The registry now contains all the service providers discoverable by thecodingmachine/discovery.\n```\n\nCaching of `getFactories` and `getExtensions`\n=============================================\n\nYou can use the shortcut `Registry::getFactories($key)` or `Registry::getExtensions($key)` methods to call the `getFactories` and `getExtensions` methods on a service provider. The result is cached: 2 successive calls will not call the `getFactories` or `getExtensions` methods twice.\n\n\n```php\n$factories = $registry-\u003egetFactories(0);\n\n$extensions = $registry-\u003egetExtensions(0);\n```\n\nUsing the registry to create services\n=====================================\n\nEven better, using the `createService` method of the registry, you can directly call the service factory:\n\n\n```php\n$myService = $registry-\u003ecreateService(0, 'serviceName', $container);\n```\n\nUsing the `extendService` method of the registry, you can directly call the service extension:\n\n\n```php\n$myService = $registry-\u003eextendService(0, 'serviceName', $container, $previousService);\n```\n\nWhy?\n====\n\nThis was built for improving the performance of service providers loading (in compiled container environment).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fservice-provider-registry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fservice-provider-registry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fservice-provider-registry/lists"}