Repository navigation
Expand file tree
/
Copy pathDefaultController.php
More file actions
135 lines (115 loc) · 3.87 KB
/
Copy pathDefaultController.php
File metadata and controls
135 lines (115 loc) · 3.87 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
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
<?php
namespace SyncEngine\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;
use SyncEngine\Service\Locator\Blueprints;
use SyncEngine\Service\Locator\Codecs;
use SyncEngine\Service\Locator\Columns;
use SyncEngine\Service\Locator\Tasks;
use SyncEngine\Service\Locator\Webservices;
use SyncEngine\Service\ModelDependencyManager;
use SyncEngine\Service\ModelExporter;
use SyncEngine\Service\ModelImporter;
use SyncEngine\Service\ModelNormalizer;
use SyncEngine\Service\SerializationSanitizer;
class DefaultController extends AbstractController
{
private static ContainerInterface $_container;
protected string $defaultDomain = 'messages';
public static function getContainer(): ?ContainerInterface
{
return self::$_container ?? null;
}
public function getParameter( string $name, false|string $prefix = 'syncengine' ): array|bool|float|int|null|string|\UnitEnum
{
if ( $prefix ) {
$name = $prefix . '.' . $name;
}
return parent::getParameter( $name );
}
public static function get( string $name ): mixed
{
return self::$_container->get( $name );
}
public static function getSubscribedServices(): array
{
return array_merge(
parent::getSubscribedServices(),
[
'logger' => '?' . LoggerInterface::class,
'translator' => '?' . TranslatorInterface::class,
'entitymanager' => '?' . EntityManagerInterface::class,
Columns::class => '?' . Columns::class,
Codecs::class => '?' . Codecs::class,
Tasks::class => '?' . Tasks::class,
Webservices::class => '?' . Webservices::class,
Blueprints::class => '?' . Blueprints::class,
ModelNormalizer::class => '?' . ModelNormalizer::class,
ModelDependencyManager::class => '?' . ModelDependencyManager::class,
ModelExporter::class => '?' . ModelExporter::class,
ModelImporter::class => '?' . ModelImporter::class,
]
);
}
#[Required]
public function setContainer( ContainerInterface $container ): ?ContainerInterface
{
$previous = parent::setContainer( $container );
if ( ! isset( self::$_container ) ) {
self::$_container = $this->container;
}
return $previous;
}
/**
* @return EntityManagerInterface|null
*/
public static function getEntityManager(): ?EntityManagerInterface
{
return self::get( 'entitymanager' );
}
public function isDebug()
{
return true === $this->getParameter( 'kernel.debug', false );
}
public function json( mixed $data, int $status = 200, array $headers = [], array $context = [], $sanitizeOptions = [] ): JsonResponse
{
$html = $data['html'] ?? null;
if ( $html ) {
unset( $data['html'] );
}
$normalized = $this->container->get( ModelNormalizer::class )->normalize( $data );
$sanitized = ( new SerializationSanitizer() )->sanitize( $normalized, $sanitizeOptions );
if ( $html ) {
$sanitized['html'] = $html;
}
return parent::json( $sanitized, $status, $headers, $context );
}
protected function trans( ?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null ): string
{
return $this->container->get( 'translator' )->trans(
$id,
$parameters,
$domain ?? $this->defaultDomain,
$locale
);
}
protected function logException( \Exception $e, string $message = 'An error occurred' ): void
{
$this->get( 'logger' )->error(
$message,
[
'message' => $e->getMessage(),
'type' => get_class( $e ),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
]
);
}
}