namespaced.php 1.2 KB

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