Test Failed
Pull Request — stable (#73)
by Nuno
04:10
created

Renamer::rename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework\Commands\App;
4
5
use Illuminate\Support\Str;
6
use LaravelZero\Framework\Commands\Command;
7
8
/**
9
 * This is the Laravel Zero Framework renamer command class.
10
 *
11
 * @author Nuno Maduro <[email protected]>
12
 */
13
class Renamer extends Command
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $signature = 'app:rename {name? : The new name}';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $description = 'Perform an application rename';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function handle(): void
29
    {
30
        $this->alert('Renaming the application...');
31
32
        $this->rename();
33
    }
34
35
    /**
36
     * Perform project modifications in order to apply the
37
     * application name on the composer and on the binary.
38
     *
39
     * @return $this
40
     */
41
    protected function rename(): self
42
    {
43
        $name = $this->asksForApplicationName();
44
45
        if (file_exists(BASE_PATH.'/'.$name)) {
46
            $this->error("Could't rename: Folder or file already exists.");
47
        } else {
48
            $this->renameBinary($name)
49
                ->updateComposer($name);
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * Display an welcome message.
57
     *
58
     * @return $this
59
     */
60
    protected function displayWelcomeMessage(): self
61
    {
62
        return $this;
63
    }
64
65
    /**
66
     * Asks for the application name.
67
     *
68
     * If there is no interaction, we take the folder basename.
69
     *
70
     * @return string
71
     */
72
    protected function asksForApplicationName(): string
73
    {
74
        if (empty($name = $this->input->getArgument('name'))) {
75
            $name = $this->ask('What is your application name?');
76
        }
77
78
        if (empty($name)) {
79
            $name = trim(basename(BASE_PATH));
80
        }
81
82
        return Str::lower($name);
83
    }
84
85
    /**
86
     * Update composer json with related information.
87
     *
88
     * @param string $name
89
     *
90
     * @return $this
91
     */
92
    protected function updateComposer(string $name): self
93
    {
94
        $this->setComposer(
95
            Str::replaceFirst(
96
                '"bin": ["'.$this->getCurrentBinaryName().'"]',
97
                '"bin": ["'.$name.'"]',
98
                $this->getComposer()
99
            )
100
        );
101
102
        $this->output->writeln('Updating composer: <info>✔</info>');
103
104
        return $this;
105
    }
106
107
    /**
108
     * Renames the application binary.
109
     *
110
     * @param string $name
111
     *
112
     * @return $this
113
     */
114
    protected function renameBinary(string $name): self
115
    {
116
        rename(BASE_PATH.'/'.$this->getCurrentBinaryName(), BASE_PATH.'/'.$name);
117
118
        $this->output->writeln("Renaming application to: <info>$name</info>");
119
120
        return $this;
121
    }
122
123
    /**
124
     * Set composer file.
125
     *
126
     * @param string $composer
127
     *
128
     * @return $this
129
     */
130
    protected function setComposer(string $composer): self
131
    {
132
        file_put_contents(BASE_PATH.'/composer.json', $composer);
133
134
        return $this;
135
    }
136
137
    /**
138
     * Returns the current binary name.
139
     *
140
     * @return string
141
     */
142
    protected function getCurrentBinaryName(): string
143
    {
144
        $composer = $this->getComposer();
145
146
        return current(@json_decode($composer)->bin);
147
    }
148
149
    /**
150
     * Get composer file.
151
     *
152
     * @return string
153
     */
154
    protected function getComposer(): string
155
    {
156
        $file = BASE_PATH.'/composer.json';
157
158
        if (! file_exists($file)) {
159
            $this->error('composer.json not found.');
160
            exit(0);
161
        }
162
163
        return file_get_contents($file);
164
    }
165
}
166