/nome/user/www/ |---[.myengine] // | |---[classes] // | |---autoload.php // , | `---README.TXT // |---[classes] // | |---[articles] // | | |---[m] // | | | `---article.class.php // ( : articles_m_article) | | |---[view] // CSS | | | |---[css] // | | | | `---style.css // | | | |---article_list.tpl // | | | `---one_article.tpl // | | `---controller.class.php // ( : articles_controller) | |---[comments] // | |---[fotos] // | |---[site] // ( ) | `---[users] // |---.setup.php // ( , ..) `---index.php //
<?php // PHP, , new SomeClass() function __autoload($class_name){ $class_folder = 'classes'; // // // $class_folder. $class_paths[] = dirname($_SERVER['SCRIPT_FILENAME'])."/$class_folder/"; // $class_paths[] = __DIR__."/classes/"; // $CLASS_PATHS if(!empty($GLOBALS["CLASS_PATHS"])){ if(!is_array($GLOBALS["CLASS_PATHS"])) throw new Exception('$CLASS_PATHS must be array!'); $class_paths = array_merge($class_paths, $GLOBALS["CLASS_PATHS"]); } // ( A_B_C) $slashed_class_name = str_replace("_", "/", $class_name); // A/B/C $short_path = substr($slashed_class_name, 0, strrpos($slashed_class_name, '/')); // A/B foreach($class_paths as $class_path){ // A_B_C /A/B/C.class.php $file_full_name = "{$class_path}/{$slashed_class_name}.class.php"; if(file_exists($file_full_name)){ require_once($file_full_name); return; } // A_B_C /A/B/C/A_B_C.class.php // - ( ) $file_full_name = "{$class_path}/{$slashed_class_name}/{$class_name}.class.php"; if(file_exists($file_full_name)){ require_once($file_full_name); return; } } } ?>
<?php include __DIR__.'.myengine/autoload.php'; // // , ?>
<?php // .setup.php : include __DIR__.'/../.setup.php'; ?>
<?php include '.setup.php'; // -- // , $m = new SomeModule(); $m->run(); echo $m->getOutput(); ?>
Note: some project files start with '.' (points) so that when sorting by name they are on top
abstract class Lego{ .................. // http://habrahabr.ru/company/microset/blog/109481/ abstract public function getDir(); // , // (.. , ) public function getWebDir(){ $viewdir = str_replace('\\', '/', $this->getViewDir()); // , //, ! DOCUMENT_ROOT: return str_ireplace($_SERVER['DOCUMENT_ROOT'], '', $viewdir).'/'.$dirname; } // -, public function getJavascripts(){ $js = array(); $h = @opendir($this->getDir()."/js"); while($file = @readdir($h)) if(preg_match("/(\.js|\.js\.php)$/i", $file)) $js[] = $this->getWebDir()."/js/".$file; return $js; } // , public function getStylesheets(){ $css = array(); $h = @opendir($this->getDir()."/view/css"); while($file = @readdir($h)) if(preg_match("/(\.css|\.css\.php)$/i", $file)) $css[] = $this->getWebDir()."/view/css/".$file; return $css; } // , , public function getHeaderBlock(){ $csses = $this->getStylesheets(); $jses = $this->getJavascripts(); $ret = ""; foreach($csses as $one) $ret .= "\n<link rel='stylesheet' href='{$one}' type='text/css' media='screen' />\n"; foreach($jses as $one) $ret .= "\n<script type='text/javascript' src='{$one}'></script>\n"; return $ret; } // ( , ) public function fetch($template){ return Smarty::fetch($this->getDefaultDir().'/view/'.$template); } }
// class UriConstructor{ public $arr; public function __construct($arr = false){ $this->arr = $arr ? $arr : $_GET; } // ( ) public function put($key, $val){ $this->arr = array_replace_recursive($this->arr, array($key => $val)); return $this; } // public function remove($key){ unset($this->arr[$key]); return $this; } // public function clear(){ $this->arr = array(); return $this; } // public function set($lego_name, $action /*....*/){ if(isset($this->arr[$lego_name]) && !is_array($this->arr[$lego_name])) unset($this->arr[$lego_name]); $this->arr[$lego_name]['act'] = $action; $params = func_get_args(); array_shift($params); array_shift($params); foreach($params as $key=>$one){ $this->arr[$lego_name][$action][$key] = $one; } return $this; } // , public function setAct($action /*....*/){ $lego = Lego::current(); $params = array($lego->getName()); $params = array_merge($params, func_get_args()); return call_user_func_array( array($this, "set"), $params ); } // get-, ( ) public function url($path = false){ if(!$path) $path = $_SERVER['SCRIPT_NAME']; return $path.'?'.$this; } // - GET- public function __toString(){ return http_build_query($this->arr); } // GET- public function asArray(){ return $this->arr; } }
abstract class Lego{ .................. // public function uri(){ return new UriConstructor(); } // , , href // action- , , public function actUri($action /* params */){ $params = func_get_args(); array_unshift($params, $this->getName()); return call_user_func_array( array($this->uri(), "set"), $params ); } }
<a href="{$lego->actUri('allfotos')->url()}"> </a> , , : <a href="{$lego->actUri('showonefoto', $id)->url()}"> </a> // : <a href="/index.php?.....__...&fotos[act]=showonefoto&fotos[0]=123">....</a>
abstract class Lego{ .................. // public function run(){ Smarty::assign("lego", $this); // $lego - ..... // } }
/* , , $entity_id $entity_name */ class fotos_controller extends Lego{ private $entity_id; private $entity_name; private $num_for_page = 5; // ( ), function __construct($name = false, $entity_id = 0, $entity_name = "User"){ parent::__construct($name); $this->entity_id = $entity_id; $this->entity_name = $entity_name; } // , Lego. public function getDir(){ return __DIR__; } // - // , function action_index(){ Database::query("select * from `fotos` where `entity_name`='{$this->entity_name}' and `entity_id`={$this->entity_id} and deleted = 0 order by created desc"); $fotos = Database::fetchObjects(); Output::assign("fotos", $fotos); return $this->fetch("allfotos.tpl"); } // function action_mainbar(){ $offset = $this->_get($this->getName()."_offset", 0); Database::query("select * from `fotos` where `entity_name`='{$this->entity_name}' and `entity_id`={$this->entity_id} and deleted = 0 order by created desc limit {$offset}, ".($this->num_for_page+1)); $fotos = Database::fetchObjects(); Output::assign("fotos", $fotos); Output::assign("offset", $offset); Output::assign("num_for_page", $this->num_for_page); return $this->fetch("lego_fotos.tpl"); } // function action_sidebar(){ return $this->action_mainbar(); } // ( POST) function action_submit(){ $f = new tbl_fotos(); $f['entity_name'] = $this->entity_name; $f['entity_id'] = $this->entity_id; $f['user_id'] = User::getCurrentUser()->getId(); $f['text'] = $this->_post($this->getName()."_text"); $f['file_id'] = FotoStorage::putFromPost($this->getName()."_file"); if($f['file_id']) $f->insert(); $this->_goto($this->actUri("mainbar")->url()); //_goto - header("Location: ... } // function action_showone($foto_id){ $f = new tbl_fotos($foto_id); Output::assign("foto", $f); $ret = $this->fetch("showone.tpl"); // . $c = new comments_controller("foto_comments", "tbl_fotos", $f->getId()); $c->run(); return $ret.$c->getOutput(); // } // " " function action_set_as_main($foto_id){ Auth::authorize(); $f = new tbl_fotos($foto_id); $user = $f->getOwner(); $user['foto'] = $f['file_id']; $user->update(); $this->_goto($this->actUri("showone", $foto_id)->url()); } // " " function action_delete($foto_id){ Auth::authorize(); $f = new tbl_fotos($foto_id); //FileStorage::delete($f['file_id']); if($f->isMain()){ $user = User::getCurrentUser(); $user['foto'] = ""; $user->update(); } $f['deleted'] = 1; $f->update(); $this->_goto($this->actUri("showone", $foto_id)->url()); } // " " function action_restore($foto_id){ Auth::authorize(); $f = new tbl_fotos($foto_id); $f['deleted'] = 0; $f->update(); $this->_goto($this->actUri("showone", $foto_id)->url()); } }
<?php include ".setup.php"; // $lego = new site_controller(); // - $lego->run(); // echo $lego->getOutput();// ! ?>
Source: https://habr.com/ru/post/109890/
All Articles