{"id":26365487,"url":"https://github.com/felixdorn/box","last_synced_at":"2025-03-16T19:38:15.225Z","repository":{"id":45494853,"uuid":"260858314","full_name":"felixdorn/Box","owner":"felixdorn","description":" Box, a smart autowiring container for PHP. ","archived":false,"fork":false,"pushed_at":"2021-12-10T12:08:03.000Z","size":103,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-30T20:05:16.398Z","etag":null,"topics":["container","dependency-injection","package","php","psr-11","psr-11-compliant"],"latest_commit_sha":null,"homepage":"","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/felixdorn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null}},"created_at":"2020-05-03T08:20:40.000Z","updated_at":"2023-09-06T03:01:24.000Z","dependencies_parsed_at":"2022-07-19T02:04:19.904Z","dependency_job_id":null,"html_url":"https://github.com/felixdorn/Box","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/felixdorn%2FBox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixdorn%2FBox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixdorn%2FBox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felixdorn%2FBox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felixdorn","download_url":"https://codeload.github.com/felixdorn/Box/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243921616,"owners_count":20369252,"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","dependency-injection","package","php","psr-11","psr-11-compliant"],"created_at":"2025-03-16T19:38:12.421Z","updated_at":"2025-03-16T19:38:15.207Z","avatar_url":"https://github.com/felixdorn.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/felixdorn/box\"\u003e\n        \u003cimg src=\"https://res.cloudinary.com/dy3jxhiba/image/upload/v1588493084/logo_rx8y5s.svg\" width=\"150\" alt=\"\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003e\n        Box, a smart autowiring container for PHP.\n    \u003c/h1\u003e\n    \u003cp align=\"center\"\u003e\n        \u003cimg src=\"https://github.com/felixdorn/box/workflows/CI/badge.svg?branch=master\" alt=\"CI\" /\u003e\n        \u003cimg src=\"https://github.styleci.io/repos/260858314/shield?branch=master\u0026style=flat\" alt=\"Style CI\" /\u003e\n        \u003cimg alt=\"Codecov\" src=\"https://img.shields.io/codecov/c/github/felixdorn/box\"\u003e\n        \u003cimg src=\"https://img.shields.io/packagist/l/delights/box\" alt=\"License\" /\u003e\n        \u003cimg src=\"https://img.shields.io/packagist/v/delights/box\" alt=\"Last Version\" /\u003e\n    \u003c/p\u003e\n\u003c/p\u003e\n\n## Getting started\n\n### Installation\nThis library can be installed using composer, if you don't have it already, [download it](https://getcomposer.org/download).\n\nYou can either run this command :\n```bash\ncomposer require delights/box\n```\nOr by adding a requirement in your `composer.json` :\n```json\n{\n  \"require\": {\n    \"delights/box\": \"0.3.0\"  \n  }\n}\n```\nDon't forget to run `composer install` after adding the requirement.\n\nBefore diving into the autowiring and stuff, we need to create a container instance.\n```php\nuse Delights\\Box\\Container;\n\n$container = new Container();\n```\n\n## Bindings\nYou can bind something into the container, it works just like a key =\u003e value array.\n```php\n$container-\u003ebind('key', 'value');\necho $container-\u003eresolve('key'); // prints \"value\"\n```\n\nYou may want to bind a class in a closure if you need more specific arguments,\n```php\n$container-\u003ebind(Crawler::class, function () {\n    return new Crawler('f4dg65gd6fg465g');\n});\n```\nEvery time you ask for a `Crawler::class`, this closure will be executed and a fresh instance of `Crawler` will be created.\nTo avoid that, you can use the `singleton` method.\n\n```php\n$container-\u003esingleton(Connection::class, function () {\n    return new Connection();\n});\n```\nNow, the `Connection` class will be instantiated once, and the closure never executed again. \n\nYou may want to use an interface for your `Crawler` so you can easily swap instances and stuff. However, you want to retrieve the `Crawler` instance when `CrawlerInterface` is needed.\nInstead of manually bind these classes, you can use the `bindToImplementation` method.\n\n```php\n$container-\u003ebindToImplementation(CrawlerInterface::class, Crawler::class);\n// same as\n$container-\u003ebindToImplementation(CrawlerInterface::class, new Crawler);\n// same as\n$container-\u003ebindToImplementation(CrawlerInterface::class, function () {\n    return new Crawler;\n});\n```\n\n## Resolving\nSmartly resolving parameters is the primary goal of this package. You can resolve anything that needs parameter including, constructors, closures, methods, functions.\nWe even support resolving properties if there is an annotation .\n\n### Autowiring\nAutowiring allows the container to auto-magically resolve dependencies using the Reflection API.\n\n```php\nuse Delights\\Box\\Container;\n$container = new Container();\n\nclass SomeClass {}\n$container-\u003eresolve(SomeClass::class); // returns an instance of \"SomeClass\" \n\nclass SomeOtherClass {\n    public function __construct(SomeClass $dep) {\n        $this-\u003edep = $dep;\n    }\n}\n$container-\u003eresolve(SomeOtherClass::class); \n// returns an instance of \"SomeOtherClass\"\n// with $this-\u003edep set to an instance of \"SomeClass\"\n```\n\nThe container can resolve non-typed argument but only in two cases : they should either allow null or have a default value.\n\n### Resolving an object method\n```php\nclass PostsRepository {\n    public function all() {\n        return ['My article'];    \n    }\n}\n\nclass PostController {\n    public function index(PostsRepository $repository) {\n        return $repository-\u003eall();    \n    }\n}\n\n$container-\u003ecall(PostController::class, 'index');\n// This returns |'My article']\n```\n### Resolving a Closure\n```php\n$container-\u003eclosure(function (SomeClass $class) {\n    return $class instanceof SomeClass;\n}); // returns true\n```\n\n### Resolve with arbitrary parameters\n```php\nclass Vec2 {\n    protected int $x;\n    protected int $y;\n\n    public function __construct(int $x, int $y) {\n        $this-\u003ex = $x;\n        $this-\u003ey = $y;\n    }\n}\n$container-\u003eresolve(Vec2::class, [\n    'x' =\u003e 2,\n    'y' =\u003e 4\n]); // returns an instance of \"Vec2\" \n```\n\nThe order does not matter, in our example, it can be either `[x, y]` or `[y, x]`.\n\nYou can pass arbitrary parameters to any resolve function.\n\n## Singleton\nYou may want to have a global Container that keeps the same bindings across your app.\n\u003e Usually, you want to avoid this kind of behavior.\n\n```php\nuse Delights\\Box\\PersistentContainer;\n\nPersistentContainer::getInstance();\n```\nHere, we this is the first time you call it, a new instance will be created.\nHowever, if you already created one, the same instance will be returned.\n\n\n### Static Proxies\nThere is a little shortcut.\n```php\nuse Delights\\Box\\PersistentContainer;\n\n// instead of this\nPersistentContainer::getInstance()-\u003ebind('some', 'thing');\n\n// you can do that\nPersistentContainer::bind('some', 'thing');\n\n```\nThis is available for any of the `PersistentContainer` public methods.\n\n## Security \nIf you discover any security related issues, please email oss@dorns.fr instead of using the issue tracker.\n\n## Credits\n* [Félix Dorn](https://felixdorn.fr)\n\n## Licensing\nCopyright 2020 Félix Dorn\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelixdorn%2Fbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelixdorn%2Fbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelixdorn%2Fbox/lists"}