{"id":16292243,"url":"https://github.com/divineniiquaye/php-invoker","last_synced_at":"2025-03-20T03:30:46.385Z","repository":{"id":56551709,"uuid":"284898885","full_name":"divineniiquaye/php-invoker","owner":"divineniiquaye","description":"A library that provides the abilities to invoking callables with named parameters in a generic and extensible way.","archived":false,"fork":false,"pushed_at":"2021-03-14T20:45:01.000Z","size":198,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T22:49:47.540Z","etag":null,"topics":["biurad","callables","dependency-injection","di-invoker","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/divineniiquaye.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG-0.x.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"patreon":"biurad","custom":["https://biurad.com/sponsor"]}},"created_at":"2020-08-04T06:42:09.000Z","updated_at":"2023-03-09T00:45:57.000Z","dependencies_parsed_at":"2022-08-15T20:50:18.481Z","dependency_job_id":null,"html_url":"https://github.com/divineniiquaye/php-invoker","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":"biurad/php-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divineniiquaye%2Fphp-invoker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divineniiquaye%2Fphp-invoker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divineniiquaye%2Fphp-invoker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/divineniiquaye%2Fphp-invoker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/divineniiquaye","download_url":"https://codeload.github.com/divineniiquaye/php-invoker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244543726,"owners_count":20469551,"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":["biurad","callables","dependency-injection","di-invoker","php"],"created_at":"2024-10-10T20:05:09.258Z","updated_at":"2025-03-20T03:30:46.018Z","avatar_url":"https://github.com/divineniiquaye.png","language":"PHP","funding_links":["https://patreon.com/biurad","https://biurad.com/sponsor","https://www.patreon.com/biurad"],"categories":[],"sub_categories":[],"readme":"# The PHP Invoker\n\n[![Latest Version](https://img.shields.io/packagist/v/divineniiquaye/php-invoker.svg?style=flat-square)](https://packagist.org/packages/divineniiquaye/php-invoker)\n[![Software License](https://img.shields.io/badge/License-BSD--3-brightgreen.svg?style=flat-square)](LICENSE)\n[![Workflow Status](https://img.shields.io/github/workflow/status/divineniiquaye/php-invoker/Tests?style=flat-square)](https://github.com/divineniiquaye/php-invoker/actions?query=workflow%3ATests)\n[![Code Maintainability](https://img.shields.io/codeclimate/maintainability/divineniiquaye/php-invoker?style=flat-square)](https://codeclimate.com/github/divineniiquaye/php-invoker)\n[![Coverage Status](https://img.shields.io/codecov/c/github/divineniiquaye/php-invoker?style=flat-square)](https://codecov.io/gh/divineniiquaye/php-invoker)\n[![Quality Score](https://img.shields.io/scrutinizer/g/divineniiquaye/php-invoker.svg?style=flat-square)](https://scrutinizer-ci.com/g/divineniiquaye/php-invoker)\n[![Sponsor development of this project](https://img.shields.io/badge/sponsor%20this%20package-%E2%9D%A4-ff69b4.svg?style=flat-square)](https://biurad.com/sponsor)\n\n**divineniiquaye/php-invoker** is a php library that allows invoking callables with named parameters in a generic and extensible way for [PHP] 7.1+, based on reference implementation [PHP-DI Invoker][di-invoker] created by [Matthieu Napoli][@mnapoli]. This library provides clear extension points to let frameworks/projects implement any kind of dependency injection support they want, but not limited to dependency injection. Again, any [PSR-11] compliant container can be provided.\n\n## 📦 Installation \u0026 Basic Usage\n\nThis project requires [PHP] 7.1 or higher. The recommended way to install, is via [Composer]. Simply run:\n\n```bash\n$ composer require divineniiquaye/php-invoker\n```\n\nLet's say you working on a project and want to invoke some named parameters in callables with whatever the order of parameters, but should be matched by their names or instance. Then we'll need an over-engineered `call_user_func()`.\n\nIn short, this library is meant to be a base building block for calling a function with named parameters and/or dependency injection.\n\nUsing `DivineNii\\Invoker\\Invoker` class method `call`:\n\n```php\n$invoker = new DivineNii\\Invoker\\Invoker;\n\n$invoker-\u003ecall(function () {\n    echo 'Hello world!';\n});\n\n// Simple parameter array\n$invoker-\u003ecall(function ($name) {\n    echo 'Hello ' . $name;\n}, ['John']);\n\n// Named parameters\n$invoker-\u003ecall(function ($name) {\n    echo 'Hello ' . $name;\n}, [\n    'name' =\u003e 'John'\n]);\n\n// Typehint parameters\n$invoker-\u003ecall(function (string $name) {\n    echo 'Hello ' . $name;\n}, [\n    'name' =\u003e 'John'\n]);\n\n// Use the default value\n$invoker-\u003ecall(function ($name = 'world') {\n    echo 'Hello ' . $name;\n});\n\n// Invoke any PHP callable\n$invoker-\u003ecall(['MyClass', 'myStaticMethod']);\n\n// Using Class::method syntax\n$invoker-\u003ecall('MyClass::myStaticMethod');\n\n// Using \":\" pattern syntax\n$invoker-\u003ecall('MyClass:myMethod');\n\n// Using \"@\" pattern syntax\n$invoker-\u003ecall('MyClass@myMethod');\n```\n\nUsing `DivineNii\\Invoker\\ArgumentResolver` class in `DivineNii\\Invoker\\Invoker` class:\n\nExtending the behavior of the `DivineNii\\Invoker\\Invoker` is easy and is done by adding a callable to [`ArgumentResolver`](https://github.com/divineniiquaye/php-invoker/blob/master/src/ArgumentResolver.php) class:\n\n```php\nuse ReflectionParameter;\nuse DivineNii\\Invoker\\Interfaces\\ArgumentValueResolverInterface;\n\nclass MyParameterValueResolver implements ArgumentValueResolverInterface\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function resolve(ReflectionParameter $parameter, array $providedParameters)\n    {\n        //....\n    }\n}\n```\n\n- `$providedParameters` contains the parameters provided by the user when calling `$invoker-\u003ecall($callable, $parameters)`\n\nAn `DivineNii\\Invoker\\Invoker` can chain multiple parameter resolvers to mix behaviors, e.g. you can mix \"named parameters\" support with \"dependency injection\" support.\n\nHere is an implementation example for dumb dependency injection that creates a new instance of the classes type-hinted:\n\n```php\nuse {ReflectionClass, ReflectionException};\nuse DivineNii\\Invoker\\Interfaces\\ArgumentValueResolverInterface;\n\nclass MyParameterValueResolver implements ArgumentValueResolverInterface\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function resolve(ReflectionParameter $parameter, array $providedParameters)\n    {\n        $parameterClass = $parameter-\u003egetClass();\n\n        if ($parameterClass instanceof ReflectionClass) {\n            try {\n                return $class-\u003enewInstance();\n            } catch (ReflectionExcetion $e) {\n                // ...\n            }\n        }\n    }\n}\n```\n\nTo use it:\n\n```php\n$invoker = new DivineNii\\Invoker\\Invoker([new MyParameterValueResolver()]);\n\n$invoker-\u003ecall(function (ArticleManager $articleManager) {\n    $articleManager-\u003epublishArticle('Hello world', 'This is the article content.');\n});\n```\n\nA new instance of `ArticleManager` will be created by our parameter resolver. The fun starts to happen when we want to add support for many things:\n\n- named parameters\n- dependency injection for type-hinted parameters\n- ...\n\nIt allows to support even the weirdest use cases like:\n\n```php\n$parameters = [];\n\n// First parameter will receive \"Welcome\"\n$parameters[] = 'Welcome';\n\n// Parameter named \"content\" will receive \"Hello world!\"\n$parameters['content'] = 'Hello world!';\n\n// $published is not defined so it will use its default value\n$invoker-\u003ecall(function ($title, $content, $published = true) {\n    // ...\n}, $parameters);\n```\n\nRather than have you re-implement support for dependency injection with different containers every time, this package ships with 2 optional resolvers:\n\n- This resolver will inject container entries by searching for the class name using the type-hint:\n\n    ```php\n    $invoker-\u003ecall(function (Psr\\Logger\\LoggerInterface $logger) {\n        // ...\n    });\n    ```\n\n    In this example it will `-\u003eget('Psr\\Logger\\LoggerInterface')` from the container and inject it, but if instance of interface exist in `$providedParameters`, it also get injected.\n\n- This resolver will inject container entries by searching for the name of the parameter:\n\n    ```php\n    $invoker-\u003ecall(function ($twig) {\n        // ...\n    });\n    ```\n\n    In this example it will `-\u003eget('twig')` from the container and inject it or from `$providedParameters`.\n\nThe `DivineNii\\Invoker\\Invoker` can be wired to your DI container to resolve the callables, but can resolve all callables including invokable class or object.\n\nFor example with an invokable class:\n\n```php\nclass MyHandler\n{\n    public function __invoke()\n    {\n        // ...\n    }\n}\n\n// By default this work\n$invoker-\u003ecall('MyHandler');\n\n// If we set up the container to use\n$invoker = new Invoker\\Invoker([], $container);\n// Now 'MyHandler' parameters is resolved using the container if any!\n$invoker-\u003ecall('MyHandler');\n```\n\nThe same works for a class method:\n\n```php\nclass WelcomeController\n{\n    public function home()\n    {\n        // ...\n    }\n}\n\n// By default this doesn't work: home() is not a static method\n$invoker-\u003ecall(['WelcomeController', 'home']);\n\n// If we set up the container to use\n$invoker = new Invoker\\Invoker([], $container);\n// Now 'WelcomeController' is resolved using the container!\n$invoker-\u003ecall(['WelcomeController', 'home']);\n// Alternatively we can use the Class::method syntax\n$invoker-\u003ecall('WelcomeController::home');\n```\n\n## 📓 Documentation\n\nFor in-depth documentation before using this library. Full documentation on advanced usage, configuration, and customization can be found at [docs.biurad.com][docs].\n\n## ⏫ Upgrading\n\nInformation on how to upgrade to newer versions of this library can be found in the [UPGRADE].\n\n## 🏷️ Changelog\n\n[SemVer](http://semver.org/) is followed closely. Minor and patch releases should not introduce breaking changes to the codebase; See [CHANGELOG] for more information on what has changed recently.\n\nAny classes or methods marked `@internal` are not intended for use outside of this library and are subject to breaking changes at any time, so please avoid using them.\n\n## 🛠️ Maintenance \u0026 Support\n\nWhen a new **major** version is released (`1.0`, `2.0`, etc), the previous one (`0.19.x`) will receive bug fixes for _at least_ 3 months and security updates for 6 months after that new release comes out.\n\n(This policy may change in the future and exceptions may be made on a case-by-case basis.)\n\n**Professional support, including notification of new releases and security updates, is available at [Biurad Commits][commit].**\n\n## 👷‍♀️ Contributing\n\nTo report a security vulnerability, please use the [Biurad Security](https://security.biurad.com). We will coordinate the fix and eventually commit the solution in this project.\n\nContributions to this library are **welcome**, especially ones that:\n\n- Improve usability or flexibility without compromising our ability to adhere to [PSR-12] coding stardand.\n- Optimize performance\n- Fix issues with adhering to [PSR-11] support and backward compatability.\n\nPlease see [CONTRIBUTING] for additional details.\n\n## 🧪 Testing\n\n```bash\n$ composer test\n```\n\nThis will tests divineniiquaye/php-invoker will run against PHP 7.2 version or higher.\n\n## 👥 Credits \u0026 Acknowledgements\n\n- [Divine Niiquaye Ibok][@divineniiquaye]\n- [All Contributors][]\n\n## 🙌 Sponsors\n\nAre you interested in sponsoring development of this project? Reach out and support us on [Patreon](https://www.patreon.com/biurad) or see \u003chttps://biurad.com/sponsor\u003e for a list of ways to contribute.\n\n## 📄 License\n\n**divineniiquaye/php-invoker** is licensed under the BSD-3 license. See the [`LICENSE`](LICENSE) file for more details.\n\n## 🏛️ Governance\n\nThis project is primarily maintained by [Divine Niiquaye Ibok][@divineniiquaye]. Members of the [Biurad Lap][] Leadership Team may occasionally assist with some of these duties.\n\n## 🗺️ Who Uses It?\n\nYou're free to use this package, but if it makes it to your production environment we highly appreciate you sending us an [email] or [message] mentioning this library. We publish all received request's at \u003chttps://patreons.biurad.com\u003e.\n\nCheck out the other cool things people are doing with `divineniiquaye/php-invoker`: \u003chttps://packagist.org/packages/divineniiquaye/php-invoker/dependents\u003e\n\n[Composer]: https://getcomposer.org\n[@divineniiquaye]: https://github.com/divineniiquaye\n[docs]: https://docs.biurad.com/php-invoker\n[commit]: https://commits.biurad.com/php-invoker.git\n[UPGRADE]: UPGRADE-1.x.md\n[CHANGELOG]: CHANGELOG-0.x.md\n[CONTRIBUTING]: ./.github/CONTRIBUTING.md\n[All Contributors]: https://github.com/divineniiquaye/php-invoker/contributors\n[Biurad Lap]: https://team.biurad.com\n[email]: support@biurad.com\n[message]: https://projects.biurad.com/message\n[PHP]: https://php.net\n[PSR-11]: http://www.php-fig.org/psr/psr-11/\n[PSR-12]: http://www.php-fig.org/psr/psr-12/\n[@mnapoli]: https://github.com/mnapoli\n[di-invoker]: https://github.com/PHP-DI/Invoker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivineniiquaye%2Fphp-invoker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdivineniiquaye%2Fphp-invoker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivineniiquaye%2Fphp-invoker/lists"}