<?php

namespace auctionforge\inc\helpers;

class View
{
    /**
     * Render view file
     * @param $fileName string
     * @param $params array
     * @return false|string
     */
    public static function render($fileName, $params)
    {
        $template = self::template($fileName);
        if (file_exists($template)) {
            extract($params);
            ob_start();
            require($template);
            return ob_get_clean();
        } else {
            return "template " . $fileName . " not found";
        }
    }

    /**
     * Prepare name to file name
     * @param $name string
     * @return string
     */
    protected static function template($name)
    {
        $template = get_stylesheet_directory();
        $name = preg_replace("/\./", "/", $name);

        $base = __DIR__ . "/../../" . $name . ".php";
        $reload = $template . '/' . preg_replace('/(templates)/', 'auctionforge', $name, 1) . ".php";

        if (file_exists($reload)) {
            return $reload;
        } else {
            return $base;
        }
    }
}