За последние 24 часа нас посетили 32911 программистов и 1820 роботов. Сейчас ищут 866 программистов ...

Упрощение кода

Тема в разделе "PHP для новичков", создана пользователем Elect, 14 авг 2015.

  1. Elect

    Elect Новичок

    С нами с:
    4 авг 2015
    Сообщения:
    70
    Симпатии:
    0
    Помогите пожалуйста с упраение кода:
    Код (PHP):
    1. [code]<?php
    2.  
    3.  
    4. $tools = array();
    5.  
    6. define('ROOT', dirname(__FILE__) . '/');
    7. define('LANG', 'ru_RU');
    8. define('TMP_NAME', 'Default');
    9.  
    10.  
    11. function router(){
    12.     if ($_SERVER['REQUEST_URI'] == "/") {
    13.         $module = 'option';
    14.         $page = 'index';
    15.     } else {
    16.         $URL_Path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    17.         $URL_Parts = explode('/', trim($URL_Path, ' /'));
    18.         $module = array_shift($URL_Parts);
    19.         $page = array_shift($URL_Parts);
    20.  
    21.         if (!empty($module)) {
    22.             $param = array();
    23.             for ($i = 0; $i < count($URL_Parts); $i++) {
    24.             $param[$URL_Parts[$i]] = $URL_Parts[++$i];
    25.             }
    26.         }
    27.     }
    28.     define('MODULE',$module);
    29.     define('PAGE',$page);
    30.     if(MODULE == "option"){
    31.         $option = ROOT.'location/'.PAGE.'.class.php';
    32.         if(!empty($page)){
    33.             require_once $option;
    34.             $view = new $page;
    35.             $view = $view->getBody();
    36.             echo $view;
    37.         }else{
    38.             echo "home page";
    39.         }
    40.     }
    41.     elseif(MODULE == "admin"){
    42.         echo "admin";
    43.     }
    44.     elseif(MODULE == "404"){
    45.         echo "404";
    46.     }
    47.     else{
    48.         echo "page";
    49.     }
    50. }
    51.  
    52. function loadTool($name, $sub_dir = '')
    53. {
    54.     global $tools;
    55.  
    56.     if (in_array($name, $tools))
    57.         return;
    58.  
    59.     $tools[] = $name;
    60.  
    61.     require( MCR_ROOT . 'system/' . $sub_dir . $name);
    62. }
    63. abstract class Core {
    64.     public function __construct(){
    65.     }
    66.     private function ReplaceStringContent($tmp_content){
    67.         return str_replace("{content}",$this->getContent(), $tmp_content);
    68.     }
    69.     
    70.     private function ReplaceString($tmp_content){
    71.         preg_match_all("/{file=(.*)}/",$tmp_content, $array_result);
    72.         for($i=0;$i < count($array_result[0]);$i++){
    73.             
    74.             $path_include = ROOT."/templates/".TMP_NAME."/".$array_result[1][$i].".tpl";
    75.             if(file_exists($path_include)){
    76.                 $content_file = file_get_contents($path_include);
    77.                 $tmp_content = str_replace($array_result[0][$i],$content_file,$tmp_content);
    78.             }
    79.         }
    80.         return $tmp_content;
    81.     }
    82.         function getRoot(){
    83.         return 'templates/'.TMP_NAME.'/';
    84.     }
    85.     private function ReplaceStringRoot($tmp_content){
    86.         return str_replace("{root}",$this->getRoot(), $tmp_content);
    87.     }
    88.     
    89.     private function ReplaceStringTitle($tmp_content){
    90.         return str_replace("{title}",$this->getTitle(), $tmp_content);
    91.     }
    92.     private function ReplaceStringSN($tmp_content){
    93.         return str_replace("{site_name}",SITE_NAME, $tmp_content);
    94.     }
    95.     
    96.     private function ReplaceStringLogin($tmp_content){
    97.         if(isset($_SESSION['login'])){
    98.         $tmp_content =  str_replace("{login}",$_SESSION['login'], $tmp_content);
    99.         }
    100.         return $tmp_content;
    101.     }
    102.     
    103.     private function ReplaceStringMenu($tmp_content){
    104.         return str_replace("{menu}",$this->menu(),$tmp_content);
    105.     }
    106.     
    107.     private function ReplaceStringlogged($tmp_content){
    108.         if(isset($_SESSION['login'])){
    109.             preg_match_all("/{is_notlogged}(.*){\/is_notlogged}/s",$tmp_content,$logged);
    110.             $tmp_content = str_replace($logged[0],"",$tmp_content);
    111.             $tmp_content = str_replace("{is_logged}","",$tmp_content);
    112.             $tmp_content = str_replace("{/is_logged}","",$tmp_content);
    113.         }else{
    114.             preg_match_all("/{is_logged}(.*){\/is_logged}/s",$tmp_content,$logged);
    115.             $tmp_content = str_replace($logged[0],"",$tmp_content);
    116.             $tmp_content = str_replace("{is_notlogged}","",$tmp_content);
    117.             $tmp_content = str_replace("{/is_notlogged}","",$tmp_content);
    118.         }
    119.         return $tmp_content;
    120.     }
    121.     private function ReplaceStringPage($tmp_content){
    122.         preg_match("/{page=([a-z,]+)?}([^{}]+){\/page}/i",$tmp_content,$m);
    123.         if(!empty($m)){
    124.             if(!isset($_GET['page'])){
    125.                 if(isset($_GET['option']) == in_array($_GET['option'],explode(',', $m[1]))){
    126.                     $tmp_content = str_replace("{page=".$m[1]."}","",$tmp_content);
    127.                     $tmp_content = str_replace("{/page}","",$tmp_content);
    128.                 }else{
    129.                     $tmp_content = str_replace($m[0],"",$tmp_content);
    130.                 }
    131.             }else{
    132.                 $tmp_content = str_replace($m[0],"",$tmp_content);
    133.             }
    134.         }
    135.         return $tmp_content;
    136.     }
    137.     private function ReplaceStringHomePage($tmp_content){
    138.         preg_match("/{homepage}([^{}]+){\/homepage}/i",$tmp_content,$m);
    139.         if(!empty($m)){
    140.             if(isset($_GET['option'])){
    141.                 if($_GET['option'] == "index" || $_GET['option'] == "category" || $_GET['option'] == "page" || $_GET['option'] == "search"){
    142.                     $tmp_content = str_replace("{homepage}","",$tmp_content);
    143.                     $tmp_content = str_replace("{/homepage}","",$tmp_content);
    144.                 }else{
    145.                     $tmp_content = str_replace($m,"",$tmp_content);
    146.                 }
    147.             }else{
    148.                 $tmp_content = str_replace($m,"",$tmp_content);
    149.             }
    150.         }
    151.         return $tmp_content;
    152.     }
    153.     
    154.     private function ReplaceStringGroup($tmp_content){
    155.         //preg_match_all("/{group=(.*)}/d",$tmp_content,$gr);
    156.         //preg_match_all($gr[0]."/(.*){\/group}/s",$tmp_content,$group);
    157.         preg_match('/{group=(\d+)}(.*){\/group}/s', $tmp_content, $m);
    158.         if(!empty($m)){
    159.             if(isset($_SESSION['login'])){
    160.                 $user=$_SESSION['login'];
    161.                 global $mysqli;
    162.                 $groups = $mysqli->query("SELECT group_id FROM users WHERE login = '$user'");
    163.                 $groups = $groups->fetch_assoc();
    164.                 if($groups['group_id'] < $m[1]+1){
    165.                     $tmp_content = str_replace("{group=".$m[1]."}","",$tmp_content);
    166.                     $tmp_content = str_replace("{/group}","",$tmp_content);
    167.                 }else{
    168.                 $tmp_content = str_replace($m[0],"",$tmp_content);
    169.                 }
    170.             }else{
    171.                 $tmp_content = str_replace($m[0],"",$tmp_content);
    172.             }
    173.         }
    174.         return $tmp_content;
    175.     }
    176.     
    177.     private function ReplaceStringNotGroup($tmp_content){
    178.         preg_match('/{notgroup=(\d+)}(.*){\/notgroup}/s', $tmp_content, $m);
    179.         if(!empty($m)){
    180.         if(isset($_SESSION['login'])){
    181.                 $user=$_SESSION['login'];
    182.                 global $mysqli;
    183.                 $groups = $mysqli->query("SELECT group_id FROM users WHERE login = '$user'");
    184.                 $groups = $groups->fetch_assoc();
    185.                 if($groups['group_id'] > $m[1]+1){
    186.                     $tmp_content = str_replace("{notgroup=".$m[1]."}","",$tmp_content);
    187.                     $tmp_content = str_replace("{/notgroup}","",$tmp_content);
    188.                 }else{
    189.                 $tmp_content = str_replace($m[0],"",$tmp_content);
    190.                 }
    191.             }else{
    192.                 $tmp_content = str_replace($m,"",$tmp_content);
    193.             }
    194.         }
    195.         return $tmp_content;
    196.     }
    197.     private function ReplaceStringError($tmp_content){
    198.         preg_match('/{error}/', $tmp_content, $m);
    199.         if(isset($_POST['error'])){
    200.             return str_replace($m[0],$_POST['error'],$tmp_content);
    201.         }else{
    202.             return str_replace($m[0],"",$tmp_content);
    203.         }
    204.     }
    205.         
    206.     public function getBody() {
    207.         $tmp_url = ROOT.'templates/'.TMP_NAME.'/main.tpl';
    208.         if(file_exists($tmp_url)){
    209.             $tmp = file_get_contents($tmp_url);
    210.             $tmp = $this ->ReplaceString($tmp);
    211.             $tmp = $this ->ReplaceString($tmp);
    212.             
    213.             $tmp = $this ->ReplaceStringTitle($tmp);
    214.             $tmp = $this ->ReplaceStringSN($tmp);
    215.             $tmp = $this ->ReplaceStringContent($tmp);
    216.             $tmp = $this ->ReplaceStringRoot($tmp);
    217.             //$tmp = $this->ReplaceStringMenu($tmp);
    218.             $tmp = $this->ReplaceStringPage($tmp);
    219.             $tmp = $this->ReplaceStringGroup($tmp);
    220.             $tmp = $this ->ReplaceStringNotGroup($tmp);
    221.             $tmp = $this->ReplaceStringHomePage($tmp);
    222.             $tmp = $this->ReplaceStringError($tmp);
    223.             
    224.             $tmp = $this ->ReplaceStringlogged($tmp);
    225.             $tmp = $this ->ReplaceStringLogin($tmp);
    226.             return $tmp;
    227.             
    228.             //file_put_contents($save, $tmp);
    229.             //require_once $save;
    230.             
    231.             //unlink($save);
    232.             
    233.         }else{
    234.             throw new Exception("Шаблон не найден или не существует");
    235.         }
    236.     }
    237.     abstract function getContent();
    238.     abstract function getTitle();
    239. }
    240. ?>
    [/code]
    {/code}
     
  2. Ganzal

    Ganzal Суперстар
    Команда форума Модератор

    С нами с:
    15 мар 2007
    Сообщения:
    9.893
    Симпатии:
    965
    Код (Text):
    1. truncate code.php --size 0
     
  3. Fell-x27

    Fell-x27 Суперстар
    Команда форума Модератор

    С нами с:
    25 июл 2013
    Сообщения:
    12.156
    Симпатии:
    1.771
    Адрес:
    :сердА
    А что вам в нем не нравится? Вы его сами писали?