Drupal 7.x module which autoloads your modules namespaced classes.
- Drupal 7.x
- PHP 5.3.3+
The recommended way to install Nsautoload is with Composer.
Just add the following to your composer.json
:
{
"minimum-stability": "dev",
"require": {
"korstiaan/nsautoload": "dev-master"
}
}
Now update Composer and install the newly added requirement and its dependencies:
$ php composer.phar update korstiaan/nsautoload
If all went well and composer/installers
did its job, Nsautoload was installed to modules/nsautoload
.
If you don't want it there, or it's not part of your Drupal rootdir, symlink it to your folder of choice.
Using Composer
means including its autoloader. Add the following to your Drupals settings.php:
// /path/to/sites/default/settings.php
require '/path/to/vendor/autoload.php';
2. Use composer_loader
Just follow its readme.
There are 2 ways to enable Nsautoload:
Add the following to your project's settings.php:
<?php
// /path/to/sites/default/settings.php
use Nsautoload\Nsautoload;
$loader = new Nsautoload();
$loader->register();
Go to site/all/modules
and enable it on http://yourdomain.com/admin/modules/list
.
(If you're using voiture just add nsautoload
to cnf/shared/modules.php
)
In order for Nsautoload to be able to find your classes some conventions have to be followed:
Your namespace must be the name of your module with an under_score
to CamelCase
conversion.
For example:
my_module
has namespaceMyModule
my_foo_module
has namespaceMyFooModule
mymodule
has namespaceMymodule
Two conventions can be used for this, one following the PSR-0 standard, and a more Drupal'ish convention:
This one completely follows the PSR-0 standard, for example:
MyModule\Foo
is located at/path/to/my_module/MyModule/Foo.php
Mymodule\Foo\Bar
is located at/path/to/mymodule/Mymodule/Foo/Bar.php
Mymodule\Foo\Bar_Crux
is located at/path/to/mymodule/Mymodule/Foo/Bar/Crux.php
When this convention is followed, a class is located at module_name/class/_class_.class.inc
. Only a 2 level namespace can be used. Examples:
MyModule\Foo
is located at/path/to/my_module/class/foo.class.inc
MyModule\Foo_Bar
is located at/path/to/my_module/class/foo_bar.class.inc
MyModule\Foo\Bar
can't be mapped with this convention.
This convention also adds another namespace naming strategy for BC purposes. Next to a under_score
to CamelCase
conversion, it also allows you to randomly add capitals in your namespace.
This allows the following mapping:
MyOldModule\Foo
to/path/to/myoldmodule/class/foo.class.inc
In order to cache Nsautoload's autoload map you can wrap it into Symfony's ApcClassLoader.
First add Symfony's ClassLoader component to your composer.json
:
{
"require": {
"symfony/class-loader": "dev-master"
}
}
Install it:
$ php composer.phar update symfony/class-loader
Then change your project's settings.php
Nsautoload lines to look like this:
<?php
// /path/to/sites/default/settings.php
use Nsautoload\Nsautoload;
$loader = new Nsautoload();
$apcLoader = new Symfony\Component\ClassLoader\ApcClassLoader(md5(__DIR__), $loader);
$apcLoader->register(true);
And you're done!
Nsautoload is licensed under the MIT license.