<? php
class Template {
private $ vars = array ( ) ;
function __set ( $ varname , $ value ) {
$ this -> vars [ $ varname ] = $ value ;
return true ;
}
function show ( $ name , $ data = false , $ display = true ) {
$ path = site_path . 'views' . DIRSEP . $ name . '.php' ;
if ( ! file_exists ( $ path ) ) {
trigger_error ( 'Template `' . $ name . '` does not exist.' , E_USER_NOTICE ) ;
return false ;
}
if ( $ data ) {
// fill with data
if ( is_array ( $ data ) ) {
foreach ( $ data as $ key => $ value ) {
$ this -> __set ( $ key , $ value ) ;
}
}
}
extract ( $ this -> vars ) ;
if ( $ display ) {
include ( $ path ) ;
} else {
if ( is_file ( $ path ) ) {
ob_start ( ) ;
include $ path ;
$ contents = ob_get_contents ( ) ;
ob_end_clean ( ) ;
return $ contents ;
}
return false ;
}
}
}
?>
<? php
class Router {
public $ path ;
private $ args = array ( ) ;
function setPath ( $ path ) {
$ path = trim ( $ path , '/ \\' ) ;
$ path . = DIRSEP ;
if ( ! is_dir ( $ path ) ) {
throw new Exception ( 'Invalid controller path: `' . $ path . '`' ) ;
}
$ this -> path = $ path ;
}
function delegate ( ) {
// analize path
$ this -> getController ( $ file , $ controller , $ action , $ args ) ;
// is file available?
if ( ! is_readable ( $ file ) ) {
trigger_error ( 'File `' . $ file . '` not found' , E_USER_ERROR ) ;
}
include ( $ file ) ;
// create controller
$ class = 'Controller_' . $ controller ;
$ controller = new $ class ( ) ;
// action is available in object?
if ( ! is_callable ( array ( $ controller , $ action ) ) ) {
trigger_error ( 'No method `' . $ action . '` was found in class' , E_USER_ERROR ) ;
}
// call action
call_user_func_array ( array ( $ controller , $ action ) , $ args ) ;
}
private function getController ( & $ file , & $ controller , & $ action , & $ args ) {
$ route = ( empty ( $ _GET [ 'route' ] ) ) ? 'index' : $ _GET [ 'route' ] ;
// split called url
$ route = trim ( $ route , '/ \\' ) ;
$ parts = explode ( '/' , $ route ) ;
// find right controller
$ cmd_path = $ this -> path ;
// set default controller name (will be found if found another)
$ controller = 'index' ;
foreach ( $ parts as $ part ) {
$ fullpath = $ cmd_path . $ part ;
// is dir?
if ( is_dir ( $ fullpath ) ) {
$ cmd_path . = $ part . DIRSEP ;
array_shift ( $ parts ) ;
continue ;
}
// is file?
if ( is_file ( $ fullpath . '.php' ) ) {
$ controller = $ part ;
array_shift ( $ parts ) ;
break ;
}
}
// get action method in controller
$ action = ( isset ( $ parts [ 0 ] ) && ! is_numeric ( $ parts [ 0 ] ) ) ? array_shift ( $ parts ) : 'index' ;
$ file = $ cmd_path . $ controller . '.php' ;
$ args = $ parts ;
}
}
?>
<? php
class loader {
public $ instance ;
public $ vars = array ( ) ;
function __construct ( $ parent ) {
$ this -> instance = $ parent ;
}
function model ( $ model_name ) {
$ filename = strtolower ( $ model_name ) . '.php' ;
$ file = site_path . 'models' . DIRSEP . $ filename ;
if ( ! file_exists ( $ file ) ) {
return false ;
}
include ( $ file ) ;
$ this -> instance -> $ model_name = new $ model_name ( new ActiveRecords ( ) ) ;
}
function library ( $ name ) {
$ filename = strtolower ( $ name ) . '.php' ;
$ file = site_path . 'libraries' . DIRSEP . $ filename ;
if ( ! file_exists ( $ file ) ) {
return false ;
}
include ( $ file ) ;
$ this -> instance -> $ name = new $ name ( new ActiveRecords ( ) ) ;
}
}
?>
<? php
class Model {
var $ db ;
function __construct ( $ db ) {
$ this -> db = $ db ;
}
}
?>
<? php
class Controller_Base {
public $ template ;
public $ load ;
function __construct ( ) {
$ this -> template = new Template ( ) ;
$ this -> load = new loader ( $ this ) ;
}
function __set ( $ varname , $ value ) {
$ this -> $ varname = $ value ;
return true ;
}
}
?>
$ this -> load -> model ( 'links' ) ;
$ this -> links -> add ( ' new ' ) ;
$ this -> [ model name ] -> [ method name in model ]
function redirect ( $ url )
{
Header ( "Location:" . $ Url ) ;
}
<? php
require 'includes / start.php' ;
$ router = new Router ( ) ;
$ router -> setPath ( site_path . 'controllers' ) ;
$ router -> delegate ( ) ;
?>
Source: https://habr.com/ru/post/127798/
All Articles