bringyourownideas / silverstripe-composer-update-checker
1
<?php
2

3
namespace BringYourOwnIdeas\UpdateChecker\Extensions;
4

5
use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
6
use BringYourOwnIdeas\UpdateChecker\UpdateChecker;
7
use Psr\Log\LoggerInterface;
8
use RuntimeException;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Core\Extension;
11
use SilverStripe\Core\Injector\Injector;
12

13
/**
14
 * Task which does the actual checking of updates
15
 *
16
 * Originally from https://github.com/XploreNet/silverstripe-composerupdates
17
 *
18
 * @author Matt Dwen
19
 * @license MIT
20
 */
21
class CheckComposerUpdatesExtension extends Extension
22
{
23
    private static $dependencies = [
24
        'UpdateChecker' => '%$BringYourOwnIdeas\\UpdateChecker\\UpdateChecker',
25
    ];
26

27
    /**
28
     * @var UpdateChecker
29
     */
30
    protected $updateChecker;
31

32
    /**
33
     * Runs the actual steps to verify if there are updates available
34
     *
35
     * @param array[] $installedPackageList
36
     */
37 2
    public function updatePackageInfo(array &$installedPackageList)
38
    {
39
        // Fetch types of packages that are "allowed" - ie. dependencies that we actually care about
40 2
        $allowedTypes = (array) Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types');
41 2
        $composerPackagesAndConstraints = $this->owner->getComposerLoader()->getPackages($allowedTypes);
42

43
        // Loop list of packages given by owner task
44 2
        foreach ($installedPackageList as &$installedPackage) {
45
            /** @var array $installedPackage */
46 2
            if (empty($installedPackage['Name'])) {
47 0
                continue;
48
            }
49 2
            $packageName = $installedPackage['Name'];
50

51
            // Continue if we have no composer constraint details
52 2
            if (!isset($composerPackagesAndConstraints[$packageName])) {
53 2
                continue;
54
            }
55 2
            $packageData = $composerPackagesAndConstraints[$packageName];
56

57
            try {
58
                // Check for a relevant update version to recommend returned as keyed array and add to existing package
59
                // details array
60 2
                $updates = $this->getUpdateChecker()
61 2
                    ->checkForUpdates($packageData['package'], $packageData['constraint']);
62 0
            } catch (RuntimeException $ex) {
63
                // If exceptions are thrown during execution, fail gracefully and allow the rest of the report
64
                // generation to continue
65 0
                $updates = [];
66 0
                Injector::inst()->get(LoggerInterface::class)->debug($ex->getMessage());
67
            }
68

69 2
            $installedPackage = array_merge($installedPackage, $updates);
70
        }
71
    }
72

73
    /**
74
     * @param UpdateChecker $updateChecker
75
     * @return $this
76
     */
77 2
    public function setUpdateChecker(UpdateChecker $updateChecker)
78
    {
79 2
        $this->updateChecker = $updateChecker;
80 2
        return $this;
81
    }
82

83
    /**
84
     * @return UpdateChecker
85
     */
86 2
    public function getUpdateChecker()
87
    {
88 2
        return $this->updateChecker;
89
    }
90
}

Read our documentation on viewing source code .

Loading