Classes

File mvc/dispatcher/UrlControllerMapper.class.php

File mvc/dispatcher/UrlControllerMapper.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: 
<?php
/**
 * Call the controller method matching an url
 * @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 15
 * @since       PHPBoost 3.0 - 2009 06 08
 * @contributor mipel <mipel@phpboost.com>
*/

class UrlControllerMapper extends AbstractUrlMapper
{
    private $classname;
    private $parameters_names;

    /**
     * build a new UrlDispatcherItem
     * @param string $classpath the controller classname
     * @param string $capture_regex the regular expression matching the url
     * and capturing the controller method parameters. By default, match the empty url <code>/</code>
     * @param string $parameters_names the names of the parameters in the capture order
     * @throws NoSuchControllerException
     */
    public function __construct($classname, $capture_regex = '`^/?$`u', $parameters_names = array())
    {
        $this->classname =& $classname;
        $this->parameters_names = $parameters_names;
        parent::__construct($capture_regex);
    }

    /**
     * Call the controller method if the url match and if the method exists
     */
    public function call()
    {
        $this->build_parameters();
        $this->do_call();
    }

    private function build_parameters()
    {
        $captured_parameters = $this->get_captured_parameters();

        $i = 1;
        foreach ($this->parameters_names as $parameter_name)
        {
            $value = null;
            if (isset($captured_parameters[$i]))
            {
                $value = $captured_parameters[$i];
            }
            AppContext::get_request()->set_getvalue($parameter_name, $value);
            $i++;
        }
    }

    private function do_call()
    {
        $controller = new $this->classname();
        if (!($controller instanceof Controller))
        {
            throw new NoSuchControllerException($this->classname);
        }
        $controller_to_execute = $controller->get_right_controller_regarding_authorizations();
        $response = $controller_to_execute->execute(AppContext::get_request());
        $response->send();
    }
}
?>