-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
284 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
extensions/package-manager/js/src/admin/components/ConfigureComposer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import app from 'flarum/admin/app'; | ||
import type Mithril from 'mithril'; | ||
import Component, { type ComponentAttrs } from 'flarum/common/Component'; | ||
import { CommonSettingsItemOptions, type SettingsComponentOptions } from '@flarum/core/src/admin/components/AdminPage'; | ||
import AdminPage from 'flarum/admin/components/AdminPage'; | ||
import type ItemList from 'flarum/common/utils/ItemList'; | ||
import Stream from 'flarum/common/utils/Stream'; | ||
import Button from 'flarum/common/components/Button'; | ||
|
||
export interface IConfigureComposer extends ComponentAttrs { | ||
buildSettingComponent: (entry: ((this: this) => Mithril.Children) | SettingsComponentOptions) => Mithril.Children; | ||
} | ||
|
||
export default class ConfigureComposer<CustomAttrs extends IConfigureComposer = IConfigureComposer> extends Component<CustomAttrs> { | ||
protected settings: Record<string, Stream<any>> = {}; | ||
protected loading: boolean = false; | ||
|
||
oninit(vnode: Mithril.Vnode<CustomAttrs, this>) { | ||
super.oninit(vnode); | ||
|
||
this.submit(); | ||
} | ||
|
||
view(): Mithril.Children { | ||
return ( | ||
<div className="Form"> | ||
{this.attrs.buildSettingComponent.call(this, { | ||
setting: 'minimum-stability', | ||
label: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.label'), | ||
help: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.help'), | ||
type: 'select', | ||
options: { | ||
stable: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.stable'), | ||
RC: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.rc'), | ||
beta: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.beta'), | ||
alpha: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.alpha'), | ||
dev: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.dev'), | ||
}, | ||
})} | ||
|
||
<div className="Form-group"> | ||
<Button className="Button Button--primary" loading={this.loading} onclick={this.submit.bind(this)}> | ||
{app.translator.trans('core.admin.settings.submit_button')} | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
customSettingComponents(): ItemList<(attributes: CommonSettingsItemOptions) => Mithril.Children> { | ||
return AdminPage.prototype.customSettingComponents(); | ||
} | ||
|
||
setting(key: string) { | ||
return this.settings[key] ?? (() => null); | ||
} | ||
|
||
submit() { | ||
this.loading = true; | ||
|
||
const configuration: any = {}; | ||
|
||
Object.keys(this.settings).forEach((key) => { | ||
configuration[key] = this.settings[key](); | ||
}); | ||
|
||
app | ||
.request({ | ||
method: 'POST', | ||
url: app.forum.attribute('apiUrl') + '/package-manager/composer', | ||
body: { data: configuration }, | ||
}) | ||
.then(({ data }: any) => { | ||
Object.keys(data).forEach((key) => { | ||
this.settings[key] = Stream(data[key]); | ||
}); | ||
}) | ||
.finally(() => { | ||
this.loading = false; | ||
m.redraw(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
extensions/package-manager/src/Api/Controller/ConfigureComposerController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?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\Api\Controller; | ||
|
||
use Flarum\Foundation\Paths; | ||
use Flarum\Http\RequestUtil; | ||
use Flarum\PackageManager\ConfigureComposerValidator; | ||
use Illuminate\Support\Arr; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class ConfigureComposerController implements RequestHandlerInterface | ||
{ | ||
protected $configurable = [ | ||
'minimum-stability', | ||
]; | ||
|
||
/** | ||
* @var ConfigureComposerValidator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* @var Paths | ||
*/ | ||
protected $paths; | ||
|
||
public function __construct(ConfigureComposerValidator $validator, Paths $paths) | ||
{ | ||
$this->validator = $validator; | ||
$this->paths = $paths; | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$actor = RequestUtil::getActor($request); | ||
$data = Arr::only(Arr::get($request->getParsedBody(), 'data'), $this->configurable); | ||
|
||
$actor->assertAdmin(); | ||
|
||
$this->validator->assertValid($data); | ||
$composerJson = $this->readComposerJson(); | ||
|
||
if (! empty($data)) { | ||
foreach ($data as $key => $value) { | ||
Arr::set($composerJson, $key, $value); | ||
} | ||
|
||
$this->writeComposerJson($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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.