1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hautzi\SystemMailBundle\SystemMailer\MailDefinition; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
6
|
|
|
|
7
|
|
|
class MailDefinitionProviderSymfony implements ProviderInterface |
8
|
|
|
{ |
9
|
|
|
protected $kernel; |
10
|
|
|
protected $twig; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param KernelInterface $kernel |
14
|
|
|
* @param \Twig_Environment $twig |
15
|
|
|
*/ |
16
|
|
|
public function __construct(KernelInterface $kernel, \Twig_Environment $twig) |
17
|
|
|
{ |
18
|
|
|
$this->kernel = $kernel; |
19
|
|
|
$this->twig = $twig; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Fetches the definition of a system mail as a string |
24
|
|
|
* |
25
|
|
|
* Here we put in something like "App:your-system-mail" and this class will look up in |
26
|
|
|
* "AppBundle/Resources/emails/your-system-email.xml.twig". Take care not to provide the "Bundle" suffix for the |
27
|
|
|
* name parameter as it is added somewhere behind this lines of this method... |
28
|
|
|
* |
29
|
|
|
* Everything will be parsed by twig which adds immense superpowers to the configuration. You can do everything |
30
|
|
|
* you can imagine. |
31
|
|
|
* |
32
|
|
|
* @param string $name |
33
|
|
|
* @param array $parameters |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function fetchMailDefinition($name, $parameters = []) |
38
|
|
|
{ |
39
|
|
|
$resource = $this->locateResource($name); |
40
|
|
|
|
41
|
|
|
return $this->twig->render($resource, $parameters); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $name |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
* |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
protected function locateResource($name) |
52
|
|
|
{ |
53
|
|
|
// check format Bundle:emailXmlTemplateName |
54
|
|
|
if (!preg_match('@^([^:]+):([^:]+)$@', $name, $parts)) { |
55
|
|
|
$message = sprintf('"%s" should look like YourBundle:emailXmlTemplateName', $name); |
56
|
|
|
throw new \InvalidArgumentException($message); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$locator = sprintf('@%sBundle/Resources/emails/%s.xml.twig', $parts[1], $parts[2]); |
60
|
|
|
$resource = $this->kernel->locateResource($locator); |
61
|
|
|
|
62
|
|
|
return $resource; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|