Skip to content

Commit

Permalink
Added wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Jan 3, 2025
1 parent 19a09cd commit 8593b0b
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 517 deletions.
7 changes: 7 additions & 0 deletions modules/cms/classes/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use Winter\Storm\Halcyon\Datasource\DatasourceInterface;
use Winter\Storm\Halcyon\Datasource\DbDatasource;
use Winter\Storm\Halcyon\Datasource\FileDatasource;
use Winter\Storm\Packager\Composer;
use Winter\Storm\Support\Facades\Config;
use Winter\Storm\Support\Facades\Event;
use Winter\Storm\Support\Facades\File;
use Winter\Storm\Support\Facades\Url;
use Winter\Storm\Support\Facades\Yaml;
use Winter\Storm\Support\Str;
use Winter\Storm\Support\Traits\HasComposerPackage;

/**
* This class represents the CMS theme.
Expand All @@ -32,6 +34,8 @@
*/
class Theme extends CmsObject implements WinterExtension
{
use HasComposerPackage;

/**
* @var string Specifies the theme directory name.
*/
Expand Down Expand Up @@ -74,10 +78,13 @@ public static function load($dirName, $file = null): self
$theme = new static;
$theme->setDirName($dirName);
$theme->registerHalcyonDatasource();

if (App::runningInBackend()) {
$theme->registerBackendLocalization();
}

$theme->setComposerPackage(Composer::getPackageInfoByPath($theme->getPath()));

return $theme;
}

Expand Down
37 changes: 31 additions & 6 deletions modules/cms/classes/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Winter\Storm\Foundation\Extension\WinterExtension;
use System\Models\Parameter;
use Winter\Storm\Exception\ApplicationException;
use Winter\Storm\Packager\Composer;
use Winter\Storm\Support\Facades\File;

