<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * Modular Extensions - HMVC
 * Adapted from wiredesignz codeigniter-modular-extensions-hmvc 5.5
 *
 * MX_Controller extends CI_Controller so the framework's own bootstrap
 * (super-object assembly, loader creation/initialisation) runs exactly as
 * CodeIgniter expects. The HMVC layer then registers the controller in the
 * module registry and swaps in the module-aware loader.
 */
#[\AllowDynamicProperties]
class MX_Controller extends CI_Controller
{
	public $autoload = array();

	public function __construct()
	{
		/* Register this controller as the active HMVC super-object BEFORE
		 * the parent constructor runs: CI_Controller::__construct() triggers
		 * the loader's initialize(), and MX_Loader::initialize() reads
		 * CI::$APP->router to determine the current module. */
		CI::$APP =& $this;

		/* Run CI_Controller's constructor: builds the super-object, assigns
		 * all bootstrapped core classes, and creates + initialises $this->load
		 * (resolved as MY_Loader via the subclass prefix). */
		parent::__construct();

		$class = get_class($this);
		log_message('debug', $class.' MX_Controller Initialized');
		Modules::$registry[strtolower($class)] = $this;

		/* Re-initialise the loader in module context so module paths,
		 * helpers, models and views resolve correctly. */
		if (method_exists($this->load, 'initialize'))
		{
			$this->load->initialize($this);
		}

		/* Autoload any module items declared on the controller. */
		if (method_exists($this->load, '_autoloader'))
		{
			$this->load->_autoloader($this->autoload);
		}
	}

	/* NOTE: wiredesignz's original __get() magic accessor is intentionally
	 * omitted. CI_Controller::__construct() already copies every core class
	 * onto the controller as a real property, and a __get() handler marks
	 * the object "overloaded", which breaks CI's internal `=&` reference
	 * assignment to $this->load under PHP 8. */
}
