public function __construct($refresh = false) { if (defined('PHPFOX_NO_APPS')) { return; } $base = PHPFOX_DIR_SITE . 'Apps/'; if (!is_dir($base)) { return; } if (self::$_apps !== null && !$refresh) { return; } self::$_apps = []; foreach (scandir($base) as $app) { if ($app == '.' || $app == '..') { continue; } $path = $base . $app . '/'; if (!file_exists($path . 'app.lock')) { continue; } $jsonData = file_get_contents($path . 'app.json'); $jsonData = preg_replace_callback('/{{ ([a-zA-Z0-9_]+) }}/is', function ($matches) use($jsonData) { $_data = json_decode($jsonData); if (!isset($_data->{$matches[1]})) { return $matches[0]; } return $_data->{$matches[1]}; }, $jsonData); $data = json_decode($jsonData); $data->path = $path; if (isset($data->routes)) { foreach ((array) $data->routes as $key => $route) { $route = (array) $route; $route['id'] = $data->id; Route::$routes = array_merge(Route::$routes, [$key => $route]); } } self::$_apps[$data->id] = $data; \Core\Route\Controller::$active = $data->path; \Core\Route\Controller::$activeId = $data->id; $vendor = $data->path . 'vendor/autoload.php'; if (file_exists($vendor)) { require_once $vendor; } if (file_exists($data->path . 'start.php')) { $callback = (require_once $data->path . 'start.php'); if (is_callable($callback)) { $View = new \Core\View(); $View->loader()->addPath($data->path . 'views/', $data->id); call_user_func($callback, $this->get($data->id), $View->env()); } } } // d(self::$_apps); exit; }
/** * Saves or loads the route cache. * * @param boolean cache the current routes * @return void when saving routes * @return boolean when loading routes */ public static function cache($save = FALSE) { if ($save === TRUE) { Ko::cache('Route::cache()', Route::$routes); } else { if ($routes = Ko::cache('Route::cache()')) { Route::$routes = $routes; return TRUE; } else { return FALSE; } } }
/** * Resets the class to a first-load state. Mainly useful during testing. * * @return void */ public static function reset() { self::$route_objects = array(); self::$named_routes = array(); self::$routes = array(); self::$prefix = NULL; self::$pre_route_objects = array(); self::$pattern_list = array(); self::$filter_list = array(); }
static function start() { // контроллер и действие по умолчанию $controller_name = 'Main'; $action_name = 'index'; $routes = explode('/', $_SERVER['REQUEST_URI']); //Убираем localhost в версии для разработки if ($routes[0] === 'localhost' || empty($routes[0])) { array_splice($routes, 0, 1); self::$routes = $routes; } // получаем имя контроллера if (!empty($routes[1])) { $controller_name = $routes[1]; self::$controller_name = strtolower($controller_name); } // получаем имя экшена if (!empty($routes[2])) { $action_name = $routes[2]; self::$action_name = strtolower($action_name); } //Получаем параметры if (!empty($routes[3])) { $j = 1; //Если элементов в адресной строке четное кол-во, заполняем массив с параметрами в виде ассоциативного массива if (count($routes) % 2 == 0) { for ($i = 3; $i < count($routes); $i++) { self::$params[$j] = $routes[$i]; $j++; } //Если не четное - одномерным } else { for ($i = 3; $i < count($routes); $i += 2) { self::$params[$routes[$i]] = $routes[$i + 1]; $j++; } } } // добавляем префиксы $model_name = 'Model_' . $controller_name; $controller_name = 'Controller_' . $controller_name; $action_name = 'action_' . $action_name; // подцепляем файл с классом модели (файла модели может и не быть) $model_file = strtolower($model_name) . '.php'; $model_path = "application/models/" . $model_file; if (file_exists($model_path)) { include "application/models/" . $model_file; } // подцепляем файл с классом контроллера $controller_file = strtolower($controller_name) . '.php'; $controller_path = "application/controllers/" . $controller_file; if (file_exists($controller_path)) { include "application/controllers/" . $controller_file; } else { /* правильно было бы кинуть здесь исключение, но для упрощения сразу сделаем редирект на страницу 404 */ Route::ErrorPage404(); } // создаем контроллер $controller = new $controller_name(); $action = $action_name; if (method_exists($controller, $action)) { // вызываем действие контроллера $controller->{$action}(); } else { // здесь также разумнее было бы кинуть исключение Route::ErrorPage404(); } }
/** * Resets the class to a first-load state. Mainly useful during testing. * * @return void */ public static function reset() { self::$routes = array(); self::$named_routes = array(); self::$nested_depth = 0; }
/** * Load routes list * * @param $routes * @return string */ public static function register($routes = []) { return self::$routes = $routes; }