. */ declare(strict_types=1); namespace App\Migration; use App\Entity\UserSystem\PermissionData; use App\Security\Interfaces\HasPermissionsInterface; use App\Services\UserSystem\PermissionPresetsHelper; use Symfony\Component\DependencyInjection\ContainerInterface; trait WithPermPresetsTrait { private ?ContainerInterface $container = null; private ?PermissionPresetsHelper $permission_presets_helper = null; private function getJSONPermDataFromPreset(string $preset): string { if ($this->permission_presets_helper === null) { throw new \RuntimeException('PermissionPresetsHelper not set! There seems to be some issue with the dependency injection!'); } //Create a virtual user on which we can apply the preset $user = new class implements HasPermissionsInterface { public PermissionData $perm_data; public function __construct() { $this->perm_data = new PermissionData(); } public function getPermissions(): PermissionData { return $this->perm_data; } }; //Apply the preset to the virtual user $this->permission_presets_helper->applyPreset($user, $preset); //And return the json data return json_encode($user->getPermissions()); } public function setContainer(ContainerInterface $container = null): void { if ($container !== null) { $this->container = $container; $this->permission_presets_helper = $container->get(PermissionPresetsHelper::class); } } }