Source of file MslsRegistry.php

Size: 1,546 Bytes - Last Modified: 2018-02-05T14:23:18+00:00

/home/dennisploetner/Projects/Multisite-Language-Switcher/includes/MslsRegistry.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
<?php
/**
 * MslsRegistry
 * @author Dennis Ploetner <re@lloc.de>
 * @since 0.9.8
 */

namespace lloc\Msls;

/**
 * Registry instead of singletons
 * @package Msls
 */
class MslsRegistry {

	/**
	 * Generic container
	 *
	 * @var array
	 */
	private static $arr = array();

	/**
	 * Instance
	 *
	 * @var MslsRegistry
	 */
	private static $instance;

	/**
	 * Constructor
	 *
	 * Don't call me directly!
	 *
	 * @codeCoverageIgnore
	 */
	final private function __construct() {
	}

	/**
	 * Clone
	 *
	 * Don't call me directly!
	 *
	 * @codeCoverageIgnore
	 */
	final private function __clone() {
	}

	/**
	 * Get an object by key
	 *
	 * @param string $key
	 *
	 * @return mixed
	 */
	private function get( $key ) {
		return ( isset( self::$arr[ $key ] ) ? self::$arr[ $key ] : null );
	}

	/**
	 * Set an object
	 *
	 * @param string $key
	 * @param mixed $instance
	 */
	private function set( $key, $instance ) {
		self::$arr[ $key ] = $instance;
	}

	/**
	 * Registry is a singleton
	 *
	 * @return MslsRegistry
	 */
	public static function instance() {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Static get_object calls get
	 *
	 * @param string $key
	 *
	 * @return mixed
	 */
	public static function get_object( $key ) {
		return self::instance()->get( $key );
	}

	/**
	 * Static set_object calls set
	 *
	 * @param string $key
	 * @param mixed $instance
	 */
	public static function set_object( $key, $instance ) {
		self::instance()->set( $key, $instance );
	}

}