<commit_before><?php

/**
 * This file is part of the Arachne EntityLoader extenstion
 *
 * Copyright (c) <NAME> (<EMAIL>)
 *
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
 */

namespace Arachne\EntityLoader\Application;

use Arachne\EntityLoader\EntityLoader;
use Nette\Application\Request;
use Nette\Application\Responses\ForwardResponse;
use Nette\InvalidStateException;
use Nette\Utils\Strings;

/**
 * @author <NAME>
 */
trait TEntityLoaderPresenter
{

	/** @var EntityLoader */
	private $loader;

	final public function injectEntityLoader(EntityLoader $loader)
	{
		$this->loader = $loader;
	}

	/**
	 * Stores current request to session.
	 * @param mixed $expiration
	 * @return string
	 */
	public function storeRequest($expiration = '+ 10 minutes')
	{
		$session = $this->getSession('Arachne.Application/requests');
		do {
			$key = Strings::random(5);
		} while (isset($session[$key]));
		$request = clone $this->request;
		if (!$this->loader->removeEntities($request)) {
			throw new InvalidStateException('Failed to remove entities from request.');
		}
		$session[$key] = [ $this->getUser()->getId(), $request ];
		$session->setExpiration($expiration, $key);
		return $key;
	}

	/**
	 * Restores current request to session.
	 * @param string $key
	 */
	public function restoreRequest($key)
	{
		$session = $this->getSession('Arachne.Application/requests');
		if (!isset($session[$key]) || ($session[$key][0] !== NULL && $session[$key][0] !== $this->getUser()->getId())) {
			return;
		}
		$request = $session[$key][1];
		unset($session[$key]);
		if (!$this->loader->loadEntities($request)) {
			return;
		}
		$request->setFlag(Request::RESTORED, TRUE);
		$parameters = $request->getParameters();
		$parameters[self::FLASH_KEY] = $this->getParameter(self::FLASH_KEY);
		$request->setParameters($parameters);
		$this->sendResponse(new ForwardResponse($request));
	}

}
<commit_msg>Added possibility to store other requests than the current one<commit_after><?php

/**
 * This file is part of the Arachne EntityLoader extenstion
 *
 * Copyright (c) <NAME> (<EMAIL>)
 *
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
 */

namespace Arachne\EntityLoader\Application;

use Arachne\EntityLoader\EntityLoader;
use Nette\Application\Request;
use Nette\Application\Responses\ForwardResponse;
use Nette\InvalidStateException;
use Nette\Utils\Strings;

/**
 * @author <NAME>
 */
trait TEntityLoaderPresenter
{

	/** @var EntityLoader */
	private $loader;

	final public function injectEntityLoader(EntityLoader $loader)
	{
		$this->loader = $loader;
	}

	/**
	 * Stores request to session.
	 * @param Request $request
	 * @param mixed $expiration
	 * @return string
	 */
	public function storeRequest($request = NULL, $expiration = '+ 10 minutes')
	{
		if ($request === NULL) {
			$request = $this->request;
		} elseif (!$request instanceof Application\Request) { // first parameter is optional
			$expiration = $request;
			$request = $this->request;
		}

		$request = clone $request;
		if (!$this->loader->removeEntities($request)) {
			throw new InvalidStateException('Failed to remove entities from request.');
		}

		$session = $this->getSession('Arachne.Application/requests');
		do {
			$key = Strings::random(5);
		} while (isset($session[$key]));

		$session[$key] = [ $this->getUser()->getId(), $request ];
		$session->setExpiration($expiration, $key);
		return $key;
	}

	/**
	 * Restores current request to session.
	 * @param string $key
	 */
	public function restoreRequest($key)
	{
		$session = $this->getSession('Arachne.Application/requests');
		if (!isset($session[$key]) || ($session[$key][0] !== NULL && $session[$key][0] !== $this->getUser()->getId())) {
			return;
		}
		$request = $session[$key][1];
		unset($session[$key]);
		if (!$this->loader->loadEntities($request)) {
			return;
		}
		$request->setFlag(Request::RESTORED, TRUE);
		$parameters = $request->getParameters();
		$parameters[self::FLASH_KEY] = $this->getParameter(self::FLASH_KEY);
		$request->setParameters($parameters);
		$this->sendResponse(new ForwardResponse($request));
	}

}

{'fraction_non_alphanumeric': 0.1642249527410208, 'fraction_numerical': 0.002835538752362949, 'mean_word_length': 4.674262734584451, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '256874', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1302, 'n_words': 304}