1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SyncFS\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class InitCommand |
12
|
|
|
* |
13
|
|
|
* @package SyncFS\Command |
14
|
|
|
* @author Matej Velikonja <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class InitCommand extends Command |
17
|
|
|
{ |
18
|
|
|
const COMMAND_NAME = 'init'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Configure command. |
22
|
|
|
*/ |
23
|
5 |
|
protected function configure() |
24
|
|
|
{ |
25
|
5 |
|
$this |
26
|
5 |
|
->setName(self::COMMAND_NAME) |
27
|
5 |
|
->setDescription("Initialize empty config file.") |
28
|
5 |
|
->addArgument( |
29
|
5 |
|
self::ARG_CONFIG_PATH, |
30
|
5 |
|
InputArgument::OPTIONAL, |
31
|
5 |
|
'Path to config file. Default ' . $this->getDefaultConfiguration(), |
32
|
5 |
|
$this->getDefaultConfiguration() |
33
|
5 |
|
); |
34
|
5 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* @param OutputInterface $output |
39
|
|
|
* |
40
|
|
|
* @throws \RuntimeException |
41
|
|
|
* |
42
|
|
|
* @return int|null|void |
43
|
|
|
*/ |
44
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
2 |
|
$file = $input->getArgument(self::ARG_CONFIG_PATH); |
47
|
2 |
|
$exampleFile = __DIR__ . '/../../../tests/SyncFS/Test/Resources/data/config.example.yml'; |
48
|
|
|
|
49
|
2 |
|
if (! $content = @file_get_contents($exampleFile)) { |
50
|
|
|
throw new \RuntimeException(sprintf('<error>File `%s` cannot be read!</error>', $exampleFile)); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
if (file_exists($file)) { |
54
|
|
|
$output->writeln(sprintf('<error>File `%s` already exists.</error>', realpath($file))); |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
if (! is_writable(dirname($file))) { |
60
|
1 |
|
throw new \RuntimeException(sprintf('<error>File `%s` cannot be created!</error>', $file)); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$fs = new Filesystem(); |
64
|
1 |
|
$fs->dumpFile($file, $content); |
65
|
|
|
|
66
|
1 |
|
$output->writeln(sprintf('<info>Successfully created config file in `%s`.</info>', $file)); |
67
|
1 |
|
} |
68
|
|
|
} |
69
|
|
|
|