setCache example

$form_state = new FormState();

    $this->formCacheStore->expects($this->once())
      ->method('setWithExpire')
      ->with($form_build_id$form$this->isType('int'));

    $form_state_data = $form_state->getCacheableArray();
    $this->formStateCacheStore->expects($this->once())
      ->method('setWithExpire')
      ->with($form_build_id$form_state_data$this->isType('int'));

    $this->formCache->setCache($form_build_id$form$form_state);
  }

  /** * @covers ::setCache */
  public function testSetCacheWithoutForm() {
    $form_build_id = 'the_form_build_id';
    $form = NULL;
    $form_state = new FormState();

    $this->formCacheStore->expects($this->never())
      
return Zend_Locale_Data::getCache();
    }

    /** * Sets a cache * * @param Zend_Cache_Core $cache Cache to set * @return void */
    public static function setCache(Zend_Cache_Core $cache)
    {
        Zend_Locale_Data::setCache($cache);
    }

    /** * Returns true when a cache is set * * @return boolean */
    public static function hasCache()
    {
        return Zend_Locale_Data::hasCache();
    }

    
    if (isset($old_form['#action']) && !empty($rebuild_info['copy']['#action'])) {
      $form['#action'] = $old_form['#action'];
    }

    $this->prepareForm($form_id$form$form_state);

    // Caching is normally done in self::processForm(), but what needs to be     // cached is the $form structure before it passes through     // self::doBuildForm(), so we need to do it here.     // @todo For Drupal 8, find a way to avoid this code duplication.     if ($form_state->isCached()) {
      $this->setCache($form['#build_id']$form$form_state);
    }

    // Clear out all group associations as these might be different when     // re-rendering the form.     $form_state->setGroups([]);

    // Return a fully built form that is ready for rendering.     return $this->doBuildForm($form_id$form$form_state);
  }

  /** * {@inheritdoc} */
use Twig\Loader\ArrayLoader;

#[Package('buyers-experience')] class SeoUrlTwigFactory
{
    /** * @param ExtensionInterface[] $twigExtensions */
    public function createTwigEnvironment(SlugifyInterface $slugify, iterable $twigExtensions = []): Environment
    {
        $twig = new TwigEnvironment(new ArrayLoader());
        $twig->setCache(false);
        $twig->enableStrictVariables();
        $twig->addExtension(new SlugifyExtension($slugify));
        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension(new SecurityExtension([]));

        /** @var EscaperExtension $coreExtension */
        $coreExtension = $twig->getExtension(EscaperExtension::class);
        $coreExtension->setEscaper(
            SeoUrlGenerator::ESCAPE_SLUGIFY,
            // Do not remove $_twig, although it is marked as unused. It somehow important             static fn ($_twig$string) => rawurlencode($slugify->slugify($string))
        );


            if (!empty($args)) {
                $opt     = array_shift($args);
                $options = array_merge($opt$options);
            }
        } else if (!is_array($options)) {
            $options = array('content' => $options);
        }

        if (array_key_exists('cache', $options)) {
            self::setCache($options['cache']);
            unset($options['cache']);
        }

        if (isset(self::$_cache)) {
            $id = 'Zend_Translate_' . $this->toString() . '_Options';
            $result = self::$_cache->load($id);
            if ($result) {
                $this->_options = $result;
            }
        }

        
'strict_variables' => false,
            'autoescape' => 'html',
            'cache' => false,
            'auto_reload' => null,
            'optimizations' => -1,
        ]$options);

        $this->debug = (bool) $options['debug'];
        $this->setCharset($options['charset'] ?? 'UTF-8');
        $this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
        $this->strictVariables = (bool) $options['strict_variables'];
        $this->setCache($options['cache']);
        $this->extensionSet = new ExtensionSet();

        $this->addExtension(new CoreExtension());
        $this->addExtension(new EscaperExtension($options['autoescape']));
        $this->addExtension(new OptimizerExtension($options['optimizations']));
    }

    /** * Enables debugging mode. */
    public function enableDebug()
    {
break;
                    case 'cache' :
                        if ($value === null) {
                            parent::$_cache = null;
                        } else {
                            if (!$value instanceof Zend_Cache_Core) {
                                throw new Zend_Date_Exception("Instance of Zend_Cache expected");
                            }

                            parent::$_cache = $value;
                            parent::$_cacheTags = Zend_Date_DateObject::_getTagSupportForCache();
                            Zend_Locale_Data::setCache($value);
                        }
                        break;
                }
                self::$_options[$name] = $value;
            }
            else {
                throw new Zend_Date_Exception("Unknown option: $name = $value");
            }
        }
    }

    
case 'auto':
                $cache = $this->detectCacheProvider();
                break;
            case 'redis':
                $cache = $this->createRedisCacheProvider($options);
                break;
            default:
                $cache = $this->createDefaultProvider($provider);
        }

        if ($cache instanceof CacheProvider) {
            $this->setCache($cache);
        }
    }

    public function setCacheResource(Zend_Cache_Core $cacheResource)
    {
        $cache = new Cache($cacheResource, 'Shopware_Models_' . $this->release->getRevision() . '_', [CacheManager::ITEM_TAG_MODELS]);

        $this->setCache($cache);
    }

    /** * @return Reader */
