Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the site for each page being generated #47

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\StaticSite;

use Facades\Statamic\View\Cascade;
use Statamic\Facades\URL;
use Statamic\Support\Str;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -141,6 +142,8 @@ protected function createContentFiles()
});

$this->pages()->each(function ($page) use ($request) {
$this->updateCurrentSite($page->site());

view()->getFinder()->setPaths($this->viewPaths);

$this->count++;
Expand Down Expand Up @@ -235,7 +238,7 @@ protected function getCollectionTerms($collection)
protected function urls()
{
return collect($this->config['urls'] ?? [])->map(function ($url) {
$url = Str::start($url, '/');
$url = URL::tidy(Str::start($url, $this->config['base_url'].'/'));
return $this->createPage(new Route($url));
});
}
Expand All @@ -244,4 +247,15 @@ protected function createPage($content)
{
return new Page($this->files, $this->config, $content);
}

protected function updateCurrentSite($site)
{
Site::setCurrent($site->handle());
Cascade::withSite($site);

// Set the locale for dates, carbon, and for the translator.
// This is what happens in Statamic's Localize middleware.
setlocale(LC_TIME, $site->locale());
app()->setLocale($site->shortLocale());
}
}
5 changes: 5 additions & 0 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public function url()
{
return $this->content->url();
}

public function site()
{
return $this->content->site();
}
}
9 changes: 8 additions & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Statamic\StaticSite;

use Illuminate\Contracts\Http\Kernel;
use Statamic\Facades\Site;
use Statamic\Facades\URL;

class Route
{
Expand All @@ -15,7 +17,12 @@ public function __construct($url)

public function url()
{
return $this->url;
return URL::makeRelative($this->url);
}

public function site()
{
return Site::findByUrl($this->url);
}

public function toResponse($request)
Expand Down