Created
May 12, 2011 07:38
-
-
Save fprochazka/968111 to your computer and use it in GitHub Desktop.
Smart loaders
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 | |
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"); | |
} | |
} |
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 | |
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"); | |
} | |
} |
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 | |
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