public function testFormCacheForEntityForms() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    $form_builder = $this->container->get('form_builder');

    $form = $this->entityTypeManager->getFormObject('entity_test_mulrevpub', 'default');
    $form->setEntity(EntityTestMulRevPub::create([]));

    $form_state = new FormState();
    $built_form = $form_builder->buildForm($form$form_state);
    $form_builder->setCache($built_form['#build_id']$built_form$form_state);
  }

  /** * Tests that non-entity forms can be stored in the form cache. */
  public function testFormCacheForRegularForms() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    $form_builder = $this->container->get('form_builder');

    


        if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
            $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
        }

        if (!class_exists($options['adapter'])) {
            Zend_Loader::loadClass($options['adapter']);
        }

        if (array_key_exists('cache', $options)) {
            Zend_Translate_Adapter::setCache($options['cache']);
        }

        $adapter = $options['adapter'];
        unset($options['adapter']);
        $this->_adapter = new $adapter($options);
        if (!$this->_adapter instanceof Zend_Translate_Adapter) {
            throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
        }
    }

    /** * Returns the adapters name and it's options * * @return Zend_Translate_Adapter */

    public function factory($backend$frontendOptions$backendOptions, ShopwareReleaseStruct $release)
    {
        $backendOptions['release'] = $release;

        $backend = $this->createBackend($backend$backendOptions);
        $cacheCore = $this->createCacheCore($frontendOptions);

        $cacheCore->setBackend($backend);

        Zend_Locale_Data::setCache($cacheCore);
        Zend_Db_Table_Abstract::setDefaultMetadataCache($cacheCore);

        return $cacheCore;
    }

    /** * @param string $backend * @param array $backendOptions * * @return Zend_Cache_Backend */
    
->setTitle('message title')
            ->setPriority(NtfyOptions::PRIORITY_URGENT)
            ->setTags(['tag1', 'tag2'])
            ->addTag('tag3')
            ->setDelay($delay)
            ->setActions([['action' => 'view', 'label' => 'View', 'url' => 'https://test.com']])
            ->addAction(['action' => 'http', 'label' => 'Open', 'url' => 'https://test2.com'])
            ->setClick('https://test3.com')
            ->setAttachment('https://filesrv.lan/space.jpg')
            ->setFilename('diskspace.jpg')
            ->setEmail('me@mail.com')
            ->setCache(false)
            ->setFirebase(false)
        ;

        $this->assertSame([
            'message' => 'test message',
            'title' => 'message title',
            'priority' => NtfyOptions::PRIORITY_URGENT,
            'tags' => ['tag1', 'tag2', 'tag3'],
            'delay' => (string) $delay->getTimestamp(),
            'actions' => [
                ['action' => 'view', 'label' => 'View', 'url' => 'https://test.com'],
                [
'#property' => $this->randomMachineName(),
    ];
    $this->formState = new FormState();
    $this->formState->set('example', $this->randomMachineName());
  }

  /** * Tests the form cache with a logged-in user. */
  public function testCacheToken() {
    \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
    \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);

    $cached_form_state = new FormState();
    $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
    $this->assertEquals($this->form['#property']$cached_form['#property']);
    $this->assertNotEmpty($cached_form['#cache_token'], 'Form has a cache token');
    $this->assertEquals($this->formState->get('example')$cached_form_state->get('example'));

    // Test that the form cache isn't loaded when the session/token has changed.     // Change the private key. (We cannot change the session ID because this     // will break the parent site test runner batch.)     \Drupal::state()->set('system.private_key', 'invalid');
    
$request = Request::create('https://localhost/', 'GET');
        $response->prepare($request);

        $this->assertTrue($cookie->isSecure());
    }

    public function testSetCache()
    {
        $response = new Response();
        // ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public']         try {
            $response->setCache(['wrong option' => 'value']);
            $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
        } catch (\Exception $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class$e, '->setCache() throws an InvalidArgumentException if an option is not supported');
            $this->assertStringContainsString('"wrong option"', $e->getMessage());
        }

        $options = ['etag' => '"whatever"'];
        $response->setCache($options);
        $this->assertEquals('"whatever"', $response->getEtag());

        $now = $this->createDateTimeNow();
        
$sessionFactory = $this->createMock(SessionFactory::class);
        $sessionFactory->expects($this->once())->method('createSession')->willReturn($session);

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

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

        $request = new Request();
        $response = new Response();
        $response->setCache(['public' => true, 'max_age' => '30']);
        $listener->onKernelRequest(new RequestEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST));
        $this->assertTrue($request->hasSession());

        $subRequest = clone $request;
        $this->assertSame($request->getSession()$subRequest->getSession());
        $listener->onKernelRequest(new RequestEvent($kernel$subRequest, HttpKernelInterface::MAIN_REQUEST));
        $listener->onKernelResponse(new ResponseEvent($kernel$subRequest, HttpKernelInterface::MAIN_REQUEST, $response));

        $this->assertFalse($response->headers->has('Expires'));
        $this->assertFalse($response->headers->hasCacheControlDirective('private'));
        $this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
        
Home | Imprint | This part of the site doesn't use cookies.