<?php

namespace App\Framework\View\Loading\Resolvers;

final readonly class ControllerResolver implements TemplateResolverStrategy
{
    const string TEMPLATE_EXTENSION = '.view.php';

    public function resolve(string $template, ?string $controllerClass = null): ?string
    {
        if(!$controllerClass) {
            return null;
        }

        try {
            $rc = new \ReflectionClass($controllerClass);
            $dir = dirname($rc->getFileName());
            $path = $dir . DIRECTORY_SEPARATOR . $template . self::TEMPLATE_EXTENSION;
            return file_exists($path) ? $path : null;
        } catch (\ReflectionException) {
            return null;
        }
    }
}
