SubscribedService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Contracts\Service\Attribute;
  11. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  12. use Symfony\Contracts\Service\ServiceSubscriberTrait;
  13. /**
  14. * For use as the return value for {@see ServiceSubscriberInterface}.
  15. *
  16. * @example new SubscribedService('http_client', HttpClientInterface::class, false, new Target('githubApi'))
  17. *
  18. * Use with {@see ServiceSubscriberTrait} to mark a method's return type
  19. * as a subscribed service.
  20. *
  21. * @author Kevin Bond <kevinbond@gmail.com>
  22. */
  23. #[\Attribute(\Attribute::TARGET_METHOD)]
  24. final class SubscribedService
  25. {
  26. /** @var object[] */
  27. public $attributes;
  28. /**
  29. * @var string|null
  30. */
  31. public $key;
  32. /**
  33. * @var class-string|null
  34. */
  35. public $type;
  36. /**
  37. * @var bool
  38. */
  39. public $nullable = false;
  40. /**
  41. * @param string|null $key The key to use for the service
  42. * @param class-string|null $type The service class
  43. * @param bool $nullable Whether the service is optional
  44. * @param object|object[] $attributes One or more dependency injection attributes to use
  45. */
  46. public function __construct($key = null, $type = null, $nullable = false, $attributes = [])
  47. {
  48. $this->key = $key;
  49. $this->type = $type;
  50. $this->nullable = $nullable;
  51. $this->attributes = \is_array($attributes) ? $attributes : [$attributes];
  52. }
  53. }