Skip to content

Commit

Permalink
fix(em): composer.json schema issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Jan 12, 2024
1 parent 25beb79 commit 24b7dcb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
22 changes: 22 additions & 0 deletions extensions/package-manager/src/AllValidatorRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Flarum\ExtensionManager;

/**
* @todo: fix in 2.0
*/
trait AllValidatorRules
{
protected function makeValidator(array $attributes)
{
$rules = $this->getRules();

$validator = $this->validator->make($attributes, $rules, $this->getMessages());

foreach ($this->configuration as $callable) {
$callable($this, $validator);
}

return $validator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\ExtensionManager\Api\Controller;

use Flarum\ExtensionManager\ConfigureAuthValidator;
use Flarum\Foundation\Paths;
use Flarum\Http\RequestUtil;
use Flarum\ExtensionManager\Composer\ComposerJson;
Expand Down Expand Up @@ -36,7 +37,12 @@ class ConfigureComposerController implements RequestHandlerInterface
/**
* @var ConfigureComposerValidator
*/
protected $validator;
protected $composerValidator;

/**
* @var ConfigureComposerValidator
*/
protected $authValidator;

/**
* @var Paths
Expand All @@ -53,9 +59,10 @@ class ConfigureComposerController implements RequestHandlerInterface
*/
protected $filesystem;

public function __construct(ConfigureComposerValidator $validator, Paths $paths, ComposerJson $composerJson, Filesystem $filesystem)
public function __construct(ConfigureComposerValidator $composerValidator, ConfigureAuthValidator $authValidator, Paths $paths, ComposerJson $composerJson, Filesystem $filesystem)
{
$this->validator = $validator;
$this->composerValidator = $composerValidator;
$this->authValidator = $authValidator;

Check failure on line 65 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Property Flarum\ExtensionManager\Api\Controller\ConfigureComposerController::$authValidator (Flarum\ExtensionManager\ConfigureComposerValidator) does not accept Flarum\ExtensionManager\ConfigureAuthValidator.

Check failure on line 65 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Property Flarum\ExtensionManager\Api\Controller\ConfigureComposerController::$authValidator (Flarum\ExtensionManager\ConfigureComposerValidator) does not accept Flarum\ExtensionManager\ConfigureAuthValidator.

Check failure on line 65 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Property Flarum\ExtensionManager\Api\Controller\ConfigureComposerController::$authValidator (Flarum\ExtensionManager\ConfigureComposerValidator) does not accept Flarum\ExtensionManager\ConfigureAuthValidator.

Check failure on line 65 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Property Flarum\ExtensionManager\Api\Controller\ConfigureComposerController::$authValidator (Flarum\ExtensionManager\ConfigureComposerValidator) does not accept Flarum\ExtensionManager\ConfigureAuthValidator.

Check failure on line 65 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Property Flarum\ExtensionManager\Api\Controller\ConfigureComposerController::$authValidator (Flarum\ExtensionManager\ConfigureComposerValidator) does not accept Flarum\ExtensionManager\ConfigureAuthValidator.
$this->paths = $paths;
$this->composerJson = $composerJson;
$this->filesystem = $filesystem;
Expand Down Expand Up @@ -89,7 +96,7 @@ protected function composerConfig(ServerRequestInterface $request): array
{
$data = Arr::only(Arr::get($request->getParsedBody(), 'data') ?? [], $this->configurable);

$this->validator->assertValid(['composer' => $data]);
$this->composerValidator->assertValid($data);
$composerJson = $this->composerJson->get();

if (! empty($data)) {
Expand All @@ -105,10 +112,15 @@ protected function composerConfig(ServerRequestInterface $request): array

$default = [
'minimum-stability' => 'stable',
'repositories' => [],
];

foreach ($this->configurable as $key) {
$composerJson[$key] = Arr::get($composerJson, $key, Arr::get($default, $key));

if (is_null($composerJson[$key]) && ! is_null($default[$key])) {

Check failure on line 121 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Call to function is_null() with 'stable'|array{} will always evaluate to false.

Check failure on line 121 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Call to function is_null() with 'stable'|array{} will always evaluate to false.

Check failure on line 121 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Call to function is_null() with 'stable'|array{} will always evaluate to false.

Check failure on line 121 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Call to function is_null() with 'stable'|array{} will always evaluate to false.

Check failure on line 121 in extensions/package-manager/src/Api/Controller/ConfigureComposerController.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Call to function is_null() with 'stable'|array{} will always evaluate to false.
$composerJson[$key] = $default[$key];
}
}

$composerJson = Arr::sortRecursive($composerJson);
Expand All @@ -120,7 +132,7 @@ protected function authConfig(ServerRequestInterface $request): array
{
$data = Arr::get($request->getParsedBody(), 'data');

$this->validator->assertValid(['auth' => $data]);
$this->authValidator->assertValid($data ?? []);

try {
$authJson = json_decode($this->filesystem->get($this->paths->base.'/auth.json'), true);
Expand Down
28 changes: 28 additions & 0 deletions extensions/package-manager/src/ConfigureAuthValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\ExtensionManager;

use Flarum\Foundation\AbstractValidator;

class ConfigureAuthValidator extends AbstractValidator
{
use AllValidatorRules;

protected $rules = [
'github-oauth' => ['sometimes', 'array'],
'github-oauth.*' => ['sometimes', 'string'],
'gitlab-oauth' => ['sometimes', 'array'],
'gitlab-oauth.*' => ['sometimes', 'string'],
'gitlab-token' => ['sometimes', 'array'],
'gitlab-token.*' => ['sometimes', 'string'],
'bearer' => ['sometimes', 'array'],
'bearer.*' => ['sometimes', 'string'],
];
}
23 changes: 7 additions & 16 deletions extensions/package-manager/src/ConfigureComposerValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@

class ConfigureComposerValidator extends AbstractValidator
{
use AllValidatorRules;

protected $rules = [
'composer' => [
'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'],
'repositories' => ['sometimes', 'array'],
'repositories.*.type' => ['sometimes', 'in:composer,vcs,path'],
'repositories.*.url' => ['sometimes', 'string'],
],
'auth' => [
'github-oauth' => ['sometimes', 'array'],
'github-oauth.*' => ['sometimes', 'string'],
'gitlab-oauth' => ['sometimes', 'array'],
'gitlab-oauth.*' => ['sometimes', 'string'],
'gitlab-token' => ['sometimes', 'array'],
'gitlab-token.*' => ['sometimes', 'string'],
'bearer' => ['sometimes', 'array'],
'bearer.*' => ['sometimes', 'string'],
],
'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'],
'repositories' => ['sometimes', 'array'],
'repositories.*' => ['sometimes', 'array', 'required_array_keys:type,url'],
'repositories.*.type' => ['in:composer,vcs,path'],
'repositories.*.url' => ['string', 'filled'],
];
}

0 comments on commit 24b7dcb

Please sign in to comment.