From 9dca95051e6463a3a3348bcfa4a0959246db98c2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 27 Aug 2023 12:47:10 +0200 Subject: [PATCH] added bridge for Nette DI --- composer.json | 1 + src/Bridges/DI/LatteExtension.php | 76 +++++++++++++++++++++++++ src/Bridges/DI/LatteFactory.php | 18 ++++++ tests/bridges/LatteExtension.basic.phpt | 67 ++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 src/Bridges/DI/LatteExtension.php create mode 100644 src/Bridges/DI/LatteFactory.php create mode 100644 tests/bridges/LatteExtension.basic.phpt diff --git a/composer.json b/composer.json index a20b25645..3e7c56b3c 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "nette/tester": "^2.0", "tracy/tracy": "^2.3", "nette/utils": "^3.0", + "nette/di": "^3.1 || ^4.0", "phpstan/phpstan": "^1", "nette/php-generator": "^3.6 || ^4.0" }, diff --git a/src/Bridges/DI/LatteExtension.php b/src/Bridges/DI/LatteExtension.php new file mode 100644 index 000000000..a786b095d --- /dev/null +++ b/src/Bridges/DI/LatteExtension.php @@ -0,0 +1,76 @@ +tempDir = $tempDir; + $this->debugMode = $debugMode; + } + + + public function getConfigSchema(): Nette\Schema\Schema + { + return Expect::structure([ + 'extensions' => Expect::arrayOf('string|Nette\DI\Definitions\Statement'), + 'strictTypes' => Expect::bool(false), + 'strictParsing' => Expect::bool(false), + 'phpLinter' => Expect::string(), + ]); + } + + + public function loadConfiguration() + { + $config = $this->config; + $builder = $this->getContainerBuilder(); + + $factory = $builder->addFactoryDefinition($this->prefix('factory')) + ->setImplement(LatteFactory::class) + ->getResultDefinition() + ->setFactory(Latte\Engine::class) + ->addSetup('setTempDirectory', [$this->tempDir]) + ->addSetup('setAutoRefresh', [$this->debugMode]) + ->addSetup('setStrictTypes', [$config->strictTypes]) + ->addSetup('setStrictParsing', [$config->strictParsing]) + ->addSetup('enablePhpLinter', [$config->phpLinter]); + + foreach ($config->extensions as $extension) { + $this->addExtension($extension); + } + } + + + public function addExtension(Nette\DI\Definitions\Statement|string $extension): void + { + $extension = is_string($extension) + ? new Nette\DI\Definitions\Statement($extension) + : $extension; + + $builder = $this->getContainerBuilder(); + $builder->getDefinition($this->prefix('factory')) + ->getResultDefinition() + ->addSetup('addExtension', [$extension]); + } +} diff --git a/src/Bridges/DI/LatteFactory.php b/src/Bridges/DI/LatteFactory.php new file mode 100644 index 000000000..41bc47b1b --- /dev/null +++ b/src/Bridges/DI/LatteFactory.php @@ -0,0 +1,18 @@ +arg = $arg; + } +} + + +class AnotherExtension extends Nette\DI\CompilerExtension +{ + public function beforeCompile() + { + foreach ($this->compiler->getExtensions(Latte\Bridges\DI\LatteExtension::class) as $extension) { + $extension->addExtension('MyExtension'); + } + } +} + + +$loader = new DI\Config\Loader; +$config = $loader->load(Tester\FileMock::create(' +latte: + extensions: + - MyExtension + - MyExtension(1) + - @latteExt + +services: + latteExt: MyExtension(2) +', 'neon')); + +$compiler = new DI\Compiler; +$compiler->addExtension('latte', new Latte\Bridges\DI\LatteExtension('', false)); +$compiler->addExtension('another', new AnotherExtension); +$code = $compiler->addConfig($config)->compile(); +eval($code); + +$container = new Container; + + +$factory = $container->getService('latte.factory'); +Assert::type(Latte\Bridges\DI\LatteFactory::class, $factory); +$latte = $factory->create(); +$extensions = Assert::with($latte, fn() => $this->extensions); + +Assert::equal([ + new Latte\Essential\CoreExtension, + new Latte\Sandbox\SandboxExtension, + new MyExtension, + new MyExtension(1), + new MyExtension(2), + new MyExtension, +], $extensions);