{"id":18620802,"url":"https://github.com/exts/canister","last_synced_at":"2025-11-03T12:30:26.941Z","repository":{"id":62503354,"uuid":"91209634","full_name":"exts/Canister","owner":"exts","description":"Canister is a psr-11 auto-wiring container for PHP 7. Will add examples later.","archived":false,"fork":false,"pushed_at":"2017-05-23T22:03:18.000Z","size":33,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-27T04:26:21.783Z","etag":null,"topics":["autowiring","container-interop","dependency-injection","php","php7","psr-11","psr-16"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/exts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-14T00:07:33.000Z","updated_at":"2022-09-21T01:58:58.000Z","dependencies_parsed_at":"2022-11-02T10:01:27.088Z","dependency_job_id":null,"html_url":"https://github.com/exts/Canister","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exts%2FCanister","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exts%2FCanister/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exts%2FCanister/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exts%2FCanister/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/exts","download_url":"https://codeload.github.com/exts/Canister/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239414217,"owners_count":19634395,"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":["autowiring","container-interop","dependency-injection","php","php7","psr-11","psr-16"],"created_at":"2024-11-07T04:08:01.951Z","updated_at":"2025-11-03T12:30:26.913Z","avatar_url":"https://github.com/exts.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Canister\nCanister is a psr-11 auto-wiring container for PHP 7. Will add examples later.\n\n#### Notes\n\n- All classes that aren't in the container are automatically resolved if they exist\n- All resolved classes are shared by default\n- Reflector uses a simple array cache by default, but can be overridden by using a PSR-16 simple cache interface when passed to the constructor of a new reflector instance\n- Container is an implementation of `ArrayAccess` so you can do pretty much what you would normally do using `ArrayAccess`\n- You can define/override default values for class instances when the code tries to resolve it from the container.\n- The get method checks in this order when calling the `get` method from the container: container, factory callables, shared callables, then attempts to resolve any classes if they exists and attempt to grab them from the reflector's cache first then resolve it automatically. If all else fails it'll return `NULL`\n\n# Examples\n\nBelow are simple examples of it's usage.\n\n##### Basic instantiation\n\n```php\nuse Canister;\n\n$container = new Canister;\n```\n\n##### Storing basic data into the container\n\n```php\n$container['my-key-value'] = 'accessed anywhere';\n \necho $container-\u003eget('my-key-value'); // or $container['my-key-value'];\n```\n\n##### Auto-Resolving a class\n\n```php\nclass Example\n{\n    public function foo() {\n        return 'bar';\n    }\n}\n\n$example = $container-\u003eget(Example::class);\necho $example-\u003efoo(); // -\u003e 'bar'\n```\n\n##### Creating an alias to a class\n\n```php\n$container-\u003ealias(ExampleInterface::class, Example::class);\n\n$example = $container-\u003eget(ExampleInterface::class);\necho $example-\u003efoo(); // -\u003e 'bar'\n```\n\nThis also works for automatically passing dependencies to classes\n\n```php\nclass Test implements TestInterface {\n    public function example() {\n        return 'example';\n    }\n}\n\nclass TestExample {\n    public function __construct(TestInterface $test) {\n        $this-\u003etest = $test;\n    }\n    public function testing() {\n        return $this-\u003etest-\u003eexample();\n    }\n}\n\n$container-\u003ealias(TestInterface::class, Test::class);\n$test_example = $container-\u003eget(TestExample::class);\necho $test_example-\u003etesting(); // -\u003e 'example'\n```\n\n##### Define class as a factory\n\nEverytime you call `FactoryClass` it'll be a new instance instead of being shared by default.\n\n```php\n$container-\u003efactory(FactoryClass::class);\n```\n\n##### Define factory callable\n\nDependencies to callables are also auto resolved by default, so you can access the container directly because it's automatically passed to the container by default.\n\n```php\n$container-\u003efactory(FactoryClass::class, function() {\n    //...\n});\n```\n\n##### Define shared callable\n\nWant to do the same thing instead store the value instead of creating new instances every call? Well replace `factory` with `shared` and you get the same functionality.\n\n_(Note: since auto resolution is shared by default, you cannot pass a class name by itself like you can with the `factory` method)_\n\n\n##### Defining default parameters for callables \u0026 classes\n\nFor callables it's as simple as\n\n```php\n$container-\u003eshare('example', function($foo, $bar) {\n    return $foo + $bar;\n});\n\n$container-\u003edefine('example', [\n    'foo' =\u003e val(5),\n    'bar' =\u003e val(21)\n]);\n\necho $container-\u003eget('example');\n```\n\nThe first parameter is the class name or callable name that we're trying to define parameters for. The second parameter is an array used to match the parameters we want to define.\n\nYou don't have to order them in any order, just need to know the variable name and make that as the key.\n\n**Global Definition functions**\n\nThere's two global definition functions inside the `Canister` namespace called `val` (or `Canister\\val()`) and `bag` or (`Canister\\bag()`). \n\nThe `val` function is used to tell the resolution method that we're using a raw value.\n\nThe `bag` function is used to tell the resolution method that we should check the container for this value.\n\n\n### Other Notes\n\n- You can resolve php classes as well as define their value too.\n\n```php\n$container = new Canister;\n$container-\u003edefine(\\PDO::class, [\n    'dsn' =\u003e val('sqlite::memory:'),\n]);\n\n$pdo = $container-\u003eget(\\PDO::class);\n\necho is_a($pdo, \\PDO::class) ? 'true' : 'false'; // -\u003e true\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexts%2Fcanister","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexts%2Fcanister","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexts%2Fcanister/lists"}