From bdf9c1576e393c9c2d7437be46abb05f01fa03eb Mon Sep 17 00:00:00 2001 From: Uladzimir Tsykun Date: Fri, 22 Dec 2023 21:33:05 +0100 Subject: [PATCH] Added events hooks to simplify user menu customization --- src/Controller/ZipballController.php | 6 ++++ src/Event/MenuLoadEvent.php | 33 +++++++++++++++++++ src/Event/ZipballEvent.php | 49 ++++++++++++++++++++++++++++ src/Menu/MenuBuilder.php | 12 +++++++ templates/user/update.html.twig | 9 +++-- 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 src/Event/MenuLoadEvent.php create mode 100644 src/Event/ZipballEvent.php diff --git a/src/Controller/ZipballController.php b/src/Controller/ZipballController.php index 8c0b8338..97020943 100644 --- a/src/Controller/ZipballController.php +++ b/src/Controller/ZipballController.php @@ -8,6 +8,7 @@ use Packeton\Attribute\Vars; use Packeton\Entity\Package; use Packeton\Entity\Zipball; +use Packeton\Event\ZipballEvent; use Packeton\Model\UploadZipballStorage; use Packeton\Package\RepTypes; use Packeton\Service\DistManager; @@ -20,6 +21,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Attribute\IsGranted; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class ZipballController extends AbstractController { @@ -27,6 +29,7 @@ public function __construct( protected DistManager $dm, protected UploadZipballStorage $storage, protected ManagerRegistry $registry, + protected EventDispatcherInterface $dispatcher ) { } @@ -99,6 +102,9 @@ public function zipballAction(#[Vars('name')] Package $package, string $hash): R return $this->createNotFound($msg); } + $this->dispatcher->dispatch($event = new ZipballEvent($package, $reference, $dist), ZipballEvent::DOWNLOAD); + $dist = $event->getDist(); + if (is_string($dist)) { return new BinaryFileResponse($dist); } diff --git a/src/Event/MenuLoadEvent.php b/src/Event/MenuLoadEvent.php new file mode 100644 index 00000000..612a42b0 --- /dev/null +++ b/src/Event/MenuLoadEvent.php @@ -0,0 +1,33 @@ +menu; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } +} diff --git a/src/Event/ZipballEvent.php b/src/Event/ZipballEvent.php new file mode 100644 index 00000000..8f004646 --- /dev/null +++ b/src/Event/ZipballEvent.php @@ -0,0 +1,49 @@ +package; + } + + /** + * @return string + */ + public function getReference(): string + { + return $this->reference; + } + + /** + * @return mixed + */ + public function getDist(): mixed + { + return $this->dist; + } + + public function setDist(mixed $dist) + { + $this->dist = $dist; + } +} diff --git a/src/Menu/MenuBuilder.php b/src/Menu/MenuBuilder.php index ed70c25d..bde02b5e 100644 --- a/src/Menu/MenuBuilder.php +++ b/src/Menu/MenuBuilder.php @@ -5,10 +5,12 @@ use Knp\Menu\FactoryInterface; use Knp\Menu\ItemInterface; use Packeton\Entity\User; +use Packeton\Event\MenuLoadEvent; use Packeton\Integrations\IntegrationRegistry; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\Translation\TranslatorInterface; class MenuBuilder @@ -19,6 +21,7 @@ public function __construct( private readonly TranslatorInterface $translator, private readonly AuthorizationCheckerInterface $checker, private readonly IntegrationRegistry $integrations, + private readonly EventDispatcherInterface $dispatcher, ) { } @@ -59,6 +62,8 @@ public function createAdminMenu() $menu->addChild($this->translator->trans('menu.integrations'), ['label' => 'menu.integrations_icon', 'route' => 'integration_list', 'extras' => ['safe_label' => true]]); } + $this->dispatchLoad($menu, 'admin_menu'); + return $menu; } @@ -80,6 +85,8 @@ private function addProfileMenu(ItemInterface $menu) } else if ($user instanceof UserInterface) { $menu->addChild($this->translator->trans('menu.my_tokens'), ['label' => 'menu.my_tokens_icon', 'route' => 'profile_list_tokens', 'extras' => ['safe_label' => true]]); } + + $this->dispatchLoad($menu, 'user_menu'); } private function getUsername() @@ -90,4 +97,9 @@ private function getUsername() return null; } + + private function dispatchLoad(ItemInterface $menu, string $name) + { + $this->dispatcher->dispatch(new MenuLoadEvent($menu, $name), MenuLoadEvent::NAME); + } } diff --git a/templates/user/update.html.twig b/templates/user/update.html.twig index b38add4a..3cacf08f 100644 --- a/templates/user/update.html.twig +++ b/templates/user/update.html.twig @@ -11,10 +11,6 @@
{{ form_start(form, { attr: { class: 'col-md-6' } }) }} - {% set formEnd %} - {{ form_row(form.expiresAt) }} - {{ form_row(form.groups) }} - {% endset %} {{ form_row(form.username) }} {{ form_row(form.email) }} @@ -22,8 +18,11 @@ {{ form_row(form.expiredUpdatesAt) }} {{ form_row(form.enabled) }} {{ form_row(form.fullAccess) }} + {{ form.isMaintainer is defined ? form_row(form.isMaintainer) : '' }} + {{ form.invitation is defined ? form_row(form.invitation) : '' }} + {{ form_row(form.expiresAt) }} + {{ form_row(form.groups) }} {{ form_rest(form) }} - {{ formEnd|raw }} {{ form_end(form) }}