There is a way to toad every namespace you need, I had to reproduce a structure I know well, and so I messed up a little with config and loader to force loading Module.php before dispatch.
So basically I have an /Config/application.config.php
where I register every module, and tell if module will be "shared".
return array(
'module' => array(
'Core' => array(
'className' => 'Core\Module',
'path' => __DIR__ . '/../Module/Core/Module.php',
'shared' => true,
),
'Test' => array(
'className' => 'Test\Module',
'path' => __DIR__ . '/../Module/Test/Module.php',
),
),
);
In my bootloader I load module that way :
$di = new FactoryDefault();
// Registering vendors namespaces
$loader = new Loader();
$loader->registerNamespaces(require __DIR__ . "/Vendor/register_namespace.php", true);
$loader->register();
// Import Application Configuration
$appConfig = new Config(require __DIR__ . '/Config/application.config.php');
// Registering Application config as a service
$di->set('appConfig', $appConfig);
// Import Global and Local configuration
$globalConfig = new Config(require __DIR__ . '/Config/global.config.php');
$globalConfig->merge(new Config(require __DIR__ . '/Config/global.config.php'));
$di->set('config', $globalConfig);
foreach ($appConfig['module'] as $module) {
$dir = preg_replace("/\/Module.php$/", "", $module['path']);
// Register Namespace for Shared Module (will not be called by dispatch)
if (array_key_exists('shared', $module) && $module['shared']) {
$namespace = preg_replace('/\\\Module$/', "", $module['className']);
$loader->registerNamespaces(array($namespace => $dir), true);
// Loading Module class bootloader Reflection Instance
$reflexion = new ReflectionClass($module['className']);
$instance = $reflexion->newInstanceWithoutConstructor();
// Execute every 'register' methods
foreach ($reflexion->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if(preg_match('/^register/', $method->name)) {
$instance->{$method->name}($di);
}
}
}
}