/**
Expand Down Expand Up @@ -60,9 +61,8 @@ public function findByDirName($dirName)

public function list(): array
{
$themes = Theme::all();
return array_combine(
array_map(fn ($theme) => $theme->getIdentifier(), $themes),
array_map(fn ($theme) => $theme->getIdentifier(), $themes = Theme::all()),
$themes
);
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public function rollback(WinterExtension|string|null $extension = null, ?string
* @return mixed
* @throws ApplicationException
*/
public function uninstall(WinterExtension|string|null $theme = null): mixed
public function uninstall(WinterExtension|string|null $theme = null, bool $noRollback = false, bool $preserveFiles = false): mixed
{
if (!$theme) {
return false;
Expand All @@ -168,7 +168,7 @@ public function uninstall(WinterExtension|string|null $theme = null): mixed
* Delete from file system
*/
$themePath = $theme->getPath();
if (File::isDirectory($themePath)) {
if (File::isDirectory($themePath) && !$preserveFiles) {
File::deleteDirectory($themePath);
}

Expand Down Expand Up @@ -201,11 +201,36 @@ public function deleteTheme($theme): mixed

public function availableUpdates(WinterExtension|string|null $extension = null): ?array
{
// TODO: Implement availableUpdates() method.
$toCheck = $extension ? [$this->get($extension)] : $this->list();

$composerUpdates = Composer::getAvailableUpdates();

$updates = [];
foreach ($toCheck as $theme) {
if ($theme->getComposerPackageName()) {
if (isset($composerUpdates[$theme->getComposerPackageName()])) {
$updates[$theme->getIdentifier()] = [
'from' => $composerUpdates[$theme->getComposerPackageName()][0],
'to' => $composerUpdates[$theme->getComposerPackageName()][1],
];
}
continue;
}
// @TODO: Add market place support for updates
}

return $updates;
}

/**
* @throws ApplicationException
*/
public function tearDown(): static
{
// TODO: Implement tearDown() method.
foreach ($this->list() as $theme) {
$this->uninstall($theme);
}

return $this;
}
}
158 changes: 29 additions & 129 deletions modules/system/classes/UpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Facades\App;
use Mix\TestA\Plugin;
use System\Classes\Core\MarketPlaceApi;
use System\Classes\Extensions\ModuleManager;
use System\Classes\Extensions\PluginManager;
use System\Helpers\Cache as CacheHelper;
use System\Models\Parameter;
use Winter\Storm\Exception\ApplicationException;
use Winter\Storm\Exception\SystemException;
use Winter\Storm\Support\Facades\Config;
use Winter\Storm\Support\Facades\Schema;

Expand All @@ -27,62 +30,18 @@
class UpdateManager
{
use \Winter\Storm\Support\Traits\Singleton;
use \System\Classes\Core\UpdateManagerFileSystemTrait;
use \System\Classes\Core\UpdateManagerCoreManagerTrait;
use \System\Classes\Core\UpdateManagerPluginInstallerTrait;
use \System\Classes\Core\UpdateManagerThemeInstallerTrait;

protected PluginManager $pluginManager;
protected ThemeManager $themeManager;
protected MarketPlaceApi $api;
protected Migrator $migrator;
protected DatabaseMigrationRepository $repository;

/**
* If set to true, core updates will not be downloaded or extracted.
*/
protected bool $disableCoreUpdates = false;

/**
* Array of messages returned by migrations / seeders. Returned at the end of the update process.
*/
protected array $messages = [];

/**
* Initialize this singleton.
*/
protected function init()
{
$this->disableCoreUpdates = Config::get('cms.disableCoreUpdates', false);

$this->bindContainerObjects()
->setTempDirectory(temp_path())
->setBaseDirectory(base_path());
}

/**
* These objects are "soft singletons" and may be lost when
* the IoC container reboots. This provides a way to rebuild
* for the purposes of unit testing.
*/
public function bindContainerObjects(bool $refresh = false): static
{
$this->api = isset($this->api) && !$refresh
? $this->api
: MarketPlaceApi::instance();

$this->pluginManager = isset($this->pluginManager) && !$refresh
? $this->pluginManager
: PluginManager::instance();

$this->themeManager = isset($this->themeManager) && !$refresh
? $this->themeManager
: (class_exists(ThemeManager::class) ? ThemeManager::instance() : null);

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

return $this;
}

public function isSystemSetup(): bool
Expand All @@ -95,37 +54,6 @@ public function getMigrationTableName(): string
return Config::get('database.migrations', 'migrations');
}

/**
* Creates the migration table and updates
* @throws ApplicationException
*/
public function update(): static
{
$firstUp = $this->isSystemSetup();

$modules = Config::get('cms.loadModules', []);

if ($firstUp) {
$this->setupMigrations();
}

$this->migrateModules($modules);
$plugins = $this->mapPluginReplacements();

if ($firstUp) {
$this->seedModules($modules);
}

$this->updatePlugins($plugins);

Parameter::set('system::update.count', 0);
CacheHelper::clear();

$this->generatePluginReplacementNotices();

return $this;
}

/**
* Checks for new updates and returns the amount of unapplied updates.
* Only requests from the server at a set interval (retry timer).
Expand Down Expand Up @@ -163,38 +91,37 @@ public function check(bool $force = false): int
return $newCount;
}

public function availableUpdates(): array
{
return [
'modules' => ModuleManager::instance()->availableUpdates(),
'plugins' => PluginManager::instance()->availableUpdates(),
'themes' => ThemeManager::instance()->availableUpdates(),
];
}

/**
* Roll back all modules and plugins.
* @throws ApplicationException
* @throws SystemException
*/
public function tearDownTheSystem(): static
public function update(): static
{
/*
* Rollback plugins
*/
$plugins = array_reverse($this->pluginManager->getAllPlugins());
foreach ($plugins as $name => $plugin) {
$this->rollbackPlugin($name);
}

/*
* Register module migration files
*/
$paths = [];
$modules = Config::get('cms.loadModules', []);
ModuleManager::instance()->update();
PluginManager::instance()->update();
ThemeManager::instance()->update();

foreach ($modules as $module) {
$paths[] = base_path() . '/modules/' . strtolower($module) . '/database/migrations';
}

while (true) {
$rolledBack = $this->migrator->rollback($paths, ['pretend' => false]);

if (count($rolledBack) == 0) {
break;
}
}
return $this;
}

Schema::dropIfExists($this->getMigrationTableName());
/**
* Roll back all modules and plugins.
* @throws ApplicationException
*/
public function tearDownTheSystem(): static
{
ThemeManager::instance()->tearDown();
PluginManager::instance()->tearDown();
ModuleManager::instance()->tearDown();

return $this;
}
Expand Down Expand Up @@ -257,31 +184,4 @@ public function setBuild(string $build, ?string $hash = null, bool $modified = f

Parameter::set($params);
}

protected function message(string|object $class, string $format, mixed ...$args): static
{
$this->messages[] = [
'class' => is_object($class) ? get_class($class) : $class,
'message' => sprintf($format, ...$args),
'type' => 'info',
];

return $this;
}

protected function error(string|object $class, string $format, mixed ...$args): static
{
$this->messages[] = [
'class' => is_object($class) ? get_class($class) : $class,
'message' => sprintf($format, ...$args),
'type' => 'error',
];

return $this;
}

public function getMessages(): array
{
return $this->messages;
}
}
Loading

0 comments on commit 8593b0b

Please sign in to comment.