Command abstraction

This commit is contained in:
Simon Vieille 2015-02-18 13:39:02 +01:00
parent be970e12d5
commit 7a60faef27
3 changed files with 129 additions and 45 deletions

View file

@ -0,0 +1,44 @@
<?php
namespace Deblan\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
abstract class AbstractCommand extends Command
{
protected $input;
protected $output;
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setInput($input);
$this->setOutput($output);
}
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
public function getInput()
{
return $this->input;
}
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
public function getOutput()
{
return $this->output;
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Deblan\PowerDNS\Command;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\Zone;
use Deblan\PowerDNS\Model\ZoneVersion;
use Symfony\Component\Console\Input\InputOption;
abstract class AbstractZoneCommand extends AbstractCommand
{
protected function configure()
{
$this
->addOption('active', null, InputOption::VALUE_NONE, '')
->addOption('no-active', null, InputOption::VALUE_NONE, '');
}
protected function showZone(Zone $zone, $key)
{
$this->getOutput()->writeln(sprintf('<info>%s</info>.', $zone->getName()));
if ($zone->getDescription()) {
$this->getOutput()->writeln($zone->getDescription());
}
foreach ($zone->getZoneVersions() as $key => $zoneVersion) {
$this->showZoneVersion($zoneVersion, $key);
}
}
protected function showZoneVersion(ZoneVersion $zoneVersion, $key)
{
if ($this->getInput()->getOption('active') && false === $zoneVersion->getIsActive()) {
return;
}
if ($this->getInput()->getOption('no-active') && true === $zoneVersion->getIsActive()) {
return;
}
$this->getOutput()->writeln('');
$this->getOutput()->writeln(sprintf(
'<info>Version</info>: <comment>%d</comment> - <info>Active</info>: %s',
$zoneVersion->getVersion(),
$zoneVersion->getIsActive() ? 'Yes' : 'No'
));
$this->showZoneVersionRecords($zoneVersion);
}
protected function showZoneVersionRecords(ZoneVersion $zoneVersion)
{
$this->getOutput()->writeln('');
$this->getOutput()->writeln('<comment> ID | NAME | TYPE | TTL | PRIO | CONTENT</comment>');
$this->getOutput()->writeln('<comment>----------------------------------------------------------------------</comment>');
foreach ($zoneVersion->getZoneRecords() as $zoneRecord) {
$this->getOutput()->writeln(sprintf(
'%5d | %s | %s | %s | %s | %s',
$zoneRecord->getId(),
str_pad($zoneRecord->getName(), 21),
str_pad($zoneRecord->getType(), 9),
str_pad($zoneRecord->getTtl(), 6),
str_pad($zoneRecord->getPrio(), 7),
$zoneRecord->getContent()
));
}
}
}

View file

@ -8,77 +8,47 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command;
use Deblan\PowerDNS\Model\ZoneQuery;
use Deblan\PowerDNS\Model\Zone;
use Deblan\PowerDNS\Model\ZoneVersion;
class ZoneListCommand extends Command
class ZoneListCommand extends AbstractZoneCommand
{
protected function configure()
{
parent::configure();
$this
->setName('zone:list')
->setDescription('List DNS zones')
// ->addArgument('foo', InputArgument::OPTIONAL, '')
// ->addOption('bar', null, InputOption::VALUE_NONE, '')
->addOption('name', null, InputOption::VALUE_REQUIRED, '')
->setHelp("The <info>%command.name%</info> ");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
// $this->getContainer()->get('foo.bar');
// $output->writeln(sprintf('<comment>%s</comment> bar.', $example));
// $input->getArgument('foo');
// $input->getOption('bar');
$zones = ZoneQuery::create()->orderByName()->find();
$query = $this->getZoneQuery();
$zones = $query->find();
foreach ($zones as $key => $zone) {
$this->showZone($zone, $output, $key);
$this->showZone($zone, $key);
}
}
protected function showZone(Zone $zone, OutputInterface $output, $key)
protected function getZoneQuery()
{
$output->writeln(sprintf('<info>%s</info>.', $zone->getName()));
$query = ZoneQuery::create()->orderByName();
if ($zone->getDescription()) {
$output->writeln($zone->getDescription());
if ($this->getInput()->getOption('name')) {
$query->filterByName(sprintf('%%%s%%', $this->getInput()->getOption('name')));
}
foreach ($zone->getZoneVersions() as $key => $zoneVersion) {
$this->showZoneVersion($zoneVersion, $output, $key);
}
}
protected function showZoneVersion(ZoneVersion $zoneVersion, OutputInterface $output, $key)
{
$output->writeln('');
$output->writeln(sprintf(
'<info>Version</info>: <comment>%d</comment> - <info>Active</info>: %s',
$zoneVersion->getVersion(),
$zoneVersion->getIsActive() ? 'Yes' : 'No'
));
$this->showZoneVersionRecords($zoneVersion, $output);
}
protected function showZoneVersionRecords(ZoneVersion $zoneVersion, OutputInterface $output)
{
$output->writeln('');
$output->writeln('<comment> ID | NAME | TYPE | TTL | PRIO | CONTENT</comment>');
$output->writeln('<comment>----------------------------------------------------------------------</comment>');
foreach ($zoneVersion->getZoneRecords() as $zoneRecord) {
$output->writeln(sprintf(
'%5d | %s | %s | %s | %s | %s',
$zoneRecord->getId(),
str_pad($zoneRecord->getName(), 21),
str_pad($zoneRecord->getType(), 9),
str_pad($zoneRecord->getTtl(), 6),
str_pad($zoneRecord->getPrio(), 7),
$zoneRecord->getContent()
));
}
return $query;
}
}