| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\DI\Extensions; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Tracy; |
| 12 |
/** |
| 13 |
* DI extension. |
| 14 |
*/ |
| 15 |
final class DIExtension extends \Packetery\Nette\DI\CompilerExtension |
| 16 |
{ |
| 17 |
/** @var array */ |
| 18 |
public $exportedTags = []; |
| 19 |
/** @var array */ |
| 20 |
public $exportedTypes = []; |
| 21 |
/** @var bool */ |
| 22 |
private $debugMode; |
| 23 |
/** @var float */ |
| 24 |
private $time; |
| 25 |
public function __construct(bool $debugMode = \false) |
| 26 |
{ |
| 27 |
$this->debugMode = $debugMode; |
| 28 |
$this->time = \microtime(\true); |
| 29 |
$this->config = new class |
| 30 |
{ |
| 31 |
/** @var ?bool */ |
| 32 |
public $debugger; |
| 33 |
/** @var string[] */ |
| 34 |
public $excluded = []; |
| 35 |
/** @var ?string */ |
| 36 |
public $parentClass; |
| 37 |
/** @var object */ |
| 38 |
public $export; |
| 39 |
}; |
| 40 |
$this->config->export = new class |
| 41 |
{ |
| 42 |
/** @var bool */ |
| 43 |
public $parameters = \true; |
| 44 |
/** @var string[]|bool|null */ |
| 45 |
public $tags = \true; |
| 46 |
/** @var string[]|bool|null */ |
| 47 |
public $types = \true; |
| 48 |
}; |
| 49 |
} |
| 50 |
public function loadConfiguration() |
| 51 |
{ |
| 52 |
$builder = $this->getContainerBuilder(); |
| 53 |
$builder->addExcludedClasses($this->config->excluded); |
| 54 |
} |
| 55 |
public function afterCompile(\Packetery\Nette\PhpGenerator\ClassType $class) |
| 56 |
{ |
| 57 |
if ($this->config->parentClass) { |
| 58 |
$class->setExtends($this->config->parentClass); |
| 59 |
} |
| 60 |
$this->restrictParameters($class); |
| 61 |
$this->restrictTags($class); |
| 62 |
$this->restrictTypes($class); |
| 63 |
if ($this->debugMode && ($this->config->debugger ?? $this->getContainerBuilder()->getByType(Tracy\Bar::class))) { |
| 64 |
$this->enableTracyIntegration(); |
| 65 |
} |
| 66 |
} |
| 67 |
private function restrictParameters(\Packetery\Nette\PhpGenerator\ClassType $class) : void |
| 68 |
{ |
| 69 |
if (!$this->config->export->parameters) { |
| 70 |
$class->removeMethod('getParameters'); |
| 71 |
$class->removeMethod('getStaticParameters'); |
| 72 |
} |
| 73 |
} |
| 74 |
private function restrictTags(\Packetery\Nette\PhpGenerator\ClassType $class) : void |
| 75 |
{ |
| 76 |
$option = $this->config->export->tags; |
| 77 |
if ($option === \true) { |
| 78 |
} elseif ($option === \false) { |
| 79 |
$class->removeProperty('tags'); |
| 80 |
} elseif ($prop = $class->getProperties()['tags'] ?? null) { |
| 81 |
$prop->setValue(\array_intersect_key($prop->getValue(), $this->exportedTags + \array_flip((array) $option))); |
| 82 |
} |
| 83 |
} |
| 84 |
private function restrictTypes(\Packetery\Nette\PhpGenerator\ClassType $class) : void |
| 85 |
{ |
| 86 |
$option = $this->config->export->types; |
| 87 |
if ($option === \true) { |
| 88 |
return; |
| 89 |
} |
| 90 |
$prop = $class->getProperty('wiring'); |
| 91 |
$prop->setValue(\array_intersect_key($prop->getValue(), $this->exportedTypes + (\is_array($option) ? \array_flip($option) : []))); |
| 92 |
} |
| 93 |
private function enableTracyIntegration() : void |
| 94 |
{ |
| 95 |
\Packetery\Nette\Bridges\DITracy\ContainerPanel::$compilationTime = $this->time; |
| 96 |
$this->initialization->addBody($this->getContainerBuilder()->formatPhp('?;', [new \Packetery\Nette\DI\Definitions\Statement('@Tracy\\Bar::addPanel', [new \Packetery\Nette\DI\Definitions\Statement(\Packetery\Nette\Bridges\DITracy\ContainerPanel::class)])])); |
| 97 |
} |
| 98 |
} |
| 99 |
|