Skip to content

Commit 52dba6c

Browse files
committed
Add import for composer.json/satis.json files [Temporary]
1 parent f58804c commit 52dba6c

File tree

6 files changed

+215
-2
lines changed

6 files changed

+215
-2
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
"ext-sodium": "*",
1515
"cebe/markdown": "^1.2",
1616
"composer/composer": "^2.7",
17+
"composer/satis": "dev-main",
1718
"doctrine/doctrine-bundle": "^2.11",
1819
"doctrine/doctrine-fixtures-bundle": "^4.0",
1920
"doctrine/doctrine-migrations-bundle": "^3.3",
2021
"doctrine/orm": "^3.3",
2122
"easycorp/easyadmin-bundle": "^4.8",
23+
"justinrainbow/json-schema": "^6.4",
2224
"league/commonmark": "^2.5",
2325
"phpdocumentor/reflection-docblock": "^5.3",
2426
"phpstan/phpdoc-parser": "^2.1",
@@ -106,6 +108,7 @@
106108
},
107109
"config": {
108110
"allow-plugins": {
111+
"composer/satis": false,
109112
"php-http/discovery": true,
110113
"symfony/flex": true,
111114
"symfony/runtime": true

composer.lock

Lines changed: 91 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controller/Dashboard/DashboardPackagesController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CodedMonkey\Dirigent\Doctrine\Entity\PackageFetchStrategy;
88
use CodedMonkey\Dirigent\Doctrine\Repository\PackageRepository;
99
use CodedMonkey\Dirigent\EasyAdmin\PackagePaginator;
10+
use CodedMonkey\Dirigent\Form\PackageAddComposerFormType;
1011
use CodedMonkey\Dirigent\Form\PackageAddMirroringFormType;
1112
use CodedMonkey\Dirigent\Form\PackageAddVcsFormType;
1213
use CodedMonkey\Dirigent\Form\PackageFormType;
@@ -244,6 +245,29 @@ public function addVcsRepository(Request $request): Response
244245
]);
245246
}
246247

248+
#[Route('/dashboard/packages/add-composer', name: 'dashboard_packages_add_composer')]
249+
#[IsGranted('ROLE_ADMIN')]
250+
public function importComposerFile(Request $request): Response
251+
{
252+
$form = $this->createForm(PackageAddComposerFormType::class);
253+
254+
$form->handleRequest($request);
255+
256+
if ($form->isSubmitted() && $form->isValid()) {
257+
$rawContents = $form->get('contents')->getData();
258+
$contents = json_decode($rawContents, true);
259+
260+
$importType = $form->get('type')->getData();
261+
262+
dump($importType);
263+
dd($contents);
264+
}
265+
266+
return $this->render('dashboard/packages/add_composer.html.twig', [
267+
'form' => $form,
268+
]);
269+
}
270+
247271
#[Route('/dashboard/packages/edit/{packageName}', name: 'dashboard_packages_edit', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
248272
#[IsGranted('ROLE_ADMIN')]
249273
public function edit(Request $request, string $packageName): Response
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Form;
4+
5+
use CodedMonkey\Dirigent\Doctrine\Entity\Credentials;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
7+
use CodedMonkey\Dirigent\Doctrine\Entity\PackageFetchStrategy;
8+
use CodedMonkey\Dirigent\Package\PackageVcsRepositoryValidator;
9+
use JsonSchema\Validator as JsonValidator;
10+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
11+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
12+
use Symfony\Component\Form\AbstractType;
13+
use Symfony\Component\Form\Event\PreSubmitEvent;
14+
use Symfony\Component\Form\Event\SubmitEvent;
15+
use Symfony\Component\Form\Extension\Core\Type\FileType;
16+
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
17+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
18+
use Symfony\Component\Form\Extension\Core\Type\TextType;
19+
use Symfony\Component\Form\FormBuilderInterface;
20+
use Symfony\Component\Form\FormError;
21+
use Symfony\Component\Form\FormEvents;
22+
use Symfony\Component\OptionsResolver\OptionsResolver;
23+
24+
class PackageAddComposerFormType extends AbstractType
25+
{
26+
public function __construct(
27+
#[Autowire(param: 'kernel.project_dir')]
28+
private readonly string $projectDir,
29+
) {
30+
}
31+
32+
public function buildForm(FormBuilderInterface $builder, array $options): void
33+
{
34+
$builder
35+
->add('contents', TextareaType::class)
36+
->add('type', HiddenType::class)
37+
->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'onPreSubmit']);
38+
}
39+
40+
public function onPreSubmit(PreSubmitEvent $event): void
41+
{
42+
$data = $event->getData();
43+
$form = $event->getForm();
44+
45+
$contentsField = $form->get('contents');
46+
$rawContents = $data['contents'];
47+
48+
if (!json_validate($rawContents)) {
49+
$contentsField->addError(new FormError(json_last_error_msg()));
50+
51+
return;
52+
}
53+
54+
$contents = json_decode($rawContents);
55+
56+
$composerSchemaValidator = new JsonValidator();
57+
$composerSchemaPath = "$this->projectDir/vendor/composer/composer/res/composer-schema.json";
58+
$composerSchemaValidator->validate($contents, (object) ['$ref' => "file://$composerSchemaPath"]);
59+
60+
if ($composerSchemaValidator->isValid()) {
61+
$event->setData([
62+
'contents' => $rawContents,
63+
'type' => 'composer',
64+
]);
65+
66+
return;
67+
}
68+
69+
$satisSchemaValidator = new JsonValidator();
70+
$satisSchemaPath = "$this->projectDir/vendor/composer/satis/res/satis-schema.json";
71+
$satisSchemaValidator->validate($contents, (object) ['$ref' => "file://$satisSchemaPath"]);
72+
73+
if ($satisSchemaValidator->isValid()) {
74+
$event->setData([
75+
'contents' => $rawContents,
76+
'type' => 'satis',
77+
]);
78+
79+
return;
80+
}
81+
82+
$contentsField->addError(new FormError('Failed to match schema of file'));
83+
}
84+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends '@EasyAdmin/page/content.html.twig' %}
2+
3+
{% block page_title %}Import Composer/Satis file{% endblock %}
4+
5+
{% block page_content %}
6+
{{ form_start(form) }}
7+
{{ form_row(form.contents, {attr: {rows: 12}}) }}
8+
9+
<button class="btn btn-primary">Import file</button>
10+
{{ form_end(form) }}
11+
{% endblock %}

templates/dashboard/packages/list.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<li><a class="dropdown-item" href="{{ addVcsUrl }}">Add VCS repository</a></li>
1919
{% set addMirroringUrl = dashboard_path('dashboard_packages_add_mirroring') %}
2020
<li><a class="dropdown-item" href="{{ addMirroringUrl }}">Mirror from registry</a></li>
21+
{% set addMirroringUrl = dashboard_path('dashboard_packages_add_composer') %}
22+
<li><a class="dropdown-item" href="{{ addMirroringUrl }}">{{ 'Import Composer/Satis file'|trans }}</a></li>
2123
</ul>
2224
</div>
2325
</div>

0 commit comments

Comments
 (0)