HasGlobalScopes.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Concerns;
  3. use Closure;
  4. use Illuminate\Database\Eloquent\Attributes\ScopedBy;
  5. use Illuminate\Database\Eloquent\Scope;
  6. use Illuminate\Support\Arr;
  7. use InvalidArgumentException;
  8. use ReflectionClass;
  9. trait HasGlobalScopes
  10. {
  11. /**
  12. * Boot the has global scopes trait for a model.
  13. *
  14. * @return void
  15. */
  16. public static function bootHasGlobalScopes()
  17. {
  18. static::addGlobalScopes(static::resolveGlobalScopeAttributes());
  19. }
  20. /**
  21. * Resolve the global scope class names from the attributes.
  22. *
  23. * @return array
  24. */
  25. public static function resolveGlobalScopeAttributes()
  26. {
  27. $reflectionClass = new ReflectionClass(static::class);
  28. return collect($reflectionClass->getAttributes(ScopedBy::class))
  29. ->map(fn ($attribute) => $attribute->getArguments())
  30. ->flatten()
  31. ->all();
  32. }
  33. /**
  34. * Register a new global scope on the model.
  35. *
  36. * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope
  37. * @param \Illuminate\Database\Eloquent\Scope|\Closure|null $implementation
  38. * @return mixed
  39. *
  40. * @throws \InvalidArgumentException
  41. */
  42. public static function addGlobalScope($scope, $implementation = null)
  43. {
  44. if (is_string($scope) && ($implementation instanceof Closure || $implementation instanceof Scope)) {
  45. return static::$globalScopes[static::class][$scope] = $implementation;
  46. } elseif ($scope instanceof Closure) {
  47. return static::$globalScopes[static::class][spl_object_hash($scope)] = $scope;
  48. } elseif ($scope instanceof Scope) {
  49. return static::$globalScopes[static::class][get_class($scope)] = $scope;
  50. } elseif (is_string($scope) && class_exists($scope) && is_subclass_of($scope, Scope::class)) {
  51. return static::$globalScopes[static::class][$scope] = new $scope;
  52. }
  53. throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope or be a class name of a class extending '.Scope::class);
  54. }
  55. /**
  56. * Register multiple global scopes on the model.
  57. *
  58. * @param array $scopes
  59. * @return void
  60. */
  61. public static function addGlobalScopes(array $scopes)
  62. {
  63. foreach ($scopes as $key => $scope) {
  64. if (is_string($key)) {
  65. static::addGlobalScope($key, $scope);
  66. } else {
  67. static::addGlobalScope($scope);
  68. }
  69. }
  70. }
  71. /**
  72. * Determine if a model has a global scope.
  73. *
  74. * @param \Illuminate\Database\Eloquent\Scope|string $scope
  75. * @return bool
  76. */
  77. public static function hasGlobalScope($scope)
  78. {
  79. return ! is_null(static::getGlobalScope($scope));
  80. }
  81. /**
  82. * Get a global scope registered with the model.
  83. *
  84. * @param \Illuminate\Database\Eloquent\Scope|string $scope
  85. * @return \Illuminate\Database\Eloquent\Scope|\Closure|null
  86. */
  87. public static function getGlobalScope($scope)
  88. {
  89. if (is_string($scope)) {
  90. return Arr::get(static::$globalScopes, static::class.'.'.$scope);
  91. }
  92. return Arr::get(
  93. static::$globalScopes, static::class.'.'.get_class($scope)
  94. );
  95. }
  96. /**
  97. * Get all of the global scopes that are currently registered.
  98. *
  99. * @return array
  100. */
  101. public static function getAllGlobalScopes()
  102. {
  103. return static::$globalScopes;
  104. }
  105. /**
  106. * Set the current global scopes.
  107. *
  108. * @param array $scopes
  109. * @return void
  110. */
  111. public static function setAllGlobalScopes($scopes)
  112. {
  113. static::$globalScopes = $scopes;
  114. }
  115. /**
  116. * Get the global scopes for this class instance.
  117. *
  118. * @return array
  119. */
  120. public function getGlobalScopes()
  121. {
  122. return Arr::get(static::$globalScopes, static::class, []);
  123. }
  124. }