Completed
Push — master ( 985011...27fff5 )
by Tobias
17:41 queued 07:46
created

src/AppBundle/Command/SelfUpdateCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright  Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
4
 * @license    https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
5
 */
6
7
namespace AppBundle\Command;
8
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Humbug\SelfUpdate\Updater;
14
use Humbug\SelfUpdate\VersionParser;
15
use Humbug\SelfUpdate\Strategy\ShaStrategy;
16
use Humbug\SelfUpdate\Strategy\GithubStrategy;
17
18
class SelfUpdateCommand extends Command
19
{
20
    const MANIFEST_FILE = 'https://php-translation.github.io/cli/manifest.json';
21
    const PHAR_URL = 'https://php-translation.github.io/cli/downloads/translation.phar';
22
    const PACKAGE_NAME = 'php-translation/cli';
23
    const FILE_NAME = 'translation.phar';
24
    /**
25
     * @var OutputInterface
26
     */
27
    protected $output;
28
    /**
29
     * @var string
30
     */
31
    protected $version;
32
    /**
33
     * Execute the command.
34
     *
35
     * @param InputInterface  $input
36
     * @param OutputInterface $output
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $this->output = $output;
41
        $this->version = $this->getApplication()->getVersion();
42
        $parser = new VersionParser();
43
        /**
44
         * Check for ancilliary options.
45
         */
46
        if ($input->getOption('rollback')) {
47
            $this->rollback();
48
49
            return;
50
        }
51
        if ($input->getOption('check')) {
52
            $this->printAvailableUpdates();
53
54
            return;
55
        }
56
57
        if ($input->getOption('pre')) {
58
            $this->updateToPreReleaseBuild();
59
60
            return;
61
        }
62
        if ($input->getOption('stable')) {
63
            $this->updateToStableBuild();
64
65
            return;
66
        }
67
        if ($input->getOption('non-dev')) {
68
            $this->updateToMostRecentNonDevRemote();
69
70
            return;
71
        }
72
        /**
73
         * If current build is stable, only update to more recent stable
74
         * versions if available. User may specify otherwise using options.
75
         */
76
        if ($parser->isStable($this->version)) {
77
            $this->updateToStableBuild();
78
79
            return;
80
        }
81
        /**
82
         * By default, update to most recent remote version regardless
83
         * of stability.
84
         */
85
        $this->updateToMostRecentNonDevRemote();
86
    }
87
    protected function getStableUpdater()
88
    {
89
        $updater = new Updater();
90
        $updater->setStrategy(Updater::STRATEGY_GITHUB);
91
92
        return $this->getGithubReleasesUpdater($updater);
93
    }
94 View Code Duplication
    protected function getPreReleaseUpdater()
95
    {
96
        $updater = new Updater();
97
        $updater->setStrategy(Updater::STRATEGY_GITHUB);
98
        $updater->getStrategy()->setStability(GithubStrategy::UNSTABLE);
99
100
        return $this->getGithubReleasesUpdater($updater);
101
    }
102 View Code Duplication
    protected function getMostRecentNonDevUpdater()
103
    {
104
        $updater = new Updater();
105
        $updater->setStrategy(Updater::STRATEGY_GITHUB);
106
        $updater->getStrategy()->setStability(GithubStrategy::ANY);
107
108
        return $this->getGithubReleasesUpdater($updater);
109
    }
110
    protected function getGithubReleasesUpdater(Updater $updater)
111
    {
112
        $updater->getStrategy()->setPackageName(self::PACKAGE_NAME);
113
        $updater->getStrategy()->setPharName(self::FILE_NAME);
114
        $updater->getStrategy()->setCurrentLocalVersion($this->version);
115
116
        return $updater;
117
    }
118
119
    protected function updateToStableBuild()
120
    {
121
        $this->update($this->getStableUpdater());
122
    }
123
    protected function updateToPreReleaseBuild()
124
    {
125
        $this->update($this->getPreReleaseUpdater());
126
    }
127
    protected function updateToMostRecentNonDevRemote()
128
    {
129
        $this->update($this->getMostRecentNonDevUpdater());
130
    }
131
    protected function update(Updater $updater)
