Skip to content

Commit 69d20a9

Browse files
micahernekabalin
andauthored
Support subplugins in install.
Co-authored-by: Ruslan Kabalin <[email protected]>
1 parent 4d4e1f3 commit 69d20a9

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12-
No unreleased changes.
12+
### Added
13+
- Support for subplugins in the extra-plugins directory for install.
1314

1415
## [3.2.1] - 2021-07-30
1516
### Changed

src/Bridge/MoodlePlugin.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ class MoodlePlugin
6060
*/
6161
protected $dependencies;
6262

63+
/**
64+
* Cached subplugin types.
65+
*
66+
* @var string[]
67+
*/
68+
protected $subpluginTypes;
69+
6370
/**
6471
* @param string $directory Absolute path to a Moodle plugin
6572
*/
@@ -138,6 +145,30 @@ public function getDependencies()
138145
return $this->dependencies;
139146
}
140147

148+
/**
149+
* Get a plugin's subplugin types.
150+
*
151+
* @return string[]
152+
*/
153+
public function getSubpluginTypes()
154+
{
155+
// Simple cache.
156+
if (is_array($this->subpluginTypes)) {
157+
return $this->subpluginTypes;
158+
}
159+
$this->subpluginTypes = [];
160+
161+
$subpluginsJsonLocation = $this->directory.'/db/subplugins.json';
162+
if (file_exists($subpluginsJsonLocation)) {
163+
$subpluginData = json_decode(file_get_contents($subpluginsJsonLocation));
164+
if ($subpluginData && property_exists($subpluginData, 'plugintypes')) {
165+
$this->subpluginTypes = array_keys((array) $subpluginData->plugintypes);
166+
}
167+
}
168+
169+
return $this->subpluginTypes;
170+
}
171+
141172
/**
142173
* Determine if the plugin has any PHPUnit tests.
143174
*

src/Bridge/MoodlePluginCollection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public function sortByDependencies()
5252
$elements[$item->getComponent()] = [];
5353
}
5454

55+
$subpluginTypes = [];
56+
foreach ($this->items as $item) {
57+
foreach ($item->getSubpluginTypes() as $type) {
58+
$subpluginTypes[$type] = $item->getComponent();
59+
}
60+
}
61+
5562
// Loop through a second time, only adding dependencies that exist in our list.
5663
foreach ($this->items as $item) {
5764
$dependencies = $item->getDependencies();
@@ -60,6 +67,12 @@ public function sortByDependencies()
6067
$elements[$item->getComponent()][] = $dependency;
6168
}
6269
}
70+
71+
// Add implied dependencies for subplugins.
72+
list($type, $plugin) = explode('_', $item->getComponent(), 2);
73+
if (array_key_exists($type, $subpluginTypes)) {
74+
$elements[$item->getComponent()][] = $subpluginTypes[$type];
75+
}
6376
}
6477

6578
$sorter = new StringSort($elements, false);

tests/Bridge/MoodlePluginCollectionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,24 @@ public function testSortByDependenciesCircularError()
7575
$this->assertSame($plugin3, $all[1]);
7676
$this->assertSame($plugin1, $all[2]);
7777
}
78+
79+
public function testSortByDependenciesWithSubplugins()
80+
{
81+
$plugin1 = new DummyMoodlePlugin('');
82+
$plugin1->component = 'mod_1';
83+
$plugin1->subpluginTypes = ['subplugin'];
84+
85+
$plugin2 = new DummyMoodlePlugin('');
86+
$plugin2->component = 'subplugin_1';
87+
88+
$plugins = new MoodlePluginCollection();
89+
$plugins->add($plugin2);
90+
$plugins->add($plugin1);
91+
92+
$sorted = $plugins->sortByDependencies();
93+
$all = $sorted->all();
94+
$this->assertCount(2, $all);
95+
$this->assertSame($plugin1, $all[0]);
96+
$this->assertSame($plugin2, $all[1]);
97+
}
7898
}

tests/Bridge/MoodlePluginTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function testGetDependencies()
3131
$this->assertSame(['mod_forum'], $plugin->getDependencies());
3232
}
3333

34+
public function testGetSubpluginTypes()
35+
{
36+
$plugintypes = ['subplugin' => 'some/plugin/dir'];
37+
file_put_contents($this->pluginDir.'/db/subplugins.json', json_encode(['plugintypes' => $plugintypes]));
38+
$plugin = new MoodlePlugin($this->pluginDir);
39+
$this->assertSame(array_keys($plugintypes), $plugin->getSubpluginTypes());
40+
}
41+
3442
public function testHasUnitTests()
3543
{
3644
$plugin = new MoodlePlugin($this->pluginDir);

tests/Fake/Bridge/DummyMoodlePlugin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
class DummyMoodlePlugin extends MoodlePlugin
1818
{
19-
public $component = 'local_ci';
20-
public $dependencies = ['mod_forum'];
19+
public $component = 'local_ci';
20+
public $dependencies = ['mod_forum'];
21+
public $subpluginTypes = [];
2122
}

0 commit comments

Comments
 (0)