<?php
// 현재 파일의 물리적 상대경로를 상수로 저장
define('ROOT_PATH', dirname(__FILE__));
$GLOBALS['content'] = '';

$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2);
$uri = trim($request_uri[0], '/');
$uri_array = explode('/', $uri);

// 기본적으로 'main' 페이지로 설정
if(empty($uri_array[0])) $uri_array = ['main']; // $uri_array[0]이 비어있으면 $uri_array[0]에 'main'을 저장
$layout_name = $uri_array[0]; // $uri_array[0]의 값을 $layout_name에 저장
$page = ROOT_PATH . '/page/'.implode('-', $uri_array) . '.html';

if (file_exists($page)) {

    ob_start();
    include $page; // page 내용 로드하면서 layout() 실행해서 layout 에 page 내용 렌더링
    $GLOBALS['content'] = ob_get_clean();
    // 만약 $layout_name 이 '404', 'contact', 'previous-privacy', 'privacy' 중 하나라면 $layout_name 을 'etc'로 변경
    if(in_array($layout_name, ['404', 'contact', 'previous-privacy', 'privacy'])) $layout_name = 'etc';
    include ROOT_PATH.'/layout/layout-'.$layout_name.'.html';
    exit();
} else {
    echo '['.$page.'] Page Not Found'; // 에러 페이지 출력 등의 처리
    exit();
}

?>
