• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cweagans / composer-patches / 4116908618

pending completion
4116908618

push

github

GitHub
Merge pull request #447 from cweagans/2.x-wip

457 of 457 new or added lines in 28 files covered. (100.0%)

456 of 570 relevant lines covered (80.0%)

3.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.36
/src/Downloader.php
1
<?php
2

3
namespace cweagans\Composer;
4

5
use Composer\Composer;
6
use cweagans\Composer\Downloader\DownloaderInterface;
7
use Composer\IO\IOInterface;
8
use cweagans\Composer\Capability\Downloader\DownloaderProvider;
9
use cweagans\Composer\Event\PluginEvent;
10
use cweagans\Composer\Event\PluginEvents;
11
use UnexpectedValueException;
12

13
class Downloader
14
{
15
    protected Composer $composer;
16

17
    protected IOInterface $io;
18

19
    protected array $disabledDownloaders;
20

21
    protected string $cacheDir;
22

23
    public function __construct(Composer $composer, IOInterface $io, array $disabledDownloaders)
24
    {
25
        $this->composer = $composer;
8✔
26
        $this->io = $io;
8✔
27
        $this->disabledDownloaders = $disabledDownloaders;
8✔
28

29
        $this->cacheDir = $composer->getConfig()->get('cache-dir') . '/patches';
8✔
30
        if (!is_dir($this->cacheDir)) {
8✔
31
            mkdir($this->cacheDir);
1✔
32
        }
33
    }
34

35
    /**
36
     * Download a patch using the available downloaders.
37
     *
38
     * A downloader will update Patch->localPath if it was able to download the patch.
39
     *
40
     * @param Patch $patch
41
     *   The patch to download.
42
     */
43
    public function downloadPatch(Patch $patch)
44
    {
45
        if (isset($patch->sha256)) {
7✔
46
            $cachedPatch = $this->cacheDir . '/' . $patch->sha256 . '.patch';
7✔
47
            if (file_exists($cachedPatch)) {
7✔
48
                $this->io->write("      - Found cached patch at <info>{$cachedPatch}</info>", IOInterface::VERBOSE);
7✔
49
                $patch->localPath = $cachedPatch;
7✔
50
                return;
7✔
51
            }
52
        }
53

54
        foreach ($this->getDownloaders() as $downloader) {
7✔
55
            if (in_array(get_class($downloader), $this->disabledDownloaders, true)) {
7✔
56
                if ($this->io->isVerbose()) {
×
57
                    $this->io->write('<info>  - Skipping downloader ' . get_class($downloader) . '</info>');
×
58
                }
59
                continue;
×
60
            }
61

62
            $downloader->download($patch);
7✔
63

64
            if (isset($patch->localPath)) {
7✔
65
                $cachedPatch = $this->cacheDir . '/' . $patch->sha256 . '.patch';
7✔
66
                if (rename($patch->localPath, $cachedPatch)) {
7✔
67
                    $patch->localPath = $cachedPatch;
7✔
68
                }
69
                return;
7✔
70
            }
71
        }
72
    }
73

74
    /**
75
     * Gather a list of all patch downloaders from all enabled Composer plugins.
76
     *
77
     * @return DownloaderInterface[]
78
     *   A list of Downloaders that are available.
79
     */
80
    protected function getDownloaders(): array
81
    {
82
        static $downloaders;
7✔
83
        if (!is_null($downloaders)) {
7✔
84
            return $downloaders;
6✔
85
        }
86

87
        $downloaders = [];
1✔
88
        $plugin_manager = $this->composer->getPluginManager();
1✔
89
        $capabilities = $plugin_manager->getPluginCapabilities(
1✔
90
            DownloaderProvider::class,
1✔
91
            ['composer' => $this->composer, 'io' => $this->io]
1✔
92
        );
1✔
93
        foreach ($capabilities as $capability) {
1✔
94
            /** @var DownloaderProvider $capability */
95
            $newDownloaders = $capability->getDownloaders();
1✔
96
            foreach ($newDownloaders as $downloader) {
1✔
97
                if (!$downloader instanceof DownloaderInterface) {
1✔
98
                    throw new UnexpectedValueException(
×
99
                        'Plugin capability ' . get_class($capability) . ' returned an invalid value.'
×
100
                    );
×
101
                }
102
            }
103
            $downloaders = array_merge($downloaders, $newDownloaders);
1✔
104
        }
105

106

107
        $event = new PluginEvent(PluginEvents::POST_DISCOVER_DOWNLOADERS, $downloaders);
1✔
108
        $this->composer->getEventDispatcher()->dispatch(PluginEvents::POST_DISCOVER_DOWNLOADERS, $event);
1✔
109
        $downloaders = $event->getCapabilities();
1✔
110

111
        return $downloaders;
1✔
112
    }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc