Skip to content

Instantly share code, notes, and snippets.

@benlumley
Created January 24, 2012 19:22
Show Gist options
  • Save benlumley/1672012 to your computer and use it in GitHub Desktop.
Save benlumley/1672012 to your computer and use it in GitHub Desktop.
Imagine (or any namespaced library) in symfony 1.4
<?php
// in the top of config/projectconfiguration.php you should have
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
// Immediately beneath, add this:
require_once dirname(__FILE__).'/../lib/util/NsAutoload.class.php';
// then within ProjectConfiguration::setup() add:
$this->dispatcher->connect('context.load_factories', (array('NsAutoload', 'register')));
// I've forgotten why, but it didn't work properly if I called NsAutoLoad::register directly
// after including the file, like the sfCore one is
?>
<?php
// then this is the contents of NsAutoload.class.php, which you can put wherever you like - just change the include above to point to it
class NsAutoload {
public static function register() {
spl_autoload_register(array('NsAutoload', 'autoload'), false);
}
public static function autoload($class) {
$timer = sfTimerManager::getTimer('NsAutoload');
$namespaces = array("Imagine" => 'Imagine/lib/'); // edit this to support other libs, or you could extend it to be handled by a config handler and a yml file if you've loads
if (!strpos($class, "\\")) {
$timer->addTime();
return;
}
foreach ($namespaces as $space=>$path) {
if (strpos($class, $space)===0) {
// i put imagine within lib/vendor, amend here if you do something different.
require_once(sfConfig::get('sf_lib_dir') . '/vendor/' . $path . str_replace('\\', '/', $class) . '.php');
}
}
$timer->addTime();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment