frostbane / autoloader
Autoloader.
Requires
- php: >=5.4.16
Requires (Dev)
- phpunit/phpunit: ^4.8.36
This package is not auto-updated.
Last update: 2025-03-30 15:23:22 UTC
README
Registering Paths
Register a path as an autoload path
Assuming the following directory structure:
__DOC_ROOT__
|- common
| `- classes
| `- Util
| `- String
| |- StringUtil.php (\Util\String\StringUtil)
| `- StringSearch.php (\Util\String_StringSearch)
`- core
`- Input
|- InputValidator.php
`- InputFilter.php
※ note that the StringSearch class has 'underscores' (imagine one of the old guys in the company created it.)
To autoload the string utilities:
use frostbane\autoloader\Psr0;
use String\StringUtil;
use String\String_StringSearch;
$loader = Psr0::instance();
$loader->registerPath(__DOC_ROOT__ . "/common/classes");
// StringUtil will automatically be loaded
$strUtil = new StringUtil();
// StringSearch will automatically be loaded
$strSearch = new String_StringSearch();
Assuming the following directory structure:
__DOC_ROOT__
|- common
| `- classes
| `- Util
| `- String
| |- StringUtil.php
| `- StringSearch.php
`- core
`- Input
|- InputValidator.php (\Framework\Validation\Input\InputValidator)
`- InputFilter.php (\Framework\Validation\Input_InputFilter)
Registering the path __DOC_ROOT_/core
will not autoload the validation
classes because it has a prefixed namespace.
※ note again that the InputFilter class has 'underscores' (hey old guy welcome to the 21th century!)
To register them with a prefixed namespace:
use frostbane\autoloader\Psr0;
use Framework\Validation\Input\InputValidator;
use Framework\Validation\Input_InputFilter;
$loader = Psr0::instance();
$loader->registerNamespacePath("\\Framework\\Validation", __DOC_ROOT__ . "/core");
// InputValidator will automatically be loaded
$validator = new InputValidator();
// InputFilter will automatically be loaded
$filter = new Input_InputFilter();
Registering Multiple Paths
A couple of overloads exists to make the code a little readable.
use frostbane\autoloader\Psr0;
$loader = Psr0::instance();
$loader->registerPath(array(__DOC_ROOT__ . "/common/classes",
__DOC_ROOT__ . "/some/other/path",
__DIR__ . "/../another/path"));
and
use frostbane\autoloader\Psr0;
$loader = Psr0::instance();
$loader->registerNamespacePath(array("\\some\\namespace" => __DOC_ROOT__ . "/common/classes",
"\\some\\other\\namespace" => __DOC_ROOT__ . "/some/other/path",
"\\another\\namespace" => __DIR__ . "/../another/path"));
Debugging
If you are lost, find a bible (perhaps.)
Get the registered paths with:
use frostbane\autoloader\Psr0;
$loader = Psr0::instance();
$registeredPaths = $loader->getPaths();
Developing
Fork it! Spoon it! Whatever.
Run the tests with:
$ ./vendor/bin/phpunit