1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Rss\Bridges\NetteDI; |
5
|
|
|
|
6
|
|
|
use Nette\DI\CompilerExtension; |
7
|
|
|
use Nette\DI\Definitions\ServiceDefinition; |
8
|
|
|
use Nexendrie\Rss\Generator; |
9
|
|
|
use Nette\Schema\Expect; |
10
|
|
|
use Nexendrie\Rss\InvalidRssExtension; |
11
|
|
|
use Nexendrie\Rss\IRssExtension; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RssExtension for Nette DI Container |
15
|
|
|
* |
16
|
|
|
* @author Jakub Konečný |
17
|
|
|
*/ |
18
|
1 |
|
final class RssExtension extends CompilerExtension { |
19
|
|
|
/** @internal */ |
20
|
|
|
public const SERVICE_GENERATOR = "generator"; |
21
|
|
|
|
22
|
|
|
protected function setProperty(ServiceDefinition &$generator, \stdClass $config, string $property): void { |
23
|
1 |
|
if($config->$property !== "") { |
24
|
1 |
|
$generator->addSetup('$service->' . $property . " = ?", [$config->$property]); |
25
|
|
|
} |
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
public function getConfigSchema(): \Nette\Schema\Schema { |
29
|
1 |
|
return Expect::structure([ |
30
|
1 |
|
"shortenDescription" => Expect::int(150), |
31
|
1 |
|
"dateTimeFormat" => Expect::string(""), |
32
|
1 |
|
"template" => Expect::string(""), |
33
|
1 |
|
"extensions" => Expect::arrayOf("class")->default([]), |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function loadConfiguration(): void { |
38
|
|
|
/** @var \stdClass $config */ |
39
|
1 |
|
$config = $this->getConfig(); |
40
|
1 |
|
$builder = $this->getContainerBuilder(); |
41
|
1 |
|
$generator = $builder->addDefinition($this->prefix(static::SERVICE_GENERATOR)) |
42
|
1 |
|
->setType(Generator::class) |
43
|
1 |
|
->addSetup('$service->shortenDescription = ?', [$config->shortenDescription]); |
44
|
1 |
|
$this->setProperty($generator, $config, "dateTimeFormat"); |
45
|
1 |
|
$this->setProperty($generator, $config, "template"); |
46
|
|
|
/** @var string $extension */ |
47
|
1 |
|
foreach($config->extensions as $index => $extension) { |
48
|
1 |
|
if(!class_exists($extension) || !is_subclass_of($extension, IRssExtension::class)) { |
49
|
1 |
|
throw new InvalidRssExtension("Invalid RSS extension $extension."); |
50
|
|
|
} |
51
|
1 |
|
$builder->addDefinition($this->prefix("extension.$index")) |
52
|
1 |
|
->setType($extension); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
public function beforeCompile() { |
57
|
1 |
|
$builder = $this->getContainerBuilder(); |
58
|
|
|
/** @var ServiceDefinition $generator */ |
59
|
1 |
|
$generator = $builder->getDefinition($this->prefix(static::SERVICE_GENERATOR)); |
60
|
1 |
|
$extensions = $builder->findByType(IRssExtension::class); |
61
|
1 |
|
foreach($extensions as $extension) { |
62
|
1 |
|
$generator->addSetup('$service->extensions[] = ?', [$extension]); |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
} |
66
|
|
|
?> |