Skip to content

Helpers

This component provides commonly used helper functions for Hyperf.

Installation

shell
composer require friendsofhyperf/helpers

Configuration and Autoloading

The package autoloads its function files through Composer. Its configuration provider does not register or publish any configuration, so no additional setup is required.

Functions are defined in the FriendsOfHyperf\Helpers namespace, except for call, which is defined in FriendsOfHyperf\Helpers\Command.

Function Reference

FunctionSignature and behavior
appapp(null|string|callable $abstract = null, array $parameters = []): resolves from the container; converts a callable to a Closure.
base_pathbase_path(string $path = ''): string: returns BASE_PATH, optionally with a path appended.
blank / filledDetermine whether a value is blank or not blank. Models, numbers, and booleans are not blank.
cachecache(...$arguments): returns the cache service with no arguments, gets a string key, or sets the first key/value pair from an array.
cookieCreates a Cookie; with no name, returns the CookieJarInterface service. A non-zero lifetime is specified in minutes.
class_namespaceclass_namespace(object|string $class): string: returns the class namespace.
didi(?string $abstract = null, array $parameters = []): resolves or makes a service. Without a container, it instantiates the class directly; calling it without an abstract then throws.
enum_valueReturns a backed enum's value, a unit enum's name, or the original value. Blank non-string values use the optional default.
eventevent(object $event): dispatches an event and returns the dispatcher's result.
fluentfluent(object|array $value): Fluent: creates a Fluent object.
get_client_ipReturns the x-real-ip header, falling back to the request's remote_addr.
infoinfo(string|Stringable $message, array $context = [], bool $backtrace = false): writes an info log; optionally adds a backtrace context value.
literalReturns the sole positional argument unchanged, or creates an object from named arguments.
loggerWith no message, returns the default logger; otherwise writes a debug log and can include a backtrace.
logslogs(string $name = 'hyperf', ?string $channel = null): LoggerInterface: gets a logger from LoggerFactory.
microseconds / milliseconds / months / weeksCreate a CarbonInterval for the given unit.
object_getGets a nested object property with dot notation; returns the object for an empty key and evaluates the default when missing.
preg_replace_arrayReplaces each regex match sequentially with values from the replacements array.
requestWith no key, returns the request; accepts a string key or an array of keys and an optional default.
resolveresolve(string|callable $abstract, array $parameters = []): resolves from di, or converts a callable to a Closure.
responseWith no arguments, returns the response service; otherwise creates a response with string or JSON-array content and a status code, and accepts a headers array.
rescueRuns a callback and returns a fallback after any Throwable; an optional exception handler receives the throwable.
sessionWith no key, returns the session; an array stores values, and a string key retrieves a value.
throw_if / throw_unlessThrow an exception instance, exception class, or RuntimeException message according to the condition; otherwise return the condition.
transformRuns the callback only for a filled value; otherwise returns or evaluates the default.
validatorWith no arguments, returns the validator factory; otherwise creates a validator from data, rules, messages, and custom attributes.
whenReturns the selected value or default according to the evaluated expression; evaluates the selected value when it is a Closure.
Command\callcall(string $command, array $arguments = []): int: runs a console command with NullOutput and returns its exit code.

Optional Dependencies

Install optional Hyperf packages only for the helpers or integrations your application uses. The package suggests compatible ~3.2.0 versions of:

PackageRelevant usage
hyperf/cachecache
hyperf/diContainer-backed resolution
hyperf/frameworkRuntime service bindings and Command\call
hyperf/loggerinfo, logger, and logs
hyperf/sessionsession
hyperf/validationvalidator
hyperf/amqp, hyperf/async-queue, hyperf/kafkaSuggested by the package metadata for their corresponding integrations; no function in this component directly references them.

Examples

Import namespaced functions before using them:

php
use function FriendsOfHyperf\Helpers\blank;
use function FriendsOfHyperf\Helpers\literal;
use function FriendsOfHyperf\Helpers\object_get;
use function FriendsOfHyperf\Helpers\transform;

$profile = literal(name: 'Taylor', contact: (object) ['email' => 'taylor@example.com']);

object_get($profile, 'contact.email'); // taylor@example.com
blank('  '); // true
transform(5, fn (int $value) => $value * 2); // 10

The cache helper selects its behavior from its arguments:

php
use function FriendsOfHyperf\Helpers\cache;

$cache = cache();
$value = cache('key', 'default');
cache(['key' => 'value'], 60);

Console commands use a separate namespace:

php
use function FriendsOfHyperf\Helpers\Command\call;

$exitCode = call('foo:bar', ['argument' => 'value']);