2015-02-10 13:41:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Console\Command;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Api\Client;
|
|
|
|
use Api\ConfigLoader;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2015-03-05 15:04:08 +01:00
|
|
|
use Api\ClientException;
|
2015-02-10 13:41:11 +01:00
|
|
|
|
|
|
|
class TypesTreeCommand extends Command
|
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('types:tree')
|
2015-02-10 20:29:55 +01:00
|
|
|
->setDescription('Show types and terms')
|
2015-02-10 13:41:11 +01:00
|
|
|
->addOption('terms', 't', InputOption::VALUE_NONE, 'Show terms')
|
2015-02-11 01:28:43 +01:00
|
|
|
->addOption('filter', 'f', InputOption::VALUE_REQUIRED, 'Filter types by ID or by name')
|
2015-02-10 20:13:46 +01:00
|
|
|
->setHelp("<info>%command.name%</info>
|
|
|
|
|
|
|
|
List all types of terms and terms.
|
|
|
|
|
|
|
|
Usage: <comment>types:tree</comment> [OPTIONS]");
|
2015-02-10 13:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$configLoader = new ConfigLoader();
|
|
|
|
|
|
|
|
if (!isset($configLoader->getConfig()['auth']['token'])) {
|
|
|
|
$output->writeln('You must login.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client->setAuthorization($configLoader->getConfig()['auth']['token']);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$categoriesReponse = $client->getCategoriesTree();
|
|
|
|
$termsResponse = $client->getTermsTree();
|
|
|
|
|
|
|
|
foreach (array($categoriesReponse, $termsResponse) as $response) {
|
|
|
|
if ($response->hasError()) {
|
|
|
|
$output->writeln(sprintf(
|
|
|
|
'<error>%s</error> <comment>(%d)</comment>',
|
|
|
|
$response->getErrorMessage(),
|
|
|
|
$response->getErrorCode()
|
|
|
|
));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filter = $input->getOption('filter');
|
|
|
|
|
|
|
|
if (!empty($filter)) {
|
|
|
|
if (is_numeric($filter)) {
|
|
|
|
$filter = (int) $filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($termsResponse->getData() as $categoryId => $termTypes) {
|
|
|
|
$stop = false;
|
|
|
|
|
|
|
|
foreach ($termTypes as $termTypeId => $termType) {
|
|
|
|
if ($filter !== null) {
|
|
|
|
if (is_int($filter)) {
|
|
|
|
if ((int) $termTypeId === $filter) {
|
|
|
|
$stop = true;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (0 === preg_match(sprintf('/%s/U', preg_quote($filter)), $termType['type'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->writeln(sprintf('<comment>%3d</comment> <info>%s</info>', $termTypeId, $termType['type']));
|
|
|
|
|
|
|
|
if ($input->getOption('terms')) {
|
|
|
|
$isFirst = true;
|
|
|
|
foreach ($termType['terms'] as $termId => $term) {
|
|
|
|
$char = '|';
|
|
|
|
if ($isFirst) {
|
|
|
|
$isFirst = false;
|
|
|
|
$char = '`';
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->writeln(sprintf(' %s- <comment>%4d</comment> %s', $char, $termId, $term));
|
|
|
|
}
|
|
|
|
|
|
|
|
$output->writeln('');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ClientException $e) {
|
|
|
|
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|