<?php
/**
 * Dependency Container
 *
 * @package SEOvaultSEO
 */

namespace SEOvaultSEO\Core;

/**
 * Simple dependency container for managing plugin services
 *
 * @internal Free-only DI bag. Not a public extension surface. Pro must use
 *           Extension\ExtensionApi hooks and documented public classes.
 */
class Container {

	/**
	 * Stored services
	 *
	 * @var array
	 */
	private $services = array();

	/**
	 * Register a service
	 *
	 * @param string $key Service identifier
	 * @param mixed  $value Service value or callback
	 */
	public function set( $key, $value ) {
		$this->services[ $key ] = $value;
	}

	/**
	 * Get a service
	 *
	 * @param string $key Service identifier
	 * @return mixed|null
	 */
	public function get( $key ) {
		if ( ! isset( $this->services[ $key ] ) ) {
			return null;
		}

		$value = $this->services[ $key ];

		// If the value is a callable, execute it and cache the result
		if ( is_callable( $value ) ) {
			$this->services[ $key ] = $value();
			return $this->services[ $key ];
		}

		return $value;
	}

	/**
	 * Check if a service exists
	 *
	 * @param string $key Service identifier
	 * @return bool
	 */
	public function has( $key ) {
		return isset( $this->services[ $key ] );
	}
}
