Created
April 19, 2016 21:27
-
-
Save githubcom13/74b6b4a1e6e8e1a625af9b9c76258aad to your computer and use it in GitHub Desktop.
PHP: CLI script that can also be run in a web browser
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 | |
// I had a command line (CLI) script written in PHP that I wanted to be able to | |
// run via the web, to save having to open a terminal and SSH connection each | |
// time. | |
// First I had to modify the console script so it could output messages to | |
// either the console, using STDOUT or STDERR, or the web, which requires | |
// flushing the buffer each time: | |
// (This could also be achieved by running generate.php in a sub-process (see | |
// popen() and related functions), which would save modifying the console script | |
// at all – but that’s an exercise for another day.) | |
function output($stream, $message) | |
{ | |
if ($stream) { | |
// CLI - output to given stream | |
fputs($stream, $message); | |
} elseif (ob_get_level()) { | |
// Web but output buffering is on - bypass it | |
$buffer = ob_get_clean(); | |
echo $message; | |
flush(); | |
ob_start(); | |
echo $buffer; | |
} else { | |
// Web without output buffering | |
echo $message; | |
flush(); | |
} | |
} | |
if (!defined('STDOUT')) | |
define('STDOUT', null); | |
if (!defined('STDERR')) | |
define('STDERR', null); | |
// Example: | |
output(STDOUT, "Information\n"); | |
output(STDERR, "Error\n"); |
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 | |
// Then I wrote the following script which is styled to look somewhat like a | |
// console (white text, black background, monospace font) and calls the console | |
// script. It scrolls automatically and changes the window/tab title on | |
// completion. | |
// Load WordPress | |
require 'wp-load.php'; | |
// Ensure the user is logged in | |
if (!is_user_logged_in()) { | |
wp_redirect(wp_login_url($_SERVER['REQUEST_URI'])); | |
exit; | |
} | |
// Turn off the output buffering that WordPress starts | |
if (ob_get_level()) | |
ob_end_clean(); | |
// Display errors in the browser | |
ini_set('display_errors', true); | |
ini_set('html_errors', false); | |
?> | |
<html> | |
<head> | |
<title>Generating documentation...</title> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> | |
<style type="text/css"> | |
body { | |
background: #000; | |
padding: 15px; | |
} | |
pre { | |
font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace, serif; | |
font-size: 14px; | |
} | |
body, | |
a { | |
color: #fff; | |
} | |
</style> | |
<script> | |
function scrollToBottom() { | |
window.scrollTo(0, document.body.scrollHeight); | |
} | |
var timer = setInterval(scrollToBottom, 100); | |
var endTitle = 'ERROR Generating documentation!'; | |
// When the script is finished loading (whether successfully or | |
// it failed with a fatal error): | |
window.onload = function() { | |
clearInterval(timer); | |
scrollToBottom(); | |
document.title = endTitle; | |
} | |
</script> | |
</head> | |
<body> | |
<pre><?php | |
// Set the command line arguments | |
if (!empty($_GET['live'])) | |
$argv = array('generate.php', 'live'); | |
else | |
$argv = array('generate.php', '--symlink', 'dev'); | |
// Call the generator script | |
require '../documentation/scripts/generate.php'; | |
?> | |
<a href="../documentation/" onclick="window.location.replace(this.href); return false;">Visit documentation →</a></pre> | |
<script> | |
endTitle = "Documentation generated."; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment