Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293221

Add a generic PSR-0 autoloader in Drupal 7 core in order to solve stupid dependencies and various fallbacks everywhere

$
0
0

Ship a PSR-0 compatible autoloader (for both PEAR code style and newest PHP 5.3 namespaced code). This autoloader needs to be configurable (either at runtime, or in settings.php file), and be up before the cache system (and before database).

Motivation

Today Drupal 7 core does not follow the PSR-0 standard for loading OOP code, nevertheless:

  • A lot of external dependencies, libraries do (e.g. Solr PHP client, any piece of PEAR code, components from other frameworks)
  • Some contrib modules do (even if core does not, it's not a violation of the coding standards to do it)
  • All of contrib modules that do, or need an external libraries are forced to either (oftenly both):
    • Force a dependency over another contrib module that ships a PSR-0 autoloader
    • Implement an ugly fallback wih a custom autoloader, reducing drastically autoloading performances due to the autoloader bloat in SPL autoloader stack.
  • Some dependencies (external libraries such as Predis) must exist before the cache system which forces us to either dictate ugly patches in the settings.php file to users, either taint the module code with fallbacks loaders hurting performances.
  • The number of autoload modules starts to grow insanely, giving way too much alternatives. When using multiple modules having each one one of those as a dependency, you need to enable all them and duplicate functionnality (and hurt performances by loading too much code at runtime):
  • Core autoloader does not support namespace parsing when parsing files, and fails blatlantly loading any PHP 5.3 code.
  • Core autoloader is not tied to any standard, most libraries out there now does.
  • Even if Drupal 8 is coming Drupal 7 support and lifetime is going to be long, and this feature would give great benefits to a lot of contrib modules.

Proposed solution

  • Ship core with a PSR-0 autoloader
  • Ship core with an APC based autoloader and activate it per default when APC is enabled, in order to ensure best performances
  • Write it custom, because most of packaged ones out there are PHP 5.3, but Drupal 7 is PHP 5.2
  • Regarding the PSR-0 standard, and PHP 5.2 compatibility: a PHP 5.2 compatible autoloader can support PHP 5.3 code, it does only string operations, so no questions asked about compatibility there is none to ask.
  • Provide a settings.php variable to disable APC explicitely
  • Provide a settings.php variable for registering namespaces and associated include paths
  • Provide a singleton accessor to fetch the autoloader instance, allowing modules to register or unregister namespaces at runtime
  • Chain it with the core loader, possibly prepend it, so it can be used safely before cache or database ar up
  • Add its initialization in DRUPAL_BOOTSTRAP_CONFIGURATION phase, soon enough so that everything else can use it

Current patch exemple usage

Registering a namespace in settings.php file:

$conf['classload_namespaces'] = array(
  // Registering Solr PHP client library (PEAR style).
  'Apache_Solr' => '/some/include/path/solr-php-client',
  // Alternative (when in webroot).
  'Apache_Solr' => 'sites/all/libraries/solr-php-client',
  // Registering Predis (PHP 5.3 newer PSR-0 style).
  'Predis' => 'sites/all/libraries/predis/lib',
);

(De)Registering a namespace at runtime:

// Single namespace.
drupal_autoload_get()->registerNamespace('Apache_Solr', '/some/include/path/solr-php-client')

// Multiple namespaces.
drupal_autoload_get()->registerNamespaces(array(
  'Apache_Solr' => 'sites/all/libraries/solr-php-client',
  'Predis' => 'sites/all/libraries/predis/lib',
));

// Unregistering a namespace.
drupal_autoload_get()->unregisterNamespace('Apache_Solr', '/some/include/path/solr-php-client')

Explicit APC autoloader unregister in settings.php file:

$conf['apc_classloader_enable'] = FALSE;

Viewing all articles
Browse latest Browse all 293221

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>