1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GravatarExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Gravatar! |
9
|
|
|
* @subpackage DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 05.04.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Gravatar\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\Bridges; |
21
|
|
|
use Nette\DI; |
22
|
|
|
|
23
|
|
|
use IPub\Gravatar; |
24
|
|
|
use IPub\Gravatar\Caching; |
25
|
|
|
use IPub\Gravatar\Templating; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Gravatar extension container |
29
|
|
|
* |
30
|
|
|
* @package iPublikuj:Gravatar! |
31
|
|
|
* @subpackage DI |
32
|
|
|
* |
33
|
|
|
* @author Adam Kadlec <[email protected]> |
34
|
|
|
*/ |
35
|
1 |
|
final class GravatarExtension extends DI\CompilerExtension |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $defaults = [ |
41
|
|
|
'expiration' => 172800, |
42
|
|
|
'size' => 80, |
43
|
|
|
'defaultImage' => FALSE |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function loadConfiguration() : void |
50
|
|
|
{ |
51
|
|
|
// Get container builder |
52
|
1 |
|
$builder = $this->getContainerBuilder(); |
53
|
|
|
// Get extension configuration |
54
|
1 |
|
$configuration = $this->getConfig($this->defaults); |
55
|
|
|
|
56
|
|
|
// Install Gravatar service |
57
|
1 |
|
$builder->addDefinition($this->prefix('gravatar')) |
58
|
1 |
|
->setType(Gravatar\Gravatar::class) |
59
|
1 |
|
->addSetup('setSize', [$configuration['size']]) |
60
|
1 |
|
->addSetup('setExpiration', [$configuration['expiration']]) |
61
|
1 |
|
->addSetup('setDefaultImage', [$configuration['defaultImage']]); |
62
|
|
|
|
63
|
|
|
// Create cache services |
64
|
1 |
|
$builder->addDefinition($this->prefix('cache')) |
65
|
1 |
|
->setType(Caching\Cache::class) |
66
|
1 |
|
->setArguments(['@cacheStorage', 'IPub.Gravatar']) |
67
|
1 |
|
->setInject(FALSE); |
68
|
|
|
|
69
|
|
|
// Register template helpers |
70
|
1 |
|
$builder->addDefinition($this->prefix('helpers')) |
71
|
1 |
|
->setType(Templating\Helpers::class) |
72
|
1 |
|
->setFactory($this->prefix('@gravatar') . '::createTemplateHelpers') |
73
|
1 |
|
->setInject(FALSE); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function beforeCompile() : void |
80
|
|
|
{ |
81
|
1 |
|
parent::beforeCompile(); |
82
|
|
|
|
83
|
|
|
// Get container builder |
84
|
1 |
|
$builder = $this->getContainerBuilder(); |
85
|
|
|
|
86
|
|
|
// Install extension latte macros |
87
|
1 |
|
$latteFactory = $builder->getDefinition($builder->getByType(Bridges\ApplicationLatte\ILatteFactory::class) ?: 'nette.latteFactory'); |
88
|
|
|
|
89
|
|
|
$latteFactory |
90
|
1 |
|
->addSetup('IPub\Gravatar\Latte\Macros::install(?->getCompiler())', ['@self']) |
91
|
1 |
|
->addSetup('addFilter', ['gravatar', [$this->prefix('@helpers'), 'gravatar']]) |
92
|
1 |
|
->addSetup('addFilter', ['getGravatarService', [$this->prefix('@helpers'), 'getGravatarService']]); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Nette\Configurator $config |
97
|
|
|
* @param string $extensionName |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'gravatar') : void |
102
|
|
|
{ |
103
|
1 |
|
$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) : void { |
104
|
1 |
|
$compiler->addExtension($extensionName, new GravatarExtension()); |
105
|
1 |
|
}; |
106
|
1 |
|
} |
107
|
|
|
} |
108
|
|
|
|