Swift.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * General utility class in Swift Mailer, not to be instantiated.
  11. *
  12. *
  13. * @author Chris Corbyn
  14. */
  15. abstract class Swift
  16. {
  17. /** Swift Mailer Version number generated during dist release process */
  18. const VERSION = '@SWIFT_VERSION_NUMBER@';
  19. public static $initialized = false;
  20. public static $inits = array();
  21. /**
  22. * Registers an initializer callable that will be called the first time
  23. * a SwiftMailer class is autoloaded.
  24. *
  25. * This enables you to tweak the default configuration in a lazy way.
  26. *
  27. * @param mixed $callable A valid PHP callable that will be called when autoloading the first Swift class
  28. */
  29. public static function init($callable)
  30. {
  31. self::$inits[] = $callable;
  32. }
  33. /**
  34. * Internal autoloader for spl_autoload_register().
  35. *
  36. * @param string $class
  37. */
  38. public static function autoload($class)
  39. {
  40. // Don't interfere with other autoloaders
  41. if (0 !== strpos($class, 'Swift_')) {
  42. return;
  43. }
  44. $path = dirname(__FILE__).'/'.str_replace('_', '/', $class).'.php';
  45. if (!file_exists($path)) {
  46. return;
  47. }
  48. require $path;
  49. if (self::$inits && !self::$initialized) {
  50. self::$initialized = true;
  51. foreach (self::$inits as $init) {
  52. call_user_func($init);
  53. }
  54. }
  55. }
  56. /**
  57. * Configure autoloading using Swift Mailer.
  58. *
  59. * This is designed to play nicely with other autoloaders.
  60. *
  61. * @param mixed $callable A valid PHP callable that will be called when autoloading the first Swift class
  62. */
  63. public static function registerAutoload($callable = null)
  64. {
  65. if (null !== $callable) {
  66. self::$inits[] = $callable;
  67. }
  68. spl_autoload_register(array('Swift', 'autoload'));
  69. }
  70. }