Skip to content

Commit

Permalink
chore: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 13, 2023
1 parent 1ccbd36 commit d6632ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ export default class ConfigureComposer<CustomAttrs extends IConfigureComposer =
this.settings[key] = Stream(data[key]);
});

if (this.initialSettings === null) {
this.initialSettings = data;
}
this.initialSettings = data;
})
.finally(() => {
this.loading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Flarum\Foundation\Paths;
use Flarum\Http\RequestUtil;
use Flarum\PackageManager\Composer\ComposerJson;
use Flarum\PackageManager\ConfigureComposerValidator;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\JsonResponse;
Expand All @@ -34,10 +35,16 @@ class ConfigureComposerController implements RequestHandlerInterface
*/
protected $paths;

public function __construct(ConfigureComposerValidator $validator, Paths $paths)
/**
* @var ComposerJson
*/
protected $composerJson;

public function __construct(ConfigureComposerValidator $validator, Paths $paths, ComposerJson $composerJson)
{
$this->validator = $validator;
$this->paths = $paths;
$this->composerJson = $composerJson;
}

public function handle(ServerRequestInterface $request): ResponseInterface
Expand All @@ -48,32 +55,18 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$actor->assertAdmin();

$this->validator->assertValid($data);
$composerJson = $this->readComposerJson();
$composerJson = $this->composerJson->get();

if (! empty($data)) {
foreach ($data as $key => $value) {
Arr::set($composerJson, $key, $value);
}

$this->writeComposerJson($composerJson);
$this->composerJson->set($composerJson);
}

return new JsonResponse([
'data' => Arr::only($composerJson, $this->configurable),
]);
}

protected function readComposerJson()
{
$composerJson = file_get_contents($this->paths->base.'/composer.json');
$composerJson = json_decode($composerJson, true);

return $composerJson;
}

protected function writeComposerJson($composerJson)
{
$composerJson = json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
file_put_contents($this->paths->base.'/composer.json', $composerJson);
}
}
2 changes: 1 addition & 1 deletion extensions/package-manager/src/Composer/ComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function get(): array
return $json;
}

protected function set(array $json): void
public function set(array $json): void
{
$this->filesystem->put($this->getComposerJsonPath(), json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
Expand Down

0 comments on commit d6632ca

Please sign in to comment.