Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored factories for SM v3 #558

Merged
merged 3 commits into from Jul 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 15 additions & 14 deletions composer.json
Expand Up @@ -35,24 +35,25 @@
"doctrine/common": "~2.6",
"doctrine/cache": "~1.5",
"symfony/console": "~2.3|~3.0",
"zendframework/zend-authentication": "~2.3",
"zendframework/zend-cache": "~2.3",
"zendframework/zend-paginator": "~2.3",
"zendframework/zend-stdlib": "~2.3",
"zendframework/zend-mvc": "~2.3",
"zendframework/zend-servicemanager": "~2.3",
"zendframework/zend-validator": "~2.3"
"zendframework/zend-authentication": "^2.5.2",
"zendframework/zend-cache": "^2.5.2",
"zendframework/zend-paginator": "^2.6",
"zendframework/zend-stdlib": "^2.7.6",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomHAnderson zend-stdlib <2.7.6 versions have BC issues so maybe ^2.7.6 are better? I can change if you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zend-stdlib > 2.7 https://github.com/zendframework/zend-stdlib/releases/tag/release-2.7.7 no longer has hydrators so this cannot be merged unless zend-stdlib is limited to <= 2.7

I have said in other posts that 2.6 is the limit. I can be sure it's one of these two and 3.0 is completely out of the question so anything > 2.7 is a no-go. So the only long term solution for ^2.7.6 is to merge #548 with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zend-stdlib 2.* does have hydrators, yes main code is moved to zend-hydrator but it still exists and they will not remove them in 2.* releases. And this PR isn't long term solution (just best I can do to support SMv3 without making BC break) you can expect long term solution in DoctrineModule 1.0 version when will be possible to remove stdlib-hydrator. with DoctrineModule 1.0 we can drop support for zend-servicemanager 2.* and zend-stdlib 2.* also can bump zend-hydrator 1.* to 2.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also I would vote that hydrators should be moved into separate module this could be done now and also would solve a lot problems for other modules depending on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought ^2.7.6 would include 3.0 and above too. At 3.0 the hydrators are no longer in zend-stdlib.

I'm fine with moving the hydrators out. @veewee ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I dont mind if they move. Maybe it's time to rethink the hydrator completely in another repo... If we make it better by default, we don't need the phpro hydrator module anymore.

"zendframework/zend-hydrator": "^1.1",
"zendframework/zend-mvc": "^2.5.2",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
"zendframework/zend-validator": "^2.5.2"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"doctrine/coding-standard": "dev-master",
"zendframework/zend-test": "~2.3",
"zendframework/zend-modulemanager": "~2.3",
"zendframework/zend-serializer": "~2.3",
"zendframework/zend-log": "~2.3",
"zendframework/zend-i18n": "~2.3",
"zendframework/zend-version": "~2.3",
"zendframework/zend-session": "~2.3"
"zendframework/zend-test": "^2.5.2",
"zendframework/zend-modulemanager": "^2.6.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zend-modulemanager had some bugs and with composer update --prefer-lowest tests were failing so I had to limit to ^2.6.1 I hope this is ok?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya, fine

