Skip to content

Commit

Permalink
Fix date to string format (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Aug 10, 2022
1 parent d426056 commit d5664b7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Statamic\StaticSite;

use Carbon\Carbon;
use Spatie\Fork\Fork;
use Statamic\Statamic;
use Facades\Statamic\View\Cascade;
use Statamic\Facades\URL;
use Statamic\Support\Str;
Expand Down Expand Up @@ -258,6 +260,13 @@ protected function makeContentGenerationClosures($pages, $request)
$errors = [];

foreach ($pages as $page) {
// There is no getter method, so use reflection.
$oldCarbonFormat = (new \ReflectionClass(Carbon::class))->getStaticPropertyValue('toStringFormat');

if ($this->shouldSetCarbonFormat($page)) {
Carbon::setToStringFormat(Statamic::dateFormat());
}

$this->updateCurrentSite($page->site());

view()->getFinder()->setPaths($this->viewPaths);
Expand All @@ -277,6 +286,8 @@ protected function makeContentGenerationClosures($pages, $request)

$errors[] = $e->consoleMessage();
continue;
} finally {
Carbon::setToStringFormat($oldCarbonFormat);
}

if ($generated->hasWarning()) {
Expand Down Expand Up @@ -385,7 +396,7 @@ protected function routes()
&& ! Str::contains($route->uri(), '{');
})->map(function ($route) {
$url = URL::tidy(Str::start($route->uri(), $this->config['base_url'].'/'));
return $this->createPage(new Route($url));
return $this->createPage(new StatamicRoute($url));
});
}

Expand Down Expand Up @@ -424,4 +435,13 @@ protected function shouldFail($item)

return $config === 'warnings';
}

protected function shouldSetCarbonFormat($page)
{
$content = $page->content();

return $content instanceof \Statamic\Contracts\Entries\Entry
|| $content instanceof \Statamic\Contracts\Taxonomies\Term
|| $content instanceof StatamicRoute;
}
}
5 changes: 5 additions & 0 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function __construct(Filesystem $files, array $config, $content)
$this->content = $content;
}

public function content()
{
return $this->content;
}

public function isGeneratable()
{
return $this->content->published();
Expand Down
8 changes: 8 additions & 0 deletions src/StatamicRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Statamic\StaticSite;

class StatamicRoute extends Route
{
//
}

0 comments on commit d5664b7

Please sign in to comment.