Autoloader.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. spl_autoload_register('Autoloader::autoload');
  21. class Autoloader
  22. {
  23. /**
  24. * @var array
  25. */
  26. private static $autoloadPathArray = array(
  27. 'aliyun-php-sdk-core',
  28. 'aliyun-php-sdk-core/Auth',
  29. 'aliyun-php-sdk-core/Http',
  30. 'aliyun-php-sdk-core/Profile',
  31. 'aliyun-php-sdk-core/Regions',
  32. 'aliyun-php-sdk-core/Exception',
  33. );
  34. /**
  35. * Automatically find the class and load it.
  36. *
  37. * @param string $className
  38. */
  39. public static function autoload($className)
  40. {
  41. $directories = dirname(dirname(__DIR__));
  42. foreach (self::$autoloadPathArray as $path) {
  43. $file = $directories . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $className . '.php';
  44. $file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
  45. if (is_file($file)) {
  46. include_once $file;
  47. break;
  48. }
  49. }
  50. }
  51. /**
  52. * Load all product folders.
  53. *
  54. * @return void
  55. */
  56. public static function loadDirectories()
  57. {
  58. $directories = dirname(dirname(__DIR__));
  59. foreach (glob($directories . DIRECTORY_SEPARATOR . '*') as $directory) {
  60. if (is_dir($directory) && basename($directory) !== 'aliyun-php-sdk-core') {
  61. self::$autoloadPathArray[] = basename($directory);
  62. }
  63. }
  64. }
  65. /**
  66. * @param string $path
  67. */
  68. public static function addAutoloadPath($path)
  69. {
  70. self::$autoloadPathArray[] = $path;
  71. }
  72. }