Skip to content

Instantly share code, notes, and snippets.

@grafluxe
Created December 16, 2016 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grafluxe/b6521901216a3c8e09f36cc31988a66c to your computer and use it in GitHub Desktop.
Save grafluxe/b6521901216a3c8e09f36cc31988a66c to your computer and use it in GitHub Desktop.
Easily create PHP docs for your project using apigen (apigen.org)
<?php
/**
* @author Leandro Silva | Grafluxe, 2016
*/
/**
* Easily create PHP docs for your project using apigen (apigen.org).
*
* To keep things simple, apigen CLI options are automatically set.
*
* Use:
* - Open your favorite CLI.
* - Run the following command: php <this-file> <project-path>
*
* Example:
* `php gendoc.php projects/api`
*/
$script_path = $argv[0];
$project_path = $argv[1];
if (!$project_path) {
exit("usage: php $script_path <project-path>" . PHP_EOL);
}
if (!file_exists($project_path)) {
exit("The project '$project_path' could not be found." . PHP_EOL);
}
$project_path = rtrim($project_path, "/") . "/";
$doc_path = $project_path . "doc/";
$apigen = dirname($script_path) . "/apigen.phar";
if (!file_exists($apigen)) {
echo "Downloading apigen..." . PHP_EOL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://github.com/ApiGen/ApiGen.github.io/raw/master/apigen.phar");
$data = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info["http_code"] !== 200) {
exit("The apigen download failed." . PHP_EOL);
}
file_put_contents($apigen, $data);
curl_close($ch);
}
/**
* Temp solution until apigen updates their phar with the
* logic from commit >= c2a2ac8 (which adds the 'overwrite' flag).
*/
if (file_exists($doc_path)) {
echo "Deleting current docs..." . PHP_EOL;
/**
* @ignore
*/
function empty_dir($path) {
$files = scandir($path);
$dirs = array();
for ($i = 0, $len = count($files); $i < $len; $i++) {
$fi = $files[$i];
if ($fi !== "." && $fi !== "..") {
$fi = rtrim($path, "/") . "/" . $fi;
if (is_dir($fi)) {
empty_dir($fi);
rmdir($fi);
} else {
unlink($fi);
}
}
}
}
empty_dir($doc_path);
}
echo shell_exec("php $apigen generate -s $project_path -d $doc_path --template-theme bootstrap");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment