Issues (67)

src/Console/Command/InstallerCommand.php (3 issues)

1
<?php
2
3
/*
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Console\Command;
38
39
use Exception;
40
use Pickle\Base\Abstracts\Console\Command\BuildCommand;
41
use Pickle\Base\Archive\Factory;
42
use Pickle\Base\Util;
43
use Pickle\Engine;
44
use Pickle\Package\Command\Install;
45
use Pickle\Package\Util\Windows;
46
use Symfony\Component\Console\Helper\ProgressBar;
47
use Symfony\Component\Console\Helper\Table;
48
use Symfony\Component\Console\Input\InputInterface;
49
use Symfony\Component\Console\Input\InputOption;
50
use Symfony\Component\Console\Output\OutputInterface;
51
use Symfony\Component\Console\Question\ChoiceQuestion;
52
use Symfony\Component\Console\Question\ConfirmationQuestion;
53
54
class InstallerCommand extends BuildCommand
55
{
56
    protected function configure()
57
    {
58
        parent::configure();
59
60
        $this
61
            ->setName('install')
62
            ->setDescription('Install a php extension')
63
            ->addOption(
64
                'dry-run',
65
                null,
66
                InputOption::VALUE_NONE,
67
                'Do not install extension'
68
            )
69
            ->addOption(
70
                'php',
71
                null,
72
                InputOption::VALUE_REQUIRED,
73
                'path to an alternative php (exec)'
74
            )
75
            ->addOption(
76
                'ini',
77
                null,
78
                InputOption::VALUE_REQUIRED,
79
                'path to an alternative php.ini'
80
            )
81
            ->addOption(
82
                'source',
83
                null,
84
                InputOption::VALUE_NONE,
85
                'use source package'
86
            )
87
            ->addOption(
88
                'save-logs',
89
                null,
90
                InputOption::VALUE_REQUIRED,
91
                'path to save the build logs'
92
            )
93
            ->addOption(
94
                'tmp-dir',
95
                null,
96
                InputOption::VALUE_REQUIRED,
97
                'path to a custom temp dir',
98
                sys_get_temp_dir()
99
            )
100
            ->addOption(
101
                'version-override',
102
                null,
103
                InputOption::VALUE_OPTIONAL,
104
                'Override detected version (no value - or empty value - to use the version from package.xml)'
105
            )
106
        ;
107
108
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
109
            $this->addOption(
110
                'binary',
111
                null,
112
                InputOption::VALUE_NONE,
113
                'use binary package'
114
            );
115
        }
116
    }
117
118
    /**
119
     * @param string $path
120
     */
121
    protected function binaryInstallWindows($path, InputInterface $input, OutputInterface $output)
122
    {
123
        Factory::getUnzipperClassName(); // Be sure we have a way to unzip files
124
        $php = Engine::factory();
125
        $table = new Table($output);
126
        $table
127
            ->setRows([
128
                ['<info>' . $php->getName() . ' Path</info>', $php->getPath()],
129
                ['<info>' . $php->getName() . ' Version</info>', $php->getVersion()],
130
                ['<info>Compiler</info>', $php->getCompiler()],
131
                ['<info>Architecture</info>', $php->getArchitecture()],
132
                ['<info>Thread safety</info>', $php->getZts() ? 'yes' : 'no'],
133
                ['<info>Extension dir</info>', $php->getExtensionDir()],
134
                ['<info>php.ini</info>', $php->getIniPath()],
135
            ])
136
            ->render()
137
        ;
138
139
        $inst = Install::factory($path);
140
        $progress = new ProgressBar($output, 100);
141
142
        $inst->setProgress($progress);
143
        $inst->setInput($input);
0 ignored issues
show
The method setInput() does not exist on Pickle\Package\PHP\Command\Install\Windows\Binary. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
        $inst->/** @scrutinizer ignore-call */ 
144
               setInput($input);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $inst->setOutput($output);
145
        $inst->install();
146
147
        $deps_handler = new Windows\DependencyLib($php);
148
        $deps_handler->setProgress($progress);
149
        $deps_handler->setInput($input);
0 ignored issues
show
The method setInput() does not exist on Pickle\Package\Util\Windows\DependencyLib. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        $deps_handler->/** @scrutinizer ignore-call */ 
150
                       setInput($input);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
        $deps_handler->setOutput($output);
151
152
        $helper = $this->getHelperSet()->get('question');
153
154
        $cb = function ($choices) use ($helper, $input, $output) {
155
            $question = new ChoiceQuestion(
156
                'Multiple choices found, please select the appropriate dependency package',
157
                $choices
158
            );
159
            $question->setMultiselect(false);
160
161
            return $helper->ask($input, $output, $question);
0 ignored issues
show
The method ask() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\QuestionHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
            return $helper->/** @scrutinizer ignore-call */ ask($input, $output, $question);
Loading history...
162
        };
163
164
        foreach ($inst->getExtDllPaths() as $dll) {
165
            if (!$deps_handler->resolveForBin($dll, $cb)) {
166
                throw new Exception('Failed to resolve dependencies');
167
            }
168
        }
169
    }
170
171
    /*  The most of this needs to be incapsulated into an extra Build class*/
172
    protected function sourceInstall($package, InputInterface $input, OutputInterface $output, $optionsValue = [], $force_opts = ''): bool
173
    {
174
        $helper = $this->getHelperSet()->get('question');
175
176
        $build = \Pickle\Package\Command\Build::factory($package, $optionsValue);
177
178
        try {
179
            $build->prepare();
180
            $build->createTempDir($package->getUniqueNameForFs());
181
            $build->configure($force_opts);
182
            $build->make();
183
            $build->install();
184
185
            $this->saveBuildLogs($input, $build);
186
            $result = true;
187
        } catch (Exception $e) {
188
            $this->saveBuildLogs($input, $build);
189
190
            $output->writeln('The following error(s) happened: ' . $e->getMessage());
191
            $prompt = new ConfirmationQuestion('Would you like to read the log?', true);
192
            if ($helper->ask($input, $output, $prompt)) {
193
                $output->write($build->getLog());
194
            }
195
            $result = false;
196
        }
197
        $build->cleanup();
198
199
        return $result;
200
    }
201
202
    protected function execute(InputInterface $input, OutputInterface $output)
203
    {
204
        $path = rtrim($input->getArgument('path'), '/\\');
205
        Util\TmpDir::set($input->getOption('tmp-dir'));
206
207
        /* if windows, try bin install by default */
208
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
209
            $sourceRequested = $input->getOption('source');
210
            if (!$sourceRequested) {
211
                $this->binaryInstallWindows($path, $input, $output);
212
213
                return 0;
214
            }
215
        }
216
217
        $package = $this->getHelper('package')->convey($input, $output, $path);
218
        $this->getHelper('package')->showInfo($output, $package);
219
220
        [$optionsValue, $force_opts] = $this->buildOptions($package, $input, $output);
221
222
        if ($input->getOption('dry-run')) {
223
            return 0;
224
        }
225
226
        return $this->sourceInstall($package, $input, $output, $optionsValue, $force_opts) ? 0 : 1;
227
    }
228
}
229
230
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
231