Create a php version of page template

This commit is contained in:
2024-07-30 23:00:02 +10:00
parent a75f4ca936
commit cd9135e3bd
6 changed files with 145 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/php
{
"name": "PHP",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/php:1-8.3",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Configure tool-specific properties.
// "customizations": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [8000],
// Use 'portsAttributes' to set default properties for specific forwarded ports. More info: https://code.visualstudio.com/docs/remote/devcontainerjson-reference.
"portsAttributes": {
"8000": {
"label": "Hello Remote World",
"onAutoForward": "notify"
}
}
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

14
php-template/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch application",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/main.php",
"args": ["${workspaceFolder}/example-input"],
"cwd": "${workspaceFolder}",
"port": 9000
}
]
}

View File

@@ -0,0 +1,15 @@
{
"shortName": "Ballarat",
"councilName": "Ballarat City Council",
"wardNames": [
"Alfredton",
"Delacombe",
"Sebastopol",
"Central",
"Wendouree",
"Golden Point",
"North",
"Brown Hill",
"Buninyong"
]
}

28
php-template/main.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
require_once "page_renderer.php";
// TODO: Need to handle arguments better
// Assume that the first argument is a path to a file contain the config.
// If the argument isn't present, load the json from standard input
if (isset($argv[1])) {
$jsonData = file_get_contents($argv[1]);
} else {
$jsonData = file_get_contents("php://stdin");
}
$config = json_decode($jsonData, true);
// Check for decoding errors
if (json_last_error() !== JSON_ERROR_NONE) {
error_log('Error decoding JSON: ' . json_last_error_msg());
exit(1);
}
$renderer = new SPLPageRenderer();
$pageContent = $renderer->renderCouncilPage($config);
if ($pageContent === null) {
exit(2);
}
echo $pageContent;
exit(0);

View File

@@ -0,0 +1,27 @@
<?php
class SPLPageRenderer {
public function renderCouncilPage($config) {
ob_start();
set_error_handler(function($errno, $errstr, $errfile, $errline) {
// Clear any output if an error occurred
ob_get_clean();
error_log("Error: $errstr in $errfile on line $errline");
return true; // Prevent default error handling
});
require "template.php";
restore_error_handler();
$content = ob_get_clean();
// Explictly return null if we didn't generate any content
if (!empty($content)) {
return $content;
} else {
return null;
}
}
}

31
php-template/template.php Normal file
View File

@@ -0,0 +1,31 @@
<!-- wp:paragraph -->
<p><?php echo $config['councilName']; ?></p>
<!-- /wp:paragraph -->
<?php foreach ($config['wardNames'] as $index => $wardName): ?>
<!-- wp:heading {"className":"is-style-default"} -->
<h2 class="wp-block-heading is-style-default" id="<?php echo strtolower(str_replace(' ', '-', $wardName)); ?>"><?php echo $wardName; ?></h2>
<!-- /wp:heading -->
<!-- wp:group {"layout":{"type":"grid"}} -->
<div class="wp-block-group">
<?php for ($i = 0; $i < 5; $i++): ?>
<!-- wp:group {"layout":{"type":"flex","orientation":"vertical"}} -->
<div class="wp-block-group">
<!-- wp:heading {"fontSize":"medium"} -->
<h2 class="wp-block-heading has-medium-font-size">Candidate <?php echo $i + 1; ?></h2>
<!-- /wp:heading -->
<!-- wp:image {"aspectRatio":"1","scale":"cover","style":{"color":{}}} -->
<figure class="wp-block-image"><img alt="" style="aspect-ratio:1;object-fit:cover"/></figure>
<!-- /wp:image -->
<!-- wp:paragraph -->
<p>Lorem Ipsum</p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:group -->
<?php endfor; ?>
</div>
<?php endforeach; ?>