protected function execute(InputInterface $input, OutputInterface $output)
{
$filePath = $input->getOption('path');
$output->writeln('<info>Working Path: ' . $filePath . '</info>');
$file = $filePath . DIRECTORY_SEPARATOR . $input->getArgument('input-file');
if (!file_exists($file)) {
throw new \Exception(sprintf("API Blueprint file [%s] not found.", $file));
}
$blueprint = json_decode(file_get_contents($file));
if (isset($blueprint->ast) === false) {
throw new \Exception('Your API Blueprint file is not in the AST format. When parsing your API Blueprint file with Drafter add the -f flag to set the parse result type, e.g. `drafter -f json -t ast -o api.json api.md`.');
}
$tests = array();
if ($input->getOption('include-tests')) {
$testsFile = $filePath . $input->getOption('tests-filename');
if (file_exists($testsFile)) {
$output->writeln('<info>Using Blueman file with Postman tests: ' . $testsFile . '</info>');
$tests = $this->parseTestsFile($testsFile);
} else {
$output->writeln('<comment>Blueman file with Postman tests NOT found:' . $testsFile . '</comment>');
}
}
$blueprint = $blueprint->ast;
$collection = array();
$collection['id'] = (string) Uuid::uuid4();
$collection['name'] = $blueprint->name;
$collection['description'] = $blueprint->description;
$host = $input->getOption('host');
if (is_null($host)) {
// Check if the default host is set in the metadata
foreach ($blueprint->metadata as $metadata) {
if ($metadata->name === 'HOST') {
$host = $metadata->value;
}
}
}
if (empty($host)) {
$dialog = $this->getHelperSet()->get('dialog');
$host = $dialog->ask($output, "\n<info>Enter the base uri of your API</info> [<comment>https://api.example.com/v1</comment>]: ", 'https://api.example.com/v1');
}
$requests = array();
foreach ($blueprint->resourceGroups as $resourceGroup) {
$folders['id'] = (string) Uuid::uuid4();
$folders['name'] = $resourceGroup->name;
$folders['description'] = $resourceGroup->description;
$folders['order'] = array();
foreach ($resourceGroup->resources as $resource) {
/** @var object $action */
foreach ($resource->actions as $action) {
$actionId = (string) Uuid::uuid4();
$folders['order'][] = $actionId;
foreach ($action->examples as $example) {
$request['id'] = $actionId;
foreach ($example->requests as $exampleRequest) {
$headers = array();
foreach ($exampleRequest->headers as $header) {
$headers[] = sprintf('%s: %s', $header->name, $header->value);
}
$request['headers'] = implode("\n", $headers);
$request['data'] = (string) $exampleRequest->body;
$request['dataMode'] = 'raw';
}
$request['url'] = $host . $this->parseUri($resource, $action);
$request['name'] = $resource->uriTemplate;
$request['method'] = $action->method;
$request['collectionId'] = $collection['id'];
if ($tests) {
$request['tests'] = $this->getTest($action->name, $tests);
}
$requests[] = $request;
}
}
}
$folders['collection_name'] = $collection['name'];
$folders['collection_id'] = $collection['id'];
$collection['folders'][] = $folders;
}
$collection['timestamp'] = time();
$collection['synced'] = 'false';
$collection['order'] = array();
$collection['requests'] = $requests;
$file = file_put_contents($input->getOption('output'), json_encode($collection));
if ($file === false) {
throw new \Exception("Failed to write, permission denied.");
}
$output->writeln("\n<info>Done.</info>\n");
}