namespaced.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. require_once dirname(dirname(__FILE__)) . '/autoload.php';
  3. if (PHP_VERSION_ID < 50300) {
  4. return;
  5. }
  6. /*
  7. * This file is just for convenience, to allow developers to reduce verbosity when
  8. * they add this project to their libraries.
  9. *
  10. * Replace this:
  11. *
  12. * $x = ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_encrypt(...$args);
  13. *
  14. * with this:
  15. *
  16. * use ParagonIE\Sodium\Compat;
  17. *
  18. * $x = Compat::crypto_aead_xchacha20poly1305_encrypt(...$args);
  19. */
  20. spl_autoload_register(function ($class) {
  21. if ($class[0] === '\\') {
  22. $class = substr($class, 1);
  23. }
  24. $namespace = 'ParagonIE\\Sodium';
  25. // Does the class use the namespace prefix?
  26. $len = strlen($namespace);
  27. if (strncmp($namespace, $class, $len) !== 0) {
  28. // no, move to the next registered autoloader
  29. return false;
  30. }
  31. // Get the relative class name
  32. $relative_class = substr($class, $len);
  33. // Replace the namespace prefix with the base directory, replace namespace
  34. // separators with directory separators in the relative class name, append
  35. // with .php
  36. $file = dirname(dirname(__FILE__)) . '/namespaced/' . str_replace('\\', '/', $relative_class) . '.php';
  37. // if the file exists, require it
  38. if (file_exists($file)) {
  39. require_once $file;
  40. return true;
  41. }
  42. return false;
  43. });