Simple autoloader for XHP in Laravel
<?php | |
/** | |
* Simple autoloader for XHP in Laravel, when you don't want to use Composer's | |
* autoloader. To use: | |
* 1. Place this file somewhere in an autoloader-enabled directory of your | |
* Laravel installation (app/controllers is fine). | |
* 2. Modify `app/config/app.php` and add 'XHP\XHPServiceProvider' to the | |
* "Autoloaded Service Providers" section. | |
* | |
* More info on XHP in Laravel: http://dan.cx/2014/05/xhp-laravel | |
*/ | |
namespace XHP; | |
use Illuminate\Support\ServiceProvider; | |
class XHPServiceProvider extends ServiceProvider { | |
const CLASS_PREFIX = 'xhp_'; | |
public function register() { | |
spl_autoload_register(function($className) { | |
if (static::isXHPClass($className)) { | |
$filename = static::classToFilename($className); | |
$path = $this->app['view.finder']->find($filename); | |
require $path; | |
return true; | |
} | |
return false; | |
}); | |
} | |
public static function isXHPClass($className) { | |
$prefix = substr($className, 0, strlen(static::CLASS_PREFIX)); | |
return $prefix === static::CLASS_PREFIX; | |
} | |
public static function classToFilename($className) { | |
return str_replace( | |
array('__', '_'), | |
array('/', '-'), | |
substr($className, strlen(static::CLASS_PREFIX)) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment