setPrivate example


      }
    }

    // Note that we intentionally do not use:     // - \Drupal\Core\Cache\CacheableResponse because caching it on the server     // side is wasteful, hence there is no need for cacheability metadata.     // - \Drupal\Core\Render\HtmlResponse because there is no need for     // attachments nor cacheability metadata.     return (new JsonResponse($response, 200))
      // Do not allow any intermediary to cache the response, only the end user.       ->setPrivate()
      // Allow the end user to cache it for up to 5 minutes.       ->setMaxAge(300);
  }

  /** * Additional access check for ::isMediaImage(). * * This grants access if media embed filter is enabled on the filter format * and user has access to view the media entity. * * Note that access to the filter format is not checked here because the route * is configured to check entity access to the filter format. * * @param \Drupal\editor\Entity\Editor $editor * The text editor. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. * * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * Thrown when no media UUID is provided. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * Thrown when no media with the provided UUID exists. */

  public function subtreesAjax() {
    [$subtrees] = toolbar_get_rendered_subtrees();
    $response = new AjaxResponse();
    $response->addCommand(new SetSubtreesCommand($subtrees));

    // The Expires HTTP header is the heart of the client-side HTTP caching. The     // additional server-side page cache only takes effect when the client     // accesses the callback URL again (e.g., after clearing the browser cache     // or when force-reloading a Drupal page).     $max_age = 365 * 24 * 60 * 60;
    $response->setPrivate();
    $response->setMaxAge($max_age);

    $expires = new \DateTime();
    $expires->setTimestamp(REQUEST_TIME + $max_age);
    $response->setExpires($expires);

    return $response;
  }

  /** * Checks access for the subtree controller. * * @param string $hash * The hash of the toolbar subtrees. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */
$this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
                } else {
                    $this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
                }
            }
        }

        if (isset($options['public'])) {
            if ($options['public']) {
                $this->setPublic();
            } else {
                $this->setPrivate();
            }
        }

        if (isset($options['private'])) {
            if ($options['private']) {
                $this->setPrivate();
            } else {
                $this->setPublic();
            }
        }

        
if (!$hasVary && $cache->vary) {
                $response->setVary($cache->vary, false);
            }
        }

        foreach ($attributes as $cache) {
            if (true === $cache->public) {
                $response->setPublic();
            }

            if (false === $cache->public) {
                $response->setPrivate();
            }
        }
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10],
            KernelEvents::RESPONSE => ['onKernelResponse', -10],
        ];
    }

    
if (!$hasVary && $cache->vary) {
                $response->setVary($cache->vary, false);
            }
        }

        foreach ($attributes as $cache) {
            if (true === $cache->public) {
                $response->setPublic();
            }

            if (false === $cache->public) {
                $response->setPrivate();
            }
        }
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10],
            KernelEvents::RESPONSE => ['onKernelResponse', -10],
        ];
    }

    
$container->set('session_factory', $sessionFactory);

        $listener = new SessionListener($container);
        $kernel = $this->createMock(HttpKernelInterface::class);

        $request = new Request();
        $listener->onKernelRequest(new RequestEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST));

        $request->getSession();

        $response = new Response();
        $response->setPrivate();
        $expiresHeader = gmdate('D, d M Y H:i:s', time() + 600).' GMT';
        $response->setMaxAge(600);
        $listener->onKernelResponse(new ResponseEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST, $response));

        $this->assertTrue($response->headers->has('expires'));
        $this->assertSame($expiresHeader$response->headers->get('expires'));
        $this->assertFalse($response->headers->has('max-age'));
        $this->assertSame('600', $response->headers->getCacheControlDirective('max-age'));
        $this->assertFalse($response->headers->hasCacheControlDirective('public'));
        $this->assertTrue($response->headers->hasCacheControlDirective('private'));
        $this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate'));
        
$this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
                } else {
                    $this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
                }
            }
        }

        if (isset($options['public'])) {
            if ($options['public']) {
                $this->setPublic();
            } else {
                $this->setPrivate();
            }
        }

        if (isset($options['private'])) {
            if ($options['private']) {
                $this->setPrivate();
            } else {
                $this->setPublic();
            }
        }

        
#[Package('core')] class HealthCheckController
{
    /** * This is a simple health check to check that the basic application runs. * Use it in Docker HEALTHCHECK with curl --silent --fail http://localhost/api/_info/health-check */
    #[Route(path: '/api/_info/health-check', name: 'api.info.health.check', defaults: ['auth_required' => false], methods: ['GET'])]     public function check(): Response
    {
        $response = new Response('');
        $response->setPrivate();

        return $response;
    }
}
$response = new Response($this->twig->render($template$context)$statusCode);

        if ($maxAge) {
            $response->setMaxAge($maxAge);
        }

        if (null !== $sharedAge) {
            $response->setSharedMaxAge($sharedAge);
        }

        if ($private) {
            $response->setPrivate();
        } elseif (false === $private || (null === $private && (null !== $maxAge || null !== $sharedAge))) {
            $response->setPublic();
        }

        return $response;
    }

    /** * @param int $statusCode The HTTP status code (200 "OK" by default) */
    public function __invoke(string $template, int $maxAge = null, int $sharedAge = null, bool $private = null, array $context = [], int $statusCode = 200): Response
    {
$this->expectException(\RuntimeException::class);
        $downloadResponseGenerator->getResponse($media$this->salesChannelContext);
    }

    public function testThrowsExceptionWithoutDetachableResource(): void
    {
        $this->privateFilesystem->method('temporaryUrl')->willThrowException(new UnableToGenerateTemporaryUrl('foo', 'baa'));

        $media = new MediaEntity();
        $media->setId(Uuid::randomHex());
        $media->setFileName('foobar');
        $media->setPrivate(true);

        $this->expectException(MediaException::class);
        $this->expectExceptionMessage('The file "foobar." does not exist');
        $this->downloadResponseGenerator->getResponse($media$this->salesChannelContext);
    }

    /** * @dataProvider filesystemProvider */
    public function testGetResponse(bool $private, string $privateType, string $publicType, Response $expectedResponse, ?string $strategy = null): void
    {
        

        }

        if ($session instanceof Session ? 0 === $session->getUsageIndex() : !$session->isStarted()) {
            return;
        }

        if ($autoCacheControl) {
            $maxAge = $response->headers->hasCacheControlDirective('public') ? 0 : (int) $response->getMaxAge();
            $response
                ->setExpires(new \DateTimeImmutable('+'.$maxAge.' seconds'))
                ->setPrivate()
                ->setMaxAge($maxAge)
                ->headers->addCacheControlDirective('must-revalidate');
        }

        if (!$event->getRequest()->attributes->get('_stateless', false)) {
            return;
        }

        if ($this->debug) {
            throw new UnexpectedSessionUsageException('Session was used while the request was declared stateless.');
        }

        
if ($media = $this->entityRepository->loadEntityByUuid('media', $uuid)) {
      $headers['Drupal-Media-Label'] = $this->entityRepository->getTranslationFromContext($media)->label();
    }

    // Note that we intentionally do not use:     // - \Drupal\Core\Cache\CacheableResponse because caching it on the server     // side is wasteful, hence there is no need for cacheability metadata.     // - \Drupal\Core\Render\HtmlResponse because there is no need for     // attachments nor cacheability metadata.     return (new Response($html, 200, $headers))
      // Do not allow any intermediary to cache the response, only the end user.       ->setPrivate()
      // Allow the end user to cache it for up to 5 minutes.       ->setMaxAge(300);
  }

  /** * Checks access based on media_embed filter status on the text format. * * @param \Drupal\filter\FilterFormatInterface $filter_format * The text format for which to check access. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */

        }

        if ($session instanceof Session ? 0 === $session->getUsageIndex() : !$session->isStarted()) {
            return;
        }

        if ($autoCacheControl) {
            $maxAge = $response->headers->hasCacheControlDirective('public') ? 0 : (int) $response->getMaxAge();
            $response
                ->setExpires(new \DateTimeImmutable('+'.$maxAge.' seconds'))
                ->setPrivate()
                ->setMaxAge($maxAge)
                ->headers->addCacheControlDirective('must-revalidate');
        }

        if (!$event->getRequest()->attributes->get('_stateless', false)) {
            return;
        }

        if ($this->debug) {
            throw new UnexpectedSessionUsageException('Session was used while the request was declared stateless.');
        }

        


    public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
    {
        $cacheStrategy = new ResponseCacheStrategy();

        $mainResponse = new Response();
        $mainResponse->setSharedMaxAge(3600); // public, cacheable
        // The embedded response might for example contain per-user data that remains valid for 60 seconds         $embeddedResponse = new Response();
        $embeddedResponse->setPrivate();
        $embeddedResponse->setMaxAge(60); // this would implicitly set "private" as well, but let's be explicit
        $cacheStrategy->add($embeddedResponse);
        $cacheStrategy->update($mainResponse);

        $this->assertTrue($mainResponse->headers->hasCacheControlDirective('private'));
        $this->assertFalse($mainResponse->headers->hasCacheControlDirective('public'));
    }

    public function testEmbeddingPublicResponseDoesNotMakeMainResponsePublic()
    {
        
if ($this->reverseProxyEnabled) {
            return;
        }

        $response = $event->getResponse();

        $noStore = $response->headers->getCacheControlDirective('no-store');

        // We don't want that the client will cache the website, if no reverse proxy is configured         $response->headers->remove('cache-control');
        $response->headers->remove(self::INVALIDATION_STATES_HEADER);
        $response->setPrivate();

        if ($noStore) {
            $response->headers->addCacheControlDirective('no-store');
        } else {
            $response->headers->addCacheControlDirective('no-cache');
        }
    }

    /** * @param list<string> $cacheStates * @param list<string> $states */
Home | Imprint | This part of the site doesn't use cookies.