{"id":22477567,"url":"https://github.com/maplephp/container","last_synced_at":"2025-07-17T01:33:59.422Z","repository":{"id":209843893,"uuid":"620709684","full_name":"MaplePHP/Container","owner":"MaplePHP","description":"PSR Container built for PHP Fuse framework","archived":false,"fork":false,"pushed_at":"2025-05-14T19:11:14.000Z","size":87,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-14T20:28:13.711Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MaplePHP.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-03-29T08:12:26.000Z","updated_at":"2025-03-25T19:16:33.000Z","dependencies_parsed_at":"2023-11-29T13:20:17.410Z","dependency_job_id":"1cf055aa-8c4d-4672-9574-f28d98159425","html_url":"https://github.com/MaplePHP/Container","commit_stats":null,"previous_names":["maplephp/container"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/MaplePHP/Container","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FContainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FContainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FContainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FContainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MaplePHP","download_url":"https://codeload.github.com/MaplePHP/Container/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FContainer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265558627,"owners_count":23787950,"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-12-06T14:11:33.786Z","updated_at":"2025-07-17T01:33:59.401Z","avatar_url":"https://github.com/MaplePHP.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# Container, Factories and the dependency injector \nPSR Container built for MaplePHP framework\n\nContainer, Factories and dependency injectors will help to make your PHP code more maintainable, flexible, and testable by reducing coupling between objects and centralizing the management of dependencies.\n\n- [Container](#container)\n- [Factory](#factory)\n- [Dependency injector](#dependency-injector)\n- [Event handler](#event-handler)\n\n## Container\nContainers allowing you to easily create and retrieve objects that are needed throughout your application.\n```php\nuse MaplePHP\\Container\\Container;\n$container = new Container();\n$container-\u003eset(\"YourClass\", \\YourNamespace\\To\\YourClass::class); // Bind \"YourClass\" to container and dependency injector\n$yourClass = $container-\u003eget(\"YourClass\")-\u003eget(); // Will return \"YourClass\"\n//$yourClass-\u003eyourClassMehthod();\n```\nIf the constructor of \"YourClass\" contains unresolved class arguments, the dependency injector will attempt to automatically locate them for you. Read more under the headline **dependency injector**.\n\n## Factory\nFactories can be used to create new instances of objects, rather than instantiating them directly in your code. \n```php\n$container-\u003efactory(\"factoryKey\", function() {\n    $a = new TestClassA();\n    $b = new TestClassB();\n    return new TestClassC($a, $b);\n});\necho $container-\u003eget(\"factoryKey\"); // Will return TestClassC\n```\n## Dependency injector\nDependency injection is a technique for managing dependencies between objects in an application. Instead of creating objects directly in your code, you can pass them in as dependencies when you instantiate an object. This makes your code more modular and easier to test, as you can easily swap out dependencies for mock objects or other implementations.\n\nYou can use the **Dependency injector** just like create any other container, as long as you dont add arguments or try to access method, if you do that then it will automatically disable **Dependency injector**. It is design like this becouse it will load in all class reclusive into endlessly.\n\nTake a look at this example\n\n```php\n\n$container-\u003eset(\"YourClass\", \\YourNamespace\\To\\YourClass::class);\n$testService = $container-\u003eget(\"YourClass\");\necho $testService-\u003estart();\n\n```\nThe above code will load **YourClass** and auto initialize the class **Test**.\n\n```php\nnamespace YourNamespace\\To;\n\nuse YourNamespace\\ToTestClasses\\Test;\n\nclass YourClass {\n    \n    private $test;\n\n    // Dependency injector will auto load \"Test\" class and the \"Test\" classes and so on.\n    function __construct(Test $test) {\n        $this-\u003etest = $test;\n    }\n\n    function start() {\n        return $this-\u003etest-\u003eget(\"This is the start page\");\n    }\n    \n}\n```\n\n## Event handler\n\n### Initiate\n```php\nuse MaplePHP\\Container\\EventHandler;\n```\n\n### Example 1 - (Callable)\n```php\n$logger = new EventHandler();\n$logger-\u003eaddHandler(new Logger());\n$logger-\u003eaddEvent(function() {\n    var_dump(\"Executed in conjunction with logger every method\");\n    // You could add a mail function that will send log message to you,\n});\necho $logger-\u003eerror(\"A error message to log\");\n// Will log message AND execute the event\n```\n\n### Example 2 - (Bind to method)\n```php\n$logger = new EventHandler();\n$logger-\u003eaddHandler(new Logger(), [\"emergency\", \"alert\", \"critical\"]);\n$logger-\u003eaddEvent(function() {\n    var_dump(\"Executed in conjunction with logger event method\");\n    // You could add a mail function that will send log message to you,\n});\necho $logger-\u003eerror(\"A error message to log\");\n// Will only log message\necho $logger-\u003ealert(\"A error message to log\");\n// Will log message AND execute the event\n```\n\n### Example 3 - (Add service to handler)\nSet the namespace to the **EventInterface**.\n```php\nuse MaplePHP\\Container\\Interfaces\\EventInterface;\n```\nThen extend a class with implements to the interface **EventInterface** and add the method **resolve** to that class. I am using a \"Anonymous Function\" bellow as an example just to show that **EventInterface** is required, you can use a regular class.\n```php\n$callableFunction = new class implements EventInterface {\n\n    public function someMethod(string $what): string\n    {\n        return \"Hello {$what}!\";\n    }\n\n    // Resolve method will be executed in conjunction \n    // with logger event method\n    public function resolve(): void\n    {\n        var_dump($this-\u003esomeMethod(\"world\"));\n    }\n};\n\n$logger = new EventHandler();\n$logger-\u003eaddHandler(new Logger(), [\"emergency\", \"alert\", \"critical\"]);\n$logger-\u003eaddEvent(function() {\n    var_dump(\"Executed in conjunction with logger event method\");\n    // You could add a mail function that will send log message to you,\n});\necho $logger-\u003eerror(\"A error message to log\");\n// Will only log message\necho $logger-\u003ealert(\"A error message to log\");\n// Will log message AND execute the event\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaplephp%2Fcontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaplephp%2Fcontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaplephp%2Fcontainer/lists"}