wikipoints/ground/app.php

160 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
function __autoload($className)
{
$dirs = array('classes','models','controllers');
$loaded = false;
foreach ($dirs as $dir) {
$fileName = dirname(__FILE__) . '/../ground/' . $dir. '/' . $className . '.php';
if (file_exists($fileName)){
$loaded = (bool) include $fileName;
}
}
if (!$loaded){
App::error404 ('load class '.$className);
return false;
}
return true;
}
class App
{
private static $url = array();
private static $controller = 'IndexController';
private static $action = 'actionIndex';
private static $basedir = false;
private static $config = array();
private static $urlParams = array();
public static $DB = null;
/**
* Разбивает урл на контроллер, метод и выбирает параметры.
*/
private static function parseUrl()
{
self::$url = explode('/', $_SERVER['REQUEST_URI']);
self::$controller = isset(self::$url[1]) && self::$url[1] ? ucfirst(self::$url[1]) . 'Controller' : self::$controller;
self::$action = isset(self::$url[2]) && self::$url[2] ? 'action' . ucfirst(self::$url[2]) : self::$action;
// Разбираем параметры из урла
if ((count(self::$url) > 3) && (self::$url[3]))
{
for ($index = 3; $index < count(self::$url); $index = $index + 2)
{
$paramName = self::$url[$index];
$paramValue = isset(self::$url[$index + 1]) && self::$url[$index + 1] ? self::$url[$index + 1] : NULL;
self::$urlParams[$paramName] = $paramValue;
}
}
}
/**
* Основной метод для запуска приложения.
* @param string $config имя файла с конфигом
*/
public static function run($config = '')
{
ob_start();
self::$config = $config ? include_once $config : array();
self::parseUrl();
if (self::getConfig('DB'))
{
try
{
$confDB = self::getConfig('DB');
self::$DB = new PDO('mysql:host=' . $confDB['host'] . ';dbname=' . $confDB['dbname'].';charset=utf8;', $confDB['user'], $confDB['password']);
}
catch (PDOException $e)
{
echo $e->getMessage();
}
}
$controller = self::$controller;
$action = self::$action;
if (method_exists($controller, $action))
{
$controller::$action();
} else
{
self::error404();
}
}
public static function error404($message = '')
{
header("HTTP/1.0 404 Not Found");
die($message ? $message : '404 - Not Found');
}
/**
* Перенаправление на другой URL
* @param type $url
* @param type $httpCode - по умолчанию 307, временный редирект
*/
public static function redirect($url, $httpCode = 307)
{
header('Location: ' . $url, true, $httpCode);
}
/**
* Возвращает директорию фреймворка /full/path/ground/
* @return type
*/
public static function getBasedir()
{
if (!self::$basedir)
self::$basedir = dirname(__FILE__) . '/';
return self::$basedir;
}
/**
* Возвращает значение параметра из конфига
* @param string $paramName
* @return multi
*/
public static function getConfig($paramName)
{
return isset(self::$config[$paramName]) ? self::$config[$paramName] : NULL;
}
/**
* Устанавливает значение в конфиге
* @param string $paramName
* @param multi $paramValue
* @return true
*/
public static function setConfig($paramName, $paramValue)
{
self::$config[$paramName] = $paramValue;
return true;
}
/**
* Возвращает значение параметра, переданного в URL
* @param string $paramName
* @return string
*/
public static function getParam($paramName)
{
return isset(self::$urlParams[$paramName]) ? self::$urlParams[$paramName] : NULL;
}
/**
* Возвращает массив параметров, переданных в URL
* @return array
*/
public static function getParams()
{
return self::$urlParams;
}
}