| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Support; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use RuntimeException; |
| 7 |
|
| 8 |
abstract class Facade |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The application instance being facaded. |
| 12 |
* |
| 13 |
* @var \FluentCommunity\Framework\Foundation\Application |
| 14 |
*/ |
| 15 |
protected static $app; |
| 16 |
|
| 17 |
/** |
| 18 |
* The resolved object instances. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected static $resolvedInstance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Run a Closure when the facade has been resolved. |
| 26 |
* |
| 27 |
* @param \Closure $callback |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public static function resolved(Closure $callback) |
| 31 |
{ |
| 32 |
$accessor = static::getFacadeAccessor(); |
| 33 |
|
| 34 |
if (static::$app->resolved($accessor) === true) { |
| 35 |
$callback(static::getFacadeRoot()); |
| 36 |
} |
| 37 |
|
| 38 |
static::$app->afterResolving($accessor, function ($service) use ($callback) { |
| 39 |
$callback($service); |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the root object behind the facade. |
| 45 |
* |
| 46 |
* @return mixed |
| 47 |
*/ |
| 48 |
public static function getFacadeRoot() |
| 49 |
{ |
| 50 |
return static::resolveFacadeInstance(static::getFacadeAccessor()); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get the registered name of the component. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
* |
| 58 |
* @throws \RuntimeException |
| 59 |
*/ |
| 60 |
protected static function getFacadeAccessor() |
| 61 |
{ |
| 62 |
throw new RuntimeException( |
| 63 |
'Facade does not implement getFacadeAccessor method.' |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Resolve the facade root instance from the container. |
| 69 |
* |
| 70 |
* @param object|string $name |
| 71 |
* @return mixed |
| 72 |
*/ |
| 73 |
protected static function resolveFacadeInstance($name) |
| 74 |
{ |
| 75 |
if (is_object($name)) { |
| 76 |
return $name; |
| 77 |
} |
| 78 |
|
| 79 |
if (isset(static::$resolvedInstance[$name])) { |
| 80 |
return static::$resolvedInstance[$name]; |
| 81 |
} |
| 82 |
|
| 83 |
if (static::$app) { |
| 84 |
return static::$resolvedInstance[$name] = static::$app[$name]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Clear a resolved facade instance. |
| 90 |
* |
| 91 |
* @param string $name |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public static function clearResolvedInstance($name) |
| 95 |
{ |
| 96 |
unset(static::$resolvedInstance[$name]); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Clear all of the resolved instances. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public static function clearResolvedInstances() |
| 105 |
{ |
| 106 |
static::$resolvedInstance = []; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get the application instance behind the facade. |
| 111 |
* |
| 112 |
* @return \FluentCommunity\Framework\Foundation\Application |
| 113 |
*/ |
| 114 |
public static function getFacadeApplication() |
| 115 |
{ |
| 116 |
return static::$app; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Set the application instance. |
| 121 |
* |
| 122 |
* @param \FluentCommunity\Framework\Foundation\Application $app |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public static function setFacadeApplication($app) |
| 126 |
{ |
| 127 |
static::$app = $app; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Handle dynamic, static calls to the object. |
| 132 |
* |
| 133 |
* @param string $method |
| 134 |
* @param array $args |
| 135 |
* @return mixed |
| 136 |
* |
| 137 |
* @throws \RuntimeException |
| 138 |
*/ |
| 139 |
public static function __callStatic($method, $args) |
| 140 |
{ |
| 141 |
$instance = static::getFacadeRoot(); |
| 142 |
|
| 143 |
if (! $instance) { |
| 144 |
throw new RuntimeException('A facade root has not been set.'); |
| 145 |
} |
| 146 |
|
| 147 |
return $instance->$method(...$args); |
| 148 |
} |
| 149 |
} |
| 150 |
|