From d6632ca72254168fa8e3597076ba52137b560289 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Wed, 13 Dec 2023 16:25:38 +0100 Subject: [PATCH] chore: simplify --- .../admin/components/ConfigureComposer.tsx | 4 +-- .../ConfigureComposerController.php | 27 +++++++------------ .../src/Composer/ComposerJson.php | 2 +- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx b/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx index dbf9e64347..012fa97f47 100644 --- a/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx +++ b/extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx @@ -76,9 +76,7 @@ export default class ConfigureComposer { this.loading = false; diff --git a/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php b/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php index 16f399f951..ecd716c7b9 100755 --- a/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php +++ b/extensions/package-manager/src/Api/Controller/ConfigureComposerController.php @@ -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; @@ -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 @@ -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); - } } diff --git a/extensions/package-manager/src/Composer/ComposerJson.php b/extensions/package-manager/src/Composer/ComposerJson.php index 706b6fabd8..2d99ab83bf 100644 --- a/extensions/package-manager/src/Composer/ComposerJson.php +++ b/extensions/package-manager/src/Composer/ComposerJson.php @@ -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)); }