<?php
/**
 * Abstract Singleton Class
 *
 * Provides a base singleton implementation that can be extended by other classes.
 *
 * @package RedFoxWP\RedFoxTabManager
 * @since 1.0.0
 */

namespace RedFoxWP\RedFoxTabManager\Abstracts;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Abstract singleton class
 *
 * This class provides a standard singleton pattern implementation
 * that can be extended by other classes to ensure only one instance exists.
 *
 * @abstract
 * @since 1.0.0
 */
abstract class Abstract_Singleton {

	/**
	 * Stores singleton instances
	 *
	 * @since 1.0.0
	 * @access private
	 * @static
	 *
	 * @var array
	 */
	private static $instances = array();

	/**
	 * Get singleton instance
	 *
	 * @since 1.0.0
	 * @access public
	 * @static
	 *
	 * @return static Single instance of the calling class
	 */
	final public static function get_instance() {
		$class = get_called_class();

		if ( ! isset( self::$instances[ $class ] ) ) {
			self::$instances[ $class ] = new $class();
		}

		return self::$instances[ $class ];
	}

	/**
	 * Protected constructor to prevent direct instantiation
	 *
	 * @since 1.0.0
	 * @access protected
	 *
	 * @return void
	 */
	protected function __construct() {
		// Constructor logic can be implemented in child classes.
	}

	/**
	 * Prevent cloning of singleton instances
	 *
	 * @since 1.0.0
	 * @access protected
	 *
	 * @return void
	 */
	final protected function __clone() {
		// Cloning instances of the class is forbidden.
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'redfox-tab-manager' ), '1.0.0' );
	}

	/**
	 * Prevent unserialization of singleton instances
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 * @throws \Exception Cannot unserialize singleton.
	 */
	final public function __wakeup() {
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'redfox-tab-manager' ), '1.0.0' );
	}

	/**
	 * Prevent serialization of singleton instances
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 * @throws \Exception Cannot serialize singleton.
	 */
	final public function __sleep() {
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'redfox-tab-manager' ), '1.0.0' );
	}
}
