Skip to content

Commit

Permalink
Merge branch 'kilip-gh-338' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cbleek committed Mar 15, 2017
2 parents 38ac6c6 + e968ace commit 12d22a6
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,7 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$config = $this->getConfig($serviceLocator, $requestedName);
$events = $this->createEventManager($serviceLocator, $config);

$this->attachListeners($serviceLocator, $events, $config['listeners']);

return $events;
return $this($serviceLocator,$requestedName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/** */
namespace Core\Factory\Paginator;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down Expand Up @@ -47,6 +48,7 @@
* </pre>
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @author Anthonius Munthi <me@itstoni.com>
* @since 0.24
*/
class RepositoryAbstractFactory implements AbstractFactoryInterface, MutableCreationOptionsInterface
Expand All @@ -58,35 +60,21 @@ class RepositoryAbstractFactory implements AbstractFactoryInterface, MutableCrea
*/
protected $options = [];

public function setCreationOptions(array $options)
{
$this->options = $options;
}


public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$class = $this->getEntityClassName($requestedName);

return class_exists($class, true);
}

/**
* Create a paginator instance with name
* Create a new Paginator instance
*
* @param ServiceLocatorInterface $serviceLocator
* @param String $name
* @param String $requestedName
*
* @return \Zend\Paginator\Paginator
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return \Zend\Paginator\Paginator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/* @var $serviceLocator \Core\Paginator\PaginatorService
/**
* @var $repositories \Core\Repository\RepositoryService
* @var $filter \Zend\Filter\FilterInterface
*/
$services = $serviceLocator->getServiceLocator();
$services = $container->getServiceLocator();
$repositories = $services->get('repositories');
$queryBuilder = $repositories->createQueryBuilder();

Expand All @@ -112,6 +100,54 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
return $paginator;
}

/**
* Can the factory create an instance for the given $requestedName service
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return bool
*/
public function canCreate(ContainerInterface $container, $requestedName, array $options=null)
{
$class = $this->getEntityClassName($requestedName);

return class_exists($class, true);
}

public function setCreationOptions(array $options)
{
$this->options = $options;
}

/**
* Determines if we can create a paginator instance with given $requestedName
*
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
*
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->canCreate($serviceLocator,$requestedName);
}

/**
* Create a paginator instance with given $requestedName service
*
* @param ServiceLocatorInterface $serviceLocator
* @param String $name
* @param String $requestedName
*
* @return \Zend\Paginator\Paginator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this($serviceLocator,$requestedName);
}

/**
* Gets an entity class name from the requested service name.
*
Expand Down
57 changes: 44 additions & 13 deletions module/Core/src/Core/Log/LoggerAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/** LoggerAbstractFactory.php */
namespace Core\Log;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Log\Logger;
Expand All @@ -36,14 +37,36 @@ class LoggerAbstractFactory implements AbstractFactoryInterface
protected $configKey = 'log';

/**
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* Create a new Logger instance
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return Logger
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $this->getConfig($container);
$config = $config[$requestedName];
if (is_string($config) || isset($config['service'])) {
$serviceName = is_string($config) ? $config : $config['service'];
return $container->get($serviceName);
}
$this->processConfig($config, $container);
return new Logger($config);
}

/**
* Check if the factory can create an instance for the given $requestedName service
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array|null $options
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $this->getConfig($services);
$config = $this->getConfig($container);
if (empty($config)) {
return false;
}
Expand All @@ -52,21 +75,29 @@ public function canCreateServiceWithName(ServiceLocatorInterface $services, $nam
}

/**
* Determines if we can create a Logger instance with give $requestedName
*
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
return $this->canCreate($services,$requestedName);
}

/**
* Create a Logger instance with given $requestedName service
*
* @param ServiceLocatorInterface $services
* @param string $name
* @param string $requestedName
* @return Logger
*/
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
{
$config = $this->getConfig($services);
$config = $config[$requestedName];
if (is_string($config) || isset($config['service'])) {
$serviceName = is_string($config) ? $config : $config['service'];
return $services->get($serviceName);
}
$this->processConfig($config, $services);
return new Logger($config);
return $this($services,$requestedName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ public function testDeterminesIfItCanCreateAnEventManagerByName()
{
$services = new ServiceManager();
$this->assertTrue(
$this->target->canCreateServiceWithName(
$services, 'irrelevant',
'Any.string/Value/Events'
$this->target->canCreate(
$services,'Any.string/Value/Events'
),
'Checking correct name failed.'
);
$this->assertFalse(
$this->target->canCreateServiceWithName(
$services, 'irrelevant',
'Any.string.not.ending/in/Events.but has it in the middle!'
$this->target->canCreate(
$services,'Any.string.not.ending/in/Events.but has it in the middle!'
),
'Checking invalid name failed.'
);
Expand Down Expand Up @@ -107,6 +105,6 @@ public function testMergesDefaultConfigWithProvidedConfig($config, $reqName)

$target->expects($this->once())->method('createEventManager')->with($services, $expected);

$target->createServiceWithName($services, 'irrelevant', $reqName);
$target($services,$reqName);
}
}
33 changes: 33 additions & 0 deletions module/Core/test/CoreTest/Factory/OptionsAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function provideCanCreateServiceWithNameTestData()
* @param $name
* @param $requestedName
* @param $expected
* @deprecated This test should be removed in ZF3
*/
public function testCanCreateServiceWithName($optionsConfig, $name, $requestedName, $expected)
{
Expand All @@ -80,6 +81,38 @@ public function testCanCreateServiceWithName($optionsConfig, $name, $requestedNa
$this->$method($target->canCreateServiceWithName($services, $name, $requestedName));
}

public function provideCanCreateTestData()
{
$cfg1 = [
'Test.Name/Two' => [],
'othername' => []
];

return [
[ [], 'Test/Name.One', false ],
[ $cfg1, 'Test.Name/Two', true ],
[ $cfg1, 'othername', true ],
[ $cfg1, 'Non.Existant', false ],
];
}

/**
* @testdox Determines if its able to create an options instance.
*
* @dataProvider provideCanCreateTestData
* @param $optionsConfig
* @param $requestedName
* @param $expected
*/
public function testCanCreate($optionsConfig,$requestedName,$expected)
{
$target = new OptionsAbstractFactory();
$services = $this->getServiceLocatorMock($optionsConfig);

$method = "assert".($expected ? 'True' : 'False');
$this->$method($target->canCreate($services,$requestedName));
}

/**
* @testdox Throws exception if required config key "class" is missing
* @expectedException \InvalidArgumentException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ public function serviceNamesProvider()
public function testCanCreateService($serviceName, $expected)
{
$sm = $this->getMockForAbstractClass('\Zend\ServiceManager\AbstractPluginManager');
$normalizedName = strtolower(str_replace('/', '', $serviceName));

$method = "assert" . ($expected ? 'True' : 'False');


$this->{$method}($this->target->canCreateServiceWithName($sm, $normalizedName, $serviceName));
$this->{$method}($this->target->canCreate($sm, $serviceName));
}

public function servicesProvider()
Expand Down Expand Up @@ -140,12 +137,12 @@ public function testCreateServiceWithName($serviceName, $entityName, $options, $

$pm->expects($this->once())->method('getServiceLocator')->willReturn($sm);

$this->target->setCreationOptions($options);

$paginator = $this->target->createServiceWithName($pm, 'not-used-anyway', $serviceName);
$target = $this->target;
$target->setCreationOptions($options);
$paginator = $target($pm,$serviceName);

$this->assertInstanceOf('\Zend\Paginator\Paginator', $paginator, 'No Paginator returned.');
$this->assertAttributeEquals([], 'options', $this->target, 'Cleaning creation options did not work.');
$this->assertAttributeEquals([], 'options', $target, 'Cleaning creation options did not work.');
$adapter = $paginator->getAdapter();

$this->assertInstanceOf('\Core\Paginator\Adapter\DoctrineMongoCursor', $adapter, 'Adapter is not correct class instance.');
Expand Down

0 comments on commit 12d22a6

Please sign in to comment.