Last active
December 7, 2019 14:20
-
-
Save sroehrl/c81a1d90a8db87b55307ea7f791c1de7 to your computer and use it in GitHub Desktop.
PHP blua.blue webhook receiver to generate static pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('path', __DIR__); | |
// (composer require neoan3-apps/ops) | |
require_once path . '/vendor/autoload.php'; | |
function logger($input) | |
{ | |
file_put_contents(__DIR__ . '/log.json', json_encode($input)); | |
} | |
new Receiver('my-secret'); | |
class Receiver | |
{ | |
private $data; | |
private $localArticleLocation; | |
function __construct($mySecret) | |
{ | |
if (isset($_SERVER['HTTP_AUTHORIZATION']) && substr($_SERVER['HTTP_AUTHORIZATION'], 7) == $mySecret) { | |
$this->fetchData(); | |
$this->setLocalArticleLocation(); | |
switch ($this->data['event']) { | |
case 'created': | |
case 'updated': | |
$this->writeToFile(); | |
break; | |
case 'deleted': | |
break; | |
} | |
} | |
} | |
private function writeToFile() | |
{ | |
if ($this->data['payload']['publish_date']) { | |
$this->data['payload']['links'] = $this->writeAndGetMenu(); | |
$content = \Neoan3\Apps\Ops::embraceFromFile('assets/template.html', $this->data['payload']); | |
file_put_contents($this->localArticleLocation, $content); | |
} | |
} | |
private function writeAndGetMenu() | |
{ | |
$jsonFilePath = path . '/assets/menu.json'; | |
if(file_exists($jsonFilePath)){ | |
$menuJson = json_decode(file_get_contents($jsonFilePath), true); | |
} else { | |
$menuJson = []; | |
} | |
$found = false; | |
foreach ($menuJson as $menuItem){ | |
if($menuItem['link'] == $this->data['payload']['slug'] . '.html'){ | |
$menuItem['name'] = $this->data['payload']['name']; | |
$found = true; | |
} | |
} | |
if(!$found){ | |
$menuJson[] = [ | |
'link' => $this->data['payload']['slug'] . '.html', | |
'name' => $this->data['payload']['name'] | |
]; | |
} | |
file_put_contents($jsonFilePath, json_encode($menuJson)); | |
return $menuJson; | |
} | |
private function setLocalArticleLocation() | |
{ | |
$this->localArticleLocation = path . '/blog/' . $this->data['payload']['slug'] . '.html'; | |
} | |
private function fetchData() | |
{ | |
$data = file_get_contents('php://input'); | |
if (!empty($data)) { | |
$this->data = json_decode($data, true); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>{{name}}</title> | |
<link rel="stylesheet" href="../assets/style.css"> | |
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> | |
</head> | |
<body> | |
<div class="container" id="blog"> | |
<blog-menu> | |
<ul> | |
<li n-for="links as link"> | |
<a href="{{link.link}}">{{link.name}}</a> | |
</li> | |
</ul> | |
</blog-menu> | |
<h1>{{name}}</h1> | |
<div class="content-part" n-for="content as contentPart"> | |
{{contentPart.content}} | |
</div> | |
</div> | |
<script src="../assets/script.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment