public function onBootstrap(MvcEvent $event) { Json::$useBuiltinEncoderDecoder = true; $eventManager = $event->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); }
public function serviceAction() { $serviceName = strtolower($this->params()->fromRoute('service')); $format = strtolower($this->params()->fromRoute('format')); $runChecks = $this->params()->fromQuery('run', true); if (in_array($format, array(self::XML, self::RICH))) { throw new Exception\UnexpectedValueException(sprintf('Format "%s" is currently not supported', $format)); } try { $service = new Model\Service($serviceName); } catch (Model\Exception\BadModelCallException $e) { return $this->notFoundAction(); } $this->_setNewRelic($service); $content = $this->_getContentView($service, $format, $runChecks); if ($format === self::JSON) { $data = $content->getVariables(); unset($data->service); $view = new ViewModel\JsonModel($data); Json\Json::$useBuiltinEncoderDecoder = true; } else { $view = new ViewModel\ViewModel(); $view->addChild($content, 'content'); $view->service = $service; $view->format = $format; } return $view; }
/** * Get a list of groups that are nearest to the given coordinates. * * Coordinates are given via the parameters <var>lat</var and <var>lon</var>, * the maximum distance from the current location is given via <var>distance</var> * and the maximum number of entries is given via the parameter <var>max</var> * * This method will then return the maximum number of entries within the given * range. If less than the maximum number of entries is found within the distance * only that number of entries will be returned. When no distance is given or the * distance is given as "0" the maximum number of entries will be retrieved. * * @return mixed|\Zend\View\Model\ModelInterface * @throws \UnexpectedValueException */ public function nextGroupsAction() { $adapter = $this->getAdapter(); $response = $this->getResponse(); $viewModel = $this->getViewModel(); Json::$useBuiltinEncoderDecoder = true; // Get Latitude, Longitude, distance and/or number of groups to retrieve $latitude = $this->params()->fromQuery('latitude'); $longitude = $this->params()->fromQuery('longitude'); $distance = $this->params()->fromQuery('distance', null); $number = $this->params()->fromQuery('count', null); //$this->sorter = $this->getServiceManager('Phpug\Sorter\Usergroup\Distance'); $groups = $this->findGroupsWithinRangeAndDistance($latitude, $longitude, $distance, $number); $return = array('currentLocation' => array('latitude' => $latitude, 'longitude' => $longitude), 'groups' => array()); // $hydrator = $this->getServiceManager('Phpug\Hydrator\Usergroup'); foreach ($groups as $group) { $grp = array('name' => $group[0]->getName(), 'latitude' => $group[0]->getLatitude(), 'longitude' => $group[0]->getLongitude(), 'shortname' => $group[0]->getShortname(), 'distance' => $group['distance'], 'icalendar_url' => $group[0]->getIcalendar_url(), 'url' => $group[0]->getUrl(), 'contacts' => array(), 'uri' => ''); foreach ($group[0]->getContacts() as $contact) { $grp['contacts'][] = array('service' => $contact->getServiceName(), 'name' => $contact->getName(), 'uri' => $contact->getUrl()); } $return['groups'][] = $grp; } $viewModel->setVariable('groups', $return); return $viewModel; }
public function indexAction() { // 获取到请求的JSON字符串 $content = $this->request->getContent(); $this->writeConfig(); $this->errorService = $this->getServiceLocator()->get("error_service"); $this->_log = $this->getServiceLocator()->get('DhErrorLogging\\Logger'); if (empty($content)) { // 判断请求数据是否为空,直接返回给客户端,不做进一步的处理 $this->_log->err('接收到的指令为空!'); $returnMsg = $this->_processEmptyMsg(); $this->response->setContent($returnMsg); return $this->response; } try { Json::$useBuiltinEncoderDecoder = true; $ac = Json::decode($content, Json::TYPE_ARRAY); $this->_log->debug('action is: ' . var_export($ac, true)); } catch (\Exception $e) { $this->_log->err('Json解码失败!'); $codeMsg = $this->_errorCodeMeg('00000003'); $returnMsg = urldecode(Json::encode($codeMsg)); $this->response->setContent($returnMsg); return $this->response; } // 接口指令处理 if (array_key_exists($ac['action'], $this->_actionToService)) { $service = $this->getServiceLocator()->get($this->_actionToService[$ac['action']]); // 获取可用的openstack节点组 $osGroup = $this->getOSGroup(); // 对调用具体指令时捕获异常 try { $checkAuth = $service->checkSignature(json_decode($content, true)); if (!$checkAuth) { echo json_encode(array("code" => -1, "msg" => "authentication failed")); exit; } $service->setReader(); $service->getToken(); $result = $service->{$ac}['action']($ac['data']); $returnData = urldecode($result); $this->_log->debug($this->_actionToService[$ac['action']] . ' :' . $returnData . PHP_EOL); $this->response->setContent($returnData); return $this->response; } catch (\Exception $e) { file_put_contents('error_index.log', $e->getMessage() . $e->getCode() . PHP_EOL, FILE_APPEND); $this->_log->err('调用服务出错:' . $e->getMessage() . $e->getCode() . PHP_EOL); $codeMsg = $this->_errorCodeMeg('00000004'); $returnMsg = urldecode(Json::encode($codeMsg)); $this->response->setContent($returnMsg); return $this->response; } } else { $codeMsg = $this->_errorCodeMeg('00000002'); $returnMsg = urldecode(Json::encode($codeMsg)); $this->response->setContent($returnMsg); return $this->response; } }
public function nextEventAction() { $adapter = $this->getAdapter(); $response = $this->getResponse(); $viewModel = $this->getViewModel(); Json::$useBuiltinEncoderDecoder = true; $id = $this->getEvent()->getRouteMatch()->getParam('id'); if (!$id) { return $response->setContent($adapter->serialize(null)); } $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); $result = $em->getRepository('Phpug\\Entity\\Usergroup')->findBy(array('shortname' => $id)); if (!$result) { throw new \UnexpectedValueException(sprintf('Fehler!!')); } if (1 < count($result)) { throw new \UnexpectedValueException(sprintf('Fehler!!')); } $uri = $result[0]->icalendar_url; $data = @file_get_contents($uri); if (!$data) { throw new \UnexpectedValueException(sprintf('Could not read data')); } $now = new \DateTime(); $then = (new \DateTime())->add(new \DateInterval('P1Y')); $ical = VObject\Reader::read($data); $ical->expand($now, $then); $nextEvent = null; if (!isset($ical->VEVENT)) { throw new \UnexpectedValueException(sprintf('No Event available')); } foreach ($ical->VEVENT as $event) { if (null === $nextEvent || $nextEvent->DTSTART->getDateTime() > $event->DTSTART->getDateTime()) { $nextEvent = $event; } } if (!$nextEvent) { throw new \UnexpectedValueException(sprintf('No Event defined')); } $content = array('start' => $nextEvent->DTSTART->getDateTime()->format(\DateTime::RFC2822), 'end' => $nextEvent->DTEND->getDateTime()->format(\DateTime::RFC2822), 'location' => '', 'summary' => 'Next event', 'url' => '', 'description' => ''); if (isset($nextEvent->LOCATION)) { $content['location'] = $nextEvent->LOCATION->getValue(); } if (isset($nextEvent->SUMMARY)) { $content['summary'] = $nextEvent->SUMMARY->getValue(); } if (isset($nextEvent->URL)) { $content['url'] = $nextEvent->URL->getValue(); } if (isset($nextEvent->DESCRIPTION)) { $content['description'] = $nextEvent->DESCRIPTION->getValue(); } return $viewModel->setVariables($content); }
/** * Parse and return JSON content * * @param string $content * @return mixed * @throws JsonException */ public function parseJsonContent($content) { $useBuiltIn = Json::$useBuiltinEncoderDecoder; Json::$useBuiltinEncoderDecoder = true; try { $parsed = Json::decode($content, true); Json::$useBuiltinEncoderDecoder = $useBuiltIn; } catch (JsonException $e) { Json::$useBuiltinEncoderDecoder = $useBuiltIn; throw $e; } return $parsed; }
/** * @group ZF-8663 */ public function testBuiltinJSONEncoderWillProperlyEncodeSolidusInStringValues() { $source = "</foo><foo>bar</foo>"; $target = '"<\\/foo><foo>bar<\\/foo>"'; // first test ext/json Json\Json::$useBuiltinEncoderDecoder = true; $this->assertEquals($target, Json\Json::encode($source)); }
/** * Client constructor. * * @param $username * @param $apiKey */ private function __construct($username, $apiKey) { Json::$useBuiltinEncoderDecoder = true; $this->defaultOptions = ['auth' => [$username, $apiKey], 'headers' => ['Accepts' => 'application/json']]; }
/** * @group ZF-7586 */ public function testWillDecodeStructureWithEmptyKeyToObjectProperly() { Json\Json::$useBuiltinEncoderDecoder = true; $json = '{"":"test"}'; $object = Json\Json::decode($json, Json\Json::TYPE_OBJECT); $this->assertTrue(isset($object->_empty_)); $this->assertEquals('test', $object->_empty_); }
/** * @group ZF-9521 */ public function testWillEncodeArrayOfObjectsEachWithToJsonMethod() { $array = array('one' => new ToJsonClass()); $expected = '{"one":{"__className":"ZendTest\\\\Json\\\\ToJSONClass","firstName":"John","lastName":"Doe","email":"*****@*****.**"}}'; Json\Json::$useBuiltinEncoderDecoder = true; $json = Json\Encoder::encode($array); $this->assertEquals($expected, $json); }