From ba58ff06983e4eb4753fdcec8dd1a146338d27fb Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 18 Feb 2018 11:49:40 -0800 Subject: [PATCH] Trait for forthcoming singletons --- camo-image-proxy.php | 9 ++++++++- inc/trait-singleton.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 inc/trait-singleton.php diff --git a/camo-image-proxy.php b/camo-image-proxy.php index 3d1cb47..d9f8865 100755 --- a/camo-image-proxy.php +++ b/camo-image-proxy.php @@ -12,4 +12,11 @@ * @package Camo_Image_Proxy */ -// Your code starts here. +namespace Camo_Image_Proxy; + +define( __NAMESPACE__ . '\PLUGIN_PATH', dirname( __FILE__ ) ); + +/** + * Trait for singletons + */ +require_once PLUGIN_PATH . '/inc/trait-singleton.php'; diff --git a/inc/trait-singleton.php b/inc/trait-singleton.php new file mode 100644 index 0000000..b890d8d --- /dev/null +++ b/inc/trait-singleton.php @@ -0,0 +1,40 @@ +<?php +/** + * Trait file for Singletons. + * + * @package Camo_Image_Proxy + */ + +namespace Camo_Image_Proxy; + +/** + * Make a class into a singleton. + */ +trait Singleton { + /** + * Existing instance. + * + * @var object + */ + protected static $instance; + + /** + * Get class instance. + * + * @return object + */ + public static function instance() { + if ( ! isset( static::$instance ) ) { + static::$instance = new static(); + static::$instance->setup(); + } + return static::$instance; + } + + /** + * Setup the singleton. + */ + public function setup() { + // Silence. + } +} -- GitLab