1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RM\SMSender\DI; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use Nette\Configurator; |
7
|
|
|
use Nette\DI\Compiler; |
8
|
|
|
use Nette\DI\CompilerExtension; |
9
|
|
|
use Nette\DI\Helpers; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Nette DI extension for SMSender. |
13
|
|
|
*/ |
14
|
1 |
|
class SMSenderExtension extends CompilerExtension |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
public $defaults = [ |
18
|
|
|
'config' => [], |
19
|
|
|
'senderClass' => 'RM\SMSender\EuroSms\Sender', |
20
|
|
|
'setDebugMode' => FALSE, |
21
|
|
|
'messageClass' => 'RM\SMSender\EuroSms\Message', |
22
|
|
|
'messageFactoryClass' => 'RM\SMSender\MessageFactory', |
23
|
|
|
'message' => [ |
24
|
|
|
'setFrom' => NULL, |
25
|
|
|
'signature' => NULL, |
26
|
|
|
], |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public function beforeCompile() |
30
|
|
|
{ |
31
|
1 |
|
$builder = $this->getContainerBuilder(); |
32
|
|
|
|
33
|
1 |
|
$config = $this->validateConfig($this->defaults); |
|
|
|
|
34
|
|
|
|
35
|
1 |
|
$sender = $builder->addDefinition($this->prefix('sender')) |
|
|
|
|
36
|
1 |
|
->setClass($config['senderClass']); |
37
|
|
|
|
38
|
1 |
|
foreach ($config as $method => $value) { |
39
|
1 |
|
$tmp = new $config['senderClass']; |
40
|
1 |
|
if ((new ReflectionClass($tmp))->hasMethod($method)) { |
41
|
1 |
|
$sender->addSetup($method, [$value]); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if ($config['message']['signature']) { |
46
|
1 |
|
$sender->addSetup('$service->onBeforeSend[] = function ($message) { $message->setText($message->getText() . ?); };', [$config['message']['signature']]); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$builder->addDefinition($this->prefix('messageFactory')) |
|
|
|
|
50
|
1 |
|
->setClass($config['messageFactoryClass']) |
51
|
1 |
|
->addSetup('$class', [$config['messageClass']]) |
52
|
1 |
|
->addSetup('$params', [$config['message']]); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Register extension to DI Container. |
57
|
|
|
* @param Configurator $config |
58
|
|
|
*/ |
59
|
|
|
public static function register(Configurator $config) |
60
|
|
|
{ |
61
|
1 |
|
$config->onCompile[] = function (Configurator $config, Compiler $compiler) { |
62
|
1 |
|
$compiler->addExtension('smsender', new SMSenderExtension()); |
63
|
1 |
|
}; |
64
|
1 |
|
} |
65
|
|
|
} |
66
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.