<?php

defined('APS_BOOTSTRAP') || define('APS_BOOTSTRAP', __DIR__ . '/bootstrap-administrator.php');

function apsGetDatabaseType()
{
    $driver = getenv('DB_main_TYPE');
    // plan A:
    return ('mysql' == $driver) ? 'mysqli' : $driver;
    // plan B:
    return ('mysql' == $driver) ? 'pdomysql' : $driver;
}

function apsGetDatabaseHost()
{
    $host = getenv('DB_main_HOST');

    // special thanks for POA
    $port = getenv('DB_main_PORT');
    empty($port) || ($host .= ':' . $port);

    return $host;
}

defined('_JEXEC') || define('_JEXEC', 'APS');

// overwrite default function - add exit code
function jexit($message = 0)
{
    debug_print_backtrace();
    if (!is_numeric($message)) {
        echo $message, PHP_EOL;
    }
    print_r(array(__FUNCTION__ => $message));
    exit(1);
}

require_once APS_BOOTSTRAP;

/**
 * lock display page in exists handler
 * @see /libraries/cms.php
 * set_exception_handler(array('JErrorPage', 'render'));
 */
set_exception_handler(null);

class JApplicationAps extends \Joomla\Application\AbstractApplication
{
    public function __call($name, $arguments)
    {
        print_r(array('----------------------------------------------------', $name, $arguments));
        debug_print_backtrace();
        exit(1);
    }

    public function close($code = 0)
    {
        jexit($code);
    }

    protected function doExecute()
    {
    }

    // \JApplicationCms::enqueueMessage
    public function enqueueMessage($msg, $type = 'message')
    {
        print_r(array($type => $msg));
        debug_print_backtrace();
    }

    public function getLocaliseAdmin()
    {
        return array('admin' => array(), 'site' => array());
    }

    /**
     * @copypasto! \JApplicationWeb::flushAssets
     */
    public function flushAssets()
    {
        $version = new JVersion;
        $version->refreshMediaVersion();
    }

    /**
     * @copypasto! \JApplicationCms::getUserState
     * use in upgrade action
     *
     * #0  JApplicationAps->__call(getUserState, Array ([0] => com_cache.cache.filter.search))
     * #1  JApplicationAps->getUserState(com_cache.cache.filter.search) called at [.libraries/legacy/model/list.php:69
     * #2  JModelList->getUserStateFromRequest(...) called at [.administrator/components/com_cache/models/cache.php:79]
     * #3  CacheModelCache->populateState() called at [.libraries/legacy/model/legacy.php:439]
     * #4  JModelLegacy->getState(client_id) called at [.administrator/components/com_cache/models/cache.php:176]
     * #5  CacheModelCache->getCache() called at [.administrator/components/com_cache/models/cache.php:234]
     * #6  CacheModelCache->clean() called at [.administrator/components/com_admin/script.php:1886]
     * #7  JoomlaInstallerScript->cleanJoomlaCache() called at [.administrator/components/com_admin/script.php:41]
     * #8  JoomlaInstallerScript->update(...) called at [.administrator/components/com_joomlaupdate/models/default.php:694]
     * #9  JoomlaupdateModelDefault->finaliseUpgrade() called at [./scripts/Upgrader.php:35]
     */
    public function getUserState($key, $default = null)
    {
        return $default;
    }

    /**
     * @copypasto! \JApplicationCms::getUserStateFromRequest
     * use in upgrade action
     *
     * #0  JApplicationAps->__call(getUserStateFromRequest, Array ([0] => com_cache.cache.filter,[1] => filter,[2] => Array (),[3] => array))
     * #1  JApplicationAps->getUserStateFromRequest(com_cache.cache.filter, filter, Array (), array) called at [.libraries/legacy/model/list.php:488]
     * #2  JModelList->populateState(group, asc) called at [.administrator/components/com_cache/models/cache.php:86]
     * #3  CacheModelCache->populateState() called at [.libraries/legacy/model/legacy.php:439]
     * #4  JModelLegacy->getState(client_id) called at [.administrator/components/com_cache/models/cache.php:176]
     * #5  CacheModelCache->getCache() called at [.administrator/components/com_cache/models/cache.php:234]
     * #6  CacheModelCache->clean() called at [.administrator/components/com_admin/script.php:1886]
     * #7  JoomlaInstallerScript->cleanJoomlaCache() called at [.administrator/components/com_admin/script.php:41]
     * #8  JoomlaInstallerScript->update(...) called at [.administrator/components/com_joomlaupdate/models/default.php:694]
     * #9  JoomlaupdateModelDefault->finaliseUpgrade() called at [./scripts/Upgrader.php:37]
     */
    public function getUserStateFromRequest($key, $request, $default = null, $type = 'none')
    {
        return $default;
    }
}

// @todo ??? mock JSession::checkToken() and call native controllers with JApplicationAps
\JFactory::$application = new JApplicationAps();
