{"id":16329272,"url":"https://github.com/josantonius/php-hook","last_synced_at":"2025-03-20T22:32:02.532Z","repository":{"id":50532718,"uuid":"85144374","full_name":"josantonius/php-hook","owner":"josantonius","description":"PHP library for hook management","archived":false,"fork":false,"pushed_at":"2022-09-29T17:02:58.000Z","size":1910,"stargazers_count":22,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-20T07:19:00.360Z","etag":null,"topics":["actions","handling-hooks","php","php-hooks"],"latest_commit_sha":null,"homepage":"https://josantonius.dev","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/josantonius.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["Josantonius"]}},"created_at":"2017-03-16T02:40:41.000Z","updated_at":"2024-06-07T10:23:52.000Z","dependencies_parsed_at":"2022-08-30T13:51:27.126Z","dependency_job_id":null,"html_url":"https://github.com/josantonius/php-hook","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-hook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244566947,"owners_count":20473451,"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":["actions","handling-hooks","php","php-hooks"],"created_at":"2024-10-10T23:14:58.560Z","updated_at":"2025-03-20T22:32:02.208Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","readme":"# PHP Hook library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/hook/v/stable)](https://packagist.org/packages/josantonius/hook)\n[![License](https://poser.pugx.org/josantonius/hook/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/hook/downloads)](https://packagist.org/packages/josantonius/hook)\n[![CI](https://github.com/josantonius/php-hook/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-hook/actions/workflows/ci.yml)\n[![CodeCov](https://codecov.io/gh/josantonius/php-hook/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-hook)\n[![PSR1](https://img.shields.io/badge/PSR-1-f57046.svg)](https://www.php-fig.org/psr/psr-1/)\n[![PSR4](https://img.shields.io/badge/PSR-4-9b59b6.svg)](https://www.php-fig.org/psr/psr-4/)\n[![PSR12](https://img.shields.io/badge/PSR-12-1abc9c.svg)](https://www.php-fig.org/psr/psr-12/)\n\n**Translations**: [Español](.github/lang/es-ES/README.md)\n\nLibrary for handling hooks in PHP.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [Action Instance](#action-instance)\n  - [Hook Class](#hook-class)\n  - [Priority Class](#priority-class)\n- [Exceptions Used](#exceptions-used)\n- [Usage](#usage)\n- [Tests](#tests)\n- [TODO](#todo)\n- [Changelog](#changelog)\n- [Contribution](#contribution)\n- [Sponsor](#sponsor)\n- [License](#license)\n\n---\n\n## Requirements\n\n- Operating System: Linux | Windows.\n\n- PHP versions: 8.1 | 8.2.\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](http://getcomposer.org/download/).\n\nTo install **PHP Hook library**, simply:\n\n```console\ncomposer require josantonius/hook\n```\n\nThe previous command will only install the necessary files,\nif you prefer to **download the entire source code** you can use:\n\n```console\ncomposer require josantonius/hook --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-hook.git\n```\n\n## Available Classes\n\n### Action Instance\n\n`Josantonius\\Hook\\Action`\n\nGets action priority:\n\n```php\npublic function getPriority(): int;\n```\n\nGets action callback result:\n\n```php\npublic function getResult(): mixed;\n```\n\nChecks if the action is done once:\n\n```php\npublic function isOnce(): bool;\n```\n\nChecks if the action has already been done:\n\n```php\npublic function wasDone(): bool;\n```\n\n### Hook Class\n\n`Josantonius\\Hook\\Hook`\n\nRegister new hook:\n\n```php\npublic function __construct(private string $name);\n```\n\nAdds action on the hook:\n\n```php\n/**\n * Action will be maintained after performing actions and will be available if are done again.\n * \n * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php\n * \n * @return Action Added action.\n */\npublic function addAction(callable $callback, int $priority = Priority::NORMAL): Action;\n```\n\nAdds action once on the hook:\n\n```php\n/**\n * Action will only be done once and will be deleted after it is done.\n * \n * It is recommended to use this method to release the actions\n * from memory if the hook actions will only be done once.\n * \n * @return Action Added action.\n */\npublic function addActionOnce(callable $callback, int $priority = Priority::NORMAL): Action;\n```\n\nRuns the added actions for the hook:\n\n```php\n/**\n * @throws HookException if the actions have already been done.\n * @throws HookException if no actions were added for the hook.\n * \n * @return Action[] Done actions.\n */\npublic function doActions(mixed ...$arguments): array;\n```\n\nChecks if the hook has actions:\n\n```php\n/**\n * True if the hook has any action even if the action has been\n * done before (recurring actions created with addAction).\n */\npublic function hasActions(): bool;\n```\n\nChecks if the hook has undone actions:\n\n```php\n/**\n * True if the hook has some action left undone.\n */\npublic function hasUndoneActions(): bool;\n```\n\nChecks if the actions were done at least once:\n\n```php\n/**\n * If doActions was executed at least once.\n */\npublic function hasDoneActions(): bool;\n```\n\nGets hook name:\n\n```php\npublic function getName(): string;\n```\n\n### Priority Class\n\n`Josantonius\\Hook\\Priority`\n\nAvailable constants:\n\n```php\npublic const HIGHEST = 50;\npublic const HIGH    = 100;\npublic const NORMAL  = 150;\npublic const LOW     = 200;\npublic const LOWEST  = 250;\n```\n\n## Exceptions Used\n\n```php\nuse Josantonius\\Hook\\Exceptions\\HookException;\n```\n\n## Usage\n\nExample of use for this library:\n\n### Register new hook\n\n```php\nuse Josantonius\\Hook\\Hook;\n\n$hook = new Hook('name');\n```\n\n### Adds actions on the hook\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nclass Foo {\n    public static function bar() { /* do something */ }\n    public static function baz() { /* do something */ }\n}\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(Foo::bar(...));\n$hook-\u003eaddAction(Foo::baz(...));\n```\n\n### Add actions with custom priority in the hook\n\n```php\nuse Josantonius\\Hook\\Hook;\nuse Josantonius\\Hook\\Priority;\n\nclass Foo {\n    public static function bar() { /* do something */ }\n    public static function baz() { /* do something */ }\n}\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(Foo::bar(...), Priority::LOW);\n$hook-\u003eaddAction(Foo::baz(...), Priority::HIGH);\n```\n\n### Adds actions once on the hook\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nclass Foo {\n    public function bar() { /* do something */ }\n    public function baz() { /* do something */ }\n}\n\n$foo  = new Foo();\n$hook = new Hook('name');\n\n$hook-\u003eaddActionOnce($foo-\u003ebar(...));\n$hook-\u003eaddActionOnce($foo-\u003ebaz(...));\n```\n\n### Adds actions once with custom priority in the hook\n\n```php\nuse Josantonius\\Hook\\Hook;\nuse Josantonius\\Hook\\Priority;\n\nclass Foo {\n    public function bar() { /* do something */ }\n    public function baz() { /* do something */ }\n}\n\n$foo  = new Foo();\n$hook = new Hook('name');\n\n$hook-\u003eaddActionOnce($foo-\u003ebar(...), Priority::LOW);\n$hook-\u003eaddActionOnce($foo-\u003ebaz(...), Priority::HIGH);\n```\n\n### Do actions with the same priority\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction one() { /* do something */ }\nfunction two() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(one(...));\n$hook-\u003eaddAction(two(...));\n\n/**\n * The actions will be executed according to their natural order:\n * \n *  one(), two()...\n */\n$hook-\u003edoActions();\n```\n\n### Do actions with different priority\n\n```php\nuse Josantonius\\Hook\\Hook;\nuse Josantonius\\Hook\\Priority;\n\nfunction a() { /* do something */ }\nfunction b() { /* do something */ }\nfunction c() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(a(...), priority::LOW);\n$hook-\u003eaddAction(b(...), priority::NORMAL);\n$hook-\u003eaddAction(c(...), priority::HIGHEST);\n\n/**\n * Actions will be executed according to their priority:\n * \n * c(), b(), a()...\n */\n$hook-\u003edoActions();\n```\n\n### Do actions with arguments\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo($foo, $bar) { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(foo(...));\n\n$hook-\u003edoActions('foo', 'bar');\n```\n\n### Do actions recurrently\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction a() { /* do something */ }\nfunction b() { /* do something */ }\nfunction c() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(a(...));\n$hook-\u003eaddAction(b(...));\n$hook-\u003eaddActionOnce(c(...)); // Will be done only once\n\n$hook-\u003edoActions(); // a(), b(), c()\n\n$hook-\u003edoActions(); // a(), b()\n```\n\n### Do actions only once\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction one() { /* do something */ }\nfunction two() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddActionOnce(one(...));\n$hook-\u003eaddActionOnce(tho(...));\n\n$hook-\u003edoActions();\n\n// $hook-\u003edoActions(); Throw exception since there are no actions to be done\n```\n\n### Checks if the hook has actions\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(foo());\n\n$hook-\u003ehasActions(); // true\n\n$hook-\u003edoActions();\n\n$hook-\u003ehasActions(); // True since the action is recurrent and remains stored\n```\n\n### Checks if the hook has undone actions\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(foo());\n\n$hook-\u003ehasUndoneActions(); // true\n\n$hook-\u003edoActions();\n\n$hook-\u003ehasUndoneActions(); // False since there are no undone actions\n```\n\n### Checks if the actions were done at least once\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$hook-\u003eaddAction(foo());\n\n$hook-\u003ehasDoneActions(); // false\n\n$hook-\u003edoActions();\n\n$hook-\u003ehasDoneActions(); // True since the actions were done\n```\n\n### Gets hook name\n\n```php\nuse Josantonius\\Hook\\Hook;\n\n$hook = new Hook('foo');\n\n$name = $hook-\u003egetName(); // foo\n```\n\n#### Gets action priority\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$action = $hook-\u003eaddAction(foo());\n\n$action-\u003egetPriority();\n```\n\n#### Gets action callback result\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$action = $hook-\u003eaddAction(foo());\n\n$action-\u003egetResult();\n```\n\n#### Checks if the action is done once\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$action = $hook-\u003eaddAction(foo());\n\n$action-\u003eisOnce(); // false\n\n$action = $hook-\u003eaddActionOnce(foo());\n\n$action-\u003eisOnce(); // true\n```\n\n#### Checks if the action has already been done\n\n```php\nuse Josantonius\\Hook\\Hook;\n\nfunction foo() { /* do something */ }\n\n$hook = new Hook('name');\n\n$action = $hook-\u003eaddAction(foo());\n\n$action-\u003ewasDone(); // false\n\n$hook-\u003edoActions();\n\n$action-\u003ewasDone(); // true\n```\n\n## Tests\n\nTo run [tests](tests) you just need [composer](http://getcomposer.org/download/)\nand to execute the following:\n\n```console\ngit clone https://github.com/josantonius/php-hook.git\n```\n\n```console\ncd php-hook\n```\n\n```console\ncomposer install\n```\n\nRun unit tests with [PHPUnit](https://phpunit.de/):\n\n```console\ncomposer phpunit\n```\n\nRun code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):\n\n```console\ncomposer phpcs\n```\n\nRun [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:\n\n```console\ncomposer phpmd\n```\n\nRun all previous tests:\n\n```console\ncomposer tests\n```\n\n## TODO\n\n- [ ] Add new feature\n- [ ] Improve tests\n- [ ] Improve documentation\n- [ ] Improve English translation in the README file\n- [ ] Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)\n- [ ] Make Action-\u003erunCallback() accessible only to the Hook class\n- [ ] Add method to remove action?\n- [ ] Add option to add ID in actions?\n\n## Changelog\n\nDetailed changes for each release are documented in the\n[release notes](https://github.com/josantonius/php-hook/releases).\n\n## Contribution\n\nPlease make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull\nrequest, start a discussion or report a issue.\n\nThanks to all [contributors](https://github.com/josantonius/php-hook/graphs/contributors)! :heart:\n\n## Sponsor\n\nIf this project helps you to reduce your development time,\n[you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work :blush:\n\n## License\n\nThis repository is licensed under the [MIT License](LICENSE).\n\nCopyright © 2017-present, [Josantonius](https://github.com/josantonius#contact)\n","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-hook/lists"}