コード例 #1
0
 /**
  * @Route("/player/ajax/get", name="team_player_ajax_get")
  */
 public function ajaxGetAction(Request $request)
 {
     if ($request->isXmlHttpRequest()) {
         try {
             $user = $this->getUser();
             $em = $this->getDoctrine()->getManager();
             $player = $em->getRepository('TeamBundle:Player')->findOneBy(array('id' => $request->get('id')));
             if ($user->getTeam() == $player->getTeam()) {
                 $normalizer = new ObjectNormalizer();
                 $normalizer->setCircularReferenceHandler(function ($object) {
                     return $object->getId();
                 });
                 $serializer = new Serializer(array($normalizer));
                 $player = $serializer->normalize($player);
                 $response = new Response(json_encode(array('status' => 'ok', 'player' => $player)));
             } else {
                 $response = new Response(json_encode(array('status' => 'ko', 'message' => 'Vous n\'avez pas la permission d\'éditer ce joueur', 'debug' => 'Utilisateur connecté != manager de l\'équipe du joueur')));
             }
         } catch (\Exception $e) {
             $response = new Response(json_encode(array('status' => 'ko', 'message' => 'Une erreur inconnue s\'est produite', 'debug' => $e->getMessage())));
         }
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     }
     $response = new Response(json_encode(array('status' => 'ko', 'message' => 'Accès non autorisé', 'debug' => 'Bad request')));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
コード例 #2
0
 /**
  * Constructor
  */
 public function __construct()
 {
     $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
     $encoder = new JsonEncoder();
     $normalizer = new ObjectNormalizer($classMetadataFactory);
     $normalizer->setCircularReferenceHandler(function (ClassMetadata $object) {
         return $object->getName();
     });
     $this->serializer = new Serializer([$normalizer], [$encoder]);
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = array())
 {
     $encoder = new JsonEncoder();
     $normalizer = new ObjectNormalizer();
     $normalizer->setCircularReferenceHandler(function ($object) {
         return $object->getName();
     });
     $serializer = new Serializer(array($normalizer), array($encoder));
     return $serializer->normalize($object);
 }
コード例 #4
0
 public function testCircularReferenceHandler()
 {
     $serializer = new Serializer(array($this->normalizer));
     $this->normalizer->setSerializer($serializer);
     $this->normalizer->setCircularReferenceHandler(function ($obj) {
         return get_class($obj);
     });
     $obj = new CircularReferenceDummy();
     $expected = array('me' => 'Symfony\\Component\\Serializer\\Tests\\Fixtures\\CircularReferenceDummy');
     $this->assertEquals($expected, $this->normalizer->normalize($obj));
 }
コード例 #5
0
 private function getGalleryItems()
 {
     $encoders = array(new XmlEncoder(), new JsonEncoder());
     $normalizer = new ObjectNormalizer();
     $normalizer->setCircularReferenceHandler(function ($object) {
         return $object->getId();
     });
     $serializer = new Serializer(array($normalizer), $encoders);
     $em = $this->getDoctrine()->getManager();
     $galleryItems = $em->getRepository('BackendAdminBundle:GalleryItem')->findAll();
     return $galleryItems = $serializer->serialize($galleryItems, 'json');
 }
コード例 #6
0
 /**
  * @Route("/randonnee/{cleRando}")
  * @Template()
  */
 public function viewAction($cleRando, Request $request)
 {
     //if ($request->isXMLHttpRequest()) {
     $randonnee = $this->getDoctrine()->getRepository('AdherentsBundle:Randonnees')->find($cleRando);
     if (!$randonnee) {
         throw $this->createNotFoundException('Pas de randonnee pour la cle :' . $cleRando);
     }
     //see http://symfony.com/doc/current/components/serializer.html
     //$encoders = array(new XmlEncoder(), new JsonEncoder());
     $encoders = array(new JsonEncoder());
     $normalizer = new ObjectNormalizer();
     //http://symfony.com/doc/current/components/serializer.html see circulable references
     $normalizer->setCircularReferenceHandler(function ($object) {
         return $object->getCle();
     });
     $normalizers = array($normalizer);
     $serializer = new Serializer($normalizers, $encoders);
     $json_datas = $serializer->serialize($randonnee, 'json');
     return new Response($json_datas);
     //}
     //return new Response('This is not ajax!', 400);
 }
コード例 #7
0
 /**
  * @Route("/ajax/bookmark/create", name="ajax_create_bookmark")
  */
 public function ajaxCreateAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $repository = $em->getRepository('AppBundle:Bookmark');
     $tagRepository = $em->getRepository('AppBundle:Tag');
     $openGraph = $this->container->get('open_graph');
     $url = $request->request->get('url');
     $tags = $request->request->get('tags');
     $user = $this->get('security.context')->getToken()->getUser();
     if (!$url) {
         $data = array('error' => 'invalid_url', 'errorMsg' => 'URL is required');
         $json = \json_encode($data);
         $response = new Response($json, 404);
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     }
     $tags = explode(',', $tags);
     try {
         $data = $openGraph->read($url);
     } catch (RequestException $e) {
         $context = $e->getHandlerContext();
         $data = array('error' => 'invalid_url');
         if (isset($context['error'])) {
             $data['errorMsg'] = $context['error'];
         }
         $json = \json_encode($data);
         $response = new Response($json, 404);
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } catch (ConnectException $e) {
         $data = array('error' => 'invalid_page', 'exception' => $e->getMessage());
         $json = \json_encode($data);
         $response = new Response($json, 404);
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } catch (\Exception $e) {
         $data = array('error' => 'exception', 'exception' => $e->getMessage());
         $json = \json_encode($data);
         $response = new Response($json, 500);
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     }
     $title = $data['title'];
     $description = $data['description'];
     if (isset($data['og:title']) && $data['og:title']) {
         $title = $data['og:title'];
     }
     if (isset($data['og:description']) && $data['og:description']) {
         $description = $data['og:description'];
     }
     $bookmark = new Bookmark();
     $bookmark->setUrl($url);
     $bookmark->setTitle($title);
     $bookmark->setUser($user);
     $bookmark->setDescription($description);
     if (isset($data['og:image'])) {
         $bookmark->setImage($data['og:image']);
     }
     $bookmarkTags = array();
     foreach ($tags as $tagName) {
         $tag = new Tag();
         $tag->setName($tagName);
         $tag->setUser($user);
         $em->persist($tag);
         $bookmarkTags[] = $tag;
     }
     $bookmark->setTags($bookmarkTags);
     $em->persist($bookmark);
     $em->flush();
     $normalizer = new ObjectNormalizer();
     $normalizer->setCircularReferenceHandler(function ($object) {
         return $object->getId();
     });
     $json = \json_encode($bookmark);
     $response = new Response($json);
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
コード例 #8
0
 /**
  * @Route("/news/ajax/get", name="admin_news_ajax_get")
  */
 public function ajaxGetAction(Request $request)
 {
     if ($request->isXmlHttpRequest()) {
         try {
             $em = $this->getDoctrine()->getManager();
             $news = $em->getRepository('MainBundle:News')->findOneBy(array('id' => $request->get('id')));
             $normalizer = new ObjectNormalizer();
             $normalizer->setCircularReferenceHandler(function ($object) {
                 return $object->getId();
             });
             $serializer = new Serializer(array($normalizer));
             $news = $serializer->normalize($news);
             $response = new Response(json_encode(array('status' => 'ok', 'news' => $news)));
         } catch (\Exception $e) {
             $response = new Response(json_encode(array('status' => 'ko', 'message' => 'Une erreur inconnue s\'est produite', 'debug' => $e->getMessage())));
         }
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     }
     $response = new Response(json_encode(array('status' => 'ko', 'message' => 'Accès refusé', 'debug' => 'Bad request')));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
コード例 #9
0
 public function configure(ObjectNormalizer $normalizer)
 {
     $normalizer->setCircularReferenceHandler(function ($object) {
         return $object->getId();
     });
 }