DX: Allow programmer to specify a different namespace (a.k.a classes under lib/ folder) in .info files.

Created on 13 February 2013, almost 12 years ago
Updated 17 January 2025, 5 days ago

Problems/Motivations

The lib/Drupal// convention is not suitable for everything, so the programmer has the option to change himself the namespace of its registered classes. This is easily done by the following snippet:

<?php
$loader = drupal_classloader();
$loader->registerNamespace('MyFancyNamespace', DRUPAL_ROOT . '/' . drupal_get_path('module', 'myfancymodule') . '/lib');
?>

This is pure glue code which is not fun to write and maintain, we could make it sexier, by allowing programmer to specify its namespace directly within the .info/yaml file.

Moreover, if you change all your modules namespace, it feels we are adding execution time because the original routine for registering namespace depends on module_list(). We can improve it.

Proposed resolution

Writing the namespace as following in the module's .info file:

name = My fancy module
description = My fancy description

namespaces[Drupal\user] = /lib
prefixes[SOME_PEAR_PREFIX] = /lib
namespaces[Symfony\Component\HttpFoundation] = /vendor

This make sense because we don't have to allocate a hook_init() for defining new classes. We can also re-factor the registering routing so that it register only the namespace needed and not the list of enabled modules, plus the custom namespaces which will overwrite default defined namespaces.

✨ Feature request
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

system.module

Created by

πŸ‡«πŸ‡·France Sylvain Lecoy

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for sharing your idea for improving Drupal.

    We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024