2017-01-22 23:47:24 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: Игорь
|
|
|
|
* Date: 23.01.2017
|
|
|
|
* Time: 01:58
|
|
|
|
*/
|
|
|
|
|
2017-01-23 10:25:19 +00:00
|
|
|
namespace PFRM;
|
2017-01-22 23:47:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class App {
|
|
|
|
|
2017-01-23 10:25:19 +00:00
|
|
|
protected $user_agent;
|
|
|
|
|
|
|
|
protected $user_IP;
|
|
|
|
|
|
|
|
protected $route_table;
|
|
|
|
|
2017-01-25 16:08:19 +00:00
|
|
|
public $dbh;
|
|
|
|
|
|
|
|
public $config;
|
|
|
|
|
|
|
|
function __construct( $config ) {
|
|
|
|
$this->config = $config;
|
2017-01-22 23:47:24 +00:00
|
|
|
date_default_timezone_set('Europe/Moscow');
|
|
|
|
error_reporting(E_ALL);
|
2017-01-23 10:25:19 +00:00
|
|
|
$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
|
|
|
|
$this->user_IP = $_SERVER['REMOTE_ADDR'];
|
2017-01-23 20:33:16 +00:00
|
|
|
$this->setRoute( '|^/$|', 'Index', 'index' );
|
2017-01-25 16:08:19 +00:00
|
|
|
|
|
|
|
$dsn = 'mysql:host=' . $this->config->db->host
|
|
|
|
. ';dbname=' . $this->config->db->dbname . ';charset=utf8';
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->dbh = new \PDO(
|
|
|
|
$dsn,
|
|
|
|
$this->config->db->user,
|
|
|
|
$this->config->db->password
|
|
|
|
);
|
|
|
|
} catch (\PDOException $exception) {
|
|
|
|
die( 'Нет подключения к MySQL: ' . $exception->getMessage() );
|
|
|
|
}
|
2017-01-23 10:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-23 20:33:16 +00:00
|
|
|
* Получение useragent пользователя
|
|
|
|
*
|
|
|
|
* @return string
|
2017-01-23 10:25:19 +00:00
|
|
|
*/
|
2017-01-23 20:33:16 +00:00
|
|
|
public function getUserAgent() {
|
2017-01-23 10:25:19 +00:00
|
|
|
return $this->user_agent;
|
2017-01-22 23:47:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 10:25:19 +00:00
|
|
|
/**
|
2017-01-23 20:33:16 +00:00
|
|
|
* Получение IP пользователя
|
|
|
|
*
|
|
|
|
* @return string
|
2017-01-23 10:25:19 +00:00
|
|
|
*/
|
|
|
|
public function getUserIP() {
|
|
|
|
return $this->user_IP;
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:08:19 +00:00
|
|
|
public function getPostObj( $array ) {
|
|
|
|
$out = [];
|
|
|
|
foreach ( $array as $value ) {
|
|
|
|
if ( isset( $_POST[ $value ] ) ) {
|
|
|
|
$out[ $value ] = $_POST[ $value ];
|
|
|
|
} else {
|
|
|
|
$out[ $value ] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (object) $out;
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:33:16 +00:00
|
|
|
/**
|
|
|
|
* Установка маршрута
|
|
|
|
*
|
|
|
|
* @param string $regexp регулярное выражение описывающее путь
|
|
|
|
* @param string $controller котроллер.
|
|
|
|
* @param string $action действие. по умолчанию 'index'
|
|
|
|
*/
|
2017-01-23 10:25:19 +00:00
|
|
|
public function setRoute( $regexp, $controller, $action = 'index') {
|
|
|
|
$this->route_table[] = array($regexp,$controller,$action);
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:33:16 +00:00
|
|
|
/**
|
|
|
|
* Редирект
|
|
|
|
*
|
|
|
|
* @param string $url адрес переброса
|
|
|
|
*/
|
|
|
|
public function Redirect( $url ) {
|
|
|
|
header( 'Location: ' . $url );
|
|
|
|
exit(0);
|
|
|
|
}
|
2017-01-23 10:25:19 +00:00
|
|
|
|
2017-01-25 16:08:19 +00:00
|
|
|
public function routeRun( $controller, $action = 'index', $params = [] ) {
|
|
|
|
$a = $this->config->controller_namespace . $controller . 'Controller';
|
|
|
|
$obj = new $a;
|
|
|
|
call_user_func_array( [ $obj, $action . 'Action' ], $params );
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:33:16 +00:00
|
|
|
/**
|
2017-01-26 03:45:35 +00:00
|
|
|
* Запуск приложения
|
2017-01-23 20:33:16 +00:00
|
|
|
*/
|
2017-01-23 10:25:19 +00:00
|
|
|
public function run(){
|
2017-01-23 20:33:16 +00:00
|
|
|
foreach ( $this->route_table as $route_array ) {
|
|
|
|
if ( preg_match( $route_array[0], $_SERVER['REQUEST_URI'], $matches ) ) {
|
|
|
|
$a = $this->config->controller_namespace . $route_array[1] . 'Controller';
|
|
|
|
$obj = new $a;
|
|
|
|
$param_arr = [];
|
|
|
|
foreach ( $matches as $key => $value ) {
|
|
|
|
if ( ! is_int( $key ) ) {
|
|
|
|
$param_arr[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
call_user_func_array( [ $obj, $route_array[2] . 'Action' ], $param_arr );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-22 23:47:24 +00:00
|
|
|
}
|
|
|
|
}
|