/
SiteGenerateCommand.php
262 lines (210 loc) · 9.35 KB
/
SiteGenerateCommand.php
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Plugin\Site\Console\Commands\Site;
use Flextype\Plugin\Site\Site;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use function Thermage\div;
use function Thermage\renderToString;
use function Flextype\registry;
use function Flextype\entries;
use function Flextype\emitter;
use function Glowy\Filesystem\filesystem;
use function Glowy\Strings\strings;
class SiteGenerateCommand extends Command
{
protected function configure(): void
{
$this->setName('site:generate');
$this->setDescription('Generate site.');
$this->addOption('site-path', null, InputOption::VALUE_REQUIRED, 'Destination for generated static site files (without trailing and without starting slash)');
$this->addOption('site-url', null, InputOption::VALUE_REQUIRED, 'Site url (without trailing).');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$elapsedTimeStartPoint = microtime(true);
$sitePath = $input->getOption('site-path') ? $input->getOption('site-path') : registry()->get('plugins.site.settings.static.site_path');
$staticSitePath = FLEXTYPE_ROOT_DIR . '/' . $sitePath;
if ($input->getOption('site-url')) {
registry()->set('flextype.settings.base_url', $input->getOption('site-url'));
registry()->set('flextype.settings.base_path', '');
} else {
registry()->set('flextype.settings.base_url', registry()->get('plugins.site.settings.static.site_url'));
registry()->set('flextype.settings.base_path', '');
}
$staticSitePathMessage = registry()->get('plugins.site.settings.static.site_path');
// Site items.
$items = [];
$site = new Site();
$entries = entries()->fetch('', ['collection' => true, 'find' => ['depth' => '> 0']])
->sortBy('modified_at', 'asc')
->toArray();
foreach ($entries as $entry) {
// Check entry visibility field
if (isset($entry['visibility']) && ($entry['visibility'] === 'draft' || $entry['visibility'] === 'hidden')) {
continue;
}
// Check entry routable field
if (isset($entry['routable']) && ($entry['routable'] === false)) {
continue;
}
// Check entry ignore field
if (isset($entry['ignore']) && ($entry['ignore'] === true)) {
continue;
}
// Check ignore list
if (in_array($entry['id'], (array) registry()->get('plugins.site.settings.static.ignore.entries'))) {
continue;
}
// Add entry to site
$items[] = $entry;
}
// Additions
$additions = (array) registry()->get('plugins.site.settings.static.additions');
foreach ($additions as $addition) {
$items[] = $addition;
}
// Set entry to the SitemapController class property $sitemap
registry()->set('plugins.site.settings.static.entries', $items);
// Run event onSitemapAfterInitialized
emitter()->emit('onStaticSiteAfterInitialized');
// Start site generation process...
$output->write(
renderToString(
div('Static site generation...',
'px-2 pt-1 pb-1')
)
);
// Clear cache.
if (filesystem()->directory(FLEXTYPE_PATH_TMP)->exists()) {
filesystem()->directory(FLEXTYPE_PATH_TMP)->delete();
$output->write(
renderToString(
div('Cache cleared successfully.',
'color-success px-2')
)
);
}
// Create folder for static site.
if (filesystem()->directory($staticSitePath)->exists()) {
filesystem()->directory($staticSitePath)->delete();
filesystem()->directory($staticSitePath)->create(0755, true);
} else {
filesystem()->directory($staticSitePath)->create(0755, true);
}
if (! filesystem()->directory($staticSitePath)->exists()) {
$output->write(
renderToString(
div('Static site folder "' . $staticSitePathMessage . '" wasn\'t created.',
'color-danger px-2 pb-2')
)
);
return Command::FAILURE;
}
// proceed..
$output->write(
renderToString(
div('Static site folder "' . $staticSitePathMessage . '" created successfully.',
'color-success px-2')
)
);
// Iterate through the pages.
foreach(registry()->get('plugins.site.settings.static.entries') as $page) {
// Create page folder.
filesystem()->directory($staticSitePath . '/' . $page['id'])->ensureExists(0755, true);
if (! filesystem()->directory($staticSitePath . '/' . $page['id'])->exists()) {
$output->write(
renderToString(
div('Static site page folder "' . $staticSitePathMessage . '/' . $page['id'] . '" wasn\'t created.',
'color-danger px-2 pb-1')
)
);
return Command::FAILURE;
}
$renderedPage = $site->render($page);
// Save rendered page.
filesystem()->file($staticSitePath . '/' . $page['id'] . '/index.html')->put($renderedPage);
if (! filesystem()->file($staticSitePath . '/' . $page['id'])->exists()) {
$output->write(
renderToString(
div('Page "' . $page['title'] . '" wasn\'t created.',
'color-danger px-2 pb-1')
)
);
return Command::FAILURE;
}
$output->write(
renderToString(
div('Page "' . $page['title'] . '" in ' . $sitePath . '/' . $page['id'] . ' created successfully.',
' color-success px-2')
)
);
}
// Create home page
filesystem()->file($staticSitePath . '/index.html')->put($site->render(entries()->fetch(registry()->get('plugins.site.settings.entries.main'))->toArray()));
if (! filesystem()->file($staticSitePath . '/index.html')->exists()) {
$output->write(
renderToString(
div('Home page wasn\'t created.',
'color-danger px-2 pb-1')
)
);
return Command::FAILURE;
}
$output->write(
renderToString(
div('Home page in ' . $sitePath . ' created successfully.',
' color-success px-2')
)
);
// Proceed assets.
if (filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/assets')->exists()) {
filesystem()->directory($staticSitePath . '/' . FLEXTYPE_PROJECT_NAME . '/assets')->ensureExists(0755, true);
$directories = [];
foreach (filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/assets')->directories() as $directory) {
$directories[] = strings($directory)->replace(FLEXTYPE_PATH_PROJECT . '/assets/', '')->toString();
}
foreach ($directories as $directory) {
if (in_array($directory, registry()->get('plugins.site.settings.static.ignore.assets'))) {
continue;
}
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/assets/' . $directory)->copy($staticSitePath . '/' . FLEXTYPE_PROJECT_NAME . '/assets/' . $directory);
}
filesystem()->file($staticSitePath . '/' . FLEXTYPE_PROJECT_NAME . '/index.html')->put('');
if (! filesystem()->directory($staticSitePath . '/' . FLEXTYPE_PROJECT_NAME . '/assets/')->exists()) {
$output->write(
renderToString(
div('Assets wasn\'t created.',
'color-danger px-2 pb-1')
)
);
return Command::FAILURE;
}
$output->write(
renderToString(
div('Assets in ' . $sitePath . ' created successfully.',
'color-success px-2')
)
);
}
$output->write(
renderToString(
div('Site generated in [b]'. sprintf("%01.4f", microtime(true) - $elapsedTimeStartPoint) .'[/b] seconds.', 'px-2 pt-1 pb-1')
)
);
return Command::SUCCESS;
}
}