<?php

/**
 * Base singleton class for Lockee plugin
 */
abstract class Lockee_Singleton
{
  /**
   * Array of singleton instances
   */
  private static $instances = array();

  /**
   * Protected constructor to prevent direct instantiation
   */
  protected function __construct()
  {
    $this->init();
  }

  /**
   * Prevent cloning of the instance
   */
  private function __clone() {}

  /**
   * Prevent unserializing of the instance
   */
  public function __wakeup()
  {
    throw new Exception("Cannot unserialize singleton");
  }

  /**
   * Get the singleton instance
   */
  public static function get_instance()
  {
    $class = get_called_class();
    if (!isset(self::$instances[$class])) {
      self::$instances[$class] = new $class();
    }
    return self::$instances[$class];
  }

  /**
   * Initialize the singleton
   * Override this method in child classes
   */
  protected function init() {}
}
