/** * Called by PluginController::reloadAction when the addon version does not match what's installed * * @param Plugin $plugin * @param MauticFactory $factory * @param null $metadata * @param Schema $installedSchema * * @throws \Exception */ public static function onPluginUpdate(Plugin $plugin, MauticFactory $factory, $metadata = null, Schema $installedSchema = null) { // BC support; @deprecated 1.1.4; to be removed in 2.0 if (method_exists(get_called_class(), 'onUpdate')) { // Create a bogus Addon $addon = new Addon(); $addon->setAuthor($plugin->getAuthor())->setBundle($plugin->getBundle())->setDescription($plugin->getDescription())->setId($plugin->getId())->setIntegrations($plugin->getIntegrations())->setIsMissing($plugin->getIsMissing())->setName($plugin->getName())->setVersion($plugin->getVersion()); static::onUpdate($addon, $factory); } // Not recommended although availalbe for simple schema changes - see updatePluginSchema docblock //self::updatePluginSchema($metadata, $installedSchema, $factory); }
/** * Scans the addon bundles directly and loads bundles which are not registered to the database * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function reloadAction() { if (!$this->factory->getSecurity()->isGranted('plugin:plugins:manage')) { return $this->accessDenied(); } /** @var \Mautic\PluginBundle\Model\PluginModel $model */ $model = $this->factory->getModel('plugin'); $plugins = $this->factory->getParameter('plugin.bundles'); $added = $disabled = $updated = 0; // Get the metadata for plugins for installation $em = $this->factory->getEntityManager(); $allMetadata = $em->getMetadataFactory()->getAllMetadata(); $pluginMetadata = $pluginInstalledSchemas = $currentPluginTables = array(); $currentSchema = $em->getConnection()->getSchemaManager()->createSchema(); // Get current metadata and currently installed Tables /** @var \Doctrine\ORM\Mapping\ClassMetadata $meta */ foreach ($allMetadata as $meta) { $namespace = $meta->fullyQualifiedClassName(''); if (strpos($namespace, 'MauticPlugin') !== false) { $bundleName = str_replace('\\Entity\\', '', $namespace); if (!isset($pluginMetadata[$bundleName])) { $pluginMetadata[$bundleName] = array(); } $pluginMetadata[$bundleName][$meta->getName()] = $meta; $table = $meta->getTableName(); if (!isset($currentPluginTables[$bundleName])) { $currentPluginTables[$bundleName] = array(); } if ($currentSchema->hasTable($table)) { $currentPluginTables[$bundleName][] = $currentSchema->getTable($table); } } } // Create a Schema just for the plugin for updating foreach ($currentPluginTables as $bundleName => $tables) { $pluginInstalledSchemas[$bundleName] = new Schema($tables); } $persist = array(); $installedPlugins = $model->getEntities(array('index' => 'bundle')); /** * @var string $bundle * @var Plugin $plugin */ foreach ($installedPlugins as $bundle => $plugin) { $persistUpdate = false; if (!isset($plugins[$bundle])) { if (!$plugin->getIsMissing()) { //files are no longer found $plugin->setIsMissing(true); $disabled++; } } else { if ($plugin->getIsMissing()) { //was lost but now is found $plugin->setIsMissing(false); $persistUpdate = true; } $file = $plugins[$bundle]['directory'] . '/Config/config.php'; //update details of the bundle if (file_exists($file)) { /** @var array $details */ $details = (include $file); //compare versions to see if an update is necessary $version = isset($details['version']) ? $details['version'] : ''; if (!empty($version) && version_compare($plugin->getVersion(), $version) == -1) { $updated++; //call the update callback $callback = $plugins[$bundle]['bundleClass']; $metadata = isset($pluginMetadata[$plugins[$bundle]['namespace']]) ? $pluginMetadata[$plugins[$bundle]['namespace']] : null; $installedSchema = isset($pluginInstalledSchemas[$plugins[$bundle]['namespace']]) ? $pluginInstalledSchemas[$plugins[$bundle]['namespace']] : null; $callback::onPluginUpdate($plugin, $this->factory, $metadata, $installedSchema); unset($metadata, $installedSchema); $persistUpdate = true; } $plugin->setVersion($version); $plugin->setName(isset($details['name']) ? $details['name'] : $plugins[$bundle]['base']); if (isset($details['description'])) { $plugin->setDescription($details['description']); } if (isset($details['author'])) { $plugin->setAuthor($details['author']); } } unset($plugins[$bundle]); } if ($persistUpdate) { $persist[] = $plugin; } } //rest are new foreach ($plugins as $plugin) { $added++; $entity = new Plugin(); $entity->setBundle($plugin['bundle']); $file = $plugin['directory'] . '/Config/config.php'; //update details of the bundle if (file_exists($file)) { $details = (include $file); if (isset($details['version'])) { $entity->setVersion($details['version']); } $entity->setName(isset($details['name']) ? $details['name'] : $plugin['base']); if (isset($details['description'])) { $entity->setDescription($details['description']); } if (isset($details['author'])) { $entity->setAuthor($details['author']); } } // Call the install callback $callback = $plugin['bundleClass']; $metadata = isset($pluginMetadata[$plugin['namespace']]) ? $pluginMetadata[$plugin['namespace']] : null; $callback::onPluginInstall($entity, $this->factory, $metadata); $persist[] = $entity; } if (!empty($persist)) { $model->saveEntities($persist); } // Alert the user to the number of additions $this->addFlash('mautic.plugin.notice.reloaded', array('%added%' => $added, '%disabled%' => $disabled, '%updated%' => $updated)); $viewParameters = array('page' => $this->factory->getSession()->get('mautic.plugin.page')); // Refresh the index contents return $this->postActionRedirect(array('returnUrl' => $this->generateUrl('mautic_plugin_index', $viewParameters), 'viewParameters' => $viewParameters, 'contentTemplate' => 'MauticPluginBundle:Plugin:index', 'passthroughVars' => array('activeLink' => '#mautic_plugin_index', 'mauticContent' => 'plugin'))); }
/** * {@inheritDoc} */ public function __toString() { $this->__initializer__ && $this->__initializer__->__invoke($this, '__toString', array()); return parent::__toString(); }