| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Attribute; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\LogicException; |
| 15 |
|
| 16 |
#[\Attribute(\Attribute::TARGET_METHOD)] |
| 17 |
class Interact implements InteractiveAttributeInterface |
| 18 |
{ |
| 19 |
private \ReflectionMethod $method; |
| 20 |
|
| 21 |
/** |
| 22 |
* @internal |
| 23 |
*/ |
| 24 |
public static function tryFrom(\ReflectionMethod $method): ?self |
| 25 |
{ |
| 26 |
/** @var self|null $self */ |
| 27 |
if (!$self = ($method->getAttributes(self::class)[0] ?? null)?->newInstance()) { |
| 28 |
return null; |
| 29 |
} |
| 30 |
|
| 31 |
if (!$method->isPublic() || $method->isStatic()) { |
| 32 |
throw new LogicException(\sprintf('The interactive method "%s::%s()" must be public and non-static.', $method->getDeclaringClass()->getName(), $method->getName())); |
| 33 |
} |
| 34 |
|
| 35 |
if ('__invoke' === $method->getName()) { |
| 36 |
throw new LogicException(\sprintf('The "%s::__invoke()" method cannot be used as an interactive method.', $method->getDeclaringClass()->getName())); |
| 37 |
} |
| 38 |
|
| 39 |
$self->method = $method; |
| 40 |
|
| 41 |
return $self; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @internal |
| 46 |
*/ |
| 47 |
public function getFunction(object $instance): \ReflectionFunction |
| 48 |
{ |
| 49 |
return new \ReflectionFunction($this->method->getClosure($instance)); |
| 50 |
} |
| 51 |
} |