Registrace DI extension v jiné CompilerExtension

Petr Parolek
Člen | 455
+
0
-

Ahoj, můj kod se mi zdá napsán ne moc dobře:

<?php

namespace Bar\Foo\DI;

use Kdyby\Translation\DI\ITranslationProvider;
use Nette;
use Nette\Application\IPresenterFactory;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;

class AbcExtension extends CompilerExtension implements ITranslationProvider
{

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

		$this->compiler->loadDefinitions(
			$builder,
			$config['services'],
			$this->name
		);

		....
	}

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

		...
	}

	public function setCompiler(Compiler $compiler, $name)
	{
		$neon = new Nette\Neon\Neon();
		$config = $neon->decode(file_get_contents(__DIR__ . '/config.neon'));

		foreach ($config['extensions'] as $extension => $extensionClass) {
			if (count($compiler->getExtensions($extensionClass)) === 0) {
				$compiler->addExtension($extension, new $extensionClass)
					->addConfig([$extension => $config[$extension]]);
			}
		}

		return parent::setCompiler($compiler, $name);
	}

	/**
	 * @return array|string[]
	 */
	public function getTranslationResources(): array
	{
		return [
			__DIR__ . '/../lang',
		];
	}
}

Máte někdo prosím napady na zlepšení kodu? Nebo se vám zdá řešení čisté?

Editoval ppar (30. 9. 2019 12:59)