Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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 Psy\VersionUpdater\Downloader;
13
 
14
use Psy\Exception\ErrorException;
15
use Psy\Shell;
16
use Psy\VersionUpdater\Downloader;
17
 
18
class CurlDownloader implements Downloader
19
{
20
    private $tempDir = null;
21
    private $outputFile = null;
22
 
23
    /** {@inheritDoc} */
24
    public function setTempDir(string $tempDir)
25
    {
26
        $this->tempDir = $tempDir;
27
    }
28
 
29
    /** {@inheritDoc} */
30
    public function download(string $url): bool
31
    {
32
        $tempDir = $this->tempDir ?: \sys_get_temp_dir();
33
        $this->outputFile = \tempnam($tempDir, 'psysh-archive-');
34
        $targetName = $this->outputFile.'.tar.gz';
35
 
36
        if (!\rename($this->outputFile, $targetName)) {
37
            return false;
38
        }
39
 
40
        $this->outputFile = $targetName;
41
 
42
        $outputHandle = \fopen($this->outputFile, 'w');
43
        if (!$outputHandle) {
44
            return false;
45
        }
46
        $curl = \curl_init();
47
        \curl_setopt_array($curl, [
48
            \CURLOPT_FAILONERROR    => true,
49
            \CURLOPT_HEADER         => 0,
50
            \CURLOPT_FOLLOWLOCATION => true,
51
            \CURLOPT_TIMEOUT        => 10,
52
            \CURLOPT_FILE           => $outputHandle,
53
            \CURLOPT_HTTPHEADER     => [
54
                'User-Agent' => 'PsySH/'.Shell::VERSION,
55
            ],
56
        ]);
57
        \curl_setopt($curl, \CURLOPT_URL, $url);
58
        $result = \curl_exec($curl);
59
        $error = \curl_error($curl);
60
        \curl_close($curl);
61
 
62
        \fclose($outputHandle);
63
 
64
        if (!$result) {
65
            throw new ErrorException('cURL Error: '.$error);
66
        }
67
 
68
        return (bool) $result;
69
    }
70
 
71
    /** {@inheritDoc} */
72
    public function getFilename(): string
73
    {
74
        return $this->outputFile;
75
    }
76
 
77
    /** {@inheritDoc} */
78
    public function cleanup()
79
    {
80
        if (\file_exists($this->outputFile)) {
81
            \unlink($this->outputFile);
82
        }
83
    }
84
}