Skip to content

Commit

Permalink
Merge pull request #206 from vtsykun/feat/cust1
Browse files Browse the repository at this point in the history
Added events hooks to simplify user menu customization
  • Loading branch information
vtsykun authored Dec 22, 2023
2 parents e90da30 + bdf9c15 commit 2f5ad26
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/Controller/ZipballController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,13 +21,15 @@
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
{
public function __construct(
protected DistManager $dm,
protected UploadZipballStorage $storage,
protected ManagerRegistry $registry,
protected EventDispatcherInterface $dispatcher
) {
}

Expand Down Expand Up @@ -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);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Event/MenuLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Packeton\Event;

use Knp\Menu\ItemInterface;
use Symfony\Contracts\EventDispatcher\Event;

class MenuLoadEvent extends Event
{
public const NAME = 'menuLoad';

public function __construct(private readonly ItemInterface $menu, private readonly string $name)
{
}

/**
* @return ItemInterface
*/
public function getMenu(): ItemInterface
{
return $this->menu;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
49 changes: 49 additions & 0 deletions src/Event/ZipballEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Packeton\Event;

use Packeton\Entity\Package;
use Symfony\Contracts\EventDispatcher\Event;

class ZipballEvent extends Event
{
public const DOWNLOAD = 'zipballDownload';

public function __construct(
private Package $package,
private string $reference,
private mixed $dist,
) {
}

/**
* @return Package
*/
public function getPackage(): Package
{
return $this->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;
}
}
12 changes: 12 additions & 0 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +21,7 @@ public function __construct(
private readonly TranslatorInterface $translator,
private readonly AuthorizationCheckerInterface $checker,
private readonly IntegrationRegistry $integrations,
private readonly EventDispatcherInterface $dispatcher,
) {
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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()
Expand All @@ -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);
}
}
9 changes: 4 additions & 5 deletions templates/user/update.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
<section class="row">
{{ 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) }}
{{ form_row(form.plainPassword) }}
{{ 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 }}
<input class="btn btn-block btn-success btn-lg" type="submit" value="{{ 'submit.submit'|trans }}" />
{{ form_end(form) }}
<div class="col-md-6">
Expand Down

0 comments on commit 2f5ad26

Please sign in to comment.