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

cweagans / composer-patches / 4110992550

pending completion
4110992550

Pull #447

github

GitHub
Merge eaa8ee760 into a4b573156
Pull Request #447: 2.x WIP (don't merge yet)

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

89.19
/src/Patcher.php
1
<?php
2

3
namespace cweagans\Composer;
4

5
use Composer\Composer;
6
use cweagans\Composer\Event\PluginEvent;
7
use cweagans\Composer\Event\PluginEvents;
8
use cweagans\Composer\Patcher\PatcherInterface;
9
use Composer\IO\IOInterface;
10
use cweagans\Composer\Capability\Patcher\PatcherProvider;
11
use UnexpectedValueException;
12
use Exception;
13

14
class Patcher
15
{
16
    protected Composer $composer;
17

18
    protected IOInterface $io;
19

20
    protected array $disabledPatchers;
21

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

29
    /**
30
     * Apply a patch using the available Patchers.
31
     *
32
     * @param Patch $patch
33
     *   The patch to apply.
34
     *
35
     * @param string $path
36
     *   The path to where the package was installed by Composer.
37
     *
38
     * @return bool
39
     *   true if the patch was applied successfully.
40
     */
41
    public function applyPatch(Patch $patch, string $path): bool
42
    {
43
        foreach ($this->getPatchers() as $patcher) {
7✔
44
            if (in_array(get_class($patcher), $this->disabledPatchers, true)) {
7✔
45
                if ($this->io->isVerbose()) {
1✔
46
                    $this->io->write('<info>  - Skipping patcher ' . get_class($patcher) . '</info>');
1✔
47
                }
48
                continue;
1✔
49
            }
50

51
            $result = $patcher->apply($patch, $path);
6✔
52

53
            if ($result === true) {
6✔
54
                return true;
6✔
55
            }
56
        }
57

58
        return false;
1✔
59
    }
60

61
    /**
62
     * Gather a list of all Patchers from all enabled Composer plugins.
63
     *
64
     * @return PatcherInterface[]
65
     *   A list of Patchers that are available.
66
     */
67
    protected function getPatchers(): array
68
    {
69
        static $patchers;
7✔
70

71
        if (!is_null($patchers)) {
7✔
72
            return $patchers;
6✔
73
        }
74

75
        $patchers = [];
1✔
76
        $plugin_manager = $this->composer->getPluginManager();
1✔
77
        $capabilities = $plugin_manager->getPluginCapabilities(
1✔
78
            PatcherProvider::class,
1✔
79
            ['composer' => $this->composer, 'io' => $this->io]
1✔
80
        );
1✔
81
        foreach ($capabilities as $capability) {
1✔
82
            /** @var PatcherProvider $capability */
83
            $newPatchers = $capability->getPatchers();
1✔
84
            foreach ($newPatchers as $i => $patcher) {
1✔
85
                if (!$patcher instanceof PatcherInterface) {
1✔
86
                    throw new UnexpectedValueException(
×
87
                        'Plugin capability ' . get_class($capability) . ' returned an invalid value.'
×
88
                    );
×
89
                }
90

91
                if (!$patcher->canUse()) {
1✔
92
                    unset($newPatchers[$i]);
1✔
93
                }
94
            }
95

96
            $patchers = array_merge($patchers, $newPatchers);
1✔
97
        }
98

99
        $event = new PluginEvent(PluginEvents::POST_DISCOVER_PATCHERS, $patchers);
1✔
100
        $this->composer->getEventDispatcher()->dispatch(PluginEvents::POST_DISCOVER_PATCHERS, $event);
1✔
101
        $patchers = $event->getCapabilities();
1✔
102

103
        if (count($patchers) === 0) {
1✔
104
            throw new Exception('No patchers available.');
×
105
        }
106

107
        return $patchers;
1✔
108
    }
109
}
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