-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
10 changed files
with
188 additions
and
233 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
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
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,90 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Toolkit; | ||
|
||
use Nette\DI\Compiler; | ||
use Nette\DI\Container as NetteContainer; | ||
use Nette\DI\ContainerLoader; | ||
use Nettrine\Cache\DI\CacheExtension; | ||
use Nettrine\DBAL\DI\DbalExtension; | ||
use Tracy\Bridges\Nette\TracyExtension; | ||
|
||
final class Container | ||
{ | ||
|
||
/** @var string */ | ||
private $key; | ||
|
||
/** @var callable[] */ | ||
private $onCompile = []; | ||
|
||
public function __construct(string $key) | ||
{ | ||
$this->key = $key; | ||
} | ||
|
||
public static function of(?string $key = null): Container | ||
{ | ||
return new static($key ?? uniqid(random_bytes(16))); | ||
} | ||
|
||
public function withDefaults(): Container | ||
{ | ||
$this->withDefaultExtensions(); | ||
$this->withDefaultParameters(); | ||
|
||
return $this; | ||
} | ||
|
||
public function withDefaultExtensions(): Container | ||
{ | ||
$this->onCompile[] = function (Compiler $compiler): void { | ||
$compiler->addExtension('nettrine.dbal', new DbalExtension()); | ||
$compiler->addExtension('nettrine.cache', new CacheExtension()); | ||
$compiler->addExtension('nette.tracy', new TracyExtension()); | ||
}; | ||
|
||
return $this; | ||
} | ||
|
||
public function withDefaultParameters(): Container | ||
{ | ||
$this->onCompile[] = function (Compiler $compiler): void { | ||
$compiler->addConfig([ | ||
'parameters' => [ | ||
'tempDir' => Tests::TEMP_PATH, | ||
'appDir' => Tests::APP_PATH, | ||
], | ||
]); | ||
$compiler->addConfig(Helpers::neon(' | ||
nettrine.dbal: | ||
connection: | ||
driver: pdo_sqlite | ||
')); | ||
}; | ||
|
||
return $this; | ||
} | ||
|
||
public function withCompiler(callable $cb): Container | ||
{ | ||
$this->onCompile[] = function (Compiler $compiler) use ($cb): void { | ||
$cb($compiler); | ||
}; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): NetteContainer | ||
{ | ||
$loader = new ContainerLoader(Tests::TEMP_PATH, true); | ||
$class = $loader->load(function (Compiler $compiler): void { | ||
foreach ($this->onCompile as $cb) { | ||
$cb($compiler); | ||
} | ||
}, $this->key); | ||
|
||
return new $class(); | ||
} | ||
|
||
} |
Oops, something went wrong.