fuel / core

Fuel PHP Framework - The core of the Fuel v1 framework
http://fuelphp.com
813 stars 345 forks source link

Config::load() doesn't load properly for package config file #1115

Closed crucid closed 12 years ago

crucid commented 12 years ago

The docs (http://docs.fuelphp.com/general/packages.html) specify to use:

// Add namespace, necessary if you want the autoloader to be able to find classes
Autoloader::add_namespace('Mypackage', __DIR__.'/classes/');

where namespace is NOT preceded by a backslash. Other default packages use the same format. However, when trying to load a config file using \Config::load('package::config'), the config is never loaded. After drilling down I found that in \Fuel\Core\Finder on line 303, the logic is:

if ($path = \Autoloader::namespace_path('\\'.ucfirst(substr($file, 0, $pos))))

It prefixes the package/module name with a backslash for namespace lookup. The \Autoloader::namespace_path key-searches the internal namespace array which has the namespaces as keys with NO prefixed backslash (as directed by documentation), thus the namespace is never found and ultimately the config file isn't found.

A consist method of storing namespaces and looking up namespaces needs to be handled. Either backslashes need to be used or not when adding and checking namespaces; the core methods can obviously be modified to handle this. To fix the situation for my project, I went against the documentation and all examples and used

Autoloader::add_namespace('\Mypackage', __DIR__.'/classes/');

This works because it stores the namespace with the backslash as the key in the internal array so it can be found by \Autoloader::namespace_path. Some of the other \Autoloader functions are "smart enough" to trim off the prefixed backslash.

WanWizard commented 12 years ago

The core is never designed to load a config file from a specfic package. The idea is that you just load the config file, and the core will load it. It will always check app and all loaded packages, so there is no need to load it from a package.

The '::' notation was designed to load a config from a loaded by non-active module, for which NO automatic loading mechanism exists. That it (sort of) works for packages too is an unintended side-effect.