Skip to content

Commit

Permalink
Merge pull request #9 from Awkan/feature/6-dump-dictionary-command
Browse files Browse the repository at this point in the history
Implement dump command for dictionaries, #6
  • Loading branch information
Maxime Veber authored Dec 22, 2017
2 parents 07629ce + 4f5e73c commit fde67a9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ App\Entity\User:
city: <dictionary('cities')>
```

## 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
Expand Down
98 changes: 98 additions & 0 deletions src/Knp/DictionaryBundle/Command/DictionaryDumpCommand.php
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;
}
}
5 changes: 5 additions & 0 deletions src/Knp/DictionaryBundle/Resources/config/dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@
</service>

<service id="knp_dictionary.registry" alias="knp_dictionary.dictionary.dictionary_registry"/>

<service id="knp_dictionary.command.dictionary_dump" class="Knp\DictionaryBundle\Command\DictionaryDumpCommand">
<argument type="service" id="knp_dictionary.registry"/>
<tag name="console.command" command="knp:dictionary:dump" />
</service>
</services>
</container>

0 comments on commit fde67a9

Please sign in to comment.