<?php
/**
 * Singleton class trait taking a Shared instance.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Traits;

use Northmill_Online_Shared\Abstract_Shared_Base;

trait Shared_Singleton_Trait {
	use Shared_Trait;

	/**
	 * Represents an instance of the class.
	 *
	 * @var null|static
	 */
	private static ?self $instance = null;

	/**
	 * Get the instance of this class
	 *
	 * @param Abstract_Shared_Base $shared The shared instance.
	 *
	 * If the instance doesn't already exist, instantiate a new instance of the class.
	 *
	 * @return static
	 */
	public static function instance( Abstract_Shared_Base $shared ): self {
		if ( null === self::$instance ) {
			self::$instance = new self( $shared );
		}

		return self::$instance;
	}

	/**
	 * Prevent cloning.
	 */
	private function __clone() {}
}
