<?php

    namespace App\System\Basic;

    class beViewer {
        static $bePath = "",$beCache = "",$bePrefix = "",$beViewName = "",$beViewPath = "",$beExtented = false;
        static $beView,$beData,$beExtends = [],$beYields = [],$beSections = [];
        //@ioncube.dk beEncrypt("be_diyocp","beSetParams") -> "a85b4e98a32f2ed0241050fe3c935f6f" RANDOM
        public static function beSetParams($path = "",$cache = "",$prefix = "be"){   
            self::$bePath = BE_APPPATH.$path;
            self::$beCache = BE_APPPATH.$cache;
            self::$bePrefix = $prefix;
        }
        //@ioncube.dk beEncrypt("be_diyocp","beGetViewRaw") -> "a4472ba65204d5c20cacb084cd27aba3" RANDOM
        public static function beGetViewRaw($text,$data = []){
            self::$beView = $text;
            self::beParse();
            extract($data);
            ob_start();
            eval(" ?>".self::$beView."<?php ");
            return ob_get_clean();
        }
        //@ioncube.dk beEncrypt("be_diyocp","beGetView") -> "54627337dfe9d6cb640acfc85f110025" RANDOM
        public static function beGetView($name,$data = [],$extend = false){
            global $be;
            $path = str_replace(".","/",$name);
            $theme = $be->settings["system_themename"];
            $viewfile = self::$bePath."/".$theme."/".$path.".".self::$bePrefix.".php";
            if(!file_exists($viewfile)){
                $viewfile = self::$bePath."/".$path.".".self::$bePrefix.".php";
            }
            if(!$extend){
                self::$beViewName = $name;
                self::$beViewPath = $viewfile;
                self::$beData = $data;
            }
            $cachefile = self::$beCache."/".self::$beViewName.".".self::$bePrefix.".php";
            if(BE_SYSTEMMODE & BE_DEVELOPMENT){
                if(!file_exists($cachefile)){
                    self::$beView = file_get_contents($viewfile);
                    self::beParse();
                    if(self::$beExtented == false){
                        if($extend){
                            self::$beExtented = true;
                        }
                        if(!file_put_contents($cachefile,self::$beView)){
                            echo "Bir yazma hatası oluştu lütfen yönetici ile iletişime geçin";
                            exit;
                        }
                    }
                }
                else{
                    $cachetime = filemtime($cachefile);
                    if($cachetime < filemtime($viewfile) || $cachetime < filemtime(self::$beViewPath)){
                        self::$beView = file_get_contents($viewfile);
                        self::beParse();
                        if(self::$beExtented == false){
                            if($extend){
                                self::$beExtented = true;
                            }
                            if(!file_put_contents($cachefile,self::$beView)){
                                echo "Bir yazma hatası oluştu lütfen yönetici ile iletişime geçin";
                                exit;
                            }
                        }
                    }
                }
            }

            global $be,$headerinclude,$header,$footer;
            
            extract($data);
            ob_start();
            require $cachefile;
            return ob_get_clean();

        }
        //@ioncube.dk beEncrypt("be_diyocp","beParse") -> "ba7de65ed63a13f519d37e976e3b8817" RANDOM
        public static function beParse(){
            self::beParseEcho();
            self::beParsePhp();
            self::beParseForeach();
            self::beParseFor();
            self::beParseWhile();
            self::beParseIf();
            self::beParseContinue();
            self::beParseBreak();
            self::beParseSection();
            self::beParseExtend();
            self::beParseYield();
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseEcho") -> "de9df9a8161e41599ebeaa8a13f29c02" RANDOM
        public static function beParseEcho(){
            self::$beView = preg_replace_callback("/{{(.*?)}}/",function($data){
                return "<?= ".trim($data[1])." ?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParsePhp") -> "f6243ef29ffce754e26e3b71dd0fd930" RANDOM
        public static function beParsePhp(){
            self::$beView = preg_replace_callback("/@php/",function($data){
                return "<?php";
            },self::$beView);
            self::$beView = preg_replace_callback("/@endphp/",function($data){
                return "?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseForeach") -> "e497966e380a4e3f38b1be4fa14ecc55" RANDOM
        public static function beParseForeach(){
            self::$beView = preg_replace_callback("/@foreach(.*)/",function($data){
                return "<?php foreach".trim($data[1]).": ?>";
            },self::$beView);
            self::$beView = preg_replace_callback("/@endforeach/",function($data){
                return "<?php endforeach; ?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseFor") -> "5ac88a0cbd27d14cb60d71cc48a8eda9" RANDOM
        public static function beParseFor(){
            self::$beView = preg_replace_callback("/@for(.*)/",function($data){
                return "<?php for".trim($data[1]).": ?>";
            },self::$beView);
            self::$beView = preg_replace_callback("/@endfor/",function($data){
                return "<?php endfor; ?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseWhile") -> "05c6488e9af2e3c32ac5499db3dcde87" RANDOM
        public static function beParseWhile(){
            self::$beView = preg_replace_callback("/@while(.*)/",function($data){
                return "<?php while".trim($data[1]).": ?>";
            },self::$beView);
            self::$beView = preg_replace_callback("/@endwhile/",function($data){
                return "<?php endwhile; ?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseIf") -> "4d58db133ea38254c88c5534b75db572" RANDOM
        public static function beParseIf(){
            self::$beView = preg_replace("/@(else|)if\((.*)\)/","<?php $1if($2): ?>",self::$beView);
            self::$beView = preg_replace("/@else/","<?php else: ?>",self::$beView);
            self::$beView = preg_replace("/@endif/","<?php endif; ?>",self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseContinue") -> "9eaf775c8ab67841ecc184e583bf2ab8" RANDOM
        public static function beParseContinue(){
            self::$beView = preg_replace_callback("/@continue/",function($data){
                return "<?php continue; ?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseBreak") -> "4caa4d0330fe95664b56381ac19bfee4" RANDOM
        public static function beParseBreak(){
            self::$beView = preg_replace_callback("/@break/",function($data){
                return "<?php break; ?>";
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseExtend") -> "cb3d6df2b5189613da34ffbcadc3f30c" RANDOM
        public static function beParseExtend(){
            self::$beView = preg_replace_callback("/@extend\('(.*?)'\)/",function($data){
                return self::beGetView($data[1],self::$beData,true);
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseYield") -> "e3be4ae219a8799c71adbde52c6c1afe" RANDOM
        public static function beParseYield(){
            self::$beView = preg_replace_callback("/@yield\('(.*?)'\)/",function($data){
                return self::$beSections[$data[1]];
            },self::$beView);
        }
        //@ioncube.dk beEncrypt("be_diyocp","beParseSection") -> "cd59891b6d41f5bb943aa0b28743cd6e" RANDOM
        public static function beParseSection(){
            self::$beView = preg_replace_callback("/@section\('(.*?)'\)(.*?)@endsection/s",function($data){
                self::$beSections[$data[1]] = trim($data[2]);
                return "";
            },self::$beView);
        }
    }
?>