| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmerge\Application; |
| 11 |
|
| 12 |
use WPEmerge\Exceptions\ConfigurationException; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provides static access to an Application instance. |
| 16 |
* |
| 17 |
* @mixin ApplicationMixin |
| 18 |
*/ |
| 19 |
trait ApplicationTrait { |
| 20 |
/** |
| 21 |
* Application instance. |
| 22 |
* |
| 23 |
* @var Application|null |
| 24 |
*/ |
| 25 |
public static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Make and assign a new application instance. |
| 29 |
* |
| 30 |
* @return Application |
| 31 |
*/ |
| 32 |
public static function make() { |
| 33 |
static::setApplication( Application::make() ); |
| 34 |
|
| 35 |
return static::getApplication(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the Application instance. |
| 40 |
* |
| 41 |
* @codeCoverageIgnore |
| 42 |
* @return Application|null |
| 43 |
*/ |
| 44 |
public static function getApplication() { |
| 45 |
return static::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set the Application instance. |
| 50 |
* |
| 51 |
* @codeCoverageIgnore |
| 52 |
* @param Application|null $application |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function setApplication( $application ) { |
| 56 |
static::$instance = $application; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Invoke any matching instance method for the static method being called. |
| 61 |
* |
| 62 |
* @param string $method |
| 63 |
* @param array $parameters |
| 64 |
* @return mixed |
| 65 |
*/ |
| 66 |
public static function __callStatic( $method, $parameters ) { |
| 67 |
$application = static::getApplication(); |
| 68 |
|
| 69 |
if ( ! $application ) { |
| 70 |
throw new ConfigurationException( |
| 71 |
'Application instance not created in ' . static::class . '. ' . |
| 72 |
'Did you miss to call ' . static::class . '::make()?' |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
return call_user_func_array( [$application, $method], $parameters ); |
| 77 |
} |
| 78 |
} |
| 79 |
|