PHPackages                             apricity/handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. apricity/handler

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

apricity/handler
================

A PHP class for parsing and executing various types of handlers, including strings in the format 'Class@method', callable functions, and arrays with class and method names.

v2.1.1(1y ago)09BSD-3-ClausePHPPHP &gt;=8.0

Since Jun 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/weroro-sk/apricity-handler)[ Packagist](https://packagist.org/packages/apricity/handler)[ RSS](/packages/apricity-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Handler
=======

[](#handler)

The `Handler` class is responsible for triggering and parsing handlers. Handlers can be strings in the format `Class@method`, `callable` functions, `array`s containing a class and method name or `array`s containing `callable`function. The class ensures the handler is valid and can be executed with the provided variables.

Installation
------------

[](#installation)

```
composer require apricity/handler
```

Table of contents
-----------------

[](#table-of-contents)

1. Usage
    - API
        - [Handler::simpleTrigger](#handlersimpletrigger)
        - [Handler::trigger](#handlertrigger)
        - [Handler::parse](#handlerparse)
2. [Tests](#run-tests)
3. [Contributing](#contributing)
4. [Changelog](CHANGELOG.md)
5. [License](#license)

---

### Handler::simpleTrigger

[](#handlersimpletrigger)

Executes a given handler with provided variables.

This method can handle multiple types of handlers: an array with a class and method, a callable, or a string. It attempts to execute the handler with the provided variables and returns the result. If the handler is invalid or execution fails, it throws a HandlerException.

```
public static function simpleTrigger(array|callable|string $handler, array $vars = []): mixed
```

**Parameters:**

- `array|callable|string $handler`: The handler to be executed.
    - **It can be**:
        - A callable/closure function.
        - A string representing a function name.
        - An array containing a single string element representing a function name.
        - An array containing a class and method.
        - An array containing a class name and method as strings.
- `array $vars`: \[optional\] The variables to pass to the handler. Default is an empty array.

**Returns:** `mixed` - The result of the handler execution.

**Throws:** `HandlerException` - If the handler is invalid or execution fails.

#### Example

[](#example)

```
namespace Apricity;

use HandlerException;

class ExampleClass
{
    public function exampleMethod($param1, $param2)
    {
        return "Example method executed with $param1 and $param2.";
    }
}

// Define a standalone function to use as a handler
function exampleFunction($param1, $param2)
{
    return "Example function executed with $param1 and $param2.";
}

try {
    $result = Handler::simpleTrigger(function($a, $b) { return $a + $b; }, [5, 3]);
    echo $result; // Outputs: 8

    $result = Handler::simpleTrigger('strtolower', ['HELLO']);
    echo $result; // Outputs: hello

    // Using callable array [Class, method]
    $result = Handler::simpleTrigger([ExampleClass::class, 'exampleMethod'], ['value1', 'value2']);
    echo $result; // Outputs: Example method executed with value1 and value2.

    // Using callable array ["function"]
    $result = Handler::simpleTrigger(['exampleFunction'], ['value1', 'value2']);
    echo $result; // Outputs: Example function executed with value1 and value2.

} catch (HandlerException $e) {
    echo 'Error: ' . $e->getMessage();
}
```

---

### Handler::trigger

[](#handlertrigger)

Triggers the execution of a given handler with provided variables.

This method first parses the handler to ensure it's in the correct format, and then executes it using the simpleTrigger method. The handler can be a string, callable, or an array. It returns the result of the handler execution.

```
public static function trigger(array|callable|string $handler, array $vars = []): mixed
```

**Parameters:**

- `array|callable|string $handler`: The handler to be executed.
    - **It can be**:
        - A string in the format "Class@method".
        - A string representing a function name.
        - An array containing a single string element representing a function name.
        - An array containing a class and method.
        - An array containing a class name and method as strings.
- `array $vars`: \[optional\] The variables to pass to the handler. Default is an empty array.

**Returns:** `mixed` - The result of the handler execution.

**Throws:** `HandlerException` - If the handler is invalid or execution fails.

#### Example

[](#example-1)

```
namespace Apricity;

use HandlerException;

class ExampleClass
{
    public function exampleMethod($param1, $param2)
    {
        return "Example method executed with $param1 and $param2.";
    }
}

// Define a standalone function to use as a handler
function exampleFunction($param1, $param2)
{
    return "Example function executed with $param1 and $param2.";
}

try {
    // Example 1: Using 'Class@method' string format
    $result1 = Handler::trigger('ExampleClass@exampleMethod', ['value1', 'value2']);
    echo $result1; // Outputs: Example method executed with value1 and value2.

    // Example 2: Using callable array [Class, method]
    $result2 = Handler::trigger([ExampleClass::class, 'exampleMethod'], ['value1', 'value2']);
    echo $result2; // Outputs: Example method executed with value1 and value2.

    // Example 3: Using callable "function"
    $result3 = Handler::trigger('exampleFunction', ['value1', 'value2']);
    echo $result3; // Outputs: Example function executed with value1 and value2.

    // Example 4: Using callable array ["function"]
    $result4 = Handler::trigger(['exampleFunction'], ['value1', 'value2']);
    echo $result4; // Outputs: Example function executed with value1 and value2.

} catch (HandlerException $e) {
    echo 'Error: ' . $e->getMessage();
}
```

---

### Handler::parse

[](#handlerparse)

Parses a given handler into a standardized array format.

This method accepts a handler in various formats and returns it as an array. It handles strings, callables, and arrays, caching the result for future use. If the handler is invalid, it throws a HandlerException.

```
public static function parse(array|callable|string $handler): array
```

**Parameters:**

- `array|callable|string $handler`: The handler to be executed.
    - **It can be**:
        - A string in the format "Class@method".
        - A string representing a function name.
        - An array containing a single string element representing a function name.
        - An array containing a class and method.
        - An array containing a class name and method as strings.

**Returns**: `array` - The parsed handler as an array.

**Throws**: `HandlerException` - If the handler is invalid or not found.

#### Example

[](#example-2)

```
namespace Apricity;

use HandlerException;

class ExampleClass
{
    public function exampleMethod()
    {
        return "Example method executed.";
    }
}

// Define a standalone function to use as a handler
function exampleFunction() {
    return "Example function executed.";
}

try {
    // Example 1: Using 'Class@method' string format
    $parsedHandler1 = Handler::parse('ExampleClass@exampleMethod');
    print_r($parsedHandler1); // Outputs: Array ( [0] => ExampleClass [1] => exampleMethod )

    // Example 2: Using callable array [Class, method]
    $parsedHandler2 = Handler::parse([ExampleClass::class, 'exampleMethod']);
    print_r($parsedHandler2); // Outputs: Array ( [0] => ExampleClass [1] => exampleMethod )

    // Example 3: Using callable function
    $parsedHandler3 = Handler::parse('exampleFunction');
    print_r($parsedHandler3); // Outputs: Array ( [0] => exampleFunction )

    // Example 4: Using callable ["function"]
    $parsedHandler4 = Handler::parse(['exampleFunction']);
    print_r($parsedHandler4); // Outputs: Array ( [0] => exampleFunction )

} catch (HandlerException $e) {
    echo 'Error: ' . $e->getMessage();
}
```

---

### Run tests

[](#run-tests)

```
composer test
```

---

Contributing
------------

[](#contributing)

We welcome contributions from the community! For guidelines on how to contribute, please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.

---

License
-------

[](#license)

This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details.

---

The repository has been migrated from GitLab.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

708d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6fb5f475e10d38a5a2de402a087594342d6be9bd42797d5d4819f33772009087?d=identicon)[weroro-sk](/maintainers/weroro-sk)

---

Top Contributors

[![weroro-sk](https://avatars.githubusercontent.com/u/29892165?v=4)](https://github.com/weroro-sk "weroro-sk (8 commits)")

---

Tags

phpdependencycallableparserclasshandlerexecutorcontrollermanagementmethodweroro

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/apricity-handler/health.svg)

```
[![Health](https://phpackages.com/badges/apricity-handler/health.svg)](https://phpackages.com/packages/apricity-handler)
```

###  Alternatives

[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[simplehtmldom/simplehtmldom

A fast, simple and reliable HTML document parser for PHP.

1921.3M14](/packages/simplehtmldom-simplehtmldom)[corveda/php-sandbox

A PHP library that can be used to run PHP code in a sandboxed environment

23483.5k2](/packages/corveda-php-sandbox)[leonelquinteros/php-toml

PHP parser for TOML language ( https://github.com/toml-lang/toml )

266.7k](/packages/leonelquinteros-php-toml)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
