Skip to content

Commit

Permalink
Minification of metadata: added option 'di › export › types' & Compil…
Browse files Browse the repository at this point in the history
…er::addExportedType()
  • Loading branch information
dg committed Apr 3, 2019
1 parent 71a9ed2 commit dd3170c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/DITracy/templates/ContainerPanel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use Tracy\Helpers;
<?php $autowired = in_array($name, array_merge($wiring[$type][0] ?? [], $wiring[$type][1] ?? []), true) ?>
<tr>
<td class="<?= isset($instances[$name]) ? 'created' : '' ?>"><?= is_numeric($name) ? "<span title='$name'>–</span>" : Helpers::escapeHtml($name) ?></td>
<td class="<?= $autowired ? 'yes' : '' ?>"><?= $autowired ? 'yes' : 'no' ?></td>
<td class="<?= $autowired ? 'yes' : '' ?>"><?= $autowired ? 'yes' : (isset($wiring[$type]) ? 'no' : '?') ?></td>
<td>
<?php if (isset($instances[$name]) && !$instances[$name] instanceof Nette\DI\Container): ?>
<?= Dumper::toHtml($instances[$name], [Dumper::COLLAPSE => true, Dumper::LIVE => true]); ?>
Expand Down
12 changes: 12 additions & 0 deletions src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ public function addExportedTag(string $tag)
}


/**
* @return static
*/
public function addExportedType(string $type)
{
if (isset($this->extensions['di'])) {
$this->extensions['di']->exportedTypes[$type] = true;
}
return $this;
}


public function compile(): string
{
$this->processExtensions();
Expand Down
19 changes: 17 additions & 2 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,16 @@ public function exportMeta(): array
$meta['aliases'] = $this->aliases;
ksort($meta['aliases']);

$meta['wiring'] = $this->exportTypes();
return $meta;
}


/**
* @internal
*/
public function exportTypes(array $limit = null): array
{
$all = [];
foreach ($this->definitions as $name => $def) {
if ($type = $def->getType()) {
Expand All @@ -370,15 +380,20 @@ public function exportMeta(): array
}

[$low, $high] = $this->autowiring->getClassList();
if (is_array($limit)) {
$all = array_intersect_key($all, $limit);
}

$wiring = [];
foreach ($all as $class => $names) {
$meta['wiring'][$class] = array_filter([
$wiring[$class] = array_filter([
$high[$class] ?? [],
$low[$class] ?? [],
array_diff($names, $low[$class] ?? [], $high[$class] ?? []),
]);
}

return $meta;
return $wiring;
}


Expand Down
15 changes: 15 additions & 0 deletions src/DI/Extensions/DIExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ final class DIExtension extends Nette\DI\CompilerExtension
/** @var string[] */
public $exportedTags = [];

/** @var string[] */
public $exportedTypes = [];

/** @var bool */
private $debugMode;

Expand Down Expand Up @@ -47,6 +50,8 @@ public function __construct(bool $debugMode = false)
public $parameters = true;
/** @var string[]|bool|null */
public $tags = true;
/** @var string[]|bool|null */
public $types = true;
};
$this->config->debugger = interface_exists(\Tracy\IBarPanel::class);
}
Expand All @@ -67,6 +72,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)

$this->exportParameters($class);
$this->exportTags($class);
$this->exportTypes($class);

if ($this->debugMode && $this->config->debugger) {
$this->enableTracyIntegration($class);
Expand Down Expand Up @@ -97,6 +103,15 @@ private function exportTags(Nette\PhpGenerator\ClassType $class): void
}


private function exportTypes(Nette\PhpGenerator\ClassType $class): void
{
$option = $this->config->export->types;
$exported = $option === true ? null : $this->exportedTypes + array_flip(is_array($option) ? $option : []);
$data = $this->getContainerBuilder()->exportTypes($exported);
$class->addProperty('wiring')->setVisibility('protected')->setValue($data);
}


private function initializeTaggedServices(Nette\PhpGenerator\ClassType $class): void
{
foreach (array_filter($this->getContainerBuilder()->findByTag('run')) as $name => $on) {
Expand Down
132 changes: 132 additions & 0 deletions tests/DI/DIExtension.exportTypes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

/**
* Test: DIExtension types exporting
*/

declare(strict_types=1);

use Nette\DI;
use Nette\DI\Extensions\DIExtension;
use Tester\Assert;


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


class Foo
{
}


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$container = createContainer($compiler, '
di:
export:
types: true
services:
one:
factory: stdClass
autowired: no
second: stdClass
');

Assert::same(['second', 'one'], $container->findByType(stdClass::class));
Assert::same(['second'], $container->findAutowired(stdClass::class));
});


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$container = createContainer($compiler, '
di:
export:
types: false
services:
one:
factory: stdClass
autowired: no
second: stdClass
');

Assert::same([], $container->findByType(stdClass::class));
Assert::same([], $container->findAutowired(stdClass::class));
});


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$compiler->addExportedType(stdClass::class);
$container = createContainer($compiler, '
di:
export:
types: false
services:
one:
factory: stdClass
autowired: no
second: stdClass
');

Assert::same(['second', 'one'], $container->findByType(stdClass::class));
Assert::same(['second'], $container->findAutowired(stdClass::class));
});


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$compiler->addExportedType(stdClass::class);
$container = createContainer($compiler, '
di:
export:
types:
services:
one:
factory: Foo
autowired: no
second: stdClass
');

Assert::same([], $container->findByType(Foo::class));
Assert::same([], $container->findAutowired(Foo::class));
Assert::same(['second'], $container->findByType(stdClass::class));
Assert::same(['second'], $container->findAutowired(stdClass::class));
});


test(function () {
$compiler = new DI\Compiler;
$compiler->addExtension('di', new DIExtension);
$compiler->addExportedType(stdClass::class);
$container = createContainer($compiler, '
di:
export:
types:
- Foo
services:
one:
factory: Foo
autowired: no
second: stdClass
');

Assert::same(['one'], $container->findByType(Foo::class));
Assert::same([], $container->findAutowired(Foo::class));
Assert::same(['second'], $container->findByType(stdClass::class));
Assert::same(['second'], $container->findAutowired(stdClass::class));
});

0 comments on commit dd3170c

Please sign in to comment.