From d9830f38ce348e0a65c015ea6112fa64f5b80b52 Mon Sep 17 00:00:00 2001 From: Jack Wilkinson Date: Thu, 19 Dec 2024 13:18:05 +0000 Subject: [PATCH] Added wip --- modules/cms/ServiceProvider.php | 18 ++- modules/cms/classes/Theme.php | 44 +------ modules/cms/classes/ThemeManager.php | 2 +- modules/system/ServiceProvider.php | 37 +++++- .../extensions/ExtensionManagerInterface.php | 2 +- .../classes/extensions/ModuleManager.php | 122 +++++++++++++++++- .../system/classes/extensions/PluginBase.php | 48 +------ .../classes/extensions/PluginManager.php | 2 +- .../classes/extensions/WinterExtension.php | 15 +-- .../system/console/BaseScaffoldCommand.php | 18 ++- modules/system/console/CreateMigration.php | 6 +- modules/system/console/JaxTest.php | 44 +++++++ .../tests/bootstrap/PluginManagerTestCase.php | 4 +- 13 files changed, 241 insertions(+), 121 deletions(-) create mode 100644 modules/system/console/JaxTest.php diff --git a/modules/cms/ServiceProvider.php b/modules/cms/ServiceProvider.php index f114cb9238..406d99e2be 100644 --- a/modules/cms/ServiceProvider.php +++ b/modules/cms/ServiceProvider.php @@ -21,6 +21,7 @@ use Illuminate\Support\Facades\View; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use System\Classes\CombineAssets; +use System\Classes\Extensions\WinterExtension; use System\Classes\MarkupManager; use System\Classes\SettingsManager; use Twig\Cache\FilesystemCache as TwigCacheFilesystem; @@ -28,7 +29,7 @@ use Winter\Storm\Support\Facades\Url; use Winter\Storm\Support\ModuleServiceProvider; -class ServiceProvider extends ModuleServiceProvider +class ServiceProvider extends ModuleServiceProvider implements WinterExtension { /** * Register the service provider. @@ -477,4 +478,19 @@ protected function registerHalcyonModels() ]; }); } + + public function getPath(): string + { + return __DIR__; + } + + public function getVersion(): string + { + // TODO: Implement getVersion() method. + } + + public function getIdentifier(): string + { + return 'Cms'; + } } diff --git a/modules/cms/classes/Theme.php b/modules/cms/classes/Theme.php index f8d0e28b36..74a505104b 100644 --- a/modules/cms/classes/Theme.php +++ b/modules/cms/classes/Theme.php @@ -715,52 +715,12 @@ public function __isset($key) return false; } - public function extensionInstall(): static - { - // TODO: Implement extensionInstall() method. - } - - public function extensionUninstall(): static - { - // TODO: Implement extensionUninstall() method. - } - - public function extensionEnable(): static - { - // TODO: Implement extensionEnable() method. - } - - public function extensionDisable(): static - { - // TODO: Implement extensionDisable() method. - } - - public function extensionRollback(): static - { - // TODO: Implement extensionRollback() method. - } - - public function extensionRefresh(): static - { - // TODO: Implement extensionRefresh() method. - } - - public function extensionUpdate(): static - { - // TODO: Implement extensionUpdate() method. - } - - public function extensionPath(): string - { - // TODO: Implement extensionPath() method. - } - - public function extensionVersion(): string + public function getVersion(): string { // TODO: Implement extensionVersion() method. } - public function extensionIdentifier(): string + public function getIdentifier(): string { // TODO: Implement extensionIdentifier() method. } diff --git a/modules/cms/classes/ThemeManager.php b/modules/cms/classes/ThemeManager.php index 7c44651dda..0d313ec816 100644 --- a/modules/cms/classes/ThemeManager.php +++ b/modules/cms/classes/ThemeManager.php @@ -115,7 +115,7 @@ public function disable(WinterExtension|string $extension, string|bool $flag = s // TODO: Implement disable() method. } - public function update(WinterExtension|string $extension): Theme + public function update(WinterExtension|string|null $extension): Theme { // TODO: Implement update() method. } diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index 5660d06697..67d5427851 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -13,7 +13,9 @@ use Illuminate\Support\Facades\View; use System\Classes\CombineAssets; use System\Classes\ErrorHandler; +use System\Classes\Extensions\ModuleManager; use System\Classes\Extensions\PluginManager; +use System\Classes\Extensions\WinterExtension; use System\Classes\FileManifest; use System\Classes\MailManager; use System\Classes\MarkupManager; @@ -26,6 +28,7 @@ use System\Twig\Engine as TwigEngine; use Twig\Environment; use Twig\Extension\CoreExtension; +use Winter\Storm\Console\Command; use Winter\Storm\Exception\SystemException; use Winter\Storm\Router\Helper as RouterHelper; use Winter\Storm\Support\ClassLoader; @@ -34,14 +37,14 @@ use Winter\Storm\Support\Facades\Validator; use Winter\Storm\Support\ModuleServiceProvider; -class ServiceProvider extends ModuleServiceProvider +class ServiceProvider extends ModuleServiceProvider implements WinterExtension { /** * Register the service provider. * * @return void */ - public function register() + public function register(): void { parent::register(); @@ -90,6 +93,18 @@ public function register() $this->registerBackendReportWidgets(); $this->registerBackendSettings(); } + + /* + * Console specific + */ + if ($this->app->runningInConsole()) { + Command::extend(function (Command $command) { + $command->bindEvent('beforeRun', function () use ($command) { + ModuleManager::instance()->setOutput($command->getOutput()); + PluginManager::instance()->setOutput($command->getOutput()); + }); + }); + } } /** @@ -342,6 +357,9 @@ protected function registerConsole() $this->registerConsoleCommand('npm.install', Console\Asset\Npm\NpmInstall::class); $this->registerConsoleCommand('npm.update', Console\Asset\Npm\NpmUpdate::class); $this->registerConsoleCommand('npm.version', Console\Asset\Npm\NpmVersion::class); + + // @TODO: remove + $this->registerConsoleCommand('jax.test', Console\JaxTest::class); } /* @@ -672,4 +690,19 @@ protected function registerGlobalViewVars() { View::share('appName', Config::get('app.name')); } + + public function getPath(): string + { + return __DIR__; + } + + public function getVersion(): string + { + // TODO: Implement extensionVersion() method. + } + + public function getIdentifier(): string + { + return 'System'; + } } diff --git a/modules/system/classes/extensions/ExtensionManagerInterface.php b/modules/system/classes/extensions/ExtensionManagerInterface.php index 93fa9f76d2..613ed041a9 100644 --- a/modules/system/classes/extensions/ExtensionManagerInterface.php +++ b/modules/system/classes/extensions/ExtensionManagerInterface.php @@ -43,7 +43,7 @@ public function enable(WinterExtension|string $extension, string|bool $flag = se public function disable(WinterExtension|string $extension, string|bool $flag = self::DISABLED_BY_USER): mixed; - public function update(WinterExtension|string $extension): mixed; + public function update(WinterExtension|string|null $extension): mixed; public function availableUpdates(WinterExtension|string|null $extension = null): ?array; diff --git a/modules/system/classes/extensions/ModuleManager.php b/modules/system/classes/extensions/ModuleManager.php index a0406f8909..97acdf4fdb 100644 --- a/modules/system/classes/extensions/ModuleManager.php +++ b/modules/system/classes/extensions/ModuleManager.php @@ -2,18 +2,49 @@ namespace System\Classes\Extensions; +use Illuminate\Console\OutputStyle; +use Illuminate\Database\Migrations\DatabaseMigrationRepository; +use Illuminate\Database\Migrations\Migrator; +use Illuminate\Support\Facades\App; use System\Classes\Extensions\Source\ExtensionSource; +use System\Classes\UpdateManager; +use System\Helpers\Cache as CacheHelper; +use System\Models\Parameter; +use System\ServiceProvider; +use Winter\Storm\Exception\ApplicationException; +use Winter\Storm\Support\Facades\Config; -class ModuleManager implements ExtensionManagerInterface +class ModuleManager extends ExtensionManager implements ExtensionManagerInterface { + protected Migrator $migrator; + protected DatabaseMigrationRepository $repository; + + protected function init(): void + { + $this->migrator = App::make('migrator'); + + $this->migrator->setOutput($this->output); + + $this->repository = App::make('migration.repository'); + } + + public function setOutput(OutputStyle $output): static + { + $this->output = $output; + + $this->migrator->setOutput($this->output); + + return $this; + } + public function list(): array { - // TODO: Implement list() method. + return Config::get('cms.loadModules', []); } public function create(string $extension): WinterExtension { - // TODO: Implement create() method. + throw new ApplicationException('Support for creating extensions needs implementing'); } public function install(WinterExtension|ExtensionSource|string $extension): WinterExtension @@ -31,9 +62,51 @@ public function disable(WinterExtension|string $extension, string|bool $flag = s // TODO: Implement disable() method. } - public function update(WinterExtension|string $extension): mixed + public function update(WinterExtension|string|null $extension): ?bool { - // TODO: Implement update() method. + $firstUp = UpdateManager::instance()->isSystemSetup(); + + if ($extension && !($resolved = $this->resolve($extension))) { + throw new ApplicationException('Unable to locate extension'); + } + + $modules = $extension + ? [$resolved->getIdentifier()] + : $this->list(); + + if ($firstUp) { + $this->repository->createRepository(); + $this->output->info('Migration table created'); + } + + foreach ($modules as $module) { + $this->output->info(sprintf('Migrating %s module...', $module)); + $this->migrator->run(base_path() . '/modules/' . strtolower($module) . '/database/migrations'); + } + + if ($firstUp) { + $className = '\\' . $module . '\Database\Seeds\DatabaseSeeder'; + if (class_exists($className)) { + $this->output->info(sprintf('Seeding %s module...', $module)); + + $seeder = App::make($className); + $return = $seeder->run(); + + if ($return && (is_string($return) || is_array($return))) { + $return = is_string($return) ? [$return] : $return; + foreach ($return as $item) { + $this->output->info(sprintf('[%s]: %s', $className, $item)); + } + } + + $this->output->info(sprintf('Seeded %s', $module)); + } + } + + Parameter::set('system::update.count', 0); + CacheHelper::clear(); + + return true; } public function refresh(WinterExtension|string $extension): mixed @@ -58,6 +131,43 @@ public function isInstalled(WinterExtension|ExtensionSource|string $extension): public function get(WinterExtension|ExtensionSource|string $extension): ?WinterExtension { - // TODO: Implement get() method. + if ($extension instanceof WinterExtension) { + return $extension; + } + + // @TODO: improve + try { + if (is_string($extension) && ($resolved = App::get($extension . '\\ServiceProvider'))) { + return $resolved; + } + } catch (\Throwable $e) { +// $this->output->error($e->getMessage()); + } + + try { + return App::get(ucfirst($extension) . '\\ServiceProvider'); + } catch (\Throwable $e) { + $this->output->error($e->getMessage()); + return null; + } + } + + public function availableUpdates(WinterExtension|string|null $extension = null): ?array + { + // TODO: Implement availableUpdates() method. + } + + public function tearDown(): static + { + // TODO: Implement tearDown() method. + } + + protected function resolve(WinterExtension|ExtensionSource|string $extension): ?WinterExtension + { + if ($extension instanceof WinterExtension) { + return $extension; + } + + return $this->get($extension instanceof ExtensionSource ? $extension->getCode() : $extension); } } diff --git a/modules/system/classes/extensions/PluginBase.php b/modules/system/classes/extensions/PluginBase.php index 6d879cd143..8e9c8fc704 100644 --- a/modules/system/classes/extensions/PluginBase.php +++ b/modules/system/classes/extensions/PluginBase.php @@ -564,59 +564,17 @@ public function checkDependencies(PluginManager $manager): bool return true; } - public function extensionInstall(): static - { - // TODO: Implement install() method. - return $this; - } - - public function extensionUninstall(): static - { - // TODO: Implement uninstall() method. - return $this; - } - - public function extensionEnable(): static - { - // TODO: Implement enable() method. - return $this; - } - - public function extensionDisable(): static - { - // TODO: Implement disable() method. - return $this; - } - - public function extensionRollback(): static - { - // TODO: Implement rollback() method. - return $this; - } - - public function extensionRefresh(): static - { - // TODO: Implement refresh() method. - return $this; - } - - public function extensionUpdate(): static - { - // TODO: Implement update() method. - return $this; - } - - public function extensionVersion(): string + public function getVersion(): string { return $this->getPluginVersion(); } - public function extensionPath(): string + public function getPath(): string { return $this->getPluginPath(); } - public function extensionIdentifier(): string + public function getIdentifier(): string { return strtolower($this->getPluginIdentifier()); } diff --git a/modules/system/classes/extensions/PluginManager.php b/modules/system/classes/extensions/PluginManager.php index 571fcabade..0fc8b1726c 100644 --- a/modules/system/classes/extensions/PluginManager.php +++ b/modules/system/classes/extensions/PluginManager.php @@ -279,7 +279,7 @@ public function disable(WinterExtension|string $extension, string|bool $flag = s return true; } - public function update(WinterExtension|string $extension): ?bool + public function update(WinterExtension|string|null $extension): ?bool { if (!($code = $this->resolveExtensionCode($extension))) { return null; diff --git a/modules/system/classes/extensions/WinterExtension.php b/modules/system/classes/extensions/WinterExtension.php index 25a98610f7..b9cd706fa7 100644 --- a/modules/system/classes/extensions/WinterExtension.php +++ b/modules/system/classes/extensions/WinterExtension.php @@ -4,18 +4,9 @@ interface WinterExtension { - public function extensionInstall(): static; - public function extensionUninstall(): static; - public function extensionEnable(): static; - public function extensionDisable(): static; - public function extensionRollback(): static; - public function extensionRefresh(): static; + public function getPath(): string; - public function extensionUpdate(): static; + public function getVersion(): string; - public function extensionPath(): string; - - public function extensionVersion(): string; - - public function extensionIdentifier(): string; + public function getIdentifier(): string; } diff --git a/modules/system/console/BaseScaffoldCommand.php b/modules/system/console/BaseScaffoldCommand.php index 5f8187d6a8..869cf06938 100644 --- a/modules/system/console/BaseScaffoldCommand.php +++ b/modules/system/console/BaseScaffoldCommand.php @@ -3,6 +3,7 @@ use InvalidArgumentException; use Winter\Storm\Parse\PHP\ArrayFile; use Winter\Storm\Scaffold\GeneratorCommand; +use function Termwind\render; abstract class BaseScaffoldCommand extends GeneratorCommand { @@ -89,18 +90,27 @@ public function makeStubs(): void . DIRECTORY_SEPARATOR . 'lang.php' ); + + $relativeFile = str_replace(base_path(), '', $langFilePath); + $mode = 'updated'; + if (!file_exists($langFilePath)) { $this->makeDirectory($langFilePath); - $comment = 'File generated: ' . str_replace(base_path(), '', $langFilePath); - } else { - $comment = 'File updated: ' . str_replace(base_path(), '', $langFilePath); + $mode = 'generated'; } // Store the localization messages to the determined file path ArrayFile::open($langFilePath)->set($langKeys)->write(); // Inform the user - $this->comment($comment); + render(<< +
File $mode
+ + $relativeFile + + + HTML); } /** diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 886cb82848..0f7a06a31a 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -1,9 +1,7 @@ option('for-version'); } else { $currentVersion = $this->getPlugin()->getPluginVersion(); - if ($currentVersion === VersionManager::NO_VERSION_VALUE) { + if ($currentVersion === PluginVersionManager::NO_VERSION_VALUE) { throw new InvalidArgumentException('The plugin [' . $this->getPluginIdentifier() . '] does not have a version set and no --version option was provided. Please set a version in the plugin\'s updates/version.yaml file.'); } $version = $this->getNextVersion($currentVersion); diff --git a/modules/system/console/JaxTest.php b/modules/system/console/JaxTest.php new file mode 100644 index 0000000000..add1e5ebed --- /dev/null +++ b/modules/system/console/JaxTest.php @@ -0,0 +1,44 @@ +output = new OutputStyle( new ArrayInput([]),