Add autoloader

This commit is contained in:
Ivan Shalganov 2014-04-17 23:50:18 +04:00
parent e740bb2bd9
commit 02368a0a47
3 changed files with 26 additions and 2 deletions

View File

@ -12,4 +12,8 @@ For installation use [composer](http://getcomposer.org). Add in your `composer.j
or use shell
`composer require fenom/fenom`
If you do not use composer - use `psr-0` format for loading Fenom's classes.
If you do not use composer - use `psr-0` format for loading Fenom's classes or just register Fenom's autoloader:
```php
Fenom::registerAutoload();
```

View File

@ -9,7 +9,9 @@ namespace Ts {
}
namespace {
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../src/Fenom.php';
\Fenom::registerAutoload();
$fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled', Fenom::FORCE_COMPILE);

View File

@ -964,4 +964,22 @@ class Fenom
}
return $mask;
}
/**
* Register PSR-0 autoload for Fenom
* @param string $dir custom directory for autoloading, if NULL autoload itself
* @return bool
*/
public static function registerAutoload($dir = null)
{
if(!$dir) {
$dir = __DIR__;
}
return spl_autoload_register(function($classname) use ($dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $classname).'.php';
if(is_file($file)) {
require_once $file;
}
});
}
}