<commit_before><?php

declare(strict_types=1);

namespace NAttreid\Cms\Configurator;

use NAttreid\AppManager\AppManager;
use NAttreid\Cms\Model\Configuration\Configuration;
use NAttreid\Cms\Model\Orm;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nextras\Orm\Model\Model;

/**
 * Nastaveni aplikace
 *
 * @author Attreid <<EMAIL>>
 */
class Configurator implements IConfigurator
{
	private $default = [
		'sendNewUserPassword' => true,
		'sendChangePassword' => true,
		'dockbarAdvanced' => false,
		'mailPanel' => false
	];

	private $tag = 'cache/configuration';

	/** @var Orm */
	private $orm;

	/** @var Cache */
	private $cache;

	public function __construct(Model $orm, IStorage $storage, AppManager $app)
	{
		$this->orm = $orm;
		$this->cache = new Cache($storage, 'nattreid-cms-configurator');
		$app->onInvalidateCache[] = [$this, 'cleanCache'];
		$this->orm->configuration->onFlush[] = function ($persisted, $removed) {
			if (!empty($persisted) || !empty($removed)) {
				$this->cleanCache();
			}
		};
	}

	/**
	 * Prida vychozi hodnotu
	 * @param string $property
	 * @param mixed $value
	 */
	public function addDefault(string $property, $value): void
	{
		$this->default[$property] = $value;
	}

	/**
	 * smaze cache
	 */
	public function cleanCache(): void
	{
		$this->cache->clean([
			Cache::TAGS => [$this->tag]
		]);
	}

	public function __get(string $name)
	{
		$result = $this->cache->load($name);
		if ($result === null) {
			$result = $this->cache->save($name, function () use ($name) {
				$configuration = $this->orm->configuration->get($name);
				if ($configuration) {
					return $configuration->value;
				} else {
					if (isset($this->default[$name])) {
						return $this->$name = $this->default[$name];
					}
				}
				return false;
			}, [
				Cache::TAGS => [$this->tag]
			]);
		}
		return $result;
	}

	public function __set(string $name, $value)
	{
		$configuration = $this->orm->configuration->getById($name);
		if ($configuration === null) {
			$configuration = new Configuration;
		}
		$configuration->name = $name;
		$configuration->value = $value;
		$this->orm->persistAndFlush($configuration);
	}<commit_msg>configurator: neon @cms.configurator::variable->subVar<commit_after><?php

declare(strict_types=1);

namespace NAttreid\Cms\Configurator;

use NAttreid\AppManager\AppManager;
use NAttreid\Cms\Model\Configuration\Configuration;
use NAttreid\Cms\Model\Orm;
use NAttreid\Utils\Strings;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nextras\Orm\Model\Model;

/**
 * Nastaveni aplikace
 *
 * @author Attreid <<EMAIL>>
 */
class Configurator implements IConfigurator
{
	private $default = [
		'sendNewUserPassword' => true,
		'sendChangePassword' => true,
		'dockbarAdvanced' => false,
		'mailPanel' => false
	];

	private $tag = 'cache/configuration';

	/** @var Orm */
	private $orm;

	/** @var Cache */
	private $cache;

	public function __construct(Model $orm, IStorage $storage, AppManager $app)
	{
		$this->orm = $orm;
		$this->cache = new Cache($storage, 'nattreid-cms-configurator');
		$app->onInvalidateCache[] = [$this, 'cleanCache'];
		$this->orm->configuration->onFlush[] = function ($persisted, $removed) {
			if (!empty($persisted) || !empty($removed)) {
				$this->cleanCache();
			}
		};
	}

	/**
	 * Prida vychozi hodnotu
	 * @param string $property
	 * @param mixed $value
	 */
	public function addDefault(string $property, $value): void
	{
		$this->default[$property] = $value;
	}

	/**
	 * smaze cache
	 */
	public function cleanCache(): void
	{
		$this->cache->clean([
			Cache::TAGS => [$this->tag]
		]);
	}

	public function __get(string $name)
	{
		if (Strings::contains($name, '->')) {
			list($name, $variable) = explode('->', $name);
			return $this->get($name)->$variable ?? false;
		} else {
			return $this->get($name);
		}
	}

	private function get($name)
	{
		$result = $this->cache->load($name);
		if ($result === null) {
			$result = $this->cache->save($name, function () use ($name) {
				$configuration = $this->orm->configuration->get($name);
				if ($configuration) {
					return $configuration->value;
				} else {
					if (isset($this->default[$name])) {
						return $this->$name = $this->default[$name];
					}
				}
				return false;
			}, [
				Cache::TAGS => [$this->tag]
			]);
		}
		return $result;
	}

	public function __set(string $name, $value)
	{
		$configuration = $this->orm->configuration->getById($name);
		if ($configuration === null) {
			$configuration = new Configuration;
		}
		$configuration->name = $name;
		$configuration->value = $value;
		$this->orm->persistAndFlush($configuration);
	}
{'fraction_non_alphanumeric': 0.18274221453287198, 'fraction_numerical': 0.00043252595155709344, 'mean_word_length': 3.8633017875920084, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 85, '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': '1988049', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1593, 'n_words': 313}