Skip to content

Commit

Permalink
chore: track
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 8, 2023
1 parent a7b49fa commit 1e390fe
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 56 deletions.
9 changes: 9 additions & 0 deletions extensions/package-manager/js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ app.initializers.add('flarum-package-manager', (app) => {
</Alert>
</div>
))
.registerSetting(() =>
app.data.debugEnabled ? (
<div className="Form-group">
<Alert type="warning" dismissible={false}>
{app.translator.trans('flarum-package-manager.admin.settings.debug_mode_warning')}
</Alert>
</div>
) : null
)
.registerSetting({
setting: 'flarum-package-manager.queue_jobs',
label: app.translator.trans('flarum-package-manager.admin.settings.queue_jobs'),
Expand Down
1 change: 1 addition & 0 deletions extensions/package-manager/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ flarum-package-manager:

settings:
access_warning: Please be careful to who you give access to the admin area, the package manager could be misused by bad actors to install packages that can lead to security breaches.
debug_mode_warning: You are running in debug mode, the package manager cannot properly install and update local development packages. Please use the command line interface instead for such purposes.
queue_jobs: Run operations in the background queue
queue_jobs_help: >
You can read about a <a href='{basic_impl_link}'>basic queue</a> implementation or a <a href='{adv_impl_link}'>more advanced</a> one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);

dd('wat');

$response = $this->bus->dispatch(

Check failure on line 42 in extensions/package-manager/src/Api/Controller/GlobalUpdateController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Unreachable statement - code above always terminates.

Check failure on line 42 in extensions/package-manager/src/Api/Controller/GlobalUpdateController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Unreachable statement - code above always terminates.

Check failure on line 42 in extensions/package-manager/src/Api/Controller/GlobalUpdateController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Unreachable statement - code above always terminates.

Check failure on line 42 in extensions/package-manager/src/Api/Controller/GlobalUpdateController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Unreachable statement - code above always terminates.

Check failure on line 42 in extensions/package-manager/src/Api/Controller/GlobalUpdateController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Unreachable statement - code above always terminates.
new GlobalUpdate($actor)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

use Flarum\PackageManager\Composer\ComposerAdapter;
use Flarum\PackageManager\Exception\ComposerCommandFailedException;
use Flarum\PackageManager\Extension\ExtensionUtils;
use Flarum\PackageManager\Settings\LastUpdateCheck;
use Flarum\PackageManager\Support\Util;
use Symfony\Component\Console\Input\ArrayInput;

class CheckForUpdatesHandler
Expand Down Expand Up @@ -59,7 +59,7 @@ public function handle(CheckForUpdates $command)
$majorUpdates = false;

foreach ($firstOutput['installed'] as $package) {
if (isset($package['latest-status']) && $package['latest-status'] === 'update-possible' && ExtensionUtils::isMajorUpdate($package['version'], $package['latest'])) {
if (isset($package['latest-status']) && $package['latest-status'] === 'update-possible' && Util::isMajorUpdate($package['version'], $package['latest'])) {
$majorUpdates = true;
break;
}
Expand All @@ -77,7 +77,7 @@ public function handle(CheckForUpdates $command)
foreach ($firstOutput['installed'] as &$mainPackageUpdate) {
$mainPackageUpdate['latest-minor'] = $mainPackageUpdate['latest-major'] = null;

if (isset($mainPackageUpdate['latest-status']) && $mainPackageUpdate['latest-status'] === 'update-possible' && ExtensionUtils::isMajorUpdate($mainPackageUpdate['version'], $mainPackageUpdate['latest'])) {
if (isset($mainPackageUpdate['latest-status']) && $mainPackageUpdate['latest-status'] === 'update-possible' && Util::isMajorUpdate($mainPackageUpdate['version'], $mainPackageUpdate['latest'])) {
$mainPackageUpdate['latest-major'] = $mainPackageUpdate['latest'];

$minorPackageUpdate = array_filter($secondOutput['installed'], function ($package) use ($mainPackageUpdate) {
Expand Down
23 changes: 20 additions & 3 deletions extensions/package-manager/src/Command/GlobalUpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace Flarum\PackageManager\Command;

use Flarum\Bus\Dispatcher as FlarumDispatcher;
use Flarum\Foundation\Config;
use Flarum\PackageManager\Composer\ComposerAdapter;
use Flarum\PackageManager\Event\FlarumUpdated;
use Flarum\PackageManager\Exception\ComposerUpdateFailedException;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Input\ArrayInput;

class GlobalUpdateHandler
{
Expand All @@ -33,11 +34,17 @@ class GlobalUpdateHandler
*/
protected $commandDispatcher;

public function __construct(ComposerAdapter $composer, Dispatcher $events, FlarumDispatcher $commandDispatcher)
/**
* @var Config
*/
protected $config;

public function __construct(ComposerAdapter $composer, Dispatcher $events, FlarumDispatcher $commandDispatcher, Config $config)
{
$this->composer = $composer;
$this->events = $events;
$this->commandDispatcher = $commandDispatcher;
$this->config = $config;
}

/**
Expand All @@ -47,8 +54,18 @@ public function handle(GlobalUpdate $command)
{
$command->actor->assertAdmin();

$input = [
'command' => 'update',
'--prefer-dist' => true,
'--no-dev' => ! $this->config->inDebugMode(),
'-a' => true,
'--with-all-dependencies' => true,
];

dd($this->config->inDebugMode());

$output = $this->composer->run(

Check failure on line 67 in extensions/package-manager/src/Command/GlobalUpdateHandler.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Unreachable statement - code above always terminates.

Check failure on line 67 in extensions/package-manager/src/Command/GlobalUpdateHandler.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Unreachable statement - code above always terminates.

Check failure on line 67 in extensions/package-manager/src/Command/GlobalUpdateHandler.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Unreachable statement - code above always terminates.

Check failure on line 67 in extensions/package-manager/src/Command/GlobalUpdateHandler.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Unreachable statement - code above always terminates.

Check failure on line 67 in extensions/package-manager/src/Command/GlobalUpdateHandler.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Unreachable statement - code above always terminates.
new StringInput('update --prefer-dist --no-dev -a --with-all-dependencies'),
new ArrayInput($input),
$command->task ?? null
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Flarum\PackageManager\Exception\ComposerRequireFailedException;
use Flarum\PackageManager\Exception\ExtensionAlreadyInstalledException;
use Flarum\PackageManager\Extension\Event\Installed;
use Flarum\PackageManager\Extension\ExtensionUtils;
use Flarum\PackageManager\RequirePackageValidator;
use Flarum\PackageManager\Support\Util;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Console\Input\StringInput;

Expand Down Expand Up @@ -59,7 +59,7 @@ public function handle(RequireExtension $command)

$this->validator->assertValid(['package' => $command->package]);

$extensionId = ExtensionUtils::nameToId($command->package);
$extensionId = Util::nameToId($command->package);
$extension = $this->extensions->getExtension($extensionId);

if (! empty($extension)) {
Expand Down
3 changes: 2 additions & 1 deletion extensions/package-manager/src/Composer/ComposerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Composer\Console\Application;
use Flarum\Foundation\Paths;
use Flarum\PackageManager\OutputLogger;
use Flarum\PackageManager\Support\Util;
use Flarum\PackageManager\Task\Task;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function run(InputInterface $input, ?Task $task = null): ComposerOutput
chdir($currDir);

// @phpstan-ignore-next-line
$command = $input->__toString();
$command = Util::readableConsoleInput($input);

Check failure on line 65 in extensions/package-manager/src/Composer/ComposerAdapter.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

No error to ignore is reported on line 65.

Check failure on line 65 in extensions/package-manager/src/Composer/ComposerAdapter.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

No error to ignore is reported on line 65.

Check failure on line 65 in extensions/package-manager/src/Composer/ComposerAdapter.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

No error to ignore is reported on line 65.

Check failure on line 65 in extensions/package-manager/src/Composer/ComposerAdapter.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

No error to ignore is reported on line 65.

Check failure on line 65 in extensions/package-manager/src/Composer/ComposerAdapter.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

No error to ignore is reported on line 65.
$output = $this->output->fetch();

if ($task) {
Expand Down
42 changes: 0 additions & 42 deletions extensions/package-manager/src/Extension/ExtensionUtils.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Composer\Config;
use Composer\Console\Application;
use Composer\Util\Platform;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Paths;
Expand Down Expand Up @@ -40,9 +41,9 @@ public function register()
/** @var Paths $paths */
$paths = $container->make(Paths::class);

putenv("COMPOSER_HOME={$paths->storage}/.composer");
putenv("COMPOSER={$paths->base}/composer.json");
putenv('COMPOSER_DISABLE_XDEBUG_WARN=1');
Platform::putenv('COMPOSER_HOME', "$paths->storage/.composer");
Platform::putenv("COMPOSER", "$paths->base/composer.json");
Platform::putenv('COMPOSER_DISABLE_XDEBUG_WARN', '1');
Config::$defaultConfig['vendor-dir'] = $paths->vendor;

// When running simple require, update and remove commands on packages,
Expand Down
73 changes: 73 additions & 0 deletions extensions/package-manager/src/Support/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\PackageManager\Support;

use Illuminate\Support\Str;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;

class Util
{
public static function nameToId(string $name): string
{
[$vendor, $package] = explode('/', $name);
$package = str_replace(['flarum-ext-', 'flarum-'], '', $package);

return "$vendor-$package";
}

public static function isMajorUpdate(string $currentVersion, string $latestVersion): bool
{
// Drop any v prefixes
if(str_starts_with($currentVersion, 'v')) {
$currentVersion = substr($currentVersion, 1);
}

$currentVersion = explode('.', $currentVersion);
$latestVersion = explode('.', $latestVersion);

if (! is_numeric($currentVersion[0]) || ! is_numeric($latestVersion[0])) {
return false;
}

if (intval($currentVersion[0]) < intval($latestVersion[0])) {
return true;
}

return false;
}

public static function readableConsoleInput(InputInterface $input): string
{
if ($input instanceof ArrayInput) {
$input = explode(' ', $input->__toString());

foreach ($input as $key => $value) {
if (str_starts_with($value, '--')) {
if (! str_contains($value, '=')) {
unset($input[$key]);
} else {
$input[$key] = Str::before($value, '=');
}
}

if (is_numeric($value) && isset($input[$key - 1]) && str_starts_with($input[$key - 1], '-') && ! str_starts_with($input[$key - 1], '--')) {
unset($input[$key]);
}
}

return implode(' ', $input);
} elseif (method_exists($input, '__toString')) {
return $input->__toString();
}

return '';
}
}
4 changes: 2 additions & 2 deletions extensions/package-manager/tests/integration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Flarum\Foundation\Paths;
use Flarum\PackageManager\Composer\ComposerAdapter;
use Flarum\PackageManager\Composer\ComposerJson;
use Flarum\PackageManager\Extension\ExtensionUtils;
use Flarum\PackageManager\Support\Util;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -45,7 +45,7 @@ protected function assertExtension(string $id, bool $exists)
return $package['type'] === 'flarum-extension';
});
$installedExtensionIds = array_map(function (string $name) {
return ExtensionUtils::nameToId($name);
return Util::nameToId($name);
}, Arr::pluck($installedExtensions, 'name'));

if ($exists) {
Expand Down

0 comments on commit 1e390fe

Please sign in to comment.