Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Macroable.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Illuminate\Support\Traits;
  3. use Closure;
  4. use ReflectionClass;
  5. use ReflectionMethod;
  6. use BadMethodCallException;
  7. trait Macroable
  8. {
  9. /**
  10. * The registered string macros.
  11. *
  12. * @var array
  13. */
  14. protected static $macros = [];
  15. /**
  16. * Register a custom macro.
  17. *
  18. * @param string $name
  19. * @param object|callable $macro
  20. *
  21. * @return void
  22. */
  23. public static function macro($name, $macro)
  24. {
  25. static::$macros[$name] = $macro;
  26. }
  27. /**
  28. * Mix another object into the class.
  29. *
  30. * @param object $mixin
  31. * @param bool $replace
  32. * @return void
  33. *
  34. * @throws \ReflectionException
  35. */
  36. public static function mixin($mixin, $replace = true)
  37. {
  38. $methods = (new ReflectionClass($mixin))->getMethods(
  39. ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
  40. );
  41. foreach ($methods as $method) {
  42. if ($replace || ! static::hasMacro($method->name)) {
  43. $method->setAccessible(true);
  44. static::macro($method->name, $method->invoke($mixin));
  45. }
  46. }
  47. }
  48. /**
  49. * Checks if macro is registered.
  50. *
  51. * @param string $name
  52. * @return bool
  53. */
  54. public static function hasMacro($name)
  55. {
  56. return isset(static::$macros[$name]);
  57. }
  58. /**
  59. * Dynamically handle calls to the class.
  60. *
  61. * @param string $method
  62. * @param array $parameters
  63. * @return mixed
  64. *
  65. * @throws \BadMethodCallException
  66. */
  67. public static function __callStatic($method, $parameters)
  68. {
  69. if (! static::hasMacro($method)) {
  70. throw new BadMethodCallException(sprintf(
  71. 'Method %s::%s does not exist.', static::class, $method
  72. ));
  73. }
  74. if (static::$macros[$method] instanceof Closure) {
  75. return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
  76. }
  77. return call_user_func_array(static::$macros[$method], $parameters);
  78. }
  79. /**
  80. * Dynamically handle calls to the class.
  81. *
  82. * @param string $method
  83. * @param array $parameters
  84. * @return mixed
  85. *
  86. * @throws \BadMethodCallException
  87. */
  88. public function __call($method, $parameters)
  89. {
  90. if (! static::hasMacro($method)) {
  91. throw new BadMethodCallException(sprintf(
  92. 'Method %s::%s does not exist.', static::class, $method
  93. ));
  94. }
  95. $macro = static::$macros[$method];
  96. if ($macro instanceof Closure) {
  97. return call_user_func_array($macro->bindTo($this, static::class), $parameters);
  98. }
  99. return call_user_func_array($macro, $parameters);
  100. }
  101. }