Skip to content

Commit

Permalink
added bridge for Nette DI
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 11, 2023
1 parent 82f8355 commit 9dca950
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
76 changes: 76 additions & 0 deletions src/Bridges/DI/LatteExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Latte\Bridges\DI;

use Latte;
use Nette;
use Nette\Schema\Expect;


/**
* Latte extension for Nette DI.
*/
final class LatteExtension extends Nette\DI\CompilerExtension
{
private bool $debugMode;
private string $tempDir;


public function __construct(string $tempDir, bool $debugMode = false)
{
$this->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]);
}
}
18 changes: 18 additions & 0 deletions src/Bridges/DI/LatteFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Latte\Bridges\DI;

use Latte;


interface LatteFactory
{
function create(): Latte\Engine;
}
67 changes: 67 additions & 0 deletions tests/bridges/LatteExtension.basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class MyExtension extends Latte\Extension
{
public $arg;


public function __construct($arg = null)
{
$this->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);

0 comments on commit 9dca950

Please sign in to comment.