From 5e8004530730ffe169a04a1efd477e068ead4442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20H=C3=A9rault?= Date: Fri, 13 May 2011 17:42:21 +0200 Subject: [PATCH] Add gaufrette:filesystem:keys command --- Command/FilesystemKeysCommand.php | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Command/FilesystemKeysCommand.php diff --git a/Command/FilesystemKeysCommand.php b/Command/FilesystemKeysCommand.php new file mode 100644 index 0000000..c383509 --- /dev/null +++ b/Command/FilesystemKeysCommand.php @@ -0,0 +1,78 @@ + + */ +class FilesystemKeysCommand extends Command +{ + /** + * {@inheritDoc} + */ + protected function configure() + { + $this + ->setName('gaufrette:filesystem:keys') + ->setDescription('List all the file keys of a filesystem') + ->addArgument('filesystem', InputArgument::REQUIRED, 'The filesystem to use') + ->addArgument('glob', InputArgument::OPTIONAL, 'An optional glob pattern') + ->setHelp(<<gaufrette:filesystem:list command lists all the file keys of the specified filesystem: + + ./app/console gaufrette:filesystem:list my_filesystem + +You can also optionaly specify a glob pattern to filter the results: + + ./app/console gaufrette:filesystem:list my_filesystem media_* +EOT + ); + } + + /** + * {@inheritDoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $filesystem = $input->getArgument('filesystem'); + $glob = $input->getArgument('glob'); + $container = $this->getApplication()->getKernel()->getContainer(); + $serviceId = sprintf('gaufrette.%s_filesystem', $filesystem); + + if (!$container->has($serviceId)) { + throw new \RuntimeException(sprintf('There is no \'%s\' filesystem defined.', $filesystem)); + } + + $filesystem = $container->get($serviceId); + $keys = $filesystem->keys(); + + if (!empty($glob)) { + $glob = new Glob($glob); + $keys = $glob->filter($keys); + } + + $count = count($keys); + + $output->writeln( + sprintf( + 'Bellow %s the %s key%s that where found:', + $count > 1 ? 'are' : 'is', + $count, + $count > 1 ? 's': '' + ) + ); + + $output->setDecorated(true); + foreach ($keys as $key) { + $output->writeln(' - ' . $key . ''); + } + } +}