|
| 1 | +<?php |
| 2 | +namespace Tuck\ConverterBundle\Command; |
| 3 | + |
| 4 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 5 | +use Symfony\Component\Console\Input\InputArgument; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Input\InputOption; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use \SplFileInfo; |
| 10 | +use Symfony\Component\HttpKernel\Bundle\Bundle; |
| 11 | +use Tuck\ConverterBundle\Exception\UnknownBundleException; |
| 12 | +use Tuck\ConverterBundle\Exception\EmptyDirectoryException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Converts a service config file to another format. |
| 16 | + * |
| 17 | + * @author Ross Tuck <[email protected]> |
| 18 | + */ |
| 19 | +class ConvertCommand extends ContainerAwareCommand |
| 20 | +{ |
| 21 | + protected function configure() |
| 22 | + { |
| 23 | + $this |
| 24 | + ->setName('container:convert') |
| 25 | + ->setDescription('Convert a service container config from one format to another') |
| 26 | + ->addArgument('format', InputArgument::REQUIRED, 'The format to convert to (yml, xml, graphviz, php)') |
| 27 | + ->addArgument('file', InputArgument::OPTIONAL, 'Source file to convert') |
| 28 | + ->addOption('output', 'o', InputOption::VALUE_NONE, 'Echo the new config instead of writing it to a file'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Interactively determine the file and convert it |
| 33 | + * |
| 34 | + * @param InputInterface $input |
| 35 | + * @param OutputInterface $output |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 39 | + { |
| 40 | + $fileToConvert = $this->determineFile($output, $input->getArgument('file')); |
| 41 | + $newFormat = $input->getArgument('format'); |
| 42 | + |
| 43 | + $newConfig = $this |
| 44 | + ->getContainer() |
| 45 | + ->get('tuck_converter.config_format_converter') |
| 46 | + ->convertFile($fileToConvert, $newFormat); |
| 47 | + |
| 48 | + if ($input->getOption('output')) { |
| 49 | + $output->write($newConfig); |
| 50 | + } else { |
| 51 | + $this->writeToFile($output, $fileToConvert, $newConfig, $newFormat); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Choose file from cli input or interactively |
| 57 | + * |
| 58 | + * @param OutputInterface $output |
| 59 | + * @param string $givenFileName |
| 60 | + * @return SplFileInfo |
| 61 | + */ |
| 62 | + protected function determineFile(OutputInterface $output, $givenFileName) |
| 63 | + { |
| 64 | + if (is_file($givenFileName)) { |
| 65 | + return new \SplFileInfo($givenFileName); |
| 66 | + } |
| 67 | + |
| 68 | + return $this->chooseFileInDirectory( |
| 69 | + $output, |
| 70 | + $this->chooseBundle($output)->getPath().'/Resources/config/' |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Prompt the user to choose the bundle whose config they want to convert |
| 76 | + * |
| 77 | + * @param OutputInterface $output |
| 78 | + * @return Bundle |
| 79 | + * @throws UnknownBundleException |
| 80 | + */ |
| 81 | + protected function chooseBundle(OutputInterface $output) |
| 82 | + { |
| 83 | + $bundles = $this->getContainer()->get('kernel')->getBundles(); |
| 84 | + |
| 85 | + $selectedBundle = $this->getHelperSet()->get('dialog')->ask( |
| 86 | + $output, |
| 87 | + '<info>Which bundle\'s config would you like to convert? </info>', |
| 88 | + null, |
| 89 | + array_keys($bundles) |
| 90 | + ); |
| 91 | + |
| 92 | + if (!isset($bundles[$selectedBundle])) { |
| 93 | + throw UnknownBundleException::create($selectedBundle); |
| 94 | + } |
| 95 | + |
| 96 | + return $bundles[$selectedBundle]; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * |
| 101 | + * @param OutputInterface $output |
| 102 | + * @param string $path |
| 103 | + * @return SplFileInfo |
| 104 | + * @throws EmptyDirectoryException |
| 105 | + */ |
| 106 | + protected function chooseFileInDirectory(OutputInterface $output, $path) |
| 107 | + { |
| 108 | + // Gather files in directory |
| 109 | + $files = array(); |
| 110 | + foreach (new \FilesystemIterator($path) as $file) { |
| 111 | + $files[] = $file; |
| 112 | + } |
| 113 | + |
| 114 | + // Handle edge cases |
| 115 | + if (count($files) === 0) { |
| 116 | + throw EmptyDirectoryException::create($path); |
| 117 | + } elseif (count($files) === 1) { |
| 118 | + return current($files); |
| 119 | + } |
| 120 | + |
| 121 | + // Prompt user for which file |
| 122 | + $fileIndex = $this->getHelperSet()->get('dialog')->select( |
| 123 | + $output, |
| 124 | + '<info>Which file should be converted? </info>', |
| 125 | + array_map( |
| 126 | + function ($fileInfo) { |
| 127 | + return $fileInfo->getFilename(); |
| 128 | + }, |
| 129 | + $files |
| 130 | + ) |
| 131 | + ); |
| 132 | + |
| 133 | + return $files[$fileIndex]; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Write the new config to the same directory as the original file |
| 138 | + * |
| 139 | + * @param OutputInterface $output |
| 140 | + * @param SplFileInfo $originalFile |
| 141 | + * @param string $newConfig |
| 142 | + * @param string $newFormat The short file name given, like |
| 143 | + */ |
| 144 | + protected function writeToFile(OutputInterface $output, SplFileInfo $originalFile, $newConfig, $newFormat) |
| 145 | + { |
| 146 | + $proposedLocation = $originalFile->getPath().'/services.'.$newFormat; |
| 147 | + $location = $this->getHelperSet()->get('dialog')->ask( |
| 148 | + $output, |
| 149 | + "<info>Where should the new config be written?</info> [{$proposedLocation}] ", |
| 150 | + $proposedLocation |
| 151 | + ); |
| 152 | + |
| 153 | + if (file_exists($location) |
| 154 | + && !$this->getHelperSet()->get('dialog')->askConfirmation($output, 'File exists. Overwrite? ') |
| 155 | + ) { |
| 156 | + $output->writeln('Aborting...'); |
| 157 | + |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + file_put_contents($location, $newConfig); |
| 162 | + $output->writeln( |
| 163 | + "Written! Don't forget to update the DependencyInjection/*Extension class in your bundle to use the new ". |
| 164 | + "loader class and delete the old config file." |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments