|
1
|
|
|
<?php |
|
2
|
|
|
namespace AcMailer\Controller\Plugin\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use AcMailer\Controller\Plugin\SendMailPlugin; |
|
5
|
|
|
use AcMailer\Factory\AbstractAcMailerFactory; |
|
6
|
|
|
use AcMailer\Service\Factory\MailServiceAbstractFactory; |
|
7
|
|
|
use AcMailer\Service\MailServiceInterface; |
|
8
|
|
|
use Interop\Container\ContainerInterface; |
|
9
|
|
|
use Interop\Container\Exception\ContainerException; |
|
10
|
|
|
use Zend\Filter\Word\CamelCaseToUnderscore; |
|
11
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
12
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
|
13
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
14
|
|
|
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager; |
|
15
|
|
|
use Zend\Stdlib\StringUtils; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class SendMailPluginAbstractFactory |
|
19
|
|
|
* @author Alejandro Celaya Alastrué |
|
20
|
|
|
* @link http://www.alejandrocelaya.com |
|
21
|
|
|
*/ |
|
22
|
|
|
class SendMailPluginAbstractFactory extends AbstractAcMailerFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Determine if we can create a service with name |
|
26
|
|
|
* |
|
27
|
|
|
* @param ContainerInterface $container |
|
28
|
|
|
* @param $requestedName |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function canCreate(ContainerInterface $container, $requestedName) |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var ControllerPluginManager $serviceLocator */ |
|
34
|
3 |
|
if (strpos($requestedName, 'sendMail') !== 0) { |
|
35
|
1 |
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
if ($requestedName === 'sendMail') { |
|
39
|
1 |
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$specificServiceName = $this->getSpecificServiceName($requestedName); |
|
43
|
1 |
|
return array_key_exists($specificServiceName, $this->getConfig($container)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create an object |
|
48
|
|
|
* |
|
49
|
|
|
* @param ContainerInterface $container |
|
50
|
|
|
* @param string $requestedName |
|
51
|
|
|
* @param null|array $options |
|
52
|
|
|
* @return object |
|
53
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
54
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
55
|
|
|
* creating a service. |
|
56
|
|
|
* @throws ContainerException if any other error occurs |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$specificServiceName = $this->getSpecificServiceName($requestedName); |
|
61
|
|
|
/** @var MailServiceInterface $mailService */ |
|
62
|
1 |
|
$mailService = $container->get( |
|
63
|
1 |
|
sprintf('%s.%s.%s', self::ACMAILER_PART, MailServiceAbstractFactory::SPECIFIC_PART, $specificServiceName) |
|
64
|
|
|
); |
|
65
|
1 |
|
return new SendMailPlugin($mailService); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Fetches a mail service name from the requested plugin name. |
|
70
|
|
|
* sendMailCustomers -> customers |
|
71
|
|
|
* sendMail -> default |
|
72
|
|
|
* |
|
73
|
|
|
* @param $requestedName |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
2 |
|
protected function getSpecificServiceName($requestedName) |
|
77
|
|
|
{ |
|
78
|
2 |
|
$parts = explode('_', $this->camelCaseToUnderscore($requestedName)); |
|
79
|
2 |
|
if (count($parts) === 2) { |
|
80
|
|
|
return 'default'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Discard the sendMail part |
|
84
|
2 |
|
$parts = array_slice($parts, 2); |
|
85
|
2 |
|
$specificServiceName = ''; |
|
86
|
2 |
|
foreach ($parts as $part) { |
|
87
|
2 |
|
$specificServiceName .= $part; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Convert from camelcase to underscores and set to lower |
|
91
|
2 |
|
return strtolower($specificServiceName); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
protected function camelCaseToUnderscore($value) |
|
95
|
|
|
{ |
|
96
|
2 |
|
if (!is_scalar($value) && !is_array($value)) { |
|
97
|
|
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
if (StringUtils::hasPcreUnicodeSupport()) { |
|
101
|
2 |
|
$pattern = ['#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#', '#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#']; |
|
102
|
2 |
|
$replacement = ['_\1', '_\1']; |
|
103
|
|
|
} else { |
|
104
|
|
|
$pattern = ['#(?<=(?:[A-Z]))([A-Z]+)([A-Z][a-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#']; |
|
105
|
|
|
$replacement = ['\1_\2', '_\1']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
return preg_replace($pattern, $replacement, $value); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|