<commit_before><?php

namespace Contributte\Middlewares\DI;

use Contributte\Middlewares\Exception\InvalidStateException;
use Contributte\Middlewares\Utils\ChainBuilder;
use Nette\DI\CompilerExtension;
use Nette\Utils\Validators;

abstract class AbstractMiddlewareExtension extends CompilerExtension
{

	/** @var array */
	protected $defaults = [
		'middlewares' => [],
		'root' => NULL,
	];

	/**
	 * Register services
	 *
	 * @return void
	 */
	public function loadConfiguration()
	{
		$builder = $this->getContainerBuilder();
		$config = $this->validateConfig($this->defaults);

		Validators::assertField($config, 'middlewares', 'array');
		Validators::assertField($config, 'root', 'string|null');

		if (empty($config['middlewares']) && $config['root'] === NULL) {
			throw new InvalidStateException('There must be at least one middleware registered or root middleware configured.');
		}

		if ($config['root'] !== NULL) {
			if (strncmp($config['root'], '@', 1) !== 0) {
				throw new InvalidStateException('Pass root middleware as a service with @ at the beginning');
			}
		}

		// Skip next registration, if root middleware is specified
		if ($config['root'] !== NULL) return;

		// Register middleware chain builder
		$builder->addDefinition($this->prefix('chain'))
			->setClass(ChainBuilder::class)
			->setAutowired(FALSE);
	}

	/**
	 * Decorate services
	 *
	 * @return void
	 */
	public function beforeCompile()
	{
		$builder = $this->getContainerBuilder();
		$config = $this->validateConfig($this->defaults);

		// Skip next registration, if root middleware is specified
		if ($config['root'] !== NULL) return;

		// Obtain middleware chain builder
		$chain = $builder->getDefinition($this->prefix('chain'));

		// Add middleware services to chain
		foreach ($config['middlewares'] as $service) {
			// Append to chain of middlewares
			$chain->addSetup('add', [$service]);
		}
	}

}<commit_msg>Feature: load middlewares as services and also as strings<commit_after><?php

namespace Contributte\Middlewares\DI;

use Contributte\Middlewares\Exception\InvalidStateException;
use Contributte\Middlewares\Utils\ChainBuilder;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\Utils\Validators;

abstract class AbstractMiddlewareExtension extends CompilerExtension
{

	/** @var array */
	protected $defaults = [
		'middlewares' => [],
		'root' => NULL,
	];

	/**
	 * Register services
	 *
	 * @return void
	 */
	public function loadConfiguration()
	{
		$builder = $this->getContainerBuilder();
		$config = $this->validateConfig($this->defaults);

		Validators::assertField($config, 'middlewares', 'array');
		Validators::assertField($config, 'root', 'string|null');

		if (empty($config['middlewares']) && $config['root'] === NULL) {
			throw new InvalidStateException('There must be at least one middleware registered or root middleware configured.');
		}

		// Skip next registration, if root middleware is specified
		if ($config['root'] !== NULL) return;

		// Register middleware chain builder
		$builder->addDefinition($this->prefix('chain'))
			->setClass(ChainBuilder::class)
			->setAutowired(FALSE);
	}

	/**
	 * Decorate services
	 *
	 * @return void
	 */
	public function beforeCompile()
	{
		$builder = $this->getContainerBuilder();
		$config = $this->validateConfig($this->defaults);

		// Skip next registration, if root middleware is specified
		if ($config['root'] !== NULL) return;

		// Obtain middleware chain builder
		$chain = $builder->getDefinition($this->prefix('chain'));

		// Add middleware services to chain
		$counter = 0;
		foreach ($config['middlewares'] as $service) {

			// Create middleware as service
			if (strncmp($service, '@', 1) !== 0) {
				$def = $builder->addDefinition($this->prefix('middleware' . ($counter++)));
				Compiler::loadDefinition($def, $service);
			} else {
				$def = $builder->getDefinition(ltrim($service, '@'));
			}

			$def->addTag('middleware');

			// Append to chain of middlewares
			$chain->addSetup('add', [$def]);
		}
	}

}
{'fraction_non_alphanumeric': 0.15746268656716417, 'fraction_numerical': 0.0012437810945273632, 'mean_word_length': 4.455902306648575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '7234373', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1177, 'n_words': 292}