-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPage.php
More file actions
170 lines (133 loc) · 4.05 KB
/
Copy pathPage.php
File metadata and controls
170 lines (133 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Statamic\StaticSite;
use Exception;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\RedirectResponse;
use Statamic\Facades\Blink;
use Statamic\Facades\URL;
class Page
{
protected $files;
protected $config;
protected $content;
protected $paginationPageName;
protected $paginationCurrentPage;
public function __construct(Filesystem $files, array $config, $content)
{
$this->files = $files;
$this->config = $config;
$this->content = $content;
}
public function content()
{
return $this->content;
}
public function isGeneratable()
{
return $this->content->published();
}
public function generate($request)
{
try {
$generatedPage = $this->write($request);
} catch (Exception $e) {
throw new NotGeneratedException($this, $e);
}
if ($paginator = $this->detectPaginator($request)) {
$this->writePaginatedPages($request, $paginator);
}
return $generatedPage;
}
protected function write($request)
{
if ($this->paginationPageName) {
$request->merge([$this->paginationPageName => $this->paginationCurrentPage]);
}
try {
$response = $this->content->toResponse($request);
} catch (HttpResponseException $e) {
$response = $e->getResponse();
throw_unless($response instanceof RedirectResponse, $e);
}
$html = $response->getContent();
if (! $this->files->exists($this->directory())) {
// Force the creation, since when running with multiple workers,
// another worker may have created the directory in the meantime.
$this->files->makeDirectory($this->directory(), 0755, true, true);
}
$this->files->put($this->path(), $html);
return new GeneratedPage($this, $response);
}
protected function writePaginatedPages($request, $paginator)
{
collect(range(1, $paginator->lastPage()))->each(function ($pageNumber) use ($request) {
$page = clone $this;
try {
$page
->setPaginationCurrentPage($pageNumber)
->write($request);
} catch (Exception $e) {
throw new NotGeneratedException($page, $e);
}
});
$this->clearPaginator($request);
}
public function directory()
{
return dirname($this->path());
}
public function path()
{
if ($this->is404()) {
return $this->config['destination'].'/404.html';
}
$url = $this->url();
$ext = pathinfo($url, PATHINFO_EXTENSION) ?: 'html';
$url = $this->config['destination'].$url;
if ($ext === 'html') {
$url .= '/index.html';
}
return $url;
}
public function url()
{
$url = $this->content->urlWithoutRedirect();
if ($this->paginationCurrentPage) {
$url = LengthAwarePaginator::generatePaginatedUrl(
$url,
$this->paginationPageName,
$this->paginationCurrentPage
);
}
return $url;
}
public function site()
{
return $this->content->site();
}
public function is404()
{
return $this->url() === URL::tidy('/404');
}
public function setPaginationCurrentPage($currentPage)
{
$this->paginationCurrentPage = $currentPage;
return $this;
}
protected function detectPaginator($request)
{
if ($paginator = Blink::get('tag-paginator')) {
$this->paginationPageName = $paginator->getPageName();
}
$this->clearPaginator($request);
return $paginator;
}
protected function clearPaginator($request)
{
Blink::forget('tag-paginator');
if ($this->paginationPageName) {
$request->forget($this->paginationPageName);
}
}
}