1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157:
<?php
namespace Net\Bazzline\Component\ApiDocumentBuilder\Command;
use Net\Bazzline\Component\ApiDocumentBuilder\Service\ApplicationLocator;
use Net\Bazzline\Component\Cli\Arguments\Arguments;
class Builder
{
private $arguments;
private $locator;
private $source;
function __construct(array $arguments)
{
$this->locator = new ApplicationLocator();
$this->arguments = $this->locator->getCliArguments();
$this->arguments->parseArguments($arguments);
$this->source = basename(array_shift($arguments));
}
public function execute()
{
$arguments = $this->arguments;
$locator = $this->locator;
$builder = $locator->getApigenBuilder();
if ($arguments->hasValues()) {
$values = $arguments->getValues();
$pathToConfigurationFile = array_shift($values);
$configuration = require_once $pathToConfigurationFile;
$cwd = getcwd();
$numberOfProjects = count($configuration['projects']);
$pathToCache = $configuration['paths']['cache'];
$pathToTarget = $configuration['paths']['target'];
$progressBar = $locator->getCliProgressBar();
$projects = array();
$progressBar->setTotalSteps($numberOfProjects);
echo 'updating projects ' . $numberOfProjects . PHP_EOL;
foreach ($configuration['projects'] as $project) {
$git = $locator->getGit();
$identifier = sha1($project['url']);
$pathToProjectCache = $pathToCache . '/' . $identifier;
$remove = $locator->getRemoveDirectory();
if (!is_dir($pathToProjectCache)) {
$create = $locator->getCreateDirectory();
$create($pathToProjectCache);
chdir($pathToProjectCache);
$git->create($project['url'], '.');
} else {
chdir($pathToProjectCache);
$git->update($pathToProjectCache);
}
chdir($cwd);
$remove($pathToTarget . '/' . $identifier);
$builder->setDestination($pathToTarget . '/' . $identifier);
$builder->setSource($pathToCache . '/' . $identifier . '/' . $project['source']);
$builder->setTitle($project['title']);
$builder->build();
$projects[] = array(
'path' => $identifier,
'title' => $project['title'],
'url' => $project['url']
);
$progressBar->forward();
}
$progressBar->isFinished();
echo 'generating output' . PHP_EOL;
if (!isset($configuration['template'])) {
$configuration['template'] = array(
'layout' => __DIR__ . '/../Template/layout.html',
'project_view' => __DIR__ . '/../Template/project_view.html'
);
} else {
if (!isset($configuration['template']['layout'])) {
$configuration['template']['layout'] = __DIR__ . '/../Template/layout.html';
}
if (!isset($configuration['template']['project_view'])) {
$configuration['template']['project_view'] = __DIR__ . '/../Template/project_view.html';
}
}
$this->renderOutput(
$pathToTarget . '/index.html',
$projects, $configuration['title'],
$configuration['template']['layout'],
$configuration['template']['project_view']
);
echo 'done' . PHP_EOL;
} else {
$this->printUsage();
}
}
private function printUsage()
{
$usage = $this->source . ' <path to configuration.php>';
echo $usage . PHP_EOL;
}
private function renderOutput($fileName, array $projects, $title, $pathToLayout, $pathToProjectView)
{
$layout = file_get_contents($pathToLayout);
$projectData = '';
$projectView = file_get_contents($pathToProjectView);
foreach ($projects as $project) {
$projectData .= str_replace(
array('{path}', '{title}', '{url}'),
array($project['path'], $project['title'], $project['url']),
$projectView
) . PHP_EOL;
}
$layoutData = str_replace(
array('{last_updated_at}', '{projects}', '{title}'),
array(date('Y-m-d H:i:s'), $projectData, $title),
$layout
);
file_put_contents($fileName, $layoutData);
}
}