1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiouPiou\RibsAdminBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Exception; |
7
|
|
|
use PiouPiou\RibsAdminBundle\Entity\Package; |
8
|
|
|
use PiouPiou\RibsAdminBundle\Service\Version; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
16
|
|
|
|
17
|
|
|
class PackageController extends AbstractController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @Route("/packages/", name="ribsadmin_packages") |
21
|
|
|
* @param EntityManagerInterface $em |
22
|
|
|
* @return Response |
23
|
|
|
*/ |
24
|
|
|
public function index(EntityManagerInterface $em): Response |
25
|
|
|
{ |
26
|
|
|
$packages = $em->getRepository(Package::class)->findAll(); |
27
|
|
|
|
28
|
|
|
return $this->render("@RibsAdmin/packages/list.html.twig", ["packages" => $packages]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @Route("/packages/create/", name="ribsadmin_packages_create") |
33
|
|
|
* @Route("/packages/show/{guid}", name="ribsadmin_packages_show") |
34
|
|
|
* @Route("/packages/edit/{guid}", name="ribsadmin_packages_edit") |
35
|
|
|
* @param Request $request |
36
|
|
|
* @param EntityManagerInterface $em |
37
|
|
|
* @param string|null $guid |
38
|
|
|
* @return Response |
39
|
|
|
*/ |
40
|
|
|
public function edit(Request $request, EntityManagerInterface $em, string $guid = null): Response |
41
|
|
|
{ |
42
|
|
|
$disabled_form = strpos($request->get("_route"), "_show") ? true : false; |
43
|
|
|
if (!$guid) { |
44
|
|
|
$package = new Package(); |
45
|
|
|
$text = "created"; |
46
|
|
|
} else { |
47
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]); |
48
|
|
|
$text = "edited"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$form = $this->createForm(\PiouPiou\RibsAdminBundle\Form\Package::class, $package, ["disabled" => $disabled_form]); |
52
|
|
|
$form->handleRequest($request); |
53
|
|
|
|
54
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
55
|
|
|
/** @var Package $data */ |
56
|
|
|
$data = $form->getData(); |
57
|
|
|
$em->persist($data); |
58
|
|
|
$em->flush(); |
59
|
|
|
$this->addFlash("success-flash", "Package " . $data->getProjectName() . " was " . $text); |
60
|
|
|
|
61
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->render("@RibsAdmin/packages/edit.html.twig", [ |
65
|
|
|
"form" => $form->createView(), |
66
|
|
|
"form_errors" => $form->getErrors(), |
67
|
|
|
"package" => $package |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @Route("/packages/delete/{guid}", name="ribsadmin_packages_delete") |
73
|
|
|
* @param EntityManagerInterface $em |
74
|
|
|
* @param string $guid |
75
|
|
|
* @return RedirectResponse |
76
|
|
|
*/ |
77
|
|
|
public function delete(EntityManagerInterface $em, string $guid): RedirectResponse |
78
|
|
|
{ |
79
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]); |
80
|
|
|
|
81
|
|
|
if ($package) { |
82
|
|
|
$em->remove($package); |
83
|
|
|
$em->flush(); |
84
|
|
|
$this->addFlash("success-flash", "The project package was deleted"); |
85
|
|
|
} else { |
86
|
|
|
$this->addFlash("error-flash", "The project package was not found"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Route("/packages/update/{guid}", name="ribsadmin_packages_update") |
94
|
|
|
* @param Version $version |
95
|
|
|
* @param string $guid |
96
|
|
|
* @return RedirectResponse |
97
|
|
|
* @throws Exception |
98
|
|
|
*/ |
99
|
|
|
public function updatePackage(Version $version, string $guid): RedirectResponse |
100
|
|
|
{ |
101
|
|
|
if ($guid) { |
102
|
|
|
$version->save($guid); |
103
|
|
|
|
104
|
|
|
if ($version->getMessages()) { |
105
|
|
|
$message = "<ul>"; |
106
|
|
|
$message .= "<li>The project package was not well updated</li>"; |
107
|
|
|
foreach ($version->getMessages() as $version_message) { |
108
|
|
|
$message .= "<li>".$version_message."</li>"; |
109
|
|
|
} |
110
|
|
|
$message .= "</ul>"; |
111
|
|
|
|
112
|
|
|
$this->addFlash("info-flash", $message); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->addFlash("success-flash", "The project package was updated"); |
116
|
|
|
} else { |
117
|
|
|
$this->addFlash("error-flash", "The project package was not found"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @Route("/packages/send-package/{package_name}", name="ribsadmin_packages_send", requirements={"package_name"=".+"}) |
125
|
|
|
* @param EntityManagerInterface $em |
126
|
|
|
* @param Version $version |
127
|
|
|
* @param string $package_name |
128
|
|
|
* @return mixed|null |
129
|
|
|
* @throws Exception |
130
|
|
|
*/ |
131
|
|
|
public function sendPackageInformations(EntityManagerInterface $em, Version $version, string $package_name): JsonResponse |
132
|
|
|
{ |
133
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["package_name" => $package_name]); |
134
|
|
|
|
135
|
|
|
if ($package) { |
136
|
|
|
$version->setPackageEntity($package); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return new JsonResponse([ |
140
|
|
|
"package" => $version->getPackage(), |
141
|
|
|
"package_date" => $version->getVersionDate() |
142
|
|
|
]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @Route("/packages/send-composer-lock/", name="ribsadmin_packages_send_composer_lock") |
147
|
|
|
* @return JsonResponse |
148
|
|
|
*/ |
149
|
|
|
public function sendComposerJson(): JsonResponse |
150
|
|
|
{ |
151
|
|
|
$composer_lock = file_get_contents('../composer.lock'); |
152
|
|
|
|
153
|
|
|
if ($composer_lock) { |
154
|
|
|
$composer_lock = json_decode($composer_lock); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return new JsonResponse($composer_lock); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @Route("/packages/send-token/", name="ribsadmin_packages_send_token") |
162
|
|
|
* @param ParameterBagInterface $parameter |
163
|
|
|
* @return JsonResponse |
164
|
|
|
*/ |
165
|
|
|
public function sendToken(ParameterBagInterface $parameter): JsonResponse |
166
|
|
|
{ |
167
|
|
|
return new JsonResponse([ |
168
|
|
|
"token" => $parameter->get("ribs_admin.packages_token") |
169
|
|
|
]); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|