Completed
Push — master ( c4223d...e3af10 )
by Nikita
03:25
created

RebuildSettingsCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 6
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A execute() 0 7 1
B copySettings() 0 22 4
C mergeSettings() 0 56 9
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
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
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()));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($file->getPathname()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
54
            $dest = preg_replace('/^.+?\/vendor\/(.+?)\/(.+?)\/.+?$/', TAISIYA_ROOT.'/app/config/\1.\2.settings.default.php', $file->getPathname());
0 ignored issues
show
Unused Code introduced by
$dest = preg_replace('/^... $file->getPathname()); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
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