Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Dec 9, 2024
1 parent b22d11b commit 6c4bbe8
Show file tree
Hide file tree
Showing 14 changed files with 540 additions and 417 deletions.
12 changes: 6 additions & 6 deletions modules/cms/classes/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cms\Classes;

use System\Classes\Extensions\ExtensionManager;
use System\Classes\Extensions\ExtensionManagerInterface;
use System\Classes\Extensions\Source\ExtensionSource;
use System\Classes\Extensions\WinterExtension;
use System\Models\Parameter;
Expand All @@ -15,7 +15,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
class ThemeManager implements ExtensionManager
class ThemeManager implements ExtensionManagerInterface
{
use \Winter\Storm\Support\Traits\Singleton;

Expand Down Expand Up @@ -90,7 +90,7 @@ public function list(): array
return [];
}

public function create(): Theme
public function create(string $extension): Theme
{
// TODO: Implement create() method.
}
Expand All @@ -100,17 +100,17 @@ public function install(ExtensionSource|WinterExtension|string $extension): Them
// TODO: Implement install() method.
}

public function getExtension(WinterExtension|ExtensionSource|string $extension): ?WinterExtension
public function get(WinterExtension|ExtensionSource|string $extension): ?WinterExtension
{
// TODO: Implement getExtension() method.
}

public function enable(WinterExtension|string $extension): Theme
public function enable(WinterExtension|string $extension, string|bool $flag = self::DISABLED_BY_USER): Theme
{
// TODO: Implement enable() method.
}

public function disable(WinterExtension|string $extension): Theme
public function disable(WinterExtension|string $extension, string|bool $flag = self::DISABLED_BY_USER): Theme
{
// TODO: Implement disable() method.
}
Expand Down
6 changes: 1 addition & 5 deletions modules/system/classes/UpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\App;
use System\Classes\Core\MarketPlaceApi;
use System\Classes\Extensions\PluginManager;
use System\Classes\Extensions\Plugins\VersionManager;
use System\Helpers\Cache as CacheHelper;
use System\Models\Parameter;
use Winter\Storm\Exception\ApplicationException;
Expand All @@ -35,7 +36,6 @@ class UpdateManager

protected PluginManager $pluginManager;
protected ThemeManager $themeManager;
protected VersionManager $versionManager;
protected MarketPlaceApi $api;
protected Migrator $migrator;
protected DatabaseMigrationRepository $repository;
Expand Down Expand Up @@ -81,10 +81,6 @@ public function bindContainerObjects(bool $refresh = false): static
? $this->themeManager
: (class_exists(ThemeManager::class) ? ThemeManager::instance() : null);

$this->versionManager = isset($this->versionManager) && !$refresh
? $this->versionManager
: VersionManager::instance();

$this->migrator = App::make('migrator');
$this->repository = App::make('migration.repository');

Expand Down
14 changes: 5 additions & 9 deletions modules/system/classes/core/UpdateManagerModuleManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait UpdateManagerModuleManagerTrait
public function setupMigrations(): static
{
$this->repository->createRepository();
$this->addMessage(Info::class, 'Migration table created');
$this->message($this, 'Migration table created');

return $this;
}
Expand Down Expand Up @@ -45,9 +45,7 @@ public function migrateModule(string $module): static
$this->migrator->setOutput($this->notesOutput);
}

$this->out('', true);
$this->out(sprintf('<info>Migrating %s module...</info>', $module), true);
$this->out('', true);
$this->message($this, sprintf('<info>Migrating %s module...</info>', $module), true);

$this->migrator->run(base_path() . '/modules/' . strtolower($module) . '/database/migrations');

Expand All @@ -64,18 +62,16 @@ public function seedModule(string $module): static
return $this;
}

$this->out('', true);
$this->out(sprintf('<info>Seeding %s module...</info>', $module), true);
$this->out('', true);
$this->message($this, sprintf('<info>Seeding %s module...</info>', $module), true);

$seeder = App::make($className);
$return = $seeder->run();

if (isset($return) && (is_string($return) || is_array($return))) {
$this->addMessage($className, $return);
$this->message($className, $return);
}

