/** * Generates a link to a file at the given path. * * @param string $path Path to the file. * @param Page $fromPage Page which will link to that file. * * @return string */ public function generateLink($path, Page $fromPage) { $relativeTo = $fromPage->getOutputFile()->getPath(); $relativeTo = rtrim($relativeTo, DS) . DS; if (stripos($relativeTo, $this->webDir) !== 0) { throw new \RuntimeException('The rendered page is not inside the web dir!'); } $relativeTo = mb_substr($relativeTo, mb_strlen($this->webDir)); $path = ltrim($path, '/'); $relative = ''; foreach (explode('/', $relativeTo) as $dir) { $relative .= !empty($dir) ? '../' : ''; } return $relative . $path; }
/** * Creates a Page object. * * @param string $template Page's template name. * @param array $parameters Parameters visible in this page. * @param string $outputFile Target file. * * @return Page */ public function createPage($template, array $parameters = array(), $outputFile = null) { $page = new Page(); // if absolute path given then take a name from it if (mb_substr($template, 0, 1) === DS) { $page->setTemplateFile(new SplFileInfo($template)); $page->setTemplateName($this->templateNameFromPath($template)); // if relative path given then build template location from the name } else { $page->setTemplateFile(new SplFileInfo($this->templatesDir . $template)); $page->setTemplateName($template); } // if no output file given then build it automatically if (!$outputFile) { $outputPath = $this->outputPathFromTemplate($page->getTemplateName()); $page->setOutputFile(new SplFileInfo($outputPath)); $page->setOutputName($this->outputNameFromPath($outputPath)); // if absolute path to output file given then take a name from it } elseif (mb_substr($outputFile, 0, 1) === DS) { $page->setOutputFile(new SplFileInfo($outputFile)); $page->setOutputName($this->outputNameFromPath($outputFile)); // if relative path to output given then build output location from the name } else { $page->setOutputFile(new SplFileInfo($this->webDir . $outputFile)); $page->setOutputName($outputFile); } $page->setParameters($parameters); return $page; }