ClassLoader.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright 2012-2017 Anthon Pang. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * @package WebDriver
  18. *
  19. * @author Anthon Pang <apang@softwaredevelopment.ca>
  20. */
  21. namespace WebDriver;
  22. /**
  23. * WebDriver\ClassLoader (autoloader) class
  24. *
  25. * @package WebDriver
  26. */
  27. final class ClassLoader
  28. {
  29. /**
  30. * Load class
  31. *
  32. * @param string $class Class name
  33. */
  34. public static function loadClass($class)
  35. {
  36. $file = strpos($class, '\\') !== false
  37. ? str_replace('\\', DIRECTORY_SEPARATOR, $class)
  38. : str_replace('_', DIRECTORY_SEPARATOR, $class);
  39. $path = dirname(__DIR__) . DIRECTORY_SEPARATOR . $file . '.php';
  40. if (file_exists($path)) {
  41. include_once $path;
  42. }
  43. }
  44. /**
  45. * Autoloader
  46. *
  47. * @param string $class Class name
  48. */
  49. public static function autoload($class)
  50. {
  51. try {
  52. self::loadClass($class);
  53. } catch (\Exception $e) {
  54. }
  55. }
  56. }
  57. if (function_exists('spl_autoload_register')) {
  58. /**
  59. * use the SPL autoload stack
  60. */
  61. spl_autoload_register(array('WebDriver\ClassLoader', 'autoload'));
  62. /**
  63. * preserve any existing __autoload
  64. */
  65. if (function_exists('__autoload')) {
  66. spl_autoload_register('__autoload');
  67. }
  68. } else {
  69. /**
  70. * Our fallback; only one __autoload per PHP instance
  71. *
  72. * @param string $class Class name
  73. */
  74. function __autoload($class)
  75. {
  76. ClassLoader::autoload($class);
  77. }
  78. }