{"id":15027699,"url":"https://github.com/giuseppe998e/syringe","last_synced_at":"2025-08-20T16:14:56.686Z","repository":{"id":61983497,"uuid":"543061658","full_name":"giuseppe998e/syringe","owner":"giuseppe998e","description":"Dependency injection library for PHP8","archived":false,"fork":false,"pushed_at":"2022-10-26T10:18:14.000Z","size":47,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T22:14:03.552Z","etag":null,"topics":["autowired","bean","component","configuration","dependencies","dependency-injection","inject","injection","injector","php","php8","php80","php81","provides","repository"],"latest_commit_sha":null,"homepage":"","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/giuseppe998e.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":"2022-09-29T10:34:08.000Z","updated_at":"2022-12-08T22:38:46.000Z","dependencies_parsed_at":"2022-10-24T17:30:22.824Z","dependency_job_id":null,"html_url":"https://github.com/giuseppe998e/syringe","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuseppe998e%2Fsyringe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuseppe998e%2Fsyringe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuseppe998e%2Fsyringe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuseppe998e%2Fsyringe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giuseppe998e","download_url":"https://codeload.github.com/giuseppe998e/syringe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248104703,"owners_count":21048388,"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":["autowired","bean","component","configuration","dependencies","dependency-injection","inject","injection","injector","php","php8","php80","php81","provides","repository"],"created_at":"2024-09-24T20:06:55.046Z","updated_at":"2025-04-09T20:22:21.288Z","avatar_url":"https://github.com/giuseppe998e.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Syringe\nDependency injection library for PHP8.\n\n## Usage\n#### The configuration class:\n\n```php\n\u003c?php\n\nuse PDO;\nuse Syringe\\Attribute\\{Provides, Qualifier};\n\nclass DbConfiguration {\n    // bool \"primary\" - Sets the Provides as primary if more than one of the same type is available.\n    // ?string \"name\" - Custom name (or qualifier) for the Provides. (Set \"null\" to use the method name)\n    // bool \"singleton\" - Denotes that the Provides is a singleton\n    #[Provides(primary: false, name: null, singleton: true)] // Default values\n    public function getMariaDBConnection(): PDO {\n        $dsn = 'mysql:dbname=mariadb;host=127.0.0.1;port=3307';\n        $user = 'root';\n        $password = 'password';\n        return new PDO($dsn, $user, $password);\n    }\n\n    #[Provides(primary: true)]\n    public function getMySQLConnection(): PDO {\n        $dsn = 'mysql:dbname=mysqldb;host=127.0.0.1;port=3306';\n        $user = 'root';\n        $password = 'password';\n        return new PDO($dsn, $user, $password);\n    }\n\n/*\n    public function getCarsRepository(\n        // Without \"Qualifier\" the parameter will be bound to\n        // \"getMySQLConnection\" (because it's primary for the PDO class)\n        #[Qualifier(\"getMariaDBConnection\")] PDO $db\n    ): CarsRepository {\n        return new CarsRepository($db);\n    }\n*/\n\n    // ...\n}\n```\n\n\n#### The component class:\n\n```php\n\u003c?php\n\nuse Syringe\\Attribute\\Inject;\n\nclass TestClass {\n    #[Inject] // Injects \"getMySQLConnection\" because it's set as primary\n    // #[Inject(\"getMariaDBConnection\")]\n    // or #[Inject(qualifier: \"getMySQLConnection\")]\n    private PDO $db;\n\n    public function getUserById(int $id): array {\n         $stmt = $db-\u003eprepare('SELECT * FROM users WHERE id=?');\n         $stmt-\u003eexecute([$id]);\n         return $stmt-\u003efetch();\n    }\n}\n\n```\n\n\n#### Initialize Syringe:\n\n```php\n\u003c?php\n\nuse Syringe\\Syringe;\nuse Syringe\\Repository\\ComponentRepository;\n\n$repo = new ComponentRepository();\n$repo-\u003eaddConfiguration(DbConfiguration::class);\n// $repo-\u003eaddConfiguration(OtherConfiguration::class);\nSyringe::initialize($repo);\n\n$testClass = Syringe::new(TestClass::class);\n$user = $testClass-\u003egetUserById(1);\nvar_dump($user);\n\n// ...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiuseppe998e%2Fsyringe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiuseppe998e%2Fsyringe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiuseppe998e%2Fsyringe/lists"}