Created
January 24, 2012 19:22
-
-
Save benlumley/1672012 to your computer and use it in GitHub Desktop.
Imagine (or any namespaced library) in symfony 1.4
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 | |
// 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