Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Merge branch 'feature/124' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jul 3, 2016
2 parents 48903a2 + 81f159d commit a4697b9
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 214 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Enhancements:**

* [103: Drop support for the mail_options top-level configuration key](https://github.com/acelaya/ZF2-AcMailer/issues/103)
* [124: Update to ZF3 components](https://github.com/acelaya/ZF2-AcMailer/issues/124)

**Tasks**

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ return [
]
```

> **IMPORTANT!** Version 5.0.0 has introduced some important BC breaks, so make sure not to update from earlier versions in production without reading this documentation first.
> It is possible to autogenerate the new configuration structure from the command line. Read the configuration section at the end of this document for more information.
> **IMPORTANT! Version notes**
> * Version **6.0.0**: Support for ZF2 has been dropped and this module is now compatible with ZF3. If you need ZF2 support, stick with v5 of this module.
> * Version **5.0.0**: Important BC breaks have been introduced, so make sure not to update from earlier versions in production without reading this documentation first. It is possible to autogenerate the new configuration structure from the command line. Read the configuration section at the end of this document for more information.
### Usage

Expand Down
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
"license" : "MIT",
"require" : {
"php" : "^5.6|^7.0",
"zendframework/zend-servicemanager": "~2.2",
"zendframework/zend-mail": "~2.2",
"zendframework/zend-view": "~2.2",
"zendframework/zend-eventmanager": "~2.2",
"zendframework/zend-mime": "~2.2",
"zendframework/zend-stdlib": "~2.2",
"zendframework/zend-modulemanager": "~2.2",
"zendframework/zend-mvc": "~2.2",
"zendframework/zend-console": "~2.2",
"zendframework/zend-config": "~2.2",
"zendframework/zend-json": "~2.2"
"zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-mail": "^2.6",
"zendframework/zend-view": "^2.6",
"zendframework/zend-eventmanager": "^3.0",
"zendframework/zend-mime": "^2.6",
"zendframework/zend-stdlib": "^3.0",
"zendframework/zend-console": "^2.6",
"zendframework/zend-modulemanager": "^2.6",
"zendframework/zend-mvc": "^3.0",
"zendframework/zend-mvc-console": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~2.0"
"squizlabs/php_codesniffer": "~2.0",
"zendframework/zend-json": "^3.0"
},
"autoload" : {
"psr-4" : {
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/ConfigMigrationController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace AcMailer\Controller;

use AcMailer\Exception\InvalidArgumentException;
use AcMailer\Service\ConfigMigrationServiceInterface;
use Zend\Config\Writer\WriterInterface;
use Zend\Console\ColorInterface;
use Zend\Console\Request;
use Zend\Mvc\Controller\AbstractConsoleController;
use Zend\Config\Writer\WriterInterface;
use AcMailer\Exception\InvalidArgumentException;
use Zend\Mvc\Console\Controller\AbstractConsoleController;

/**
* Class ConfigMigrationController
Expand Down
27 changes: 18 additions & 9 deletions src/Controller/Factory/ConfigMigrationControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
namespace AcMailer\Controller\Factory;

use AcMailer\Controller\ConfigMigrationController;
use AcMailer\Service\ConfigMigrationService;
use AcMailer\Service\ConfigMigrationServiceInterface;
use Zend\Mvc\Controller\ControllerManager;
use Zend\ServiceManager\FactoryInterface;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
Expand All @@ -15,18 +19,23 @@
class ConfigMigrationControllerFactory implements FactoryInterface
{
/**
* Create service
* Create an object
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** @var ControllerManager $serviceLocator */
/** @var array $config */
$config = $serviceLocator->getServiceLocator()->get('config');
$config = $container->get('config');
/** @var ConfigMigrationServiceInterface $configMigrationService */
$configMigrationService = $serviceLocator->getServiceLocator()->get('AcMailer\Service\ConfigMigrationService');
$configMigrationService = $container->get(ConfigMigrationService::class);

return new ConfigMigrationController($configMigrationService, $config);
}
Expand Down
51 changes: 37 additions & 14 deletions src/Controller/Plugin/Factory/SendMailPluginAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
use AcMailer\Factory\AbstractAcMailerFactory;
use AcMailer\Service\Factory\MailServiceAbstractFactory;
use AcMailer\Service\MailServiceInterface;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\Filter\Word\CamelCaseToUnderscore;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
use Zend\Stdlib\StringUtils;

/**
* Class SendMailPluginAbstractFactory
Expand All @@ -19,12 +24,11 @@ class SendMailPluginAbstractFactory extends AbstractAcMailerFactory
/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param ContainerInterface $container
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName)
{
/** @var ControllerPluginManager $serviceLocator */
if (strpos($requestedName, 'sendMail') !== 0) {
Expand All @@ -36,23 +40,26 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
}

$specificServiceName = $this->getSpecificServiceName($requestedName);
return array_key_exists($specificServiceName, $this->getConfig($serviceLocator->getServiceLocator()));
return array_key_exists($specificServiceName, $this->getConfig($container));
}

/**
* Create service with name
* Create an object
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** @var ControllerPluginManager $serviceLocator */
$specificServiceName = $this->getSpecificServiceName($requestedName);
/** @var MailServiceInterface $mailService */
$mailService = $serviceLocator->getServiceLocator()->get(
$mailService = $container->get(
sprintf('%s.%s.%s', self::ACMAILER_PART, MailServiceAbstractFactory::SPECIFIC_PART, $specificServiceName)
);
return new SendMailPlugin($mailService);
Expand All @@ -68,8 +75,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
*/
protected function getSpecificServiceName($requestedName)
{
$filter = new CamelCaseToUnderscore();
$parts = explode('_', $filter->filter($requestedName));
$parts = explode('_', $this->camelCaseToUnderscore($requestedName));
if (count($parts) === 2) {
return 'default';
}
Expand All @@ -84,4 +90,21 @@ protected function getSpecificServiceName($requestedName)
// Convert from camelcase to underscores and set to lower
return strtolower($specificServiceName);
}

protected function camelCaseToUnderscore($value)
{
if (!is_scalar($value) && !is_array($value)) {
return $value;
}

if (StringUtils::hasPcreUnicodeSupport()) {
$pattern = ['#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#', '#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#'];
$replacement = ['_\1', '_\1'];
} else {
$pattern = ['#(?<=(?:[A-Z]))([A-Z]+)([A-Z][a-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#'];
$replacement = ['\1_\2', '_\1'];
}

return preg_replace($pattern, $replacement, $value);
}
}
24 changes: 13 additions & 11 deletions src/Factory/AbstractAcMailerFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace AcMailer\Factory;

use Zend\ServiceManager\AbstractFactoryInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
Expand All @@ -15,16 +18,15 @@ abstract class AbstractAcMailerFactory implements AbstractFactoryInterface
const SPECIFIC_PART = '';

/**
* Determine if we can create a service with name
* Can the factory create an instance for the service?
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @param ContainerInterface $container
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName)
{
$parts = explode('.', $name);
$parts = explode('.', $requestedName);
if (count($parts) !== 3) {
return false;
}
Expand All @@ -34,17 +36,17 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
}

$specificServiceName = $parts[2];
$config = $this->getConfig($serviceLocator);
$config = $this->getConfig($container);
return array_key_exists($specificServiceName, $config);
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $container
* @return array
*/
protected function getConfig(ServiceLocatorInterface $serviceLocator)
protected function getConfig(ContainerInterface $container)
{
$config = $serviceLocator->get('Config');
$config = $container->get('Config');
if (isset($config['acmailer_options']) && is_array($config['acmailer_options'])) {
return $config['acmailer_options'];
}
Expand Down
25 changes: 16 additions & 9 deletions src/Options/Factory/MailOptionsAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

use AcMailer\Factory\AbstractAcMailerFactory;
use AcMailer\Options\MailOptions;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\Stdlib\ArrayUtils;

/**
Expand All @@ -16,17 +19,21 @@ class MailOptionsAbstractFactory extends AbstractAcMailerFactory
const SPECIFIC_PART = 'mailoptions';

/**
* Create service with name
* Create an object
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$specificServiceName = explode('.', $name)[2];
$config = $this->getConfig($serviceLocator);
$specificServiceName = explode('.', $requestedName)[2];
$config = $this->getConfig($container);
$specificConfig = $config[$specificServiceName];
if (! is_array($specificConfig)) {
$specificConfig = [];
Expand Down
Loading

0 comments on commit a4697b9

Please sign in to comment.