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 = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
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 | public function updatePackageInfo(array &$installedPackageList) |
||
38 | { |
||
39 | // Fetch types of packages that are "allowed" - ie. dependencies that we actually care about |
||
40 | $allowedTypes = (array) Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types'); |
||
41 | $composerPackagesAndConstraints = $this->owner->getComposerLoader()->getPackages($allowedTypes); |
||
42 | |||
43 | // Loop list of packages given by owner task |
||
44 | foreach ($installedPackageList as &$installedPackage) { |
||
45 | /** @var array $installedPackage */ |
||
46 | if (empty($installedPackage['Name'])) { |
||
47 | continue; |
||
48 | } |
||
49 | $packageName = $installedPackage['Name']; |
||
50 | |||
51 | // Continue if we have no composer constraint details |
||
52 | if (!isset($composerPackagesAndConstraints[$packageName])) { |
||
53 | continue; |
||
54 | } |
||
55 | $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 | $updates = $this->getUpdateChecker() |
||
61 | ->checkForUpdates($packageData['package'], $packageData['constraint']); |
||
62 | } catch (RuntimeException $ex) { |
||
63 | // If exceptions are thrown during execution, fail gracefully and allow the rest of the report |
||
64 | // generation to continue |
||
65 | $updates = []; |
||
66 | Injector::inst()->get(LoggerInterface::class)->debug($ex->getMessage()); |
||
67 | } |
||
68 | |||
69 | $installedPackage = array_merge($installedPackage, $updates); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param UpdateChecker $updateChecker |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setUpdateChecker(UpdateChecker $updateChecker) |
||
78 | { |
||
79 | $this->updateChecker = $updateChecker; |
||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return UpdateChecker |
||
85 | */ |
||
86 | public function getUpdateChecker() |
||
87 | { |
||
88 | return $this->updateChecker; |
||
89 | } |
||
90 | } |
||
91 |