|
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\InvalidRssExtensionException; |
|
11
|
|
|
use Nexendrie\Rss\IRssExtension; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* RssExtension for Nette DI Container |
|
15
|
|
|
* |
|
16
|
|
|
* @author Jakub Konečný |
|
17
|
|
|
* @method \stdClass getConfig() |
|
18
|
|
|
*/ |
|
19
|
|
|
final class RssExtension extends CompilerExtension { |
|
20
|
|
|
/** @internal */ |
|
21
|
|
|
public const SERVICE_GENERATOR = "generator"; |
|
22
|
|
|
|
|
23
|
|
|
private function setProperty(ServiceDefinition &$generator, \stdClass $config, string $property): void { |
|
24
|
1 |
|
if($config->$property !== "") { |
|
25
|
1 |
|
$generator->addSetup('$service->' . $property . " = ?", [$config->$property]); |
|
26
|
|
|
} |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getConfigSchema(): \Nette\Schema\Schema { |
|
30
|
1 |
|
return Expect::structure([ |
|
31
|
1 |
|
"shortenDescription" => Expect::int(150), |
|
32
|
1 |
|
"dateTimeFormat" => Expect::string(""), |
|
33
|
1 |
|
"template" => Expect::string(""), |
|
34
|
1 |
|
"extensions" => Expect::arrayOf("class")->default([]), |
|
35
|
|
|
]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function loadConfiguration(): void { |
|
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 InvalidRssExtensionException("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
|
|
|
?> |