-
-
Notifications
You must be signed in to change notification settings - Fork 842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning - headers getting sent twice #162 #163
Changes from 5 commits
d207f61
ab26aab
48e84e0
1bf5748
552df43
24e24d7
8af7b7b
aeeefcd
103da6b
1948749
39df778
4086de5
bfdd582
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,43 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
declare(strict_types=1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* Set headers and echo response based on type | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param string $output | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param string If a message is a error | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* @return void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
function renderOutput(string $output, bool $error = false): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
$requestedType = $_REQUEST['type'] ?? 'svg'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if ($requestedType === "json" && $error === false) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// set content type to JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||
header('Content-Type: application/json'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// echo JSON data for streak stats | ||||||||||||||||||||||||||||||||||||||||||||||||||||
echo json_encode($output); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// exit | ||||||||||||||||||||||||||||||||||||||||||||||||||||
exit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if ($requestedType === "json" && $error === true) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// set content type to JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||
header('Content-Type: application/json'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// echo JSON error message | ||||||||||||||||||||||||||||||||||||||||||||||||||||
echo json_encode(array("error" => $output)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
exit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to repeat these lines, we can do it with nesting. This could also be extracted to an
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if ($requestedType === "png") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
echoAsPng($output); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
exit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
echoAsSvg($output); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* Convert date from Y-M-D to more human-readable format | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,21 +13,13 @@ | |||||
$dotenv = \Dotenv\Dotenv::createImmutable(dirname(__DIR__, 1)); | ||||||
$dotenv->safeLoad(); | ||||||
|
||||||
$requestedType = $_REQUEST['type'] ?? 'svg'; | ||||||
|
||||||
// if environment variables are not loaded, display error | ||||||
if (!$_SERVER["TOKEN"] || !$_SERVER["USERNAME"]) { | ||||||
$message = file_exists(dirname(__DIR__ . '.env', 1)) | ||||||
? "Missing token or username in config. Check Contributing.md for details." | ||||||
: ".env was not found. Check Contributing.md for details."; | ||||||
|
||||||
$card = generateErrorCard($message); | ||||||
if ($requestedType === "png") { | ||||||
echoAsPng($card); | ||||||
} | ||||||
echoAsSvg($card); | ||||||
|
||||||
exit; | ||||||
renderOutput(generateErrorCard($message)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JSON won't work unless generateErrorCard/generateCard happens within the render function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DenverCoder1 Strange... Why is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JSON does not output use the SVG card to output. It just displays the plain message/stats. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
|
||||||
|
@@ -50,26 +42,8 @@ | |||||
$contributions = getContributionDates($contributionGraphs); | ||||||
$stats = getContributionStats($contributions); | ||||||
DenverCoder1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} catch (InvalidArgumentException $error) { | ||||||
$card = generateErrorCard($error->getMessage()); | ||||||
if ($requestedType === "png") { | ||||||
echoAsPng($card); | ||||||
} | ||||||
echoAsSvg($card); | ||||||
|
||||||
exit; | ||||||
renderOutput(generateErrorCard($error->getMessage()),true); | ||||||
} | ||||||
renderOutput(generateCard($stats)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renderOutput(generateCard($stats)); This should probably go after the last line of the try-block (right after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the third comment (below), the generateCard/generateErrorCard can happen withing renderOutput, so this would end up being just renderOutput($stats); And for error outputs, you just pass the message. |
||||||
|
||||||
if ($requestedType === "json") { | ||||||
// set content type to JSON | ||||||
header('Content-Type: application/json'); | ||||||
// echo JSON data for streak stats | ||||||
echo json_encode($stats); | ||||||
// exit | ||||||
exit; | ||||||
} | ||||||
|
||||||
$card = generateCard($stats); | ||||||
if ($requestedType === "png") { | ||||||
echoAsPng($card); | ||||||
} | ||||||
echoAsSvg($card); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing $error, we could do (similar to your first idea), pass an array or string as the first parameter.
Union types are supported in PHP 8, so we can specify the parameter as
array|string $output
and then to check if it is an error or stats, we can use gettype, for example,gettype($output) === "string"
The card can then be generated like:
And the JSON data like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, I'm not sure if that will improve readability specially for new comers. But we can go forward with that idea. @DenverCoder1