Skip to content

Commit

Permalink
allowed registering rss extensions by Nette DIC extension
Browse files Browse the repository at this point in the history
  • Loading branch information
konecnyjakub committed Feb 2, 2020
1 parent 6608106 commit 529bcca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Bridges/NetteDI/RssExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\DI\Definitions\ServiceDefinition;
use Nexendrie\Rss\Generator;
use Nette\Schema\Expect;
use Nexendrie\Rss\InvalidRssExtension;
use Nexendrie\Rss\IRssExtension;

/**
Expand All @@ -15,6 +16,7 @@
* @author Jakub Konečný
*/
final class RssExtension extends CompilerExtension {
/** @internal */
public const SERVICE_GENERATOR = "generator";

protected function setProperty(ServiceDefinition &$generator, \stdClass $config, string $property): void {
Expand All @@ -28,6 +30,7 @@ public function getConfigSchema(): \Nette\Schema\Schema {
"shortenDescription" => Expect::int(150),
"dateTimeFormat" => Expect::string(""),
"template" => Expect::string(""),
"extensions" => Expect::arrayOf("class")->default([]),
]);
}

Expand All @@ -40,6 +43,14 @@ public function loadConfiguration(): void {
->addSetup('$service->shortenDescription = ?', [$config->shortenDescription]);
$this->setProperty($generator, $config, "dateTimeFormat");
$this->setProperty($generator, $config, "template");
/** @var string $extension */
foreach($config->extensions as $index => $extension) {
if(!class_exists($extension) || !is_subclass_of($extension, IRssExtension::class)) {
throw new InvalidRssExtension("Invalid RSS extension $extension.");
}
$builder->addDefinition($this->prefix("extension.$index"))
->setType($extension);
}
}

public function beforeCompile() {
Expand Down
10 changes: 10 additions & 0 deletions src/InvalidRssExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Nexendrie\Rss;

class InvalidRssExtension extends \RuntimeException {

}

?>
12 changes: 11 additions & 1 deletion tests/Nexendrie/Rss/Bridges/NetteDI/RssExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Nexendrie\Rss\Bridges\NetteDI;

use Nexendrie\Rss\Extensions\TestExtension;
use Nexendrie\Rss\InvalidRssExtension;
use Tester\Assert;
use Nexendrie\Rss\Generator;

Expand Down Expand Up @@ -58,7 +59,16 @@ final class RssExtensionTest extends \Tester\TestCase {
}

public function testExtensions() {
$this->refreshContainer(["services" => [TestExtension::class]]);
Assert::exception(function() {
$this->refreshContainer(["rss" => [
"extensions" => [
\stdClass::class,
],
]]);
}, InvalidRssExtension::class);
$this->refreshContainer(["rss" => [
"extensions" => [TestExtension::class],
]]);
/** @var Generator $generator */
$generator = $this->getService(Generator::class);
Assert::count(1, $generator->extensions->getItems(["%class%" => TestExtension::class]));
Expand Down

0 comments on commit 529bcca

Please sign in to comment.