autoload.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright 2014 Facebook, Inc.
  4. *
  5. * You are hereby granted a non-exclusive, worldwide, royalty-free license to
  6. * use, copy, modify, and distribute this software in source code or binary
  7. * form for use in connection with the web services and APIs provided by
  8. * Facebook.
  9. *
  10. * As with any software that integrates with the Facebook platform, your use
  11. * of this software is subject to the Facebook Developer Principles and
  12. * Policies [http://developers.facebook.com/policy/]. This copyright notice
  13. * shall be included in all copies or substantial portions of the software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. /**
  25. * You only need this file if you are not using composer.
  26. * Why are you not using composer?
  27. * https://getcomposer.org/
  28. */
  29. if (version_compare(PHP_VERSION, '5.4.0', '<')) {
  30. throw new Exception('The Facebook SDK v4 requires PHP version 5.4 or higher.');
  31. }
  32. /**
  33. * Register the autoloader for the Facebook SDK classes.
  34. * Based off the official PSR-4 autoloader example found here:
  35. * https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
  36. *
  37. * @param string $class The fully-qualified class name.
  38. * @return void
  39. */
  40. spl_autoload_register(function ($class)
  41. {
  42. // project-specific namespace prefix
  43. $prefix = 'Facebook\\';
  44. // base directory for the namespace prefix
  45. $base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';
  46. // does the class use the namespace prefix?
  47. $len = strlen($prefix);
  48. if (strncmp($prefix, $class, $len) !== 0) {
  49. // no, move to the next registered autoloader
  50. return;
  51. }
  52. // get the relative class name
  53. $relative_class = substr($class, $len);
  54. // replace the namespace prefix with the base directory, replace namespace
  55. // separators with directory separators in the relative class name, append
  56. // with .php
  57. $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
  58. // if the file exists, require it
  59. if (file_exists($file)) {
  60. require $file;
  61. }
  62. });