-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |