|
1
|
|
|
<?php |
|
2
|
|
|
namespace AcMailer\Options\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use AcMailer\Factory\AbstractAcMailerFactory; |
|
5
|
|
|
use AcMailer\Options\MailOptions; |
|
6
|
|
|
use Interop\Container\ContainerInterface; |
|
7
|
|
|
use Interop\Container\Exception\ContainerException; |
|
8
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
9
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
10
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class MailOptionsAbstractFactory |
|
14
|
|
|
* @author Alejandro Celaya Alastrué |
|
15
|
|
|
* @link http://www.alejandrocelaya.com |
|
16
|
|
|
*/ |
|
17
|
|
|
class MailOptionsAbstractFactory extends AbstractAcMailerFactory |
|
18
|
|
|
{ |
|
19
|
|
|
const SPECIFIC_PART = 'mailoptions'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create an object |
|
23
|
|
|
* |
|
24
|
|
|
* @param ContainerInterface $container |
|
25
|
|
|
* @param string $requestedName |
|
26
|
|
|
* @param null|array $options |
|
27
|
|
|
* @return object |
|
28
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
29
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
30
|
|
|
* creating a service. |
|
31
|
|
|
* @throws ContainerException if any other error occurs |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
34
|
|
|
{ |
|
35
|
6 |
|
$specificServiceName = explode('.', $requestedName)[2]; |
|
36
|
6 |
|
$config = $this->getConfig($container); |
|
37
|
6 |
|
$specificConfig = $config[$specificServiceName]; |
|
38
|
6 |
|
if (! is_array($specificConfig)) { |
|
39
|
1 |
|
$specificConfig = []; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
do { |
|
43
|
|
|
// Get extends |
|
44
|
6 |
|
$extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends']) |
|
45
|
6 |
|
? trim($specificConfig['extends']) |
|
46
|
6 |
|
: null; |
|
47
|
|
|
|
|
48
|
|
|
// Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an |
|
49
|
|
|
// exception |
|
50
|
6 |
|
unset($specificConfig['extends']); |
|
51
|
|
|
|
|
52
|
|
|
// Try to extend from another configuration if defined and exists |
|
53
|
6 |
|
if (! is_null($extendsConfigKey) |
|
54
|
6 |
|
&& array_key_exists($extendsConfigKey, $config) |
|
55
|
6 |
|
&& is_array($config[$extendsConfigKey]) |
|
56
|
6 |
|
) { |
|
57
|
3 |
|
$specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig); |
|
58
|
3 |
|
} |
|
59
|
6 |
|
} while ($extendsConfigKey != null); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
6 |
|
return new MailOptions($specificConfig); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|