本文整理汇总了PHP中FOS\RestBundle\View\View::getContext方法的典型用法代码示例。如果您正苦于以下问题:PHP View::getContext方法的具体用法?PHP View::getContext怎么用?PHP View::getContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FOS\RestBundle\View\View
的用法示例。
在下文中一共展示了View::getContext方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getSerializationContext
/**
* Gets or creates a JMS\Serializer\SerializationContext and initializes it with
* the view exclusion strategies, groups & versions if a new context is created.
*
* @param View $view
*
* @return Context
*/
protected function getSerializationContext(View $view)
{
$context = $view->getContext();
$groups = $context->getGroups();
if (empty($groups) && $this->exclusionStrategyGroups) {
$context->addGroups($this->exclusionStrategyGroups);
}
if (null === $context->getVersion() && $this->exclusionStrategyVersion) {
$context->setVersion($this->exclusionStrategyVersion);
}
if (null === $context->getSerializeNull() && null !== $this->serializeNullStrategy) {
$context->setSerializeNull($this->serializeNullStrategy);
}
return $context;
}
示例2: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
return false;
}
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
$customViewDefined = true;
if (!$view instanceof View) {
if (!$configuration && !$this->forceView) {
return;
}
$view = new View($view);
$customViewDefined = false;
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
$context = $view->getContext();
if ($configuration->getSerializerGroups() && !$customViewDefined) {
$context->addGroups($configuration->getSerializerGroups());
}
if ($configuration->getSerializerEnableMaxDepthChecks()) {
$context->setMaxDepth(0);
}
$populateDefaultVars = $configuration->isPopulateDefaultVars();
} else {
$populateDefaultVars = true;
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars && $populateDefaultVars) {
$vars = $request->attributes->get('_template_default_vars');
}
if ($this->viewHandler->isFormatTemplating($view->getFormat()) && !$view->getRoute() && !$view->getLocation()) {
if (!empty($vars)) {
$parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = null !== $configuration && $configuration->getTemplate() ? $configuration->getTemplate() : $request->attributes->get('_template');
if ($template && !$view->getTemplate()) {
if ($template instanceof TemplateReferenceInterface) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $this->viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例3: testCreateResponseWithFormErrorsAndSerializationGroups
/**
* @dataProvider exceptionWrapperSerializeResponseContentProvider
*
* @param string $format
*/
public function testCreateResponseWithFormErrorsAndSerializationGroups($format)
{
// BC hack for Symfony 2.7 where FormType's didn't yet get configured via the FQN
$formType = method_exists('Symfony\\Component\\Form\\AbstractType', 'getBlockPrefix') ? 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType' : 'text';
$form = Forms::createFormFactory()->createBuilder()->add('name', $formType)->add('description', $formType)->getForm();
$form->get('name')->addError(new FormError('Invalid name'));
$exceptionWrapper = new ExceptionWrapper(['status_code' => 400, 'message' => 'Validation Failed', 'errors' => $form]);
$view = new View($exceptionWrapper);
$view->getContext()->addGroups(array('Custom'));
$translatorMock = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface', ['trans', 'transChoice', 'setLocale', 'getLocale']);
$translatorMock->expects($this->any())->method('trans')->will($this->returnArgument(0));
$viewHandler = $this->createViewHandler([]);
$response = $viewHandler->createResponse($view, new Request(), $format);
$viewHandler = $this->createViewHandler([]);
$view2 = new View($exceptionWrapper);
$response2 = $viewHandler->createResponse($view2, new Request(), $format);
$this->assertEquals($response->getContent(), $response2->getContent());
}
示例4: addJmsGroupsIntoView
/**
* @param string $route
* @param View $view
*/
private function addJmsGroupsIntoView(string $route, View $view)
{
$view->getContext()->setGroups(array_merge(['Default', $route]));
}