<?php

class Callback
{
    protected $themeDir;

    public function __construct($themeDir)
    {
        $this->themeDir = $themeDir;
    }

    public function replace($matches)
    {
        $path = $this->themeDir . '/' . $matches[3] . $matches[4];
        if (file_exists($path)) return file_get_contents($path);
        return '';
    }
}

class HookCallback extends Callback
{
    private $hookList;

    public function __construct($themeDir, $hookList)
    {
        parent::__construct($themeDir);
        
        $this->hookList = $hookList;
    }

    public function replace($matches)
    {
        $path = $this->themeDir . '/' . $matches[3] . $matches[4];
        if (file_exists($path))
        {
            $content = file_get_contents($path);
            $pattern = '~(\{assign var=hookName value=[\'"])(.*?)([\'"])([^\}]*)(\})~';
            $result = preg_replace_callback($pattern, array($this, 'hookReplace'), $content);
            return $result;	
        }
        return '';
    }

    public function hookReplace($matches)
    {
        $hook_name = $matches[2];
        $result = HookController::parseHookOutput($this->themeDir, $hook_name, $this->hookList);
        return $result;
    }
}
