Context.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Illuminate\Container\Attributes;
  3. use Attribute;
  4. use Illuminate\Contracts\Container\Container;
  5. use Illuminate\Contracts\Container\ContextualAttribute;
  6. use Illuminate\Log\Context\Repository;
  7. #[Attribute(Attribute::TARGET_PARAMETER)]
  8. class Context implements ContextualAttribute
  9. {
  10. /**
  11. * Create a new attribute instance.
  12. */
  13. public function __construct(public string $key, public mixed $default = null, public bool $hidden = false)
  14. {
  15. }
  16. /**
  17. * Resolve the context value.
  18. *
  19. * @param self $attribute
  20. * @param \Illuminate\Contracts\Container\Container $container
  21. * @return mixed
  22. */
  23. public static function resolve(self $attribute, Container $container): mixed
  24. {
  25. $repository = $container->make(Repository::class);
  26. return match ($attribute->hidden) {
  27. true => $repository->getHidden($attribute->key, $attribute->default),
  28. false => $repository->get($attribute->key, $attribute->default),
  29. };
  30. }
  31. }