{"id":18656241,"url":"https://github.com/bayfrontmedia/php-hooks","last_synced_at":"2025-04-11T17:32:09.348Z","repository":{"id":62492261,"uuid":"284725853","full_name":"bayfrontmedia/php-hooks","owner":"bayfrontmedia","description":"An easy to use hooks library for managing events and filters.","archived":false,"fork":false,"pushed_at":"2024-12-23T20:11:39.000Z","size":23,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T16:51:30.302Z","etag":null,"topics":["action","event","filter","hooks","php"],"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/bayfrontmedia.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null}},"created_at":"2020-08-03T14:43:21.000Z","updated_at":"2025-03-11T20:20:05.000Z","dependencies_parsed_at":"2023-02-15T13:50:34.109Z","dependency_job_id":null,"html_url":"https://github.com/bayfrontmedia/php-hooks","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/bayfrontmedia%2Fphp-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bayfrontmedia%2Fphp-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bayfrontmedia","download_url":"https://codeload.github.com/bayfrontmedia/php-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248449896,"owners_count":21105582,"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":["action","event","filter","hooks","php"],"created_at":"2024-11-07T07:22:29.489Z","updated_at":"2025-04-11T17:32:09.334Z","avatar_url":"https://github.com/bayfrontmedia.png","language":"PHP","readme":"## PHP hooks\n\nAn easy to use hooks library for managing events and filters.\n\n- [License](#license)\n- [Author](#author)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Usage](#usage)\n\n## License\n\nThis project is open source and available under the [MIT License](LICENSE).\n\n## Author\n\n\u003cimg src=\"https://cdn1.onbayfront.com/bfm/brand/bfm-logo.svg\" alt=\"Bayfront Media\" width=\"250\" /\u003e\n\n- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github\u0026amp;utm_medium=direct)\n- [Bayfront Media GitHub](https://github.com/bayfrontmedia)\n\n## Requirements\n\n* PHP `^8.0` (Tested up to `8.4`)\n\n## Installation\n\n```\ncomposer require bayfrontmedia/php-hooks\n```\n\n## Usage\n\n### Start using hooks\n\n```\nuse Bayfront\\Hooks\\Hooks;\n\n$hooks = new Hooks();\n```\n\n### Public methods\n\n**Events**\n\n- [addEvent](#addevent)\n- [hasEvent](#hasevent)\n- [getEvents](#getevents)\n- [removeEvent](#removeevent)\n- [removeEvents](#removeevents)\n- [doEvent](#doevent)\n\n**Filters**\n\n- [addFilter](#addfilter)\n- [hasFilter](#hasfilter)\n- [getFilters](#getfilters)\n- [removeFilter](#removefilter)\n- [removeFilters](#removefilters)\n- [doFilter](#dofilter)\n\n\u003chr /\u003e\n\n### addEvent\n\n**Description:**\n\nAdds a hook for a given event name. \n\n**NOTE:** Anonymous functions are unable to be removed with `removeEvent()`, so use them carefully.\n\n**Parameters:**\n\n- `$name` (string): Name of event\n- `$function` (callable)\n- `$priority = 10` (int): Hooks will be executed by order of priority in ascending order\n(lower numbers = earlier execution)\n\nReserved names:\n\n- `always`: These hooks will always be executed whenever `doEvent()` is called, regardless of the name.\n- `destruct`: These hooks will be executed when the script terminates.\n\n**Returns:**\n\n- (void)\n\n**Examples:**\n\nAnonymous function\n\n```\n$hooks-\u003eaddEvent('name', function($name) {\n\n    echo 'My name is ' . $name;\n\n});\n```\n\nNamed function\n\n```\nfunction my_name($name) {\n\n    echo 'My name is ' . $name;\n\n}\n\n$hooks-\u003eaddEvent('name', 'my_name');\n```\n\nInside class scope\n\n```\nuse Bayfront\\Hooks\\Hooks;\n\nclass MyClass {\n\n    protected $hooks;\n\n    public function __construct(Hooks $hooks) {\n\n        $this-\u003ehooks = $hooks;\n\n        $this-\u003ehooks-\u003eaddEvent('name', [$this, 'my_name']);\n\n    }\n\n    public function my_name($name) {\n\n        echo 'My name is ' . $name;\n\n    }\n}\n\n$my_class = new MyClass($hooks);\n```\n\nUse variables from outside scope\n\n```\n$prefix = 'My name is ';\n\n$hooks-\u003eaddEvent('name', function($name) use ($prefix) {\n\n    echo $prefix . $name;\n\n});\n```\n\n\u003chr /\u003e\n\n### hasEvent\n\n**Description:**\n\nChecks if any events exist for a given name. \n\n**Parameters:**\n\n- `$name` (string): Name of event\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\nif ($hooks-\u003ehasEvent('name')) {\n    // Do something\n}\n```\n\n\u003chr /\u003e\n\n### getEvents\n\n**Description:**\n\nReturn array of all hooks for all events, or of a given event name. \n\n**Parameters:**\n\n- `$name = NULL` (string|null): Name of event\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\nprint_r($hooks-\u003egetEvents()); // Returns all hooks for all events\n\nprint_r($hooks-\u003egetEvents('name')); // Returns all hooks for \"name\" event\n```\n\n\u003chr /\u003e\n\n### removeEvent\n\n**Description:**\n\nRemoves hook from a given event, if existing. \n\n**NOTE:** Hooks using anonymous functions cannot be removed using this method.\n\n**Parameters:**\n\n- `$name` (string): Name of event\n- `$function` (callable): Hook to remove\n\n**Returns:**\n\n- (bool): Returns `true` if the hook existed\n\n**Example:**\n\n```\n$hooks-\u003eremoveEvent('name', 'my_name');\n```\n\nTo remove a hook for a function from within a class scope, the `$function` parameter must be an array whose first value is an instance of the class, and second value is the name of the function within the class:\n\n```\n$hooks-\u003eremoveEvent('name', [$my_class, 'my_name']);\n```\n\u003chr /\u003e\n\n### removeEvents\n\n**Description:**\n\nRemoves all hooks from a given event, if existing. \n\n**Parameters:**\n\n- `$name` (string): Name of event\n\n**Returns:**\n\n- (bool): Returns `true` if the hook existed\n\n**Example:**\n\n```\n$hooks-\u003eremoveEvents('name');\n```\n\n\u003chr /\u003e\n\n### doEvent\n\n**Description:**\n\nExecute queued hooks for a given event in order of priority. \n\n**Parameters:**\n\n- `$name` (string): Name of event\n- `...$arg` (mixed): Optional additional argument(s) to be passed to the functions hooked to the event\n\n**Returns:**\n\n- (void)\n\n**Example:**\n\n```\n$hooks-\u003edoEvent('name', 'John');\n```\n\n\u003chr /\u003e\n\n### addFilter\n\n**Description:**\n\nAdds a hook for a given filter name. \n\n**Parameters:**\n\n- `$name` (string): Name of filter\n- `$function` (callable)\n- `$priority = 10` (int): Filters will be executed in order of priority in ascending order \n(lower numbers = earlier execution)\n\n**Returns:**\n\n- (void)\n\n**Examples:**\n\nAnonymous function\n\n```\n$hooks-\u003eaddFilter('name', function($name) {\n\n    return strtoupper($name);\n\n});\n```\n\nNamed function\n\n```\nfunction uppercase($name) {\n\n    return strtoupper($name);\n\n}\n\n$hooks-\u003eaddFilter('name', 'uppercase');\n```\n\nInside class scope\n\n```\nuse Bayfront\\Hooks\\Hooks;\n\nclass MyClass {\n\n    protected $hooks;\n\n    public function __construct(Hooks $hooks) {\n\n        $this-\u003ehooks = $hooks;\n\n        $this-\u003ehooks-\u003eaddFilter('name', [$this, 'uppercase']);\n\n    }\n\n    public function uppercase($name) {\n\n        return strtoupper($name);\n\n    }\n}\n\n$my_class = new MyClass($hooks);\n```\n\nUse variables from outside scope\n\n```\n$prefix = 'My name is ';\n\n$hooks-\u003eaddFilter('name', function($name) use ($prefix) {\n\n    return strtoupper($prefix . $name);\n\n});\n```\n\n\u003chr /\u003e\n\n### hasFilter\n\n**Description:**\n\nChecks if any filters exist for a given name. \n\n**Parameters:**\n\n- `$name` (string): Name of filter\n\n**Returns:**\n\n- (bool)\n\n**Example:**\n\n```\nif ($hooks-\u003ehasFilter('name')) {\n    // Do something\n}\n```\n\n\u003chr /\u003e\n\n### getFilters\n\n**Description:**\n\nReturn array of all hooks for all filters, or of a given filter name.\n\n**Parameters:**\n\n- `$name = NULL` (string|null): Name of filter\n\n**Returns:**\n\n- (array)\n\n**Example:**\n\n```\nprint_r($hooks-\u003egetFilters()); // Returns all hooks for all filters\n\nprint_r($hooks-\u003egetFilters('name')); // Returns all hooks for \"name\" filter\n```\n\n\u003chr /\u003e\n\n### removeFilter\n\n**Description:**\n\nRemoves hook from a given filter, if existing. \n\n**NOTE:** Hooks using anonymous functions cannot be removed using this method\n\n**Parameters:**\n\n- `$name` (string): Name of filter\n- `$function` (callable): Hook to remove\n\n**Returns:**\n\n- (bool): Returns `true` if the hook existed\n\n**Example:**\n\n```\n$hooks-\u003eremoveFilter('name', 'uppercase');\n```\n\nTo remove a hook for a function from within a class scope, the `$function` parameter must be an array whose first value is an instance of the class, and second value is the name of the function within the class:\n\n```\n$hooks-\u003eremoveFilter('name', [$my_class, 'uppercase']);\n```\n\n\u003chr /\u003e\n\n### removeFilters\n\n**Description:**\n\nRemoves all hooks from a given filter, if existing.\n\n**Parameters:**\n\n- `$name` (string): Name of filter\n\n**Returns:**\n\n- (bool): Returns `true` if the hook existed\n\n**Example:**\n\n```\n$hooks-\u003eremoveFilters('name');\n```\n\n\u003chr /\u003e\n\n### doFilter\n\n**Description:**\n\nFilters value through queued filters in order of priority.\n\n**Parameters:**\n\n- `$name` (string): Name of filter\n- `$value` (mixed): Original value to be filtered\n\n**Returns:**\n\n- (mixed): Filtered value\n\n**Example:**\n\n```\necho $hooks-\u003edoFilter('name', 'John');\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbayfrontmedia%2Fphp-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbayfrontmedia%2Fphp-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbayfrontmedia%2Fphp-hooks/lists"}