Swift.php 1.9 KB

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