Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Daniel15
Created June 1, 2014 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniel15/2a1b7d1be7bc40005fc6 to your computer and use it in GitHub Desktop.
Save Daniel15/2a1b7d1be7bc40005fc6 to your computer and use it in GitHub Desktop.
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