This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractBlockHandler.php
156 lines (134 loc) · 4.03 KB
/
AbstractBlockHandler.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\BlocksModule;
use LogicException;
use Symfony\Component\HttpFoundation\RequestStack;
use function Symfony\Component\String\s;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Zikula\Bundle\CoreBundle\Translation\TranslatorTrait;
use Zikula\ExtensionsModule\AbstractExtension;
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
use Zikula\ExtensionsModule\ExtensionVariablesTrait;
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
abstract class AbstractBlockHandler implements BlockHandlerInterface
{
use ExtensionVariablesTrait;
use TranslatorTrait;
/**
* @var AbstractExtension
*/
protected $extension;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var PermissionApiInterface
*/
protected $permissionApi;
/**
* @var Environment
*/
protected $twig;
public function __construct(
AbstractExtension $extension,
RequestStack $requestStack,
TranslatorInterface $translator,
VariableApiInterface $variableApi,
PermissionApiInterface $permissionApi,
Environment $twig
) {
$this->extension = $extension;
$this->extensionName = $extension->getName(); // for ExtensionVariablesTrait
$this->requestStack = $requestStack;
$this->setTranslator($translator); // for TranslatorTrait
$this->variableApi = $variableApi; // for ExtensionVariablesTrait
$this->permissionApi = $permissionApi;
$this->twig = $twig;
$this->boot($extension);
}
/**
* Boot the handler.
*/
protected function boot(AbstractExtension $extension): void
{
// load optional bootstrap
$bootstrap = $extension->getPath() . '/bootstrap.php';
if (file_exists($bootstrap)) {
include_once $bootstrap;
}
}
public function getFormClassName(): string
{
return '';
}
public function getFormOptions(): array
{
return [];
}
public function getFormTemplate(): string
{
return '@ZikulaBlocksModule/Block/default_modify.html.twig';
}
public function getPropertyDefaults(): array
{
return [];
}
public function display(array $properties): string
{
return nl2br(implode("\n", $properties));
}
public function getType(): string
{
// default to the ClassName without the `Block` suffix
// note: This string is intentionally left untranslated.
return s(static::class)->afterLast('\\')->beforeLast('Block')->toString();
}
/**
* Adds a flash message to the current session for type.
*
* @throws LogicException
*/
protected function addFlash(string $type, string $message): void
{
$request = $this->requestStack->getCurrentRequest();
if (null === $request) {
return;
}
if (!$request->hasSession()) {
throw new LogicException('You can not use the addFlash method if sessions are disabled.');
}
$request->getSession()->getFlashBag()->add($type, $message);
}
/**
* Returns a rendered view.
*/
public function renderView(string $view, array $parameters = []): string
{
return $this->twig->render($view, $parameters);
}
/**
* Convenience shortcut to check if user has requested permissions.
*/
protected function hasPermission(
string $component = null,
string $instance = null,
int $level = null,
int $user = null
): bool {
return $this->permissionApi->hasPermission($component, $instance, $level, $user);
}
public function getExtension(): AbstractExtension
{
return $this->extension;
}
}