This repository has been archived by the owner on Jan 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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
5 changed files
with
178 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Adeira; | ||
|
||
final class GroupedNeonAdapter extends \Nette\DI\Config\Adapters\NeonAdapter | ||
{ | ||
|
||
public function process(array $arr) | ||
{ | ||
foreach ($arr as &$configKeys) { | ||
if (is_array($configKeys)) { | ||
foreach ($configKeys as $originalKey => $entity) { | ||
if ($entity instanceof \Nette\Neon\Entity) { | ||
if (\Nette\Utils\Strings::endsWith($entity->value, '\\')) { | ||
if (!$this->isEntityRegisteredAsAnonymous($originalKey)) { | ||
throw new \Nette\Neon\Exception("Service with grouped classes must be anonymous. You have to remove key '$originalKey' to use this feature."); | ||
} | ||
|
||
unset($configKeys[$originalKey]); | ||
|
||
foreach ($entity->attributes as $attributeKey => $attribute) { | ||
if (!$this->isEntityRegisteredAsAnonymous($attributeKey)) { | ||
throw new \Nette\Neon\Exception("Grouped classes in service definition must be anonymous. Please remove key '$attributeKey'."); | ||
} | ||
|
||
$configKeys[] = $entity->value . $attribute; //add grouped services | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
unset($configKeys); //unreference | ||
return parent::process($arr); | ||
} | ||
|
||
private function isEntityRegisteredAsAnonymous($entityKey) | ||
{ | ||
return (string)(int)$entityKey === (string)$entityKey; //anonymous | ||
} | ||
|
||
} |
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,84 @@ | ||
<?php | ||
|
||
namespace Adeira\Tests; | ||
|
||
use Adeira\GroupedNeonAdapter; | ||
use Tester\Assert; | ||
use Tester\FileMock; | ||
|
||
require dirname(__DIR__) . '/bootstrap.php'; | ||
|
||
/** | ||
* @testCase | ||
*/ | ||
class GroupedNeonAdapterTest extends \Tester\TestCase | ||
{ | ||
|
||
public function testThatProcessExpandsEntities() | ||
{ | ||
$loader = new \Nette\DI\Config\Loader; | ||
$loader->addAdapter($extension = 'neon', GroupedNeonAdapter::class); | ||
$configuration = <<<NEON | ||
section: | ||
- Namespace\ClassA | ||
B: Namespace\ClassB | ||
- Namespace\(Class_1) | ||
- Namespace\ClassC | ||
- Namespace\( | ||
Class_2 | ||
Class_3 | ||
Class_4 | ||
) | ||
NEON; | ||
Assert::same([ | ||
'section' => [ | ||
0 => 'Namespace\\ClassA', | ||
'B' => 'Namespace\\ClassB', | ||
2 => 'Namespace\\ClassC', // it doesn't cleanup indices but who cares | ||
4 => 'Namespace\\Class_1', | ||
5 => 'Namespace\\Class_2', | ||
6 => 'Namespace\\Class_3', | ||
7 => 'Namespace\\Class_4', | ||
], | ||
], $loader->load(FileMock::create($configuration, $extension))); | ||
} | ||
|
||
public function testThatClassMustBeAnonymous() | ||
{ | ||
$loader = new \Nette\DI\Config\Loader; | ||
$loader->addAdapter($extension = 'neon', GroupedNeonAdapter::class); | ||
$configuration = <<<NEON | ||
section: | ||
A: Namespace\ClassA | ||
B: Namespace\( | ||
Class_2 | ||
Class_3 | ||
Class_4 | ||
) | ||
NEON; | ||
Assert::exception(function () use ($loader, $configuration, $extension) { | ||
$loader->load(FileMock::create($configuration, $extension)); | ||
}, \Nette\Neon\Exception::class, 'Service with grouped classes must be anonymous. You have to remove key \'B\' to use this feature.'); | ||
} | ||
|
||
public function testThatExpandedClassMustBeAnonymous() | ||
{ | ||
$loader = new \Nette\DI\Config\Loader; | ||
$loader->addAdapter($extension = 'neon', GroupedNeonAdapter::class); | ||
$configuration = <<<NEON | ||
section: | ||
A: Namespace\ClassA | ||
- Namespace\( | ||
Class_2, | ||
c3: Class_3, | ||
Class_4, | ||
) | ||
NEON; | ||
Assert::exception(function () use ($loader, $configuration, $extension) { | ||
$loader->load(FileMock::create($configuration, $extension)); | ||
}, \Nette\Neon\Exception::class, 'Grouped classes in service definition must be anonymous. Please remove key \'c3\'.'); | ||
} | ||
|
||
} | ||
|
||
(new GroupedNeonAdapterTest)->run(); |