<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace com_eorisis_pixels\framework\install_package\lib\core\cms;
\defined('_JEXEC') or die;

use version_compare, class_exists;
use EorisisInstaller, EorisisInstallerHelper, JLoader;
use com_eorisis_pixels\framework\install_package\lib\core\cms\application;

if (version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisInstaller', '\\Joomla\\CMS\\Installer\\Installer');
	JLoader::registerAlias('EorisisInstallerHelper', '\\Joomla\\CMS\\Installer\\InstallerHelper');
}
else
{
	JLoader::registerAlias('EorisisInstaller', 'JInstaller');
	JLoader::registerAlias('EorisisInstallerHelper', 'JInstallerHelper');
}

class package
{
	public function __construct() {}

	// --------------------------------------------------

	public function install($path_to_dir) : bool
	{
		return (bool)$this->installer()->install($path_to_dir);
	}

	// --------------------------------------------------

	public function extract(string $path_to_package_file, bool $always_return_array = false)
	{
		if (version_compare(JVERSION, '3.8', '>='))
		{
			if (!class_exists('Joomla\CMS\Installer\InstallerHelper', false))
			{
				$this->register_installer_helper('InstallerHelper');
			}

			return EorisisInstallerHelper::unpack($path_to_package_file, $always_return_array);
		}

		if (!class_exists('JInstallerHelper', false))
		{
			$this->register_installer_helper('JInstallerHelper');
		}

		return EorisisInstallerHelper::unpack($path_to_package_file, $always_return_array);
	}

	// --------------------------------------------------

	private function installer()
	{
		return new EorisisInstaller;
	}

	// --------------------------------------------------

	private function register_installer_helper(string $class_name) // void
	{
		application::register_class($class_name, JPATH_ADMINISTRATOR.'/components/com_installer/helpers/installer.php');
	}
}
