/**
  * @dataProvider deserializeReturnTypeProvider
  */
 public function testDeserializeReturnType($json, $expected)
 {
     $serializer = new SettingCollectionJsonSerializer();
     $output = $serializer->deserialize($json);
     // Assert
     $this->assertEquals($expected, get_class($output));
 }
Пример #2
0
 /**
  * Import a Json file (.rzt) containing setting and setting group.
  *
  * @param string $serializedData
  * @param EntityManager $em
  *
  * @return bool
  */
 public static function importJsonFile($serializedData, EntityManager $em)
 {
     $serializer = new SettingCollectionJsonSerializer();
     $settingGroups = $serializer->deserialize($serializedData);
     $groupsNames = $em->getRepository('RZ\\Roadiz\\Core\\Entities\\SettingGroup')->findAllNames();
     $settingsNames = $em->getRepository('RZ\\Roadiz\\Core\\Entities\\Setting')->findAllNames();
     $newSettings = [];
     foreach ($settingGroups as $index => $settingGroup) {
         /*
          * Loop over settings to set their group
          * and move them to a temp collection
          */
         foreach ($settingGroup->getSettings() as $setting) {
             if (!in_array($setting->getName(), $settingsNames)) {
                 // do nothing
             } else {
                 $setting = $em->getRepository('RZ\\Roadiz\\Core\\Entities\\Setting')->findOneByName($setting->getName());
             }
             /*
              * Set array with setting and the deserialize setting's group
              * to don't take the existing setting's group
              */
             $newSettings[] = [$setting, $settingGroup];
             $settingGroup->getSettings()->clear();
         }
     }
     foreach ($newSettings as $settingArray) {
         $settingGroup = $settingArray[1];
         $setting = $settingArray[0];
         /*
          * Persist or not group
          */
         if (null !== $settingGroup) {
             if (!in_array($settingGroup->getName(), $groupsNames)) {
                 $em->persist($settingGroup);
             } else {
                 $settingGroup = $em->getRepository('RZ\\Roadiz\\Core\\Entities\\SettingGroup')->findOneByName($settingGroup->getName());
             }
         }
         /*
          * Add group to setting and persist if don't exist
          */
         $setting->setSettingGroup($settingGroup);
         if ($setting->getId() === null) {
             $em->persist($setting);
         }
     }
     $em->flush();
     // Clear result cache
     $cacheDriver = $em->getConfiguration()->getResultCacheImpl();
     if ($cacheDriver !== null) {
         $cacheDriver->deleteAll();
     }
     return true;
 }
 /**
  * Export all settings in a Json file (.rzt).
  *
  * @param Symfony\Component\HttpFoundation\Request $request
  *
  * @return Symfony\Component\HttpFoundation\Response
  */
 public function exportAllAction(Request $request)
 {
     $this->validateAccessForRole('ROLE_ACCESS_SETTINGS');
     $groups = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\SettingGroup')->findAll();
     $lonelySettings = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Setting')->findBy(['settingGroup' => null]);
     $tmpGroup = new SettingGroup();
     $tmpGroup->setName('__default__');
     $tmpGroup->addSettings($lonelySettings);
     $groups[] = $tmpGroup;
     $serializer = new SettingCollectionJsonSerializer();
     $data = $serializer->serialize($groups);
     $response = new Response($data, Response::HTTP_OK, []);
     $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'setting-all-' . date("YmdHis") . '.rzt'));
     // Rezo-Zero Type
     $response->prepare($request);
     return $response;
 }