132
    {
133
        $this->output->writeln('Updating...'.PHP_EOL);
134
        try {
135
            $result = $updater->update();
136
            $newVersion = $updater->getNewVersion();
137
            $oldVersion = $updater->getOldVersion();
138
            if (strlen($newVersion) == 40) {
139
                $newVersion = 'dev-'.$newVersion;
140
            }
141
            if (strlen($oldVersion) == 40) {
142
                $oldVersion = 'dev-'.$oldVersion;
143
            }
144
145
            if ($result) {
146
                $this->output->writeln('<fg=green>PHP-Translation has been updated.</fg=green>');
147
                $this->output->writeln(sprintf(
148
                    '<fg=green>Current version is:</fg=green> <options=bold>%s</options=bold>.',
149
                    $newVersion
150
                ));
151
                $this->output->writeln(sprintf(
152
                    '<fg=green>Previous version was:</fg=green> <options=bold>%s</options=bold>.',
153
                    $oldVersion
154
                ));
155
            } else {
156
                $this->output->writeln('<fg=green>PHP-Translation is currently up to date.</fg=green>');
157
                $this->output->writeln(sprintf(
158
                    '<fg=green>Current version is:</fg=green> <options=bold>%s</options=bold>.',
159
                    $oldVersion
160
                ));
161
            }
162
        } catch (\Exception $e) {
163
            $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage()));
164
        }
165
        $this->output->write(PHP_EOL);
166
        $this->output->writeln('You can also select update stability using --dev, --pre (alpha/beta/rc) or --stable.');
167
    }
168
    protected function rollback()
169
    {
170
        $updater = new Updater();
171
        try {
172
            $result = $updater->rollback();
173
            if ($result) {
174
                $this->output->writeln('<fg=green>PHP-Translation has been rolled back to prior version.</fg=green>');
175
            } else {
176
                $this->output->writeln('<fg=red>Rollback failed for reasons unknown.</fg=red>');
177
            }
178
        } catch (\Exception $e) {
179
            $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage()));
180
        }
181
    }
182
    protected function printAvailableUpdates()
183
    {
184
        $this->printCurrentLocalVersion();
185
        $this->printCurrentStableVersion();
186
        $this->printCurrentPreReleaseVersion();
187
        $this->output->writeln('You can select update stability using --dev, --pre or --stable when self-updating.');
188
    }
189
    protected function printCurrentLocalVersion()
190
    {
191
        $this->output->writeln(sprintf(
192
            'Your current local build version is: <options=bold>%s</options=bold>',
193
            $this->version
194
        ));
195
    }
196
    protected function printCurrentStableVersion()
197
    {
198
        $this->printVersion($this->getStableUpdater());
199
    }
200
    protected function printCurrentPreReleaseVersion()
201
    {
202
        $this->printVersion($this->getPreReleaseUpdater());
203
    }
204
    protected function printVersion(Updater $updater)
205
    {
206
        $stability = 'stable';
207
        if ($updater->getStrategy() instanceof ShaStrategy) {
208
            $stability = 'development';
209
        } elseif ($updater->getStrategy() instanceof GithubStrategy
210
            && $updater->getStrategy()->getStability() == GithubStrategy::UNSTABLE) {
211
            $stability = 'pre-release';
212
        }
213
        try {
214
            if ($updater->hasUpdate()) {
215
                $this->output->writeln(sprintf(
216
                    'The current %s build available remotely is: <options=bold>%s</options=bold>',
217
                    $stability,
218
                    $updater->getNewVersion()
219
                ));
220
            } elseif (false == $updater->getNewVersion()) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $updater->getNewVersion() of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
221
                $this->output->writeln(sprintf('There are no %s builds available.', $stability));
222
            } else {
223
                $this->output->writeln(sprintf('You have the current %s build installed.', $stability));
224
            }
225
        } catch (\Exception $e) {
226
            $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage()));
227
        }
228
    }
229
    protected function configure()
230
    {
231
        $this
232
            ->setName('self-update')
233
            ->setDescription('Update translation.phar to most recent stable, pre-release or development build.')
234
            ->addOption(
235
                'dev',
236
                'd',
237
                InputOption::VALUE_NONE,
238
                'Update to most recent development build of PHP-Translation.'
239
            )
240
            ->addOption(
241
                'non-dev',
242
                'N',
243
                InputOption::VALUE_NONE,
244
                'Update to most recent non-development (alpha/beta/stable) build of PHP-Translation tagged on Github.'
245
            )
246
            ->addOption(
247
                'pre',
248
                'p',
249
                InputOption::VALUE_NONE,
250
                'Update to most recent pre-release version of PHP-Translation (alpha/beta/rc) tagged on Github.'
251
            )
252
            ->addOption(
253
                'stable',
254
                's',
255
                InputOption::VALUE_NONE,
256
                'Update to most recent stable version tagged on Github.'
257
            )
258
            ->addOption(
259
                'rollback',
260
                'r',
261
                InputOption::VALUE_NONE,
262
                'Rollback to previous version of PHP-Translation if available on filesystem.'
263
            )
264
            ->addOption(
265
                'check',
266
                'c',
267
                InputOption::VALUE_NONE,
268
                'Checks what updates are available across all possible stability tracks.'
269
            )
270
        ;
271
    }
272
}
273