Swift.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @package Swift
  13. *
  14. * @author Chris Corbyn
  15. */
  16. abstract class Swift
  17. {
  18. public static $initialized = false;
  19. public static $inits = array();
  20. /** Swift Mailer Version number generated during dist release process */
  21. const VERSION = '@SWIFT_VERSION_NUMBER@';
  22. /**
  23. * Registers an initializer callable that will be called the first time
  24. * a SwiftMailer class is autoloaded.
  25. *
  26. * This enables you to tweak the default configuration in a lazy way.
  27. *
  28. * @param mixed $callable A valid PHP callable that will be called when autoloading the first Swift class
  29. */
  30. public static function init($callable)
  31. {
  32. self::$inits[] = $callable;
  33. }
  34. /**
  35. * Internal autoloader for spl_autoload_register().
  36. *
  37. * @param string $class
  38. */
  39. public static function autoload($class)
  40. {
  41. //Don't interfere with other autoloaders
  42. if (0 !== strpos($class, 'Swift_')) {
  43. return;
  44. }
  45. $path = dirname(__FILE__).'/'.str_replace('_', '/', $class).'.php';
  46. if (!file_exists($path)) {
  47. return;
  48. }
  49. require $path;
  50. if (self::$inits && !self::$initialized) {
  51. self::$initialized = true;
  52. foreach (self::$inits as $init) {
  53. call_user_func($init);
  54. }
  55. }
  56. }
  57. /**
  58. * Configure autoloading using Swift Mailer.
  59. *
  60. * This is designed to play nicely with other autoloaders.
  61. *
  62. * @param mixed $callable A valid PHP callable that will be called when autoloading the first Swift class
  63. */
  64. public static function registerAutoload($callable = null)
  65. {
  66. if (null !== $callable) {
  67. self::$inits[] = $callable;
  68. }
  69. spl_autoload_register(array('Swift', 'autoload'));
  70. }
  71. }