Classes

File mvc/dispatcher/Dispatcher.class.php

File mvc/dispatcher/Dispatcher.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: 
<?php
/**
 * Dispatch the current url arg to the first method matching
 * in the UrlDispatcherItem list of the controller object
 * @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: 2014 12 22
 * @since       PHPBoost 3.0 - 2009 06 08
*/

define('REQUEST_URI', $_SERVER['REQUEST_URI']);

class Dispatcher
{
    // Changing this value will result in a crash in rewrite mode.
    // To avoid this, also replace "?url=" by "?YourNewValue=" in config files
    const URL_PARAM_NAME = 'url';

    private $url_controller_mappers = array();

    /**
     * build a new Dispatcher from a UrlDispatcherItem List
     * @param <code>UrlControllerMapper[]</code> $url_controller_mappers
     * the list of the <code>UrlControllerMapper</code>
     * that could be applied on an url for that module
     */
    public function __construct($url_controller_mappers)
    {
        $this->url_controller_mappers =& $url_controller_mappers;
    }

    /**
     * dispatch the current url argument to the first method matching
     * in the <code>UrlControllerMapper</code> list of the controller object
     * @throws NoUrlMatchException
     */
    public function dispatch()
    {
        $url = AppContext::get_request()->get_getstring('url', '');
        foreach ($this->url_controller_mappers as $url_controller_mapper)
        {
            if ($url_controller_mapper->match($url))
            {
                $url_controller_mapper->call();
                Environment::destroy();
                return;
            }
        }
        throw new NoUrlMatchException($url);
        Environment::destroy();
    }
}
?>