HandlerDescriptor.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\Handler;
  11. /**
  12. * Describes a handler and the possible associated options, such as `from_transport`, `bus`, etc.
  13. *
  14. * @author Samuel Roze <samuel.roze@gmail.com>
  15. */
  16. final class HandlerDescriptor
  17. {
  18. private $handler;
  19. private $options;
  20. public function __construct(callable $handler, array $options = [])
  21. {
  22. $this->handler = $handler;
  23. $this->options = $options;
  24. }
  25. public function getHandler(): callable
  26. {
  27. return $this->handler;
  28. }
  29. public function getName(): string
  30. {
  31. $name = $this->callableName($this->handler);
  32. $alias = $this->options['alias'] ?? null;
  33. if (null !== $alias) {
  34. $name .= '@'.$alias;
  35. }
  36. return $name;
  37. }
  38. public function getOption(string $option)
  39. {
  40. return $this->options[$option] ?? null;
  41. }
  42. private function callableName(callable $handler): string
  43. {
  44. if (\is_array($handler)) {
  45. if (\is_object($handler[0])) {
  46. return \get_class($handler[0]).'::'.$handler[1];
  47. }
  48. return $handler[0].'::'.$handler[1];
  49. }
  50. if (\is_string($handler)) {
  51. return $handler;
  52. }
  53. if ($handler instanceof \Closure) {
  54. $r = new \ReflectionFunction($handler);
  55. if (false !== strpos($r->name, '{closure}')) {
  56. return 'Closure';
  57. }
  58. if ($class = $r->getClosureScopeClass()) {
  59. return $class->name.'::'.$r->name;
  60. }
  61. return $r->name;
  62. }
  63. return \get_class($handler).'::__invoke';
  64. }
  65. }