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

Move of factories from module.php to module.config.php #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
40 changes: 15 additions & 25 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
namespace ZfcUserDoctrineORM;

use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature;
use Zend\ServiceManager\ServiceManager;
use ZfcUserDoctrineORM\Options\ModuleOptions;

class Module

class Module implements Feature\AutoloaderProviderInterface, Feature\BootstrapListenerInterface
{
public function onBootstrap($e)
public function onBootstrap(EventInterface $e)
{
$app = $e->getParam('application');
$sm = $app->getServiceManager();
$app = $e->getParam('application');
/** @var ServiceManager $sm */
$sm = $app->getServiceManager();

/** @var ModuleOptions $options */
$options = $sm->get('zfcuser_module_options');

// Add the default entity driver only if specified in configuration
Expand All @@ -19,6 +27,9 @@ public function onBootstrap($e)
}
}

/**
* @return array
*/
public function getAutoloaderConfig()
{
return array(
Expand All @@ -33,27 +44,6 @@ public function getAutoloaderConfig()
);
}

public function getServiceConfig()
{
return array(
'aliases' => array(
'zfcuser_doctrine_em' => 'Doctrine\ORM\EntityManager',
),
'factories' => array(
'zfcuser_module_options' => function ($sm) {
$config = $sm->get('Configuration');
return new Options\ModuleOptions(isset($config['zfcuser']) ? $config['zfcuser'] : array());
},
'zfcuser_user_mapper' => function ($sm) {
return new \ZfcUserDoctrineORM\Mapper\User(
$sm->get('zfcuser_doctrine_em'),
$sm->get('zfcuser_module_options')
);
},
),
);
}

public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
Expand Down
23 changes: 17 additions & 6 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<?php
return array(
'doctrine' => array(
'service_manager' => array(
Copy link

@Kwido Kwido Aug 1, 2016

Choose a reason for hiding this comment

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

Why not use the FQCN?

    'aliases'   => array(
        'zfcuser_register_form_hydrator' => 'zfcuser_user_hydrator',
        'zfcuser_user_mapper'            => ZfcUserDoctrineORM\Mapper\User::class,
        'zfcuser_module_options'         => ZfcUserDoctrineORM\Options\ModuleOptions::class,
    ),
    'factories' => array(
        ZfcUserDoctrineORM\Mapper\User::class           => ZfcUserDoctrineORM\Factory\UserMapperFactory::class,
        ZfcUserDoctrineORM\Options\ModuleOptions::class => ZfcUserDoctrineORM\Factory\ModuleOptionsFactory::class,
    ),

Why? As it allows to easily refactor your code.

Why pass aliases into the factories array? If we want a Mapper\User, I rather get it by its FQCN instead of by a string as I've got to replace each string. Yet a good IDE will help you out anyway.

Copy link
Author

Choose a reason for hiding this comment

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

I know, but I wanted to keep the module as close as possible to the ZfcUser, but indeed, good practice for both modules would be to have the FQCN

'factories' => array(
'zfcuser_user_mapper' => 'ZfcUserDoctrineORM\Factory\UserMapperFactory',
'zfcuser_module_options' => 'ZfcUserDoctrineORM\Factory\ModuleOptionsFactory',
),
'aliases' => array(
'zfcuser_register_form_hydrator' => 'zfcuser_user_hydrator',
'zfcuser_zend_db_adapter' => 'Zend\Db\Adapter\Adapter',
Copy link

Choose a reason for hiding this comment

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

This is a fallback for when the aliases isn't provided in the zfcuser.global.php?

While the zfcuser.global.php(.dist) still overwrites the alias by default?

Copy link
Author

Choose a reason for hiding this comment

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

These are indeed unneeded and have been removed

'zfcuser_doctrine_em' => 'Doctrine\ORM\EntityManager',
),
),
'doctrine' => array(
'driver' => array(
'zfcuser_entity' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
'paths' => __DIR__ . '/xml/zfcuser'
'paths' => __DIR__ . '/xml/zfcuser',
),

'orm_default' => array(
'drivers' => array(
'ZfcUser\Entity' => 'zfcuser_entity'
)
)
)
'ZfcUser\Entity' => 'zfcuser_entity',
),
),
),
),
);
19 changes: 19 additions & 0 deletions src/ZfcUserDoctrineORM/Factory/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace ZfcUserDoctrineORM\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfcUserDoctrineORM\Options\ModuleOptions;

class ModuleOptionsFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Config');

return new ModuleOptions(isset($config['zfcuser']) ? $config['zfcuser'] : array());
}
}
23 changes: 23 additions & 0 deletions src/ZfcUserDoctrineORM/Factory/UserMapperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace ZfcUserDoctrineORM\Factory;

use Zend\Db;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\Hydrator;
use ZfcUser\Mapper;
use ZfcUser\Options;

class UserMapperFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new \ZfcUserDoctrineORM\Mapper\User(
$serviceLocator->get('zfcuser_doctrine_em'),
$serviceLocator->get('zfcuser_module_options')
);
}
}