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\PackagistApi; |
9
|
|
|
use PiouPiou\RibsAdminBundle\Service\Version; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
16
|
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
17
|
|
|
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
18
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
19
|
|
|
|
20
|
|
|
class PackageController extends AbstractController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @Route("/packages/", name="ribsadmin_packages") |
24
|
|
|
* @param EntityManagerInterface $em |
25
|
|
|
* @return Response |
26
|
|
|
*/ |
27
|
|
|
public function index(EntityManagerInterface $em): Response |
28
|
|
|
{ |
29
|
|
|
$packages = $em->getRepository(Package::class)->findAll(); |
30
|
|
|
|
31
|
|
|
return $this->render("@RibsAdmin/packages/list.html.twig", ["packages" => $packages]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Route("/packages/create/", name="ribsadmin_packages_create") |
36
|
|
|
* @Route("/packages/show/{guid}", name="ribsadmin_packages_show") |
37
|
|
|
* @Route("/packages/edit/{guid}", name="ribsadmin_packages_edit") |
38
|
|
|
* @param Request $request |
39
|
|
|
* @param EntityManagerInterface $em |
40
|
|
|
* @param PackagistApi $packagist |
41
|
|
|
* @param string|null $guid |
42
|
|
|
* @return Response |
43
|
|
|
* @throws ClientExceptionInterface |
44
|
|
|
* @throws RedirectionExceptionInterface |
45
|
|
|
* @throws ServerExceptionInterface |
46
|
|
|
* @throws TransportExceptionInterface |
47
|
|
|
*/ |
48
|
|
|
public function edit(Request $request, EntityManagerInterface $em, PackagistApi $packagist, string $guid = null): Response |
49
|
|
|
{ |
50
|
|
|
$disabled_form = strpos($request->get("_route"), "_show") ? true : false; |
51
|
|
|
if (!$guid) { |
52
|
|
|
$package = new Package(); |
53
|
|
|
$text = "created"; |
54
|
|
|
} else { |
55
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]); |
56
|
|
|
$text = "edited"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$form = $this->createForm(\PiouPiou\RibsAdminBundle\Form\Package::class, $package, ["disabled" => $disabled_form]); |
60
|
|
|
$form->handleRequest($request); |
61
|
|
|
|
62
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
63
|
|
|
/** @var Package $data */ |
64
|
|
|
$data = $form->getData(); |
65
|
|
|
$em->persist($data); |
66
|
|
|
$em->flush(); |
67
|
|
|
$this->addFlash("success-flash", "Package " . $data->getProjectName() . " was " . $text); |
68
|
|
|
|
69
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render("@RibsAdmin/packages/edit.html.twig", [ |
73
|
|
|
"disabled_form" => $disabled_form, |
74
|
|
|
"form" => $form->createView(), |
75
|
|
|
"form_errors" => $form->getErrors(), |
76
|
|
|
"package" => $package, |
77
|
|
|
"versions" => $packagist->getAllPackagistVersions($package->getPackageName()) |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @Route("/packages/delete/{guid}", name="ribsadmin_packages_delete") |
83
|
|
|
* @param EntityManagerInterface $em |
84
|
|
|
* @param string $guid |
85
|
|
|
* @return RedirectResponse |
86
|
|
|
*/ |
87
|
|
|
public function delete(EntityManagerInterface $em, string $guid): RedirectResponse |
88
|
|
|
{ |
89
|
|
|
$package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]); |
90
|
|
|
|
91
|
|
|
if ($package) { |
92
|
|
|
$em->remove($package); |
93
|
|
|
$em->flush(); |
94
|
|
|
$this->addFlash("success-flash", "The project package was deleted"); |
95
|
|
|
} else { |
96
|
|
|
$this->addFlash("error-flash", "The project package was not found"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @Route("/packages/update/{guid}", name="ribsadmin_packages_update") |
104
|
|
|
* @param Version $version |
105
|
|
|
* @param string $guid |
106
|
|
|
* @return RedirectResponse |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
|
|
public function updatePackage(Version $version, string $guid): RedirectResponse |
110
|
|
|
{ |
111
|
|
|
if ($guid) { |
112
|
|
|
$version->save($guid); |
113
|
|
|
|
114
|
|
|
if ($version->getMessages()) { |
115
|
|
|
$message = "<ul>"; |
116
|
|
|
$message .= "<li>The project package was not well updated</li>"; |
117
|
|
|
foreach ($version->getMessages() as $version_message) { |
118
|
|
|
$message .= "<li>".$version_message."</li>"; |
119
|
|
|
} |
120
|
|
|
$message .= "</ul>"; |
121
|
|
|
|
122
|
|
|
$this->addFlash("info-flash", $message); |
123
|
|
|
} else { |
124
|
|
|
$this->addFlash("success-flash", "The project package was updated"); |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
$this->addFlash("error-flash", "The project package was not found"); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->redirectToRoute("ribsadmin_packages"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @Route("/packages/update-version/{guid}/{install_version}", name="ribsadmin_packages_update_version") |
135
|
|
|
* @param Version $version |
136
|
|
|
* @param string $guid |
137
|
|
|
* @param string $install_version |
138
|
|
|
* @return RedirectResponse |
139
|
|
|
*/ |
140
|
|
|
public function changePackageVersion(Version $version, string $guid, string $install_version): RedirectResponse |
141
|
|
|
{ |
142
|
|
|
$version->updatePackage($guid, $install_version); |
143
|
|
|
|
144
|
|
|
return $this->redirectToRoute("ribsadmin_packages_update", ["guid" => $guid]); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|