Created
June 1, 2014 06:21
-
-
Save Daniel15/2a1b7d1be7bc40005fc6 to your computer and use it in GitHub Desktop.
Simple autoloader for XHP in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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