Spaces:
No application file
No application file
File size: 5,516 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
<?php
namespace Mautic\PluginBundle\Helper;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\PluginBundle\Entity\Plugin;
use Mautic\PluginBundle\Event\PluginInstallEvent;
use Mautic\PluginBundle\Event\PluginUpdateEvent;
use Mautic\PluginBundle\PluginEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Caution: none of the methods persist data.
*/
class ReloadHelper
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
private MauticFactory $factory
) {
}
/**
* Disables plugins that are in the database but are missing in the filesystem.
*/
public function disableMissingPlugins(array $allPlugins, array $installedPlugins): array
{
$disabledPlugins = [];
foreach ($installedPlugins as $plugin) {
if (!isset($allPlugins[$plugin->getBundle()]) && !$plugin->getIsMissing()) {
// files are no longer found
$plugin->setIsMissing(true);
$disabledPlugins[$plugin->getBundle()] = $plugin;
}
}
return $disabledPlugins;
}
/**
* Re-enables plugins that were disabled because they were missing in the filesystem
* but appeared in it again.
*/
public function enableFoundPlugins(array $allPlugins, array $installedPlugins): array
{
$enabledPlugins = [];
foreach ($installedPlugins as $plugin) {
if (isset($allPlugins[$plugin->getBundle()]) && $plugin->getIsMissing()) {
// files are no longer found
$plugin->setIsMissing(false);
$enabledPlugins[$plugin->getBundle()] = $plugin;
}
}
return $enabledPlugins;
}
/**
* Updates plugins that exist in the filesystem and in the database and their version changed.
*/
public function updatePlugins(array $allPlugins, array $installedPlugins, array $pluginMetadata, array $installedPluginsSchemas): array
{
$updatedPlugins = [];
foreach ($installedPlugins as $bundle => $plugin) {
if (isset($allPlugins[$bundle])) {
$pluginConfig = $allPlugins[$bundle];
$oldVersion = $plugin->getVersion();
$plugin = $this->mapConfigToPluginEntity($plugin, $pluginConfig);
// compare versions to see if an update is necessary
if ((empty($oldVersion) && !empty($plugin->getVersion())) || (!empty($oldVersion) && -1 == version_compare($oldVersion, $plugin->getVersion()))) {
// call the update callback
$callback = $pluginConfig['bundleClass'];
$metadata = $pluginMetadata[$pluginConfig['namespace']] ?? null;
$installedSchema = isset($installedPluginsSchemas[$pluginConfig['namespace']])
? $installedPluginsSchemas[$allPlugins[$bundle]['namespace']] : null;
$callback::onPluginUpdate($plugin, $this->factory, $metadata, $installedSchema);
$event = new PluginUpdateEvent($plugin, $oldVersion);
$this->eventDispatcher->dispatch($event, PluginEvents::ON_PLUGIN_UPDATE);
unset($metadata, $installedSchema);
$updatedPlugins[$plugin->getBundle()] = $plugin;
}
}
}
return $updatedPlugins;
}
/**
* Installs plugins that does not exist in the database yet.
*/
public function installPlugins(array $allPlugins, array $existingPlugins, array $pluginMetadata, array $installedPluginsSchemas): array
{
$installedPlugins = [];
foreach ($allPlugins as $bundle => $pluginConfig) {
if (!isset($existingPlugins[$bundle])) {
$entity = $this->mapConfigToPluginEntity(new Plugin(), $pluginConfig);
// Call the install callback
$callback = $pluginConfig['bundleClass'];
$metadata = $pluginMetadata[$pluginConfig['namespace']] ?? null;
$installedSchema = null;
if (isset($installedPluginsSchemas[$pluginConfig['namespace']]) && 0 !== count($installedPluginsSchemas[$pluginConfig['namespace']]->getTables())) {
$installedSchema = true;
}
$callback::onPluginInstall($entity, $this->factory, $metadata, $installedSchema);
$event = new PluginInstallEvent($entity);
$this->eventDispatcher->dispatch($event, PluginEvents::ON_PLUGIN_INSTALL);
$installedPlugins[$entity->getBundle()] = $entity;
}
}
return $installedPlugins;
}
private function mapConfigToPluginEntity(Plugin $plugin, array $config): Plugin
{
$plugin->setBundle($config['bundle']);
if (isset($config['config'])) {
$details = $config['config'];
if (isset($details['version'])) {
$plugin->setVersion($details['version']);
}
$plugin->setName(
$details['name'] ?? $config['base']
);
if (isset($details['description'])) {
$plugin->setDescription($details['description']);
}
if (isset($details['author'])) {
$plugin->setAuthor($details['author']);
}
}
return $plugin;
}
}
|