1
0
mirror of https://github.com/flarum/core.git synced 2025-08-05 16:07:34 +02:00

fix(em): composer.json schema issues

This commit is contained in:
Sami Mazouz
2024-01-12 17:56:58 +01:00
parent 25beb7919d
commit 24b7dcb102
4 changed files with 74 additions and 21 deletions

View File

@@ -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;
}
}

View File

@@ -9,6 +9,7 @@
namespace Flarum\ExtensionManager\Api\Controller; namespace Flarum\ExtensionManager\Api\Controller;
use Flarum\ExtensionManager\ConfigureAuthValidator;
use Flarum\Foundation\Paths; use Flarum\Foundation\Paths;
use Flarum\Http\RequestUtil; use Flarum\Http\RequestUtil;
use Flarum\ExtensionManager\Composer\ComposerJson; use Flarum\ExtensionManager\Composer\ComposerJson;
@@ -36,7 +37,12 @@ class ConfigureComposerController implements RequestHandlerInterface
/** /**
* @var ConfigureComposerValidator * @var ConfigureComposerValidator
*/ */
protected $validator; protected $composerValidator;
/**
* @var ConfigureComposerValidator
*/
protected $authValidator;
/** /**
* @var Paths * @var Paths
@@ -53,9 +59,10 @@ class ConfigureComposerController implements RequestHandlerInterface
*/ */
protected $filesystem; 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;
$this->paths = $paths; $this->paths = $paths;
$this->composerJson = $composerJson; $this->composerJson = $composerJson;
$this->filesystem = $filesystem; $this->filesystem = $filesystem;
@@ -89,7 +96,7 @@ class ConfigureComposerController implements RequestHandlerInterface
{ {
$data = Arr::only(Arr::get($request->getParsedBody(), 'data') ?? [], $this->configurable); $data = Arr::only(Arr::get($request->getParsedBody(), 'data') ?? [], $this->configurable);
$this->validator->assertValid(['composer' => $data]); $this->composerValidator->assertValid($data);
$composerJson = $this->composerJson->get(); $composerJson = $this->composerJson->get();
if (! empty($data)) { if (! empty($data)) {
@@ -105,10 +112,15 @@ class ConfigureComposerController implements RequestHandlerInterface
$default = [ $default = [
'minimum-stability' => 'stable', 'minimum-stability' => 'stable',
'repositories' => [],
]; ];
foreach ($this->configurable as $key) { foreach ($this->configurable as $key) {
$composerJson[$key] = Arr::get($composerJson, $key, Arr::get($default, $key)); $composerJson[$key] = Arr::get($composerJson, $key, Arr::get($default, $key));
if (is_null($composerJson[$key]) && ! is_null($default[$key])) {
$composerJson[$key] = $default[$key];
}
} }
$composerJson = Arr::sortRecursive($composerJson); $composerJson = Arr::sortRecursive($composerJson);
@@ -120,7 +132,7 @@ class ConfigureComposerController implements RequestHandlerInterface
{ {
$data = Arr::get($request->getParsedBody(), 'data'); $data = Arr::get($request->getParsedBody(), 'data');
$this->validator->assertValid(['auth' => $data]); $this->authValidator->assertValid($data ?? []);
try { try {
$authJson = json_decode($this->filesystem->get($this->paths->base.'/auth.json'), true); $authJson = json_decode($this->filesystem->get($this->paths->base.'/auth.json'), true);

View File

@@ -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'],
];
}

View File

@@ -13,22 +13,13 @@ use Flarum\Foundation\AbstractValidator;
class ConfigureComposerValidator extends AbstractValidator class ConfigureComposerValidator extends AbstractValidator
{ {
use AllValidatorRules;
protected $rules = [ protected $rules = [
'composer' => [
'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'], 'minimum-stability' => ['sometimes', 'in:stable,RC,beta,alpha,dev'],
'repositories' => ['sometimes', 'array'], 'repositories' => ['sometimes', 'array'],
'repositories.*.type' => ['sometimes', 'in:composer,vcs,path'], 'repositories.*' => ['sometimes', 'array', 'required_array_keys:type,url'],
'repositories.*.url' => ['sometimes', 'string'], 'repositories.*.type' => ['in:composer,vcs,path'],
], 'repositories.*.url' => ['string', 'filled'],
'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'],
],
]; ];
} }