This commit is contained in:
2023-10-05 13:03:17 +03:00
parent 718cdb54f9
commit e037f770e5
9 changed files with 280 additions and 236 deletions
+7 -35
View File
@@ -1,51 +1,23 @@
<?php namespace Lucent\StaticGenerator;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\View\View;
class StaticGenerator
{
public function __construct(
public Writer $writer,
public Filesystem $filesystem
)
{
}
public function save(string $path, string $html): void
public function run(callable $callback): void
{
$path = trim($path, "/");
$filepath = public_path($path . "/index.html");
if (!file_exists(pathinfo($filepath, PATHINFO_DIRNAME))) {
make_dir_r(pathinfo($filepath, PATHINFO_DIRNAME));
}
file_put_contents($filepath, $html);
}
public function recordIterator(callable $query, callable $parser, int $skip = 0): int
{
$limit = 100;
// logger("fetching $skip");
$records = $query($limit, $skip);
$parser($records);
if ($records->hasResults()) {
return $this->recordIterator($query, $parser, $skip + $limit);
}
return 0;
}
public function createRedirect(string $from, string $to, string $title = "Redirecting", string $message = "Redirecting Soon..."): void
{
$html = view("lucent::redirect", [
"to" => $to,
"title" => $title,
"message" => $message,
])->render();
$this->save($from, $html);
echo "Start".PHP_EOL;
$callback($this->writer);
echo "Finito".PHP_EOL;
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php namespace Lucent\StaticGenerator;
use Illuminate\Contracts\View\View;
class Writer
{
public function __construct(
)
{
}
public function save(string $path, string $html): void
{
$path = trim($path, "/");
$filepath = public_path($path . "/index.html");
if (!file_exists(pathinfo($filepath, PATHINFO_DIRNAME))) {
make_dir_r(pathinfo($filepath, PATHINFO_DIRNAME));
}
file_put_contents($filepath, $html);
echo "Path: /$path".PHP_EOL;
}
public function recordIterator(callable $query, callable $parser, int $skip = 0): int
{
$limit = 100;
// logger("fetching $skip");
$records = $query($limit, $skip);
$parser($records);
if ($records->hasResults()) {
return $this->recordIterator($query, $parser, $skip + $limit);
}
return 0;
}
public function createRedirect(string $from, string $to, string $title = "Redirecting", string $message = "Redirecting Soon..."): void
{
$html = view("lucent::redirect", [
"to" => $to,
"title" => $title,
"message" => $message,
])->render();
$this->save($from, $html);
}
}