"zendframework/zend-serializer": "^2.6",
"zendframework/zend-log": "^2.5.2",
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-version": "^2.5",
"zendframework/zend-session": "^2.5.2"
},
"suggest": {
"doctrine/data-fixtures": "Data Fixtures if you want to generate test data or bootstrap data for your deployments"
Expand Down
12 changes: 6 additions & 6 deletions src/DoctrineModule/Service/AbstractFactory.php
Expand Up @@ -19,9 +19,9 @@

namespace DoctrineModule\Service;

use Interop\Container\ContainerInterface;
use RuntimeException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Base ServiceManager factory to be extended
Expand Down Expand Up @@ -78,19 +78,19 @@ public function getMappingType()
/**
* Gets options from configuration based on name.
*
* @param ServiceLocatorInterface $sl
* @param string $key
* @param null|string $name
* @param ContainerInterface $container
* @param string $key
* @param null|string $name
* @return \Zend\Stdlib\AbstractOptions
* @throws \RuntimeException
*/
public function getOptions(ServiceLocatorInterface $sl, $key, $name = null)
public function getOptions(ContainerInterface $container, $key, $name = null)
{
if ($name === null) {
$name = $this->getName();
}

$options = $sl->get('Configuration');
$options = $container->get('Configuration');
$options = $options['doctrine'];
if ($mappingType = $this->getMappingType()) {
$options = $options[$mappingType];
Expand Down
19 changes: 14 additions & 5 deletions src/DoctrineModule/Service/Authentication/AdapterFactory.php
Expand Up @@ -20,6 +20,7 @@

use DoctrineModule\Authentication\Adapter\ObjectRepository;
use DoctrineModule\Service\AbstractFactory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
Expand All @@ -34,21 +35,29 @@ class AdapterFactory extends AbstractFactory
{
/**
* {@inheritDoc}
*
* @return \DoctrineModule\Authentication\Adapter\ObjectRepository
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/* @var $options \DoctrineModule\Options\Authentication */
$options = $this->getOptions($serviceLocator, 'authentication');
$options = $this->getOptions($container, 'authentication');

if (is_string($objectManager = $options->getObjectManager())) {
$options->setObjectManager($serviceLocator->get($objectManager));
$options->setObjectManager($container->get($objectManager));
}

return new ObjectRepository($options);
}

/**
* {@inheritDoc}
*
* @return \DoctrineModule\Authentication\Adapter\ObjectRepository
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return $this($serviceLocator, ObjectRepository::class);
}

/**
* {@inheritDoc}
*/
Expand Down
Expand Up @@ -19,6 +19,7 @@
namespace DoctrineModule\Service\Authentication;

use DoctrineModule\Service\AbstractFactory;
use Interop\Container\ContainerInterface;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -33,18 +34,26 @@
class AuthenticationServiceFactory extends AbstractFactory
{
/**
*
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
* @return \Zend\Authentication\AuthenticationService
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new AuthenticationService(
$serviceLocator->get('doctrine.authenticationstorage.' . $this->getName()),
$serviceLocator->get('doctrine.authenticationadapter.' . $this->getName())
$container->get('doctrine.authenticationstorage.' . $this->getName()),
$container->get('doctrine.authenticationadapter.' . $this->getName())
);
}

/**
*
* @param \Zend\ServiceManager\ServiceLocatorInterface $container
* @return \Zend\Authentication\AuthenticationService
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, AuthenticationService::class);
}

/**
* {@inheritDoc}
*/
Expand Down
22 changes: 15 additions & 7 deletions src/DoctrineModule/Service/Authentication/StorageFactory.php
Expand Up @@ -20,7 +20,7 @@

use DoctrineModule\Authentication\Storage\ObjectRepository;
use DoctrineModule\Service\AbstractFactory;
use Zend\Authentication\Storage\Session as SessionStorage;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
Expand All @@ -35,25 +35,33 @@ class StorageFactory extends AbstractFactory
{
/**
* {@inheritDoc}
*
* @return \DoctrineModule\Authentication\Storage\ObjectRepository
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/* @var $options \DoctrineModule\Options\Authentication */
$options = $this->getOptions($serviceLocator, 'authentication');
$options = $this->getOptions($container, 'authentication');

if (is_string($objectManager = $options->getObjectManager())) {
$options->setObjectManager($serviceLocator->get($objectManager));
$options->setObjectManager($container->get($objectManager));
}

if (is_string($storage = $options->getStorage())) {
$options->setStorage($serviceLocator->get($storage));
$options->setStorage($container->get($storage));
}

return new ObjectRepository($options);
}

/**
* {@inheritDoc}
*
* @return \DoctrineModule\Authentication\Storage\ObjectRepository
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, ObjectRepository::class);
}

/**
* {@inheritDoc}
*/
Expand Down
21 changes: 17 additions & 4 deletions src/DoctrineModule/Service/CacheFactory.php
Expand Up @@ -20,6 +20,7 @@
namespace DoctrineModule\Service;

use Doctrine\Common\Cache\CacheProvider;
use Interop\Container\ContainerInterface;
use RuntimeException;
use Doctrine\Common\Cache\MemcacheCache;
use Doctrine\Common\Cache\MemcachedCache;
Expand All @@ -42,10 +43,10 @@ class CacheFactory extends AbstractFactory
*
* @throws RuntimeException
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** @var $options \DoctrineModule\Options\Cache */
$options = $this->getOptions($serviceLocator, 'cache');
$options = $this->getOptions($container, 'cache');
$class = $options->getClass();

if (!$class) {
Expand All @@ -54,8 +55,8 @@ public function createService(ServiceLocatorInterface $serviceLocator)

$instance = $options->getInstance();

if (is_string($instance) && $serviceLocator->has($instance)) {
$instance = $serviceLocator->get($instance);
if (is_string($instance) && $container->has($instance)) {
$instance = $container->get($instance);
}

switch ($class) {
Expand Down Expand Up @@ -90,6 +91,18 @@ public function createService(ServiceLocatorInterface $serviceLocator)
return $cache;
}

/**
* {@inheritDoc}
*
* @return \Doctrine\Common\Cache\Cache
*
* @throws RuntimeException
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, \Doctrine\Common\Cache\Cache::class);
}

/**
* {@inheritDoc}
*/
Expand Down
32 changes: 25 additions & 7 deletions src/DoctrineModule/Service/CliControllerFactory.php
Expand Up @@ -20,10 +20,12 @@
namespace DoctrineModule\Service;

use DoctrineModule\Controller\CliController;
use Symfony\Component\Console\Application;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceManager;

/**
* Factory responsible of instantiating an {@see \DoctrineModule\Controller\CliController}
Expand All @@ -35,16 +37,32 @@
class CliControllerFactory implements FactoryInterface
{
/**
* {@inheritDoc}
* Create an object
*
* @return \DoctrineModule\Controller\CliController
* @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 $serviceLocator \Zend\ServiceManager\AbstractPluginManager */
/* @var $application \Symfony\Component\Console\Application */
$application = $serviceLocator->getServiceLocator()->get('doctrine.cli');
$application = $container->get('doctrine.cli');

return new CliController($application);
}

/**
* {@inheritDoc}
*
* @return \DoctrineModule\Controller\CliController
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container->getServiceLocator(), CliController::class);
}
}
21 changes: 15 additions & 6 deletions src/DoctrineModule/Service/CliFactory.php
Expand Up @@ -20,11 +20,11 @@
namespace DoctrineModule\Service;

use DoctrineModule\Version;
use Interop\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceManager;

/**
* CLI Application ServiceManager factory responsible for instantiating a Symfony CLI application
Expand All @@ -51,14 +51,14 @@ class CliFactory implements FactoryInterface
protected $commands = array();

/**
* @param ServiceLocatorInterface $sm
* @param ContainerInterface $container
* @return \Zend\EventManager\EventManagerInterface
*/
public function getEventManager(ServiceLocatorInterface $sm)
public function getEventManager(ContainerInterface $container)
{
if (null === $this->events) {
/* @var $events \Zend\EventManager\EventManagerInterface */
$events = $sm->get('EventManager');
$events = $container->get('EventManager');

$events->addIdentifiers(array(__CLASS__, 'doctrine'));

Expand All @@ -72,7 +72,7 @@ public function getEventManager(ServiceLocatorInterface $sm)
* {@inheritDoc}
* @return Application
*/
public function createService(ServiceLocatorInterface $sl)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$cli = new Application;
$cli->setName('DoctrineModule Command Line Interface');
Expand All @@ -82,8 +82,17 @@ public function createService(ServiceLocatorInterface $sl)
$cli->setAutoExit(false);

// Load commands using event
$this->getEventManager($sl)->trigger('loadCli.post', $cli, array('ServiceManager' => $sl));
$this->getEventManager($container)->trigger('loadCli.post', $cli, array('ServiceManager' => $container));

return $cli;
}

/**
* {@inheritDoc}
* @return Application
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, Application::class);
}
}