1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami\View\Twig\Functions; |
5
|
|
|
|
6
|
|
|
use Selami\View\FunctionInterface; |
7
|
|
|
use Selami\Stdlib\CaseConverter; |
8
|
|
|
use BadMethodCallException; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
use Twig\Environment; |
12
|
|
|
|
13
|
|
|
class Widget implements FunctionInterface |
14
|
|
|
{ |
15
|
|
|
private $twig; |
16
|
|
|
private $config; |
17
|
|
|
private $templateFile; |
18
|
|
|
private $widgetData; |
19
|
|
|
|
20
|
|
|
public function __construct(Environment $twig, array $config, string $widgetNameStr, string $widgetActionStr, array $args) |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
$this->twig = $twig; |
24
|
|
|
$this->config = $config; |
25
|
|
|
$widgetAction = CaseConverter::toPascalCase($widgetActionStr); |
26
|
|
|
$widgetName = CaseConverter::toPascalCase($widgetNameStr); |
27
|
|
|
$widget = '\\' . $this->config['app_namespace'] . '\\Widget\\' . $widgetName; |
28
|
|
|
$this->checkClassIfExists($widget, $widgetName, $widgetAction); |
29
|
|
|
$widgetInstance = new $widget($args); |
30
|
|
|
$templateFileBasename = $args['template'] ?? CaseConverter::toSnakeCase($widgetActionStr) . '.twig'; |
31
|
|
|
$this->checkIdTemplateFileExist($templateFileBasename, $widgetNameStr, $widgetActionStr); |
32
|
|
|
$this->templateFile = '_widgets/' |
33
|
|
|
. CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename; |
34
|
|
|
$this->widgetData = $widgetInstance->{$widgetAction}(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function checkClassIfExists($widget, $widgetName, $widgetAction) |
38
|
|
|
{ |
39
|
|
|
if (!class_exists($widget)) { |
40
|
|
|
$message = 'Widget ' . $widgetName . '_' . $widgetAction . ' has not class name as ' . $widget; |
41
|
|
|
throw new BadMethodCallException($message); |
42
|
|
|
} |
43
|
|
|
if (!method_exists($widget, $widgetAction)) { |
44
|
|
|
$message = 'Widget ' . $widget . ' has not method name as ' . $widgetAction; |
45
|
|
|
throw new BadMethodCallException($message); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function checkIdTemplateFileExist( |
50
|
|
|
string $templateFileBasename, |
51
|
|
|
string $widgetNameStr, |
52
|
|
|
string $widgetActionStr |
53
|
|
|
) { |
54
|
|
|
$templateFullPath = $this->config['templates_dir'] . '/_widgets/' |
55
|
|
|
. CaseConverter::toSnakeCase($widgetNameStr) . '/' . $templateFileBasename; |
56
|
|
|
if (!file_exists($templateFullPath)) { |
57
|
|
|
$message = sprintf( |
58
|
|
|
'%s template file not found! %s needs a main template file at: %s', |
59
|
|
|
$templateFileBasename, |
60
|
|
|
$widgetNameStr . '_' . $widgetActionStr, |
61
|
|
|
$templateFullPath |
62
|
|
|
); |
63
|
|
|
throw new InvalidArgumentException($message); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function run() : string |
68
|
|
|
{ |
69
|
|
|
return $this->twig->render($this->templateFile, $this->widgetData); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |