Skip to content

Commit

Permalink
Merge 8d9f3c0 into 77b4714
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromdev committed Mar 5, 2017
2 parents 77b4714 + 8d9f3c0 commit 75b8db9
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 77 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"type": "library",
"require": {
"elasticsearch/elasticsearch": "^2.3",
"zendframework/zend-servicemanager": "^2.7",
"zendframework/zend-servicemanager": "^3.1.1",
"zendframework/zend-modulemanager": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"zendframework/zend-stdlib": "^2.7",
"phpunit/phpunit": "^4.8 || ^5.0",
"zendframework/zend-stdlib": "^3.0.1",
"satooshi/php-coveralls": "~1.0"
},
"license": "Apache-2.0",
Expand Down
33 changes: 22 additions & 11 deletions src/Service/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace ElasticsearchModule\Service;

use ArrayObject;
use Zend\Stdlib\ArrayObject as ZendArrayObject;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\ArrayObject as ZendArrayObject;

/**
* @author Pedro Alves <pedro.m.develop@gmail.com>
Expand All @@ -33,10 +34,10 @@ public function __construct($name)
abstract public function getServiceType();

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $container
* @param array|ArrayObject|ZendArrayObject $config
*/
abstract protected function create(ServiceLocatorInterface $serviceLocator, $config);
abstract protected function create(ContainerInterface $container, $config);

