Collections
2 weeks ago
Concerns
2 weeks ago
Console
2 weeks ago
Constants
2 weeks ago
Contracts
2 weeks ago
Database
2 weeks ago
Discovery
2 weeks ago
Exceptions
2 weeks ago
Filesystem
2 weeks ago
Http
2 weeks ago
Managers
2 weeks ago
Middlewares
2 weeks ago
Polyfill
2 weeks ago
Supports
2 weeks ago
Validation
2 weeks ago
Wordpress
2 weeks ago
ApiExceptionHandler.php
2 weeks ago
Application.php
2 weeks ago
Container.php
2 weeks ago
CoreServiceProvider.php
2 weeks ago
DTO.php
2 weeks ago
Facade.php
2 weeks ago
Listener.php
2 weeks ago
Resource.php
2 weeks ago
Route.php
2 weeks ago
Sanitizer.php
2 weeks ago
ServiceProvider.php
2 weeks ago
helpers.php
2 weeks ago
Facade.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Abstract base for static facades that proxy calls to container-resolved services. |
| 5 | * Caches resolved instances per accessor and forwards __callStatic to the underlying object. |
| 6 | * Enables Facade::method() syntax throughout the framework. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | namespace Kirki\Framework; |
| 12 | |
| 13 | \defined('ABSPATH') || exit; |
| 14 | use RuntimeException; |
| 15 | use function Kirki\Framework\app; |
| 16 | abstract class Facade |
| 17 | { |
| 18 | /** |
| 19 | * The array of resolved service instances keyed by accessor name. |
| 20 | * |
| 21 | * @var array |
| 22 | * |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | protected static $resolved_instance = []; |
| 26 | /** |
| 27 | * Whether the facade is cacheable. |
| 28 | * |
| 29 | * @var bool |
| 30 | * |
| 31 | * @since 1.0.0 |
| 32 | */ |
| 33 | protected static $is_cacheable = \true; |
| 34 | /** |
| 35 | * Get the accessor key for the service in the container. |
| 36 | * |
| 37 | * Child classes must implement this method to return the string accessor |
| 38 | * used to resolve the service from the application container. |
| 39 | * |
| 40 | * @return string |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | */ |
| 44 | public static abstract function get_accessor(); |
| 45 | /** |
| 46 | * Resolve the underlying service instance for the given accessor name. |
| 47 | * |
| 48 | * If the name is already an object, it is returned directly. Otherwise, |
| 49 | * the service is resolved from the application container and cached. |
| 50 | * |
| 51 | * @param string|object $name The accessor name or an object instance |
| 52 | * |
| 53 | * @return object The resolved service instance |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | protected static function resolved_facade_instance($name) |
| 58 | { |
| 59 | if (\is_object($name)) { |
| 60 | return $name; |
| 61 | } |
| 62 | if (static::$is_cacheable && isset(static::$resolved_instance[$name])) { |
| 63 | return static::$resolved_instance[$name]; |
| 64 | } |
| 65 | $instance = app($name); |
| 66 | if (static::$is_cacheable) { |
| 67 | static::$resolved_instance[$name] = $instance; |
| 68 | } |
| 69 | return $instance; |
| 70 | } |
| 71 | /** |
| 72 | * Dynamically handle static method calls to the facade. |
| 73 | * |
| 74 | * Forwards the static call to the resolved service instance, passing all arguments. |
| 75 | * Throws a RuntimeException if the service instance cannot be resolved. |
| 76 | * |
| 77 | * @param string $method The method name being called |
| 78 | * @param array $arguments The arguments passed to the method |
| 79 | * |
| 80 | * @return mixed The result of the underlying method call |
| 81 | * |
| 82 | * @throws \RuntimeException |
| 83 | * |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public static function __callStatic($method, $arguments) |
| 87 | { |
| 88 | $instance = static::resolved_facade_instance(static::get_accessor()); |
| 89 | if (!$instance) { |
| 90 | throw new RuntimeException('A facade has not been set.'); |
| 91 | } |
| 92 | return $instance->{$method}(...$arguments); |
| 93 | } |
| 94 | } |
| 95 |