1
0
mirror of https://github.com/flarum/core.git synced 2025-10-28 22:07:33 +01:00
Files
php-flarum/src/Foundation/ContainerUtil.php
Alexander Skvortsov e7aed89e8f Broader support for callables in ContainerUtil (#2596)
It can be very annoying if we want to use something like boolval, but have to define an entire anonymous function to pass it in. This PR adds support for tpassing it in directly as a string, like is posible with User::registerPreference.
2021-02-10 14:51:31 -05:00

37 lines
994 B
PHP

<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation;
use Illuminate\Contracts\Container\Container;
class ContainerUtil
{
/**
* Wraps a callback so that string-based invokable classes get resolved only when actually used.
*
* @internal Backwards compatability not guaranteed.
*
* @param callable|string $callback: A callable, global function, or a ::class attribute of an invokable class
* @param Container $container
*/
public static function wrapCallback($callback, Container $container)
{
if (is_string($callback) && ! is_callable($callback)) {
$callback = function (&...$args) use ($container, $callback) {
$callback = $container->make($callback);
return $callback(...$args);
};
}
return $callback;
}
}