Skip to content

Commit

Permalink
Enable additional routes to be built
Browse files Browse the repository at this point in the history
  • Loading branch information
mogelbrod committed May 18, 2016
1 parent 93f063a commit bfe8605
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion core/builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function __construct() {
$this->filter = $filter;
}

$this->additionalRoutes = c::get('plugin.staticbuilder.additionalRoutes', []);

// Normalize assets config
$assetConf = c::get('plugin.staticbuilder.assets', static::$defaultAssets);
$assets = [];
Expand Down Expand Up @@ -227,11 +229,11 @@ protected function buildPage(Page $page, $write=false) {

// Render page
$text = $this->kirby->render($page, [], false);
$log['size'] = strlen($text);

// Write page content
f::write($target, $text);
$log['status'] = 'generated';
$log['size'] = strlen($text);

// Copy page files in a folder
foreach ($page->files() as $file) {
Expand All @@ -243,6 +245,57 @@ protected function buildPage(Page $page, $write=false) {
return $this->summary[] = $log;
}

protected function buildRoute($uri, $write=false) {
if (!is_string($uri)) {
return false;
}
$log = [
'type' => 'route',
'status' => '',
'reason' => '',
'source' => $uri,
'dest' => 'static/',
'size' => null,
];
$target = $this->normalizePath( $this->folder . DS . $uri . (substr($uri, -1) == '/' ? $this->suffix : ''));
$log['dest'] .= str_replace($this->folder . DS, '', $target);

if ($write == false) {
// Get status of output path
if (is_file($target)) {
$log['status'] = 'outdated';
$log['size'] = filesize($target);
}
} else {
$this->lastpage = $log['source'];

// Temporarily override request method to ensure correct route is found
$requestMethod = $_SERVER['REQUEST_METHOD'];
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->kirby->site()->visit($uri);
$route = $this->kirby->router->run($uri);

if (is_null($route)) {
// Unmatched route
$log['status'] = 'missing';
} else {
// Grab route output using output buffering
ob_start();
$response = call($route->action(), $route->arguments());
$text = ob_get_contents();
ob_end_clean();

// Write page content
f::write($target, $text);
$log['status'] = 'generated';
$log['size'] = strlen($text);
}

$_SERVER['REQUEST_METHOD'] = $requestMethod;
}
return $this->summary[] = $log;
}

/**
* Copy a file or folder to the static directory
* This function is responsible for normalizing paths and making sure
Expand Down Expand Up @@ -373,6 +426,9 @@ public function write($content) {
foreach ($this->assets as $from=>$to) {
$this->copyAsset($from, $to, true);
}
foreach ($this->additionalRoutes as $route) {
$this->buildRoute($route, true);
}
}
foreach($this->getPages($content) as $page) {
$this->buildPage($page, true);
Expand All @@ -396,6 +452,9 @@ public function dryrun($content) {
foreach ($this->assets as $from=>$to) {
$this->copyAsset($from, $to, false);
}
foreach ($this->additionalRoutes as $route) {
$this->buildRoute($route, false);
}
}
foreach($this->getPages($content) as $page) {
$this->buildPage($page, false);
Expand Down

0 comments on commit bfe8605

Please sign in to comment.