/**
* @param ServiceLocatorInterface $serviceLocator
Expand All @@ -45,25 +46,35 @@ abstract protected function create(ServiceLocatorInterface $serviceLocator, $con
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');
return $this($serviceLocator, sprintf('elasticsearch.%s.%s', $this->getServiceType(), $this->name));
}

/**
* @param ContainerInterface $container
* @return mixed
* @throws ServiceNotCreatedException
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('Config');
$serviceType = $this->getServiceType();

if (!isset($config['elasticsearch'][$serviceType][$this->name])) {
throw new ServiceNotCreatedException("elasticserach.$serviceType.{$this->name} could not be found");
throw new ServiceNotCreatedException("$requestedName could not be found");
}

return $this->create($serviceLocator, $config['elasticsearch'][$serviceType][$this->name]);
return $this->create($container, $config['elasticsearch'][$serviceType][$this->name]);
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $container
* @param string $serviceOrClass
* @return mixed
*/
protected function getServiceOrClassObject(ServiceLocatorInterface $serviceLocator, $serviceOrClass)
protected function getServiceOrClassObject(ContainerInterface $container, $serviceOrClass)
{
if ($serviceLocator->has($serviceOrClass)) {
return $serviceLocator->get($serviceOrClass);
if ($container->has($serviceOrClass)) {
return $container->get($serviceOrClass);
} else if (class_exists($serviceOrClass)) {
return new $serviceOrClass();
}
Expand Down
12 changes: 6 additions & 6 deletions src/Service/AbstractLogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ElasticsearchModule\Service;

use Interop\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @author Pedro Alves <pedro.m.develop@gmail.com>
Expand All @@ -15,10 +15,10 @@ abstract class AbstractLogFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
$logName = $config[$this->getKey()];
return $this->getLoggerOrThrowException($serviceLocator, $logName);
return $this->getLoggerOrThrowException($container, $logName);
}

/**
Expand All @@ -30,14 +30,14 @@ public function getServiceType()
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $container
* @param string $logName
* @return LoggerInterface
* @throws ServiceNotCreatedException
*/
private function getLoggerOrThrowException(ServiceLocatorInterface $serviceLocator, $logName)
private function getLoggerOrThrowException(ContainerInterface $container, $logName)
{
$logger = $this->getServiceOrClassObject($serviceLocator, $logName);
$logger = $this->getServiceOrClassObject($container, $logName);

if (!$logger instanceof LoggerInterface) {
throw new ServiceNotCreatedException("There is no '$logName' log");
Expand Down
12 changes: 8 additions & 4 deletions src/Service/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Elasticsearch\Client;
use Elasticsearch\Transport;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @author Pedro Alves <pedro.m.develop@gmail.com>
Expand All @@ -17,14 +17,18 @@ class ClientFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
$transport = $serviceLocator->get($config['transport']);
$transport = $container->get($config['transport']);

if (!$transport instanceof Transport) {
throw $this->getException('transport', Transport::class, ServiceNotCreatedException::class, $transport);
}
$endpoint = $serviceLocator->get($config['endpoint']);
$endpoint = $container->get($config['endpoint']);

if (!is_callable($endpoint)) {
throw $this->getException('endpoint', 'callable', ServiceNotCreatedException::class, $endpoint);
}
return new Client($transport, $endpoint);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Service/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use ArrayObject;
use Elasticsearch\Connections\ConnectionFactoryInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\ArrayObject as ZendArrayObject;

/**
Expand All @@ -17,12 +17,12 @@ class ConnectionFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
$factoryName = $config['factory'];
$handler = $serviceLocator->get($config['handler']);
$loggers = $serviceLocator->get($config['loggers']);
$serializer = $this->getServiceOrClassObject($serviceLocator, $config['serializer']);
$handler = $container->get($config['handler']);
$loggers = $container->get($config['loggers']);
$serializer = $this->getServiceOrClassObject($container, $config['serializer']);
$params = $this->getConnectionParametersFromConfiguration($config);
$connectionFactory = new $factoryName($handler, $params, $serializer, $loggers['logger'], $loggers['tracer']);

Expand Down
8 changes: 4 additions & 4 deletions src/Service/ConnectionPoolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
use Elasticsearch\Connections\ConnectionFactoryInterface;
use Elasticsearch\Connections\ConnectionInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\InvalidArgumentException;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\ArrayObject as ZendArrayObject;

/**
Expand All @@ -21,7 +21,7 @@ class ConnectionPoolFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
$className = $config['class'];

Expand All @@ -32,8 +32,8 @@ protected function create(ServiceLocatorInterface $serviceLocator, $config)
AbstractConnectionPool::class
));
}
$selector = $this->getServiceOrClassObject($serviceLocator, $config['selector']);
$connectionFactory = $this->getServiceOrClassObject($serviceLocator, $config['connection_factory']);
$selector = $this->getServiceOrClassObject($container, $config['selector']);
$connectionFactory = $this->getServiceOrClassObject($container, $config['connection_factory']);

if (!$connectionFactory instanceof ConnectionFactoryInterface) {
throw $this->getException(
Expand Down
18 changes: 9 additions & 9 deletions src/Service/EndpointFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Elasticsearch\Serializers\SerializerInterface;
use ElasticsearchModule\Container\EndpointsInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @author Pedro Alves <pedro.m.develop@gmail.com>
Expand All @@ -17,9 +17,9 @@ class EndpointFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
$serializer = $this->getServiceOrClassObject($serviceLocator, $config['serializer']);
$serializer = $this->getServiceOrClassObject($container, $config['serializer']);

if (!$serializer instanceof SerializerInterface) {
throw $this->getException(
Expand All @@ -29,20 +29,20 @@ protected function create(ServiceLocatorInterface $serviceLocator, $config)
$serializer
);
}
$transport = $serviceLocator->get($config['transport']);
$container = $config['container'];
$container = new $container($transport, $serializer);
$transport = $container->get($config['transport']);
$endpointContainer = $config['container'];
$endpointContainer = new $endpointContainer($transport, $serializer);

if (!$container instanceof EndpointsInterface) {
if (!$endpointContainer instanceof EndpointsInterface) {
throw $this->getException(
'container',
EndpointsInterface::class,
ServiceNotCreatedException::class,
$container
$endpointContainer
);
}

return $container;
return $endpointContainer;
}

/**
Expand Down
21 changes: 12 additions & 9 deletions src/Service/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use ArrayObject;
use Elasticsearch\ClientBuilder;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface as ContainerInterface2;
use Zend\Stdlib\ArrayObject as ZendArrayObject;

/**
Expand All @@ -16,11 +17,11 @@ class HandlerFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
return ClientBuilder::defaultHandler(
$this->getHandlerParams($serviceLocator, $config, 'multi_handler', 'handle_factory'),
$this->getHandlerParams($serviceLocator, $config, 'single_handler', 'factory')
$this->getHandlerParams($container, $config, 'multi_handler', 'handle_factory'),
$this->getHandlerParams($container, $config, 'single_handler', 'factory')
);
}

Expand All @@ -33,33 +34,35 @@ public function getServiceType()
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface2 $container
* @param array|ArrayObject|ZendArrayObject $handlerConfig
* @param string $paramKey
* @param string $factoryKey
* @return array
*/
private function getHandlerParams(ServiceLocatorInterface $serviceLocator, $handlerConfig, $paramKey, $factoryKey)
private function getHandlerParams(ContainerInterface $container, $handlerConfig, $paramKey, $factoryKey)
{
if (!isset($handlerConfig['params'][$paramKey])) {
return [];
}
return $this->createFactoryInConfig(
$serviceLocator,
$container,
$handlerConfig['params'][$paramKey],
$factoryKey
);
}

/**
* @param ContainerInterface $container
* @param array $handlerParams
* @param string $factoryKey
* @return array
*/
private function createFactoryInConfig(ServiceLocatorInterface $serviceLocator, $handlerParams, $factoryKey)
private function createFactoryInConfig(ContainerInterface $container, $handlerParams, $factoryKey)
{
if ($this->hasHandlerFactoryOption($handlerParams, $factoryKey)) {
$serviceOrClass = $handlerParams[$factoryKey];
$handlerParams[$factoryKey] = $this->getServiceOrClassObject($serviceLocator, $serviceOrClass);
$handlerParams[$factoryKey] = $this->getServiceOrClassObject($container, $serviceOrClass);

if (!is_callable($handlerParams[$factoryKey])) {
unset($handlerParams[$factoryKey]);
Expand Down
10 changes: 6 additions & 4 deletions src/Service/InvalidTypeExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ trait InvalidTypeExceptionTrait
{
/**
* @param string $name
* @param string $className
* @param string $classNameOrType
* @param string $exceptionClass
* @param mixed $var
* @return Exception
*/
private function getException($name, $className, $exceptionClass, $var)
private function getException($name, $classNameOrType, $exceptionClass, $var)
{
$message = (class_exists($classNameOrType) || interface_exists($classNameOrType)) ?
"instance of %s" : "%s";
return new $exceptionClass(sprintf(
"The %s must be instance of %s, %s given",
"The %s must be $message, %s given",
$name,
$className,
$classNameOrType,
is_object($var) ? get_class($var) : gettype($var)
));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Service/LoggersFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ElasticsearchModule\Service;

use ArrayObject;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;

/**
* @author Pedro Alves <pedro.m.develop@gmail.com>
Expand All @@ -14,15 +14,15 @@ class LoggersFactory extends AbstractFactory
/**
* {@inheritDoc}
*/
protected function create(ServiceLocatorInterface $serviceLocator, $config)
protected function create(ContainerInterface $container, $config)
{
$factories = $config['factories'];
$loggers = [];

foreach ($factories as $name => $factory) {
if (in_array(AbstractFactory::class, class_parents($factory))) {
$factory = new $factory($this->getName());
$loggers[$name] = $factory->create($serviceLocator, $config);
$loggers[$name] = $factory->create($container, $config);
}
}
return new ArrayObject($loggers);
Expand Down

0 comments on commit 75b8db9

Please sign in to comment.