Classes

File mvc/dispatcher/DispatchManager.class.php

File mvc/dispatcher/DispatchManager.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 
<?php
/**
 * @package     MVC
 * @subpackage  Dispatcher
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 11 14
 * @since       PHPBoost 3.0 - 2009 12 09
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor mipel <mipel@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class DispatchManager
{
    /**
     * Redirect the request to the right controller using the url controller mappes list
     * @param UrlControllerMapper[] $url_controller_mappers the url controllers mapper list
     */
    public static function dispatch($url_controller_mappers)
    {
        try
        {
            $dispatcher = new Dispatcher($url_controller_mappers);
            $dispatcher->dispatch();
        }
        catch (NoUrlMatchException $ex)
        {
            self::handle_dispatch_exception($ex);
        }
    }

    /**
     * Cleans the output buffer and execute the given controller before exiting
     * @param Controller $controller the controller to execute
     */
    public static function redirect(Controller $controller)
    {
        AppContext::get_response()->clean_output();
        Environment::init_output_bufferization();
        $request = AppContext::get_request();
        $response = $controller->execute($request);
        $response->send();
        Environment::destroy();
        exit;
    }

    /**
     * Returns an url object from the dispatcher path with the $url param
     * dispatcher must be in the index.php file
     * @param string $path the url to apply the rewrite form on
     * @param string $url the url to apply the rewrite form on
     * @param boolean $not_rewriting_url_forced forced to have a non-rewritten url
     * @return Url an url object relative to the current script path
     */
    public static function get_url($path, $url, $not_rewriting_url_forced = false)
    {
        $dispatcher_url = new Url(rtrim($path, '/'));
        $url = ltrim($url, '/');
        if (ServerEnvironmentConfig::load()->is_url_rewriting_enabled() && !$not_rewriting_url_forced)
        {
            return new Url(self::get_dispatcher_path($dispatcher_url->relative()) . '/' . $url);
        }
        else
        {
            $dispatcher = $dispatcher_url->relative();
            if (!preg_match('`(?:\.php)|/$`u', $dispatcher))
            {
                $dispatcher .= '/';
            }
            if (TextHelper::strpos($url, '?') !== false)
            {
                $exploded = explode('?', $url, 2);
                $exploded[1] = str_replace('?', '&', $exploded[1]);
                return new Url($dispatcher . '?' . Dispatcher::URL_PARAM_NAME .
                    '=/' . $exploded[0] . '&' . $exploded[1]);
            }
            else
            {
                return new Url($dispatcher . '?' . Dispatcher::URL_PARAM_NAME .
                    '=/' . $url);
            }
        }
    }

    private static function get_dispatcher_path($dispatcher_name)
    {
        return preg_replace('`(.*/)[a-z0-9]+\.php`u','$1', $dispatcher_name);
    }

    private static function handle_dispatch_exception($exception)
    {
        if (Debug::is_debug_mode_enabled())
        {
            self::show_error($exception);
        }
        else
        {
            $error_controller = PHPBoostErrors::unexisting_page();
            DispatchManager::redirect($error_controller);
        }
    }

    private static function show_error($exception)
    {
        Debug::fatal($exception);
    }
}
?>