/** * 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; }
/** * Generates a static page from the given Page object. * * @param Page $page Page object. * * @return string */ public function generatePage(Page $page) { if (!$page->getTemplateFile()->isFile()) { throw new NotFoundException('Could not find template to render: ' . $page->getTemplateFile()->getPathname()); } $output = $this->templating->render($page->getTemplateName(), array_merge($page->getParameters(), array('_genry_page' => $page))); // trigger event $event = new PageRendered($page, $output); $this->eventManager->trigger($event); // update the final output with the output from the event $output = $event->getOutput(); // make sure that the dir the output will be saved, exists $outputDir = $page->getOutputFile()->getPath(); if (!is_dir($outputDir)) { mkdir($outputDir, 0755, true); } file_put_contents($page->getOutputFile()->getPathname(), $output); $this->logger->debug('Generated {output} from {template}', array('output' => $page->getOutputName(), 'template' => $page->getTemplateName())); return $output; }