Use named arguments to pass files to template
This commit is contained in:
@@ -21,7 +21,7 @@ function create_or_update_page() {
|
|||||||
|
|
||||||
slug=$(echo "$council_block" | jq -r '.slug')
|
slug=$(echo "$council_block" | jq -r '.slug')
|
||||||
|
|
||||||
content=$(echo "$council_block" | jq -c | php php-template/main.php "php://stdin" "candidates/$slug.csv")
|
content=$(echo "$council_block" | jq -c | php php-template/main.php --council-file "php://stdin" --candidates-file "candidates/$slug.csv")
|
||||||
|
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
|
|
||||||
|
|||||||
2
php-template/.vscode/launch.json
vendored
2
php-template/.vscode/launch.json
vendored
@@ -6,7 +6,7 @@
|
|||||||
"type": "php",
|
"type": "php",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/main.php",
|
"program": "${workspaceFolder}/main.php",
|
||||||
"args": ["${workspaceFolder}/example-config.json", "${workspaceFolder}/example-candidates.csv"],
|
"args": ["--council-file", "${workspaceFolder}/example-config.json", "--candidates-file", "${workspaceFolder}/example-candidates.csv"],
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"port": 9000
|
"port": 9000
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,46 +1,49 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once "page_renderer.php";
|
require_once "page_renderer.php";
|
||||||
|
|
||||||
// TODO: Need to handle arguments better
|
$options = getopt("", ["council-file:", "candidates-file:"]);
|
||||||
// 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($options['council-file'])) {
|
||||||
if (isset($argv[1])) {
|
$councilFileContents = file_get_contents($options['council-file']);
|
||||||
$jsonData = file_get_contents($argv[1]);
|
|
||||||
} else {
|
} else {
|
||||||
$jsonData = file_get_contents("php://stdin");
|
error_log("Error: Missing required option '--council-file'.");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = json_decode($jsonData, true);
|
$councilData = json_decode($councilFileContents, true);
|
||||||
|
|
||||||
// Check for decoding errors
|
// Check for decoding errors
|
||||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
error_log('Error decoding JSON: ' . json_last_error_msg());
|
error_log('Error decoding council file: ' . json_last_error_msg());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($options['candidates-file'])) {
|
||||||
|
$candidatesFile = $options['candidates-file'];
|
||||||
|
} else {
|
||||||
|
error_log("Error: Missing required option '--candidates-file'.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert CSV into an array of dictionaries. Use the header as the key in the dictionary.
|
// Convert CSV into an array of dictionaries. Use the header as the key in the dictionary.
|
||||||
$candidates = [];
|
$candidateData = [];
|
||||||
if (isset($argv[2])) {
|
if (($handle = fopen($candidatesFile, "r")) !== FALSE) {
|
||||||
if (($handle = fopen($argv[2], "r")) !== FALSE) {
|
$headers = fgetcsv($handle);
|
||||||
$headers = fgetcsv($handle);
|
while (($data = fgetcsv($handle)) !== FALSE) {
|
||||||
while (($data = fgetcsv($handle)) !== FALSE) {
|
$candidate = [];
|
||||||
$candidate = [];
|
foreach ($headers as $key => $value) {
|
||||||
foreach ($headers as $key => $value) {
|
$candidate[$value] = $data[$key];
|
||||||
$candidate[$value] = $data[$key];
|
|
||||||
}
|
|
||||||
$candidates[] = $candidate;
|
|
||||||
}
|
}
|
||||||
fclose($handle);
|
$candidateData[] = $candidate;
|
||||||
} else {
|
|
||||||
error_log('Error opening CSV');
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
fclose($handle);
|
||||||
|
} else {
|
||||||
|
error_log('Error opening candidates file');
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check that candidates were read successfully??
|
|
||||||
|
|
||||||
$renderer = new SPLPageRenderer();
|
$renderer = new SPLPageRenderer();
|
||||||
$pageContent = $renderer->renderCouncilPage($config, $candidates);
|
$pageContent = $renderer->renderCouncilPage($councilData, $candidateData);
|
||||||
if ($pageContent === null) {
|
if ($pageContent === null) {
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user