Skip to content

Migrations für Plugins #4

@PortaltechGithub

Description

@PortaltechGithub

Statt Updates sollten Migrations analog zum Shopware-Core auch für Plugins implementiert werden. Folgender Code kann dafür als Basis genutzt werden:

public static function getUpdates(Plugin $plugin, $oldVersion = null)
    {
        $directory = $plugin->Path() . 'Updates' . DIRECTORY_SEPARATOR;
        $result = [];
        if (@is_dir($directory)) {
            $finder = new Finder();
            $finder->files()->name('Update*.php')->in($directory);
            foreach ($finder as $file) {
                $class = static::getClassFromFile($file);
                require_once($directory . $file->getRelativePathname());
                $reflection = new \ReflectionClass($class);
                $className  = 'Shopware\Plugins\Local\Core\ReplyBase\Components\AbstractBootstrapUpdate';
                if ($reflection->isSubclassOf($className)  && !$reflection->isAbstract()) {
                    $basename = $file->getBasename('.php');
                    $fromVersion = substr($basename, -6, 3);
                    $toVersion = substr($basename, -3);
                    if ($oldVersion !== null) {
                        $oldVersion = str_replace('.', '', $oldVersion);
                    }
                    if ($oldVersion === null || intval($fromVersion) >= intval($oldVersion)) {
                        $result[intval($fromVersion . $toVersion)] = $reflection->newInstance($plugin);
                    }
                }
            }
        }
        return $result;
    }


/**
     * Installs all plugin updates in ascending order. If the old version is provided, only updates newer than the
     * old version are applied
     *
     * @param Plugin $plugin The plugin reference
     * @param string $oldVersion Old Plugin version
     * @return boolean True on success
     */
    public static function applyUpdates(Plugin $plugin, $oldVersion = null)
    {
        $updates = static::getUpdates($plugin, $oldVersion);
        ksort($updates);
        /** @var \Shopware\Plugins\Local\Core\ReplyBase\Components\AbstractBootstrapUpdate $update */
        foreach ($updates as $update) {
            $result = $update->apply();
            if ($result === false || !isset($result['success']) || $result['success'] == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * Removes all plugin updates in ascending order
     *
     * @param Plugin $plugin The plugin reference
     * @return boolean True on success
     */
    public static function removeUpdates(Plugin $plugin)
    {
        $updates = static::getUpdates($plugin);
        krsort($updates);
        /** @var \Shopware\Plugins\Local\Core\ReplyBase\Components\AbstractBootstrapUpdate $update */
        foreach ($updates as $update) {
            $result = $update->remove();
            if ($result === false || !isset($result['success']) || $result['success'] == false) {
                return false;
            }
        }
        return true;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions