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
|
7 |
|
* @param null|array $options |
27
|
|
|
* @return object |
28
|
7 |
|
* @throws ServiceNotFoundException if unable to resolve the service. |
29
|
7 |
|
* @throws ServiceNotCreatedException if an exception is raised when |
30
|
7 |
|
* creating a service. |
31
|
7 |
|
* @throws ContainerException if any other error occurs |
32
|
1 |
|
*/ |
33
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
34
|
|
|
{ |
35
|
|
|
$specificServiceName = explode('.', $requestedName)[2]; |
36
|
|
|
$config = $this->getConfig($container); |
37
|
7 |
|
$specificConfig = $config[$specificServiceName]; |
38
|
3 |
|
if (! is_array($specificConfig)) { |
39
|
7 |
|
$specificConfig = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
do { |
43
|
7 |
|
// Get extends |
44
|
|
|
$extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends']) |
45
|
|
|
? trim($specificConfig['extends']) |
46
|
7 |
|
: null; |
47
|
7 |
|
|
48
|
7 |
|
// Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an |
49
|
|
|
// exception |
50
|
3 |
|
unset($specificConfig['extends']); |
51
|
|
|
|
52
|
7 |
|
// Try to extend from another configuration if defined and exists |
53
|
|
|
if (! is_null($extendsConfigKey) |
54
|
7 |
|
&& array_key_exists($extendsConfigKey, $config) |
55
|
|
|
&& is_array($config[$extendsConfigKey]) |
56
|
|
|
) { |
57
|
|
|
$specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig); |
58
|
|
|
} |
59
|
|
|
} while ($extendsConfigKey != null); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return new MailOptions($specificConfig); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|