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

Support Module::getServiceConfig() returning a Configuration instance #113

Open
vitorbrandao opened this issue Aug 12, 2014 · 5 comments
Open
Milestone

Comments

@vitorbrandao
Copy link
Member

From http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html#modules-as-service-providers

Module Returning an Array

The following demonstrates returning an array of configuration from a module class. It can be substantively the same as the array configuration from the previous example.

namespace SomeModule;

class Module
{
    public function getServiceConfig()
    {
        return array(
            'abstract_factories' => array(),
            'aliases' => array(),
            'factories' => array(),
            'invokables' => array(),
            'services' => array(),
            'shared' => array(),
        );
    }
}

Returning a Configuration instance

First, let’s create a class that holds configuration.

namespace SomeModule\Service;

use SomeModule\Authentication;
use SomeModule\Form;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;

class ServiceConfiguration extends Config
{
    /**
     * This is hard-coded for brevity.
     */
    public function configureServiceManager(ServiceManager $serviceManager)
    {
        $serviceManager->setFactory('User', 'SomeModule\Service\UserFactory');
        $serviceManager->setFactory('UserForm', function ($serviceManager) {
            $form = new Form\User();

            // Retrieve a dependency from the service manager and inject it!
            $form->setInputFilter($serviceManager->get('UserInputFilter'));
            return $form;
        });
        $serviceManager->setInvokableClass('UserInputFilter', 'SomeModule\InputFilter\User');
        $serviceManager->setService('Auth', new Authentication\AuthenticationService());
        $serviceManager->setAlias('SomeModule\Model\User', 'User');
        $serviceManager->setAlias('AdminUser', 'User');
        $serviceManager->setAlias('SuperUser', 'AdminUser');
        $serviceManager->setShared('UserForm', false);
    }
}

Now, we’ll consume it from our Module.

namespace SomeModule;

// We could implement Zend\ModuleManager\Feature\ServiceProviderInterface.
// However, the module manager will still find the method without doing so.
class Module
{
    public function getServiceConfig()
    {
        return new Service\ServiceConfiguration();
        // OR:
        // return 'SomeModule\Service\ServiceConfiguration';
    }
}

End of citation.

We need to support all of these alternatives.

@dragoonis
Copy link
Member

ModuleManagerFactory gets booted:
https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Service/ModuleManagerFactory.php#L50

<?php
        $serviceListener->addServiceManager(
            $serviceLocator,
            'service_manager',
            'Zend\ModuleManager\Feature\ServiceProviderInterface',
            'getServiceConfig'
        );

addServiceManager results in this:
https://github.com/zendframework/zf2/blob/master/library/Zend/ModuleManager/Listener/ServiceListener.php#L88

<?php
        $this->serviceManagers[$smKey] = array(
            'service_manager'        => $serviceManager,
            'config_key'             => $key,
            'module_class_interface' => $moduleInterface,
            'module_class_method'    => $method,
            'configuration'          => array(),
        );

This implies that getServiceConfig() becomes module_class_method and therefore when modules get loaded it's calling $module->getServiceConfig()

https://github.com/zendframework/zf2/blob/master/library/Zend/ModuleManager/Listener/ServiceListener.php#L154

<?php
            $config = $module->{$sm['module_class_method']}();
?>

It seems to get registered here: https://github.com/zendframework/zf2/blob/master/library/Zend/ModuleManager/Listener/ServiceListener.php#L223

<?php
            $serviceConfig = new ServiceConfig($smConfig);
            $serviceConfig->configureServiceManager($sm['service_manager']);

@Ocramius
Copy link

Just a note: don't use master when referencing code snippets, as it will change in future (due to lines being pushed around)

@Ocramius
Copy link

Also note that getServiceConfig and all the get([A-Z][A-Za-z]+)Config methods are only required to return an array or a Traversable

@dragoonis
Copy link
Member

@Ocramius thanks for the tips. It's a bit challenging wrapping my head around the latest ZF2 code since you've reafactored a lot of things into the new Zend\Mvc component.

@Ocramius
Copy link

@dragoonis nothing has changed since 2.0.0 on this front :-)

@dragoonis dragoonis added this to the v2.1 milestone Jan 26, 2015
@vitorbrandao vitorbrandao modified the milestones: 2.1.1, 2.1.0 May 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants