Skip to content

Commit

Permalink
Only show plugin warnings for active replacements (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko authored Apr 26, 2021
1 parent 7d8117d commit 812f524
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
20 changes: 20 additions & 0 deletions modules/system/classes/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class PluginManager
*/
protected $replacementMap = [];

/**
* @var array A map of plugins that are currently replaced [Original.Plugin => Replacement.Plugin]
*/
protected $activeReplacementMap = [];

/**
* @var bool Flag to indicate that all plugins have had the register() method called by registerAll() being called on this class.
*/
Expand Down Expand Up @@ -686,6 +691,19 @@ public function getReplacementMap()
return $this->replacementMap;
}

/**
* Returns the actively replaced plugins defined in $this->activeReplacementMap
* @param string $pluginIdentifier Plugin code/namespace
* @return array|null
*/
public function getActiveReplacementMap(string $pluginIdentifier = null)
{
if (!$pluginIdentifier) {
return $this->activeReplacementMap;
}
return $this->activeReplacementMap[$pluginIdentifier] ?? null;
}

/**
* Evaluates and initializes the plugin replacements defined in $this->replacementMap
*
Expand All @@ -710,6 +728,8 @@ public function registerReplacedPlugins()
$this->aliasPluginAs($replacement, $target);
$this->disablePlugin($target);
$this->enablePlugin($replacement);
// Register this plugin as actively replaced
$this->activeReplacementMap[$target] = $replacement;
} else {
$this->disablePlugin($replacement);
$this->enablePlugin($target);
Expand Down
6 changes: 4 additions & 2 deletions modules/system/classes/UpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ public function update()

// Set replacement warning messages
foreach ($this->pluginManager->getReplacementMap() as $alias => $plugin) {
$this->addMessage($plugin, Lang::get('system::lang.updates.update_warnings_plugin_replace_cli', [
if ($this->pluginManager->getActiveReplacementMap($alias)) {
$this->addMessage($plugin, Lang::get('system::lang.updates.update_warnings_plugin_replace_cli', [
'alias' => '<info>' . $alias . '</info>'
]));
]));
}
}

// Print messages returned by migrations / seeders
Expand Down
10 changes: 6 additions & 4 deletions modules/system/controllers/Updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ protected function getWarnings()
$replacementMap = PluginManager::instance()->getReplacementMap();

foreach ($replacementMap as $alias => $plugin) {
$warnings[] = Lang::get('system::lang.updates.update_warnings_plugin_replace', [
'plugin' => '<strong>' . $plugin . '</strong>',
'alias' => '<strong>' . $alias . '</strong>'
]);
if (PluginManager::instance()->getActiveReplacementMap($alias)) {
$warnings[] = Lang::get('system::lang.updates.update_warnings_plugin_replace', [
'plugin' => '<strong>' . $plugin . '</strong>',
'alias' => '<strong>' . $alias . '</strong>'
]);
}
}

return $warnings;
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/system/classes/PluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,14 @@ public function testReplaceVersion()
$this->assertTrue($replacementPlugin->canReplacePlugin('Winter.Original', '1.0'));
$this->assertFalse($replacementPlugin->canReplacePlugin('Winter.Original', '2.0.1'));
}

public function testActiveReplacementMap()
{
$map = $this->manager->getActiveReplacementMap();
$this->assertArrayHasKey('Winter.Original', $map);
$this->assertEquals('Winter.Replacement', $map['Winter.Original']);

$this->assertEquals('Winter.Replacement', $this->manager->getActiveReplacementMap('Winter.Original'));
$this->assertNull($this->manager->getActiveReplacementMap('Winter.InvalidReplacement'));
}
}

0 comments on commit 812f524

Please sign in to comment.