/**
  * build template path for given controller and method name
  *
  * @param string $controllerName
  * @param string $methodName
  * @param bool $forceRebuild
  */
 protected function buildTemplatePath($controllerClass, $methodName, $forceRebuild = false)
 {
     wfProfileIn(__METHOD__);
     if ($this->templatePath == null || $forceRebuild) {
         $app = F::app();
         $autoloadClasses = $app->wg->AutoloadClasses;
         if (!empty($this->response)) {
             $extension = $this->response->getTemplateEngine();
         } else {
             $extension = WikiaResponse::TEMPLATE_ENGINE_PHP;
         }
         // Service classes must be dispatched by full name otherwise we default to a controller.
         $controllerBaseName = $app->getBaseName($controllerClass);
         if ($app->isService($controllerClass)) {
             $controllerClass = $app->getServiceClassName($controllerBaseName);
         } else {
             $controllerClass = $app->getControllerClassName($controllerBaseName);
         }
         if (empty($autoloadClasses[$controllerClass])) {
             throw new WikiaException("Invalid controller or service name: {$controllerClass}");
         }
         // First we look for BaseName_MethodName
         $dirName = dirname($autoloadClasses[$controllerClass]);
         $basePath = "{$dirName}/templates/{$controllerBaseName}_{$methodName}";
         $templatePath = null;
         /**
          * per-skin template override (experimental)
          * @see $wgEnableSkinTemplateOverride
          */
         if (!empty($this->response)) {
             $requestedSkinName = $this->response->getSkinName();
             if (!empty($requestedSkinName)) {
                 $skinSpecificPath = "{$basePath}_{$requestedSkinName}.{$extension}";
                 if (file_exists($skinSpecificPath)) {
                     $templatePath = $skinSpecificPath;
                 }
             }
         }
         if (empty($templatePath)) {
             $templatePath = "{$basePath}.{$extension}";
         }
         // First we look for BaseName_MethodName
         $templateExists = file_exists($templatePath);
         // Fall back to ControllerClass_MethodName
         if (!$templateExists) {
             $templatePath = "{$dirName}/templates/{$controllerClass}_{$methodName}.{$extension}";
             $templateExists = file_exists($templatePath);
         }
         if (!$templateExists) {
             throw new WikiaException("Template file not found: {$templatePath}");
         }
         $this->setTemplatePath($templatePath);
     }
     wfProfileOut(__METHOD__);
 }