1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taisiya\CoreBundle\Console\Command\Config; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
9
|
|
|
use Taisiya\CoreBundle\Console\Command\Command; |
10
|
|
|
use Taisiya\CoreBundle\Console\Style\TaisiyaStyle; |
11
|
|
|
use Taisiya\CoreBundle\Exception\RuntimeException; |
12
|
|
|
|
13
|
|
|
final class RebuildSettingsCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this->setName('config:rebuild-settings') |
21
|
|
|
->setDescription('Rebuild project settings'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param InputInterface $input |
26
|
|
|
* @param OutputInterface $output |
27
|
|
|
*/ |
28
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
29
|
|
|
{ |
30
|
|
|
$io = new TaisiyaStyle($input, $output); |
31
|
|
|
|
32
|
|
|
$this->copySettings($io); |
33
|
|
|
$this->mergeSettings($io); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param TaisiyaStyle $io |
38
|
|
|
* @throws RuntimeException |
39
|
|
|
*/ |
40
|
|
|
final protected function copySettings(TaisiyaStyle $io): void |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$io->isVerbose() && $io->writeln('Copy bundles settings to project'); |
43
|
|
|
|
44
|
|
|
$finder = Finder::create() |
45
|
|
|
->in(TAISIYA_ROOT) |
46
|
|
|
->path('vendor') |
47
|
|
|
->files() |
48
|
|
|
->name('settings.default.php') |
49
|
|
|
->filter(function (SplFileInfo $file) { |
50
|
|
|
return (bool) preg_match('/\/app\/config\//', $file->getPathname()); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
foreach ($finder as $file) {exit(var_dump($file->getPathname())); |
|
|
|
|
54
|
|
|
$dest = preg_replace('/^.+?\/vendor\/(.+?)\/(.+?)\/.+?$/', TAISIYA_ROOT.'/app/config/\1.\2.settings.default.php', $file->getPathname()); |
|
|
|
|
55
|
|
|
if (!copy($file->getPathname(), $dest)) { |
56
|
|
|
throw new RuntimeException('Couldn\'t copy '.str_replace(TAISIYA_ROOT.'/', '', $file->getPathname()).' to '.str_replace(TAISIYA_ROOT.'/', '', $dest)); |
57
|
|
|
} else { |
58
|
|
|
$io->writeln(' - '.str_replace(TAISIYA_ROOT.'/', '', $file->getPathname()).' is copied to <info>'.str_replace(TAISIYA_ROOT.'/', '', $dest).'</info>'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param TaisiyaStyle $io |
65
|
|
|
* @throws RuntimeException |
66
|
|
|
*/ |
67
|
|
|
final protected function mergeSettings(TaisiyaStyle $io): void |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$io->isVerbose() && $io->writeln('Merge settings into one file'); |
70
|
|
|
|
71
|
|
|
$uname = php_uname('n'); |
72
|
|
|
$files = []; |
73
|
|
|
|
74
|
|
|
$finder = Finder::create() |
75
|
|
|
->in(TAISIYA_ROOT) |
76
|
|
|
->path('app/config') |
77
|
|
|
->files() |
78
|
|
|
->name('/\.settings\.default\.php$/'); |
79
|
|
|
|
80
|
|
|
foreach ($finder as $file) { |
81
|
|
|
$files[] = $file->getPathname(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$finder = Finder::create() |
85
|
|
|
->in(TAISIYA_ROOT) |
86
|
|
|
->path('app/config') |
87
|
|
|
->files() |
88
|
|
|
->name('/\.settings\.local\.php$/'); |
89
|
|
|
|
90
|
|
|
foreach ($finder as $file) { |
91
|
|
|
$files[] = $file->getPathname(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$finder = Finder::create() |
95
|
|
|
->in(TAISIYA_ROOT) |
96
|
|
|
->path('app/config') |
97
|
|
|
->files() |
98
|
|
|
->name('/\.settings\.'.preg_quote($uname, '/').'\.local\.php$/'); |
99
|
|
|
|
100
|
|
|
foreach ($finder as $file) { |
101
|
|
|
$files[] = $file->getPathname(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$files[] = TAISIYA_ROOT.'/app/config/settings.default.php'; |
105
|
|
|
if (file_exists(TAISIYA_ROOT.'/app/config/settings.local.php')) { |
106
|
|
|
$files[] = TAISIYA_ROOT.'/app/config/settings.local.php'; |
107
|
|
|
} |
108
|
|
|
if (file_exists(TAISIYA_ROOT.'/app/config/settings.'.$uname.'.local.php')) { |
109
|
|
|
$files[] = TAISIYA_ROOT.'/app/config/settings.'.$uname.'.local.php'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$settings = []; |
113
|
|
|
foreach ($files as $file) { |
114
|
|
|
$settings = array_merge_recursive($settings, require $file); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!file_put_contents(TAISIYA_ROOT.'/app/config/settings.php', "<?php\n\nreturn ".var_export($settings, true).";\n")) { |
118
|
|
|
throw new RuntimeException('Couldn\'t write to the file '.TAISIYA_ROOT.'/app/config/settings.php'); |
119
|
|
|
} else { |
120
|
|
|
$io->writeln(' - writed to <info>app/config/settings.php</info>'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|