알수없음

웹 프레임워크 공부

워드 프레스에서 $something->on('...', function ... ); 이런식으로 페이지를 확장하는걸 보고,

 

이걸 웹 프레임워크 전체에 적용하면 어떻게 되는건가 싶어서 실험 삼아 만들어 보았습니당.

 

현재 사용자 코드 경로 캐슁을 하나도 안하기 때문에 응답 속도가 느려질 수 있어요.

실무에 적용할 수는 없는 그저 실험용 코드입니다.

 

코드 리뷰 부탁드립니다!!

 

테스트용 비지니스(?) 코드

$Core = \gwf\Core::core();

$Core->on('post:init', function(gwf\Route $Router) {
    $Router->define('/')
        ->layout('example')
        ->content(__DIR__ . '/main.php');
    
    $Router->define('/sub')
        ->layout('example.sub')
        ->content(__DIR__ . '/sub.php');
});

 

테스트용 레이아웃 모듈 코드.

class LayoutModule {
    private $Route;
    
    function __construct(gwf\Route $Route) {
        $this->Route = $Route;
        
        /* 기본 레이아웃 설정. */
        $Route->layout = "example";
        $Route->on('route:define', function($Path, $Template) {
            /* 레이아웃 설정을 상속 시킵니다. */
            $Template->layout = $this->Route->layout;
        });
        
        $Route->on('pre:render', function(gwf\Route $Route) {
            ob_start();
        });
        
        $Route->on('post:render', function(gwf\Route $Route) {
            $Contents = ob_get_contents();
            ob_end_clean();

            if (!$Route->contentType || $Route->contentType == 'text/html') {
                if (isset($Route->layout) &&
                    file_exists($Layout = GWF_ROOT . "/layouts/{$Route->layout}.php")) 
                {
                    if (!is_array($Route->stylesheets)) { 
                        $Route->stylesheets = []; 
                    }
                    if (!is_array($Route->scripts)) { 
                        $Route->scripts = [];
                    }

                    $Title = $Route->title;
                    $Stylesheets = $Route->stylesheets;
                    $Scripts = $Route->scripts;

                    ob_start();
                    include ($Layout);
                    $Contents = ob_get_contents();
                    ob_end_clean();
                }
            }
            
            echo $Contents;
        });
    }
 
    /*
     * 페이지 레이아웃을 지정합니다.
     * */
    function layout($Name = null) {
        if (!$Name) {
            if (isset($this->Route->layout))
                return $this->Route->layout;
            
            return null;
        }
        
        $this->Route->layout = $Name;
        return $this->Route;
    }
}

/* 레이아웃 모듈을 등록합니다. */
\gwf\RouteTemplate::module(LayoutModule::class);

 

감사합니다!

 

풀코드: https://drive.google.com/file/d/1j70ITG4X_JTS9KbejTEwlG3mxoZd6n36/view?usp=sharing

|

댓글 2개

전...
알수없음
저도 무슨 말인지.. 모르겠네요 ㅎㅎ
댓글을 작성하시려면 로그인이 필요합니다. 로그인

자유게시판

+
제목 글쓴이 날짜 조회
5년 전 조회 855
5년 전 조회 1,132
5년 전 조회 1,162
5년 전 조회 1,280
5년 전 조회 1,138
5년 전 조회 1,272
5년 전 조회 1,095
5년 전 조회 1,088
5년 전 조회 2,740
5년 전 조회 1,116
5년 전 조회 1,168
5년 전 조회 1,025
5년 전 조회 1,241
5년 전 조회 1,117
5년 전 조회 1,102
5년 전 조회 1,138
5년 전 조회 1,200
5년 전 조회 2,561
5년 전 조회 1,082
5년 전 조회 1,432
5년 전 조회 1,091
5년 전 조회 1,340
5년 전 조회 1,124
5년 전 조회 1,237
5년 전 조회 1,129
5년 전 조회 1,139
5년 전 조회 2,367
5년 전 조회 1,290
5년 전 조회 1,351
5년 전 조회 1,081
🐛 버그신고