├── LICENSE.md ├── Traits └── Macroable.php └── composer.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Taylor Otwell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Traits/Macroable.php: -------------------------------------------------------------------------------- 1 | getMethods( 46 | ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED 47 | ); 48 | 49 | foreach ($methods as $method) { 50 | if ($replace || ! static::hasMacro($method->name)) { 51 | static::macro($method->name, $method->invoke($mixin)); 52 | } 53 | } 54 | } 55 | 56 | /** 57 | * Checks if macro is registered. 58 | * 59 | * @param string $name 60 | * @return bool 61 | */ 62 | public static function hasMacro($name) 63 | { 64 | return isset(static::$macros[$name]); 65 | } 66 | 67 | /** 68 | * Flush the existing macros. 69 | * 70 | * @return void 71 | */ 72 | public static function flushMacros() 73 | { 74 | static::$macros = []; 75 | } 76 | 77 | /** 78 | * Dynamically handle calls to the class. 79 | * 80 | * @param string $method 81 | * @param array $parameters 82 | * @return mixed 83 | * 84 | * @throws \BadMethodCallException 85 | */ 86 | public static function __callStatic($method, $parameters) 87 | { 88 | if (! static::hasMacro($method)) { 89 | throw new BadMethodCallException(sprintf( 90 | 'Method %s::%s does not exist.', static::class, $method 91 | )); 92 | } 93 | 94 | $macro = static::$macros[$method]; 95 | 96 | if ($macro instanceof Closure) { 97 | $macro = $macro->bindTo(null, static::class); 98 | } 99 | 100 | return $macro(...$parameters); 101 | } 102 | 103 | /** 104 | * Dynamically handle calls to the class. 105 | * 106 | * @param string $method 107 | * @param array $parameters 108 | * @return mixed 109 | * 110 | * @throws \BadMethodCallException 111 | */ 112 | public function __call($method, $parameters) 113 | { 114 | if (! static::hasMacro($method)) { 115 | throw new BadMethodCallException(sprintf( 116 | 'Method %s::%s does not exist.', static::class, $method 117 | )); 118 | } 119 | 120 | $macro = static::$macros[$method]; 121 | 122 | if ($macro instanceof Closure) { 123 | $macro = $macro->bindTo($this, static::class); 124 | } 125 | 126 | return $macro(...$parameters); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "illuminate/macroable", 3 | "description": "The Illuminate Macroable package.", 4 | "license": "MIT", 5 | "homepage": "https://laravel.com", 6 | "support": { 7 | "issues": "https://github.com/laravel/framework/issues", 8 | "source": "https://github.com/laravel/framework" 9 | }, 10 | "authors": [ 11 | { 12 | "name": "Taylor Otwell", 13 | "email": "taylor@laravel.com" 14 | } 15 | ], 16 | "require": { 17 | "php": "^8.2" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Illuminate\\Support\\": "" 22 | } 23 | }, 24 | "extra": { 25 | "branch-alias": { 26 | "dev-master": "13.0.x-dev" 27 | } 28 | }, 29 | "config": { 30 | "sort-packages": true 31 | }, 32 | "minimum-stability": "dev" 33 | } 34 | --------------------------------------------------------------------------------