Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Created May 12, 2011 07:38
Show Gist options
  • Save fprochazka/968111 to your computer and use it in GitHub Desktop.
Save fprochazka/968111 to your computer and use it in GitHub Desktop.
Smart loaders
<?php
namespace Kdyby\Loaders;
use Doctrine;
class DoctrineLoader
{
/** @var array */
private static $registered = FALSE;
/**
* @param string|NULL $namespace
* @return Kdyby\Loaders\DoctrineLoader
*/
public static function register()
{
if (self::$registered) {
throw DoctrineLoaderException::alreadyRegistered();
}
require_once LIBS_DIR . '/Doctrine/Common/ClassLoader.php';
$commonLoader = self::$registered[] = new Doctrine\Common\ClassLoader('Doctrine\DBAL\Migrations', LIBS_DIR . '/Doctrine'); // migrations
$commonLoader->register();
$commonLoader = self::$registered[] = new Doctrine\Common\ClassLoader('Doctrine', LIBS_DIR);
$commonLoader->register();
$commonLoader = self::$registered[] = new Doctrine\Common\ClassLoader('DoctrineExtensions', LIBS_DIR . '/Doctrine');
$commonLoader->register();
$commonLoader = self::$registered[] = new Doctrine\Common\ClassLoader('Gedmo', LIBS_DIR . '/Doctrine');
$commonLoader->register();
return new self;
}
}
class DoctrineLoaderException extends \Exception
{
/**
* @return Kdyby\Loaders\DoctrineLoaderException
*/
public static function alreadyRegistered()
{
return new self("Cannot register, already registered loader for Doctrine");
}
}
<?php
namespace Kdyby\Loaders;
use Symfony;
class SymfonyLoader
{
/** @var array */
private static $registered = FALSE;
/**
* @param string|NULL $namespace
* @return Kdyby\Loaders\SymfonyLoader
*/
public static function register()
{
if (self::$registered) {
throw SymfonyLoaderException::alreadyRegistered();
}
require_once LIBS_DIR . '/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$symfonyLoader = self::$registered[] = new Symfony\Component\ClassLoader\UniversalClassLoader();
$symfonyLoader->registerNamespaces(array(
'Symfony' => LIBS_DIR,
));
$symfonyLoader->register();
return new self;
}
}
class SymfonyLoaderException extends \Exception
{
/**
* @return Kdyby\Loaders\SymfonyLoaderException
*/
public static function alreadyRegistered()
{
return new self("Cannot register, already registered loader for Symfony");
}
}
<?php
namespace Kdyby\Loaders;
use Zend;
class ZendLoader
{
/** @var array */
private static $registered = FALSE;
/**
* @param string|NULL $namespace
* @return Kdyby\Loaders\ZendLoader
*/
public static function register()
{
if (self::$registered) {
throw ZendLoaderException::alreadyRegistered();
}
require_once LIBS_DIR . '/Zend/Loader/StandardAutoloader.php';
$zendLoader = self::$registered[] = new Zend\Loader\StandardAutoloader();
$zendLoader->register();
return new self;
}
}
class ZendLoaderException extends \Exception
{
/**
* @return Kdyby\Loaders\ZendLoaderException
*/
public static function alreadyRegistered()
{
return new self("Cannot register, already registered loader for Zend");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment