ActionScheduler
2 years ago
DateTime
3 years ago
Scripts
2 years ago
CurrencyFacade.php
3 years ago
Facade.php
4 years ago
Str.php
3 years ago
Facade.php
55 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\Facades; |
| 4 | |
| 5 | /** |
| 6 | * Class Facade |
| 7 | * |
| 8 | * This class provides a way of taking a normal instance class and creating a static facade out of it. It does it in |
| 9 | * such a way, though, that the facade is still mockable. It does this by instantiating the decorated class through the |
| 10 | * Give Service Container (SC). So by injecting a mock singleton of the decorated class in the SC, it can be mocked. |
| 11 | * |
| 12 | * To use this, simply make a new facade class which extends this once, then implement the getFacadeClass and return the |
| 13 | * class to be decorated, for example: return MyClass::class; |
| 14 | * |
| 15 | * To help the IDE, take the methods from the decorated class and add them your class docblock. So if Repository had an |
| 16 | * insert method, you would add "@method static Model insert()" to the top. |
| 17 | * |
| 18 | * @since 2.12.0 |
| 19 | */ |
| 20 | abstract class Facade |
| 21 | { |
| 22 | /** |
| 23 | * Static helper for calling the facade methods |
| 24 | * |
| 25 | * @since 2.12.0 |
| 26 | * |
| 27 | * @param string $name |
| 28 | * @param array $arguments |
| 29 | * |
| 30 | * @return mixed |
| 31 | */ |
| 32 | public static function __callStatic($name, $arguments) |
| 33 | { |
| 34 | // Make sure the static class is a singleton and get instance |
| 35 | give()->singletonIf(static::class); |
| 36 | $staticInstance = give(static::class); |
| 37 | |
| 38 | // Make sure the accessed class is a singleton and get instance |
| 39 | $accessorClass = $staticInstance->getFacadeAccessor(); |
| 40 | give()->singletonIf($accessorClass); |
| 41 | $accessorInstance = give($accessorClass); |
| 42 | |
| 43 | return $accessorInstance->$name(...$arguments); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Retrieves the fully qualified class name or alias for the class being decorated |
| 48 | * |
| 49 | * @since 2.12.0 |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | abstract protected function getFacadeAccessor(); |
| 54 | } |
| 55 |