<?php
namespace System;
class Router
{
private $registry;
private $path;
private $args = [];
public function show404 ($message)
{
exit($message);
}
function __construct($registry)
{
$this->registry = $registry;
}
function setPath($path)
{
if (!is_dir($path))
{
throw new \Exception ('Некорректный путь к контроллерам: `'.$path.'`');
}
$this->path = $path;
}
private function getController(&$file, &$controller, &$action, &$args)
{
$route = (empty($_GET['route'])) ? '' : $_GET['route'];
if (empty($route))
{
$route = 'index';
};
$route = trim($route, '/\\');
$parts = explode('/', $route);
$cmd_path = $this->path;
foreach ($parts as $part)
{
$fullpath = $cmd_path . $part;
if (is_dir($fullpath))
{
$cmd_path .= $part . DIRSEP;
array_shift($parts);
continue;
}
if (is_file($fullpath.'.php'))
{
$controller = $part;
array_shift($parts);
break;
}
};
if (empty($controller))
{
$controller = 'index';
};
$action = array_shift($parts);
if (empty($action))
{
$action = 'index';
};
$file = $cmd_path . $controller . '.php';
$args = $parts;
}
public function delegate ()
{
$this->getController($file, $controller, $action, $args);
if (!is_readable($file))
{
$this->show404('ERROR !is_readable');
}
include_once ($file);
$class = 'Controller_' . $controller;
$controller = new $class($this->registry);
//Вот тут интерпретатор выебывается
/*********************************/
if (is_callable(array($controller, $action)))
{
$this->show404('ERROR !is_callable');
}
/*********************************/
$controller->$action();
}
}