<?php

namespace Aps\App\Joomla;

require_once __DIR__ . '/bootstrap.php';

/**
 * Simulate com_joomlaupdate component - process native update model
 *
 * administrator/components/com_joomlaupdate/models/default.php
 * \JoomlaupdateControllerUpdate::finalise
 * \JoomlaupdateModelDefault::finaliseUpgrade
 * \JoomlaInstallerScript::update
 */
class Upgrader
{
    public function execute($version, $release)
    {
        jimport('joomla.filesystem.folder');
        if (file_exists(JPATH_INSTALLATION)) {
            \JFolder::delete(JPATH_INSTALLATION);
        }

        // pseudo session a specially for \JoomlaInstallerScript::flushSessions
        \JFactory::$session = new apsSession();

//        \JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_joomlaupdate/models', 'JoomlaupdateModel');
//
//        /** @var \JoomlaupdateModelDefault $model */
//        $model = \JModelLegacy::getInstance('default', 'JoomlaupdateModel');
//        $model->finaliseUpgrade();

        require_once JPATH_ADMINISTRATOR . '/components/com_joomlaupdate/models/default.php';
        $model = new \JoomlaupdateModelDefault;
        $model->finaliseUpgrade();
    }
}

// a specially for \JoomlaInstallerScript::flushSessions
class apsSession
{
    public function getState()
    {
    }

    public function restart()
    {
    }
}
