forked from KnpLabs/DictionaryBundle
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Awkan/feature/6-dump-dictionary-command
Implement dump command for dictionaries, #6
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Knp/DictionaryBundle/Command/DictionaryDumpCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Knp\DictionaryBundle\Command; | ||
|
||
use Knp\DictionaryBundle\Dictionary; | ||
use Knp\DictionaryBundle\Dictionary\DictionaryRegistry; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* Dump dictionaries with their related values | ||
*/ | ||
class DictionaryDumpCommand extends Command | ||
{ | ||
protected static $defaultName = 'knp:dictionary:dump'; | ||
|
||
/** | ||
* @var DictionaryRegistry $registry | ||
*/ | ||
private $registry; | ||
|
||
public function __construct(DictionaryRegistry $registry) | ||
{ | ||
parent::__construct(); | ||
$this->registry = $registry; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Dump Dictionaries') | ||
->setHelp('This command allows you to dump KNP dictionnaries and their values') | ||
->addArgument( | ||
'dictionary', | ||
InputArgument::OPTIONAL, | ||
'Dictionary name you want to display' | ||
) | ||
->setHelp(<<<EOF | ||
The <info>%command.name%</info> list all dictionaries with their related values. | ||
You can choose to list a specific dictionary: | ||
<info>php %command.full_name% your_dictionary_name</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$dictionaryName = $input->getArgument('dictionary'); | ||
$io = new SymfonyStyle($input, $output); | ||
$errorIo = $io->getErrorStyle(); | ||
|
||
// Add output context text | ||
$io->title('KNP Dictionary'); | ||
$io->text('Here are the dictionaries with their related key-values:'); | ||
if (!\is_null($dictionaryName)) { | ||
$io->text("Search for dictionary named <fg=green>$dictionaryName</fg=green>"); | ||
} | ||
$io->newLine(); | ||
|
||
// Read dictionaries information | ||
$tableRows = $this->getDictionariesDetail($dictionaryName); | ||
|
||
// Output data | ||
if (\sizeof($tableRows) > 0) { | ||
$io->table([], $tableRows); | ||
} elseif (!\is_null($dictionaryName) && 0 === \sizeof($tableRows)) { | ||
$errorIo->error("No dictionary named $dictionaryName"); | ||
} | ||
} | ||
|
||
/** | ||
* Get all dictionaries with they values | ||
* If $dictionaryName is set, only display dictionary matching dictionary | ||
* | ||
* @param null|string $dictionaryName the dictionary name asked for filtering | ||
* @return array rows to display | ||
*/ | ||
private function getDictionariesDetail($dictionaryName = null) | ||
{ | ||
$tableRows = []; | ||
/** @var Dictionary $dico */ | ||
foreach ($this->registry->all() as $dico) { | ||
if (!\is_null($dictionaryName) && $dictionaryName === $dico->getName() || \is_null($dictionaryName)) { | ||
$tableRows[] = ["<fg=cyan>{$dico->getName()}</fg=cyan>"]; | ||
foreach ($dico as $key => $value) { | ||
$tableRows[] = [" $key\t| $value"]; | ||
} | ||
} | ||
} | ||
|
||
return $tableRows; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters