Test Failed
Push — master ( bd07d3...d7f1d2 )
by Nuno
01:36
created

Renamer::renameBinary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of Zero Framework.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\ZeroFramework\Commands;
13
14
use Illuminate\Support\Str;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
use Symfony\Component\Console\Input\InputArgument;
17
18
/**
19
 * The is the Zero Framework rename command class.
20
 *
21
 * @author Nuno Maduro <[email protected]>
22
 */
23
class Renamer extends AbstractCommand
24
{
25
    /**
26
     * The name and signature of the console command.
27
     *
28
     * @var string
29
     */
30
    protected $signature = 'rename';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Rename your application';
38
39
    /**
40
     * Holds an instance of SymfonyStyle.
41
     *
42
     * @var \Symfony\Component\Console\Style\SymfonyStyle;
43
     */
44
    protected $style;
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function handle(): void
50
    {
51
        $this->style = new SymfonyStyle($this->input, $this->output);
52
53
        $this->displayWelcomeMessage()
54
            ->rename();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function configure(): void
61
    {
62
        $this->addArgument('name', InputArgument::OPTIONAL);
63
    }
64
65
    /**
66
     * Perform project modifications in order to apply the
67
     * application name on the composer and on the binary.
68
     *
69
     * @return $this
70
     */
71
    protected function rename(): Renamer
72
    {
73
        $name = $this->asksForApplicationName();
74
75
        return $this->renameBinary($name)
76
            ->updateComposer($name);
77
    }
78
79
    /**
80
     * Display an welcome message.
81
     *
82
     * @return $this
83
     */
84
    protected function displayWelcomeMessage(): Renamer
85
    {
86
        $this->style->title('Crafting application...');
87
88
        return $this;
89
    }
90
91
    /**
92
     * Asks for the application name.
93
     *
94
     * If there is no interaction, we take the folder basename.
95
     *
96
     * @return string
97
     */
98
    protected function asksForApplicationName(): string
99
    {
100
        if (empty($name = $this->input->getArgument('name'))) {
101
            $name = $this->ask('What is your application name?');
102
        }
103
104
        if (empty($name)) {
105
            $name = trim(basename(BASE_PATH));
106
        }
107
108
        return Str::lower($name);
109
    }
110
111
    /**
112
     * Update composer json with related information.
113
     *
114
     * @param string $name
115
     *
116
     * @return $this
117
     */
118
    protected function updateComposer(string $name): Renamer
119
    {
120
        $this->setComposer(
121
            Str::replaceFirst(
122
                '"bin": ["'.$this->getCurrentBinaryName().'"]',
123
                '"bin": ["'.$name.'"]',
124
                $this->getComposer()
125
            )
126
        );
127
128
        $this->output->writeln('Updating composer: <info>✔</info>');
129
130
        return $this;
131
    }
132
133
    /**
134
     * Renames the application binary.
135
     *
136
     * @param string $name
137
     *
138
     * @return $this
139
     */
140
    protected function renameBinary(string $name): Renamer
141
    {
142
        rename(BASE_PATH.'/'.$this->getCurrentBinaryName(), BASE_PATH.'/'.$name);
143
144
        $this->output->writeln('Renaming application: <info>✔</info>');
145
146
        return $this;
147
    }
148
149
    /**
150
     * Set composer file.
151
     *
152
     * @param string $composer
153
     *
154
     * @return $this
155
     */
156
    protected function setComposer(string $composer): Renamer
157
    {
158
        file_put_contents(BASE_PATH.'/composer.json', $composer);
159
160
        return $this;
161
    }
162
163
    /**
164
     * Returns the current binary name.
165
     *
166
     * @return string
167
     */
168
    protected function getCurrentBinaryName(): string
169
    {
170
        $composer = $this->getComposer();
171
172
        return current(@json_decode($composer)->bin);
173
    }
174
175
    /**
176
     * Get composer file.
177
     *
178
     * @return string
179
     */
180
    protected function getComposer(): string
181
    {
182
        $file = BASE_PATH.'/composer.json';
183
184
        if (! file_exists($file)) {
185
            $this->error("You can't perform a rename.");
186
            exit(0);
187
        }
188
189
        return file_get_contents($file);
190
    }
191
}
192