<?php
declare( strict_types=1 );

namespace FiboFilters\DI;

use FiboFiltersVendor\Symfony\Component\DependencyInjection\Container;
use FiboFiltersVendor\Symfony\Component\DependencyInjection\ContainerBuilder;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class ContainerFactory
 */
class ContainerFactory {

	/** @var IContainerConfigurator */
	private $configurator;

	public function __construct( IContainerConfigurator $configurator ) {
		$this->configurator = $configurator;
	}

	public function get_container(): Container {
		$dump_class = '\\' . $this->configurator->get_dump_namespace() . '\\' . $this->configurator->get_dump_class_name();
		if ( class_exists( $dump_class ) ) {
			/** @var Container $container */
			$container = new $dump_class();
		} else { // Only for dev environment.
			$container = $this->get_configured_container();
			$container->compile();
		}

		return $container;
	}

	public function get_configured_container(): ContainerBuilder {
		return $this->configurator->configure( new ContainerBuilder() );
	}
}
