From d684e1e84b042291ec538eda3a0683801f7dea5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bouc=CC=8Cek?= Date: Sun, 13 Oct 2019 14:15:34 +0200 Subject: [PATCH] Allow developer to external access to plugin instance Instead create new instance by `new Loader()` use new instance container `Simpleshop::getInstance()`. It makes container to prevent re-create it twice. Developer still can force create new instance via argument $instanceId (`Simpleshop::getInstance('test')`). --- simpleshop-cz.php | 2 +- src/SimpleShop.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/SimpleShop.php diff --git a/simpleshop-cz.php b/simpleshop-cz.php index 8403ec4..4a8c95a 100644 --- a/simpleshop-cz.php +++ b/simpleshop-cz.php @@ -29,4 +29,4 @@ /** * Start plugin */ -new Plugin(); +SimpleShop::getInstance(); diff --git a/src/SimpleShop.php b/src/SimpleShop.php new file mode 100644 index 0000000..4704006 --- /dev/null +++ b/src/SimpleShop.php @@ -0,0 +1,34 @@ + + */ + +namespace Redbit\SimpleShop\WpPlugin; + +class SimpleShop { + /** + * @var Plugin + */ + private static $instance = []; + + /** + * @return Plugin + */ + public static function getInstance() { + if ( self::$instance === null ) { + self::$instance = self::factory(); + } + + return self::$instance; + } + + /** + * @return Plugin + */ + protected static function factory() { + return new Plugin(); + } +}