<commit_before><?php

namespace NAttreid\WebManager\DI;

use NAttreid\AppManager\AppManager;
use NAttreid\Cms\DI\ModuleExtension;
use NAttreid\WebManager\Components\Footer;
use NAttreid\WebManager\Components\Header;
use NAttreid\WebManager\Components\IFooterFactory;
use NAttreid\WebManager\Components\IHeaderFactory;
use NAttreid\WebManager\Services\Hooks\GoogleAnalyticsHook;
use NAttreid\WebManager\Services\Hooks\HookFactory;
use NAttreid\WebManager\Services\Hooks\HookService;
use NAttreid\WebManager\Services\Hooks\WebMasterHook;
use NAttreid\WebManager\Services\PageService;
use Nette\DI\ServiceDefinition;
use Nette\DI\Statement;
use Nette\InvalidStateException;
use Nextras\Orm\Model\Model;

/**
 * Rozsireni
 *
 * @author Attreid <<EMAIL>>
 */
class WebManagerExtension extends ModuleExtension
{

	protected $namespace = 'webManager';
	protected $dir = __DIR__;
	protected $package = 'NAttreid\\';

	public function loadConfiguration()
	{
		$builder = $this->getContainerBuilder();
		$config = $this->validateConfig($this->loadFromFile($this->dir . '/default.neon'), $this->config);

		if ($config['homepage'] === null) {
			throw new InvalidStateException("WebManager: 'homepage' does not set in config.neon");
		}
		if ($config['page'] === null) {
			throw new InvalidStateException("WebManager: 'page' does not set in config.neon");
		}
		if ($config['module'] === null) {
			throw new InvalidStateException("WebManager: 'module' does not set in config.neon");
		}

		$builder->addDefinition($this->prefix('pageService'))
			->setClass(PageService::class)
			->setArguments([$config['homepage'], $config['page'], $config['module']]);

		$builder->addDefinition($this->prefix('headerFactory'))
			->setImplement(IHeaderFactory::class)
			->setFactory(Header::class);

		$builder->addDefinition($this->prefix('footerFactory'))
			->setImplement(IFooterFactory::class)
			->setFactory(Footer::class);

		$this->prepareHooks();
	}

	private function prepareHooks()
	{
		$builder = $this->getContainerBuilder();

		$gaHook=$builder->addDefinition($this->prefix('googleAnalyticsHook'))
			->setClass(GoogleAnalyticsHook::class);
		$wmHook=$builder->addDefinition($this->prefix('webMasterHook'))
			->setClass(WebMasterHook::class);

		$builder->addDefinition($this->prefix('hookService'))
			->setClass(HookService::class)
			->addSetup('addHook', [$gaHook])
			->addSetup('addHook', [$wmHook]);

	}

	public function beforeCompile()
	{
		parent::beforeCompile();
		$builder = $this->getContainerBuilder();

		$app = $builder->getByType(AppManager::class);
		$builder->getDefinition($app)
			->addSetup(new Statement('$service->onInvalidateCache[] = function() {?->pages->cleanCache();}', ['@' . Model::class]));

		$hook = $builder->getByType(HookService::class);
		$hookService = $builder->getDefinition($hook);
		foreach ($this->findByType(HookFactory::class) as $def) {
			$hookService->addSetup('addHook', [$def]);
		}
	}

	/**
	 *
	 * @param string $type
	 * @return ServiceDefinition[]
	 */
	private function findByType($type)
	{
		$type = ltrim($type, '\\');
		return array_filter($this->getContainerBuilder()->getDefinitions(), function (ServiceDefinition $def) use ($type) {
			return is_a($def->getClass(), $type, true) || is_a($def->getImplement(), $type, true);
		});
	}
}
<commit_msg>mensi upravy<commit_after><?php

namespace NAttreid\WebManager\DI;

use NAttreid\AppManager\AppManager;
use NAttreid\Cms\DI\ModuleExtension;
use NAttreid\WebManager\Components\Footer;
use NAttreid\WebManager\Components\Header;
use NAttreid\WebManager\Components\IFooterFactory;
use NAttreid\WebManager\Components\IHeaderFactory;
use NAttreid\WebManager\Services\Hooks\GoogleAnalyticsHook;
use NAttreid\WebManager\Services\Hooks\HookFactory;
use NAttreid\WebManager\Services\Hooks\HookService;
use NAttreid\WebManager\Services\Hooks\WebMasterHook;
use NAttreid\WebManager\Services\PageService;
use Nette\DI\ServiceDefinition;
use Nette\DI\Statement;
use Nette\InvalidStateException;
use Nextras\Orm\Model\Model;

/**
 * Rozsireni
 *
 * @author Attreid <<EMAIL>>
 */
class WebManagerExtension extends ModuleExtension
{

	protected $namespace = 'webManager';
	protected $dir = __DIR__;
	protected $package = 'NAttreid\\';

	public function loadConfiguration()
	{
		$builder = $this->getContainerBuilder();
		$config = $this->validateConfig($this->loadFromFile($this->dir . '/default.neon'), $this->config);

		if ($config['homepage'] === null) {
			throw new InvalidStateException("WebManager: 'homepage' does not set in config.neon");
		}
		if ($config['page'] === null) {
			throw new InvalidStateException("WebManager: 'page' does not set in config.neon");
		}
		if ($config['module'] === null) {
			throw new InvalidStateException("WebManager: 'module' does not set in config.neon");
		}

		$builder->addDefinition($this->prefix('pageService'))
			->setClass(PageService::class)
			->setArguments([$config['homepage'], $config['page'], $config['module']]);

		$builder->addDefinition($this->prefix('headerFactory'))
			->setImplement(IHeaderFactory::class)
			->setFactory(Header::class);

		$builder->addDefinition($this->prefix('footerFactory'))
			->setImplement(IFooterFactory::class)
			->setFactory(Footer::class);

		$this->prepareHooks();
	}

	private function prepareHooks()
	{
		$builder = $this->getContainerBuilder();

		$gaHook = $builder->addDefinition($this->prefix('googleAnalyticsHook'))
			->setClass(GoogleAnalyticsHook::class);
		$wmHook = $builder->addDefinition($this->prefix('webMasterHook'))
			->setClass(WebMasterHook::class);

		$builder->addDefinition($this->prefix('hookService'))
			->setClass(HookService::class)
			->addSetup('addHook', [$gaHook])
			->addSetup('addHook', [$wmHook]);

	}

	public function beforeCompile()
	{
		parent::beforeCompile();
		$builder = $this->getContainerBuilder();

		$app = $builder->getByType(AppManager::class);
		$builder->getDefinition($app)
			->addSetup(new Statement('$service->onInvalidateCache[] = function() {?->pages->cleanCache();}', ['@' . Model::class]));

		$hook = $builder->getByType(HookService::class);
		$hookService = $builder->getDefinition($hook);
		foreach ($this->findByType(HookFactory::class) as $def) {
			$hookService->addSetup('addHook', [$def]);
		}
	}

	/**
	 *
	 * @param string $type
	 * @return ServiceDefinition[]
	 */
	private function findByType($type)
	{
		$type = ltrim($type, '\\');
		return array_filter($this->getContainerBuilder()->getDefinitions(), function (ServiceDefinition $def) use ($type) {
			return is_a($def->getClass(), $type, true) || is_a($def->getImplement(), $type, true);
		});
	}
}

{'fraction_non_alphanumeric': 0.1732950249508544, 'fraction_numerical': 0.0, 'mean_word_length': 5.889583333333333, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 97, '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': '1339040', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2095, 'n_words': 343}