2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-30 01:29:21 +02:00
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
2016-12-29 21:09:49 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-08-30 01:29:21 +02:00
|
|
|
if (\PHP_VERSION_ID < 70100) {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress DuplicateClass */
|
2018-06-18 20:00:01 +02:00
|
|
|
trait CallableMaker
|
|
|
|
{
|
2016-08-30 01:29:21 +02:00
|
|
|
/** @var \ReflectionClass */
|
|
|
|
private static $__reflectionClass;
|
2017-03-10 21:58:46 +01:00
|
|
|
|
2016-08-30 01:29:21 +02:00
|
|
|
/** @var \ReflectionMethod[] */
|
|
|
|
private static $__reflectionMethods = [];
|
2017-03-10 21:58:46 +01:00
|
|
|
|
2016-08-30 01:29:21 +02:00
|
|
|
/**
|
2017-04-23 15:29:08 +02:00
|
|
|
* Creates a callable from a protected or private instance method that may be invoked by callers requiring a
|
2016-08-30 01:29:21 +02:00
|
|
|
* publicly invokable callback.
|
|
|
|
*
|
|
|
|
* @param string $method Instance method name.
|
|
|
|
*
|
|
|
|
* @return callable
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
2016-08-30 01:29:21 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private function callableFromInstanceMethod(string $method): callable
|
|
|
|
{
|
2016-08-30 01:29:21 +02:00
|
|
|
if (!isset(self::$__reflectionMethods[$method])) {
|
|
|
|
if (self::$__reflectionClass === null) {
|
|
|
|
self::$__reflectionClass = new \ReflectionClass(self::class);
|
|
|
|
}
|
|
|
|
self::$__reflectionMethods[$method] = self::$__reflectionClass->getMethod($method);
|
|
|
|
}
|
2017-03-10 21:58:46 +01:00
|
|
|
|
2016-08-30 01:29:21 +02:00
|
|
|
return self::$__reflectionMethods[$method]->getClosure($this);
|
|
|
|
}
|
2017-03-10 21:58:46 +01:00
|
|
|
|
2016-08-30 01:29:21 +02:00
|
|
|
/**
|
|
|
|
* Creates a callable from a protected or private static method that may be invoked by methods requiring a
|
|
|
|
* publicly invokable callback.
|
|
|
|
*
|
|
|
|
* @param string $method Static method name.
|
|
|
|
*
|
|
|
|
* @return callable
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @psalm-suppress MixedInferredReturnType
|
2016-08-30 01:29:21 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private static function callableFromStaticMethod(string $method): callable
|
|
|
|
{
|
2016-08-30 01:29:21 +02:00
|
|
|
if (!isset(self::$__reflectionMethods[$method])) {
|
|
|
|
if (self::$__reflectionClass === null) {
|
|
|
|
self::$__reflectionClass = new \ReflectionClass(self::class);
|
|
|
|
}
|
|
|
|
self::$__reflectionMethods[$method] = self::$__reflectionClass->getMethod($method);
|
|
|
|
}
|
2017-03-10 21:58:46 +01:00
|
|
|
|
2016-08-30 01:29:21 +02:00
|
|
|
return self::$__reflectionMethods[$method]->getClosure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress DuplicateClass */
|
2018-06-18 20:00:01 +02:00
|
|
|
trait CallableMaker
|
|
|
|
{
|
2016-08-30 01:29:21 +02:00
|
|
|
/**
|
2016-08-30 17:22:10 +02:00
|
|
|
* @deprecated Use \Closure::fromCallable() instead of this method in PHP 7.1.
|
2016-08-30 01:29:21 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private function callableFromInstanceMethod(string $method): callable
|
|
|
|
{
|
2016-08-30 01:29:21 +02:00
|
|
|
return \Closure::fromCallable([$this, $method]);
|
|
|
|
}
|
2017-03-10 21:58:46 +01:00
|
|
|
|
2016-08-30 01:29:21 +02:00
|
|
|
/**
|
2016-08-30 17:22:10 +02:00
|
|
|
* @deprecated Use \Closure::fromCallable() instead of this method in PHP 7.1.
|
2016-08-30 01:29:21 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
private static function callableFromStaticMethod(string $method): callable
|
|
|
|
{
|
2016-08-30 01:29:21 +02:00
|
|
|
return \Closure::fromCallable([self::class, $method]);
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 15:39:08 +02:00
|
|
|
} // @codeCoverageIgnoreEnd
|