diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ec33d2b..44226eb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Add `knp:dictionary:dump` command for dictionary listing and preview ### Added - Add auto-registration of `Dictionary` implementations as actual dictionary for users of Sf >= 3.3 diff --git a/README.md b/README.md index c47050e5..6c86125a 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,20 @@ App\Entity\User: city: ``` +## Use dictionary command + +You can use the following command to show your app dictionaries easily: +```bash +php app/console knp:dictionary:dump # SF2 +php bin/console knp:dictionary:dump # SF3 / SF4 +``` + +If you want to display only one dictionary, you can set it name in argument +```bash +php app/console knp:dictionary:dump my_dictionary # SF2 +php bin/console knp:dictionary:dump my_dictionary # SF3 / SF4 +``` + ## Create your own dictionary implementation ### Dictionary diff --git a/src/Knp/DictionaryBundle/Command/DictionaryDumpCommand.php b/src/Knp/DictionaryBundle/Command/DictionaryDumpCommand.php new file mode 100644 index 00000000..5b516418 --- /dev/null +++ b/src/Knp/DictionaryBundle/Command/DictionaryDumpCommand.php @@ -0,0 +1,98 @@ +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(<<%command.name% list all dictionaries with their related values. +You can choose to list a specific dictionary: + + php %command.full_name% your_dictionary_name +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 $dictionaryName"); + } + $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[] = ["{$dico->getName()}"]; + foreach ($dico as $key => $value) { + $tableRows[] = [" $key\t| $value"]; + } + } + } + + return $tableRows; + } +} diff --git a/src/Knp/DictionaryBundle/Resources/config/dictionary.xml b/src/Knp/DictionaryBundle/Resources/config/dictionary.xml index 3be1ea82..6ff5b5e8 100644 --- a/src/Knp/DictionaryBundle/Resources/config/dictionary.xml +++ b/src/Knp/DictionaryBundle/Resources/config/dictionary.xml @@ -57,5 +57,10 @@ + + + + +