| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils; |
| 4 |
|
| 5 |
use Templately\Core\Module; |
| 6 |
use Templately\Core\Platform; |
| 7 |
|
| 8 |
abstract class Base { |
| 9 |
/** |
| 10 |
* Holds the plugin instance. |
| 11 |
* |
| 12 |
* @since 2.0.0 |
| 13 |
* @access protected |
| 14 |
* @static |
| 15 |
* |
| 16 |
* @var Base |
| 17 |
*/ |
| 18 |
private static $instances = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* Disable class cloning and throw an error on object clone. |
| 22 |
* |
| 23 |
* The whole idea of the singleton design pattern is that there is a single |
| 24 |
* object. Therefore, we don't want the object to be cloned. |
| 25 |
* |
| 26 |
* @access public |
| 27 |
* @since 2.0.0 |
| 28 |
*/ |
| 29 |
public function __clone() { |
| 30 |
// Cloning instances of the class is forbidden. |
| 31 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'templately' ), '1.0.0' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Disable un-serializing of the class. |
| 36 |
* |
| 37 |
* @access public |
| 38 |
* @since 2.0.0 |
| 39 |
*/ |
| 40 |
public function __wakeup() { |
| 41 |
// Un-serializing instances of the class is forbidden. |
| 42 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'templately' ), '1.0.0' ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Sets up a single instance of the plugin. |
| 47 |
* |
| 48 |
* @since 2.0.0 |
| 49 |
* @access public |
| 50 |
* @static |
| 51 |
* |
| 52 |
* @return static An instance of the class. |
| 53 |
*/ |
| 54 |
public static function get_instance( ...$args ) { |
| 55 |
$module = get_called_class(); |
| 56 |
if ( ! isset( self::$instances[ $module ] ) ) { |
| 57 |
self::$instances[ $module ] = new $module( ...$args ); |
| 58 |
} |
| 59 |
|
| 60 |
return self::$instances[ $module ]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Determine the plugin module to make platform based things happened. |
| 65 |
* |
| 66 |
* @param string $id |
| 67 |
* @return Platform|null |
| 68 |
*/ |
| 69 |
public function platform( $id ){ |
| 70 |
$platform = Module::get_instance()->active( $id ); |
| 71 |
if( ! is_null( $platform ) ) { |
| 72 |
return $platform; |
| 73 |
} |
| 74 |
|
| 75 |
return null; |
| 76 |
} |
| 77 |
} |