$this->write(Info::class, sprintf('Seeded %s', $module));
$this->message($this, sprintf('Seeded %s', $module));

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function mapPluginReplacements(): array
throw new ApplicationException(Lang::get('system::lang.plugins.replace.multi_install_error'));
}
foreach ($replaces as $replace) {
$this->versionManager->replacePlugin($plugin, $replace);
$this->pluginManager->getVersionManager()->replacePlugin($plugin, $replace);
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public function updatePlugin(string $name): static

$this->message($this, sprintf('<info>Migrating %s (%s) plugin...</info>', Lang::get($plugin->pluginDetails()['name']), $name));

$this->versionManager->updatePlugin($plugin);
$this->pluginManager->getVersionManager()->updatePlugin($plugin);

return $this;
}
Expand All @@ -84,25 +84,25 @@ public function rollbackPlugin(string $name, string $stopOnVersion = null): stat
{
// Remove the plugin database and version
if (!($plugin = $this->pluginManager->findByIdentifier($name))
&& $this->versionManager->purgePlugin($name)
&& $this->pluginManager->getVersionManager()->purgePlugin($name)
) {
$this->message($this, '%s purged from database', $name);
return $this;
}

if ($stopOnVersion && !$this->versionManager->hasDatabaseVersion($plugin, $stopOnVersion)) {
if ($stopOnVersion && !$this->pluginManager->getVersionManager()->hasDatabaseVersion($plugin, $stopOnVersion)) {
throw new ApplicationException(Lang::get('system::lang.updates.plugin_version_not_found'));
}

if ($this->versionManager->removePlugin($plugin, $stopOnVersion, true)) {
if ($this->pluginManager->getVersionManager()->removePlugin($plugin, $stopOnVersion, true)) {
$this->message($this, '%s rolled back', $name);

if ($currentVersion = $this->versionManager->getCurrentVersion($plugin)) {
if ($currentVersion = $this->pluginManager->getVersionManager()->getCurrentVersion($plugin)) {
$this->message(
$this,
'Current Version: %s (%s)',
$currentVersion,
$this->versionManager->getCurrentVersionNote($plugin)
$this->pluginManager->getVersionManager()->getCurrentVersionNote($plugin)
);
}

Expand Down
92 changes: 76 additions & 16 deletions modules/system/classes/extensions/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,86 @@

namespace System\Classes\Extensions;

use System\Classes\Extensions\Source\ExtensionSource;
use Winter\Storm\Exception\ApplicationException;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Component;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

interface ExtensionManager
abstract class ExtensionManager
{
public const EXTENSION_NAME = '';
protected OutputStyle $output;

public function list(): array;
public function __construct(?OutputStyle $output = null)
{
$this->output = $output ?? new OutputStyle(new ArrayInput([]), new BufferedOutput());

$this->init();
}

public function setOutput(OutputStyle $output): static
{
$this->output = $output;

return $this;
}

public function getOutput(): OutputStyle
{
return $this->output;
}

public function termwind(string $component, ...$args): void
{
(new $component($this->output))->render(...$args);
}

/**
* Create a new instance of this singleton.
*/
final public static function instance(?Container $container = null): static
{
if (!$container) {
$container = app();
}

if (!$container->bound(static::class)) {
$container->singleton(static::class, function () {
return new static;
});
}

return $container->make(static::class);
}

/**
* Forget this singleton's instance if it exists
*/
final public static function forgetInstance(?Container $container = null): void
{
if (!$container) {
$container = app();
}

if ($container->bound(static::class)) {
$container->forgetInstance(static::class);
}
}

public function create(): WinterExtension;
/**
* @throws ApplicationException If the installation fails
* Initialize the singleton free from constructor parameters.
*/
public function install(ExtensionSource|WinterExtension|string $extension): WinterExtension;
public function isInstalled(ExtensionSource|WinterExtension|string $extension): bool;
public function getExtension(ExtensionSource|WinterExtension|string $extension): ?WinterExtension;
public function enable(WinterExtension|string $extension): mixed;
public function disable(WinterExtension|string $extension): mixed;
public function update(WinterExtension|string $extension): mixed;
public function refresh(WinterExtension|string $extension): mixed;
public function rollback(WinterExtension|string $extension, string $targetVersion): mixed;
public function uninstall(WinterExtension|string $extension): mixed;
protected function init()
{
}

public function __clone()
{
trigger_error('Cloning ' . __CLASS__ . ' is not allowed.', E_USER_ERROR);
}

public function __wakeup()
{
trigger_error('Unserializing ' . __CLASS__ . ' is not allowed.', E_USER_ERROR);
}
}
53 changes: 53 additions & 0 deletions modules/system/classes/extensions/ExtensionManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace System\Classes\Extensions;

use System\Classes\Extensions\Source\ExtensionSource;
use Winter\Storm\Exception\ApplicationException;

interface ExtensionManagerInterface
{
public const EXTENSION_NAME = '';

//
// Disabled by system
//

public const DISABLED_MISSING = 'disabled-missing';
public const DISABLED_REPLACED = 'disabled-replaced';
public const DISABLED_REPLACEMENT_FAILED = 'disabled-replacement-failed';
public const DISABLED_MISSING_DEPENDENCIES = 'disabled-dependencies';

//
// Explicitly disabled for a reason
//

public const DISABLED_REQUEST = 'disabled-request';
public const DISABLED_BY_USER = 'disabled-user';
public const DISABLED_BY_CONFIG = 'disabled-config';

public function list(): array;

public function create(string $extension): WinterExtension;

/**
* @throws ApplicationException If the installation fails
*/
public function install(ExtensionSource|WinterExtension|string $extension): WinterExtension;

public function isInstalled(ExtensionSource|WinterExtension|string $extension): bool;

public function get(ExtensionSource|WinterExtension|string $extension): ?WinterExtension;

public function enable(WinterExtension|string $extension, string|bool $flag = self::DISABLED_BY_USER): mixed;

public function disable(WinterExtension|string $extension, string|bool $flag = self::DISABLED_BY_USER): mixed;

public function update(WinterExtension|string $extension): mixed;

public function refresh(WinterExtension|string $extension): mixed;

public function rollback(WinterExtension|string $extension, string $targetVersion): mixed;

public function uninstall(WinterExtension|string $extension): mixed;
}
2 changes: 1 addition & 1 deletion modules/system/classes/extensions/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace System\Classes\Extensions;

class ModuleManager implements ExtensionManager
class ModuleManager implements ExtensionManagerInterface
{
public function list(): array
{
Expand Down
4 changes: 2 additions & 2 deletions modules/system/classes/extensions/PluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider as ServiceProviderBase;
use ReflectionClass;
use System\Classes\VersionManager;
use System\Classes\Extensions\Plugins\VersionManager;
use System\Classes\VersionYamlProcessor;
use Winter\Storm\Exception\SystemException;
use Winter\Storm\Foundation\Application;
Expand Down Expand Up @@ -446,7 +446,7 @@ public function getPluginVersion(): string

$versions = $this->getPluginVersions();
if (empty($versions)) {
return $this->version = (string) VersionManager::NO_VERSION_VALUE;
return $this->version = VersionManager::NO_VERSION_VALUE;
}

return $this->version = trim(key(array_slice($versions, -1, 1)));
Expand Down
Loading

0 comments on commit 6c4bbe8

Please sign in to comment.