|
4 | 4 |
|
5 | 5 | use Symfony\Component\Console\Helper\Helper; |
6 | 6 | use Symfony\Component\Yaml\Yaml; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | +use Symfony\Component\Filesystem\Filesystem; |
| 9 | +use Symfony\Component\Console\Helper\DialogHelper; |
7 | 10 |
|
8 | 11 | /** |
9 | 12 | * Helper for config stuff |
@@ -110,4 +113,55 @@ public function getConfig($type) |
110 | 113 | $this->loadConfig(); |
111 | 114 | return $this->cachedConfig[$type]; |
112 | 115 | } |
| 116 | + |
| 117 | + /** |
| 118 | + * Initialize a configuration files |
| 119 | + */ |
| 120 | + public function initConfig(OutputInterface $output = null, DialogHelper $dialogHelper = null) |
| 121 | + { |
| 122 | + $log = function ($message) use ($output) { |
| 123 | + if ($output) { |
| 124 | + $output->writeln($message); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + $fs = new Filesystem(); |
| 129 | + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
| 130 | + throw new \RuntimeException('This feature is currently only supported on Linux and OSX (maybe). Please submit a PR to support it on windows.'); |
| 131 | + } |
| 132 | + |
| 133 | + $configDir = $this->getConfigDir(); |
| 134 | + $distDir = __DIR__ . '/../../Resources/config.dist'; |
| 135 | + |
| 136 | + if (!file_exists($configDir)) { |
| 137 | + $log('<info>[+] Creating directory:</info> ' . $configDir); |
| 138 | + $fs->mkdir($configDir); |
| 139 | + } |
| 140 | + |
| 141 | + $configFilenames = array( |
| 142 | + 'alias.yml', |
| 143 | + ); |
| 144 | + |
| 145 | + foreach ($configFilenames as $configFilename) { |
| 146 | + $srcFile = $distDir . '/' . $configFilename; |
| 147 | + $destFile = $configDir . '/' . $configFilename; |
| 148 | + |
| 149 | + if (!file_exists($srcFile)) { |
| 150 | + throw new \Exception('Dist (source) file "' . $srcFile . '" does not exist.'); |
| 151 | + } |
| 152 | + |
| 153 | + if (file_exists($destFile)) { |
| 154 | + if (null !== $dialogHelper) { |
| 155 | + if (!$dialogHelper->askConfirmation($output, '"' . $configFilename . '" already exists, do you want to overwrite it?')) { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + } else { |
| 159 | + $log(sprintf('<info>File</info> %s <info> already exists, not overwriting.', $destFile)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + $fs->copy($srcFile, $destFile); |
| 164 | + $log('<info>[+] Creating file:</info> ' . $destFile); |
| 165 | + } |
| 166 | + } |
113 | 167 | } |
0 commit comments