-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHooksController.php
More file actions
72 lines (60 loc) · 1.66 KB
/
HooksController.php
File metadata and controls
72 lines (60 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace SyncEngine\Controller;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use SyncEngine\Event\HookEvent;
use SyncEngine\Event\RenderHookEvent;
/**
* @internal
* @experimental
*/
class HooksController extends DefaultController
{
public function __construct(
private readonly EventDispatcherInterface $dispatcher
) {}
#[Route( '/json/hook/{hookName}', name: 'json_hook' )]
public function jsonHook( string $hookName ): JsonResponse
{
try {
$html = $this->renderHookHtml( $hookName );
return $this->json( [
'success' => true,
'html' => $html,
] );
} catch ( \Exception $e ) {
return $this->json( [
'success' => false,
'message' => $e->getMessage(),
] );
}
}
public function getThemeHooks( $hookNames ): Response
{
return new JsonResponse( $this->renderHooksHtml( (array) $hookNames ) );
}
public function renderHook( string $hookName ): Response
{
return new Response( $this->renderHookHtml( $hookName ) );
}
public function renderHooksHtml( array $hookNames ): array
{
$return = [];
foreach ( $hookNames as $hookName ) {
$return[ $hookName ] = $this->renderHookHtml( $hookName );
}
return $return;
}
public function renderHookHtml( string $hookName ): string
{
$event = $this->doHook( new RenderHookEvent( '' ), $hookName );
return $event->getHtml();
}
public function doHook( HookEvent $event, string $hookName ): HookEvent
{
$this->dispatcher->dispatch( $event, 'syncengine.hook.' . $event->getHookType() . '.' . $hookName );
return $event;
}
}