AutoLoader.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Egulias;
  3. /**
  4. * PSR-0 Autoloader
  5. *
  6. * @author ieter Hordijk <info@pieterhordijk.com>
  7. */
  8. class EguliasAutoLoader
  9. {
  10. /**
  11. * @var string The namespace prefix for this instance.
  12. */
  13. protected $namespace = '';
  14. /**
  15. * @var string The filesystem prefix to use for this instance
  16. */
  17. protected $path = '';
  18. /**
  19. * Build the instance of the autoloader
  20. *
  21. * @param string $namespace The prefixed namespace this instance will load
  22. * @param string $path The filesystem path to the root of the namespace
  23. */
  24. public function __construct($namespace, $path)
  25. {
  26. $this->namespace = ltrim($namespace, '\\');
  27. $this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
  28. }
  29. /**
  30. * Try to load a class
  31. *
  32. * @param string $class The class name to load
  33. *
  34. * @return boolean If the loading was successful
  35. */
  36. public function load($class)
  37. {
  38. $class = ltrim($class, '\\');
  39. if (strpos($class, $this->namespace) === 0) {
  40. $nsparts = explode('\\', $class);
  41. $class = array_pop($nsparts);
  42. $path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/';
  43. $max=count($nsparts);
  44. for ($i=2; $i<$max;$i++) {
  45. $path .= $nsparts[$i].'/';
  46. }
  47. $nsparts = array();
  48. $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
  49. if (file_exists($path)) {
  50. require $path;
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. /**
  57. * Register the autoloader to PHP
  58. *
  59. * @return boolean The status of the registration
  60. */
  61. public function register()
  62. {
  63. return spl_autoload_register(array($this, 'load'));
  64. }
  65. /**
  66. * Unregister the autoloader to PHP
  67. *
  68. * @return boolean The status of the unregistration
  69. */
  70. public function unregister()
  71. {
  72. return spl_autoload_unregister(array($this, 'load'));
  73. }
  74. }