autoloader.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. HTTPSEverywhere for Desktopd
  4. Copyright (C) 2015 Desktopd Developer(s)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Affero General Public License as
  7. published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. https://www.gnu.org/licenses/agpl.html
  16. */
  17. /*
  18. This autoloader is intentionally written in this manner.
  19. */
  20. spl_autoload_register(function ($class) {
  21. if (class_exists($class, false)) {
  22. return true;
  23. }
  24. $name = strtr("$class", '\\', '/');
  25. if (!preg_match('~^[_a-zA-Z][_a-zA-Z0-9]*(?:/[_a-zA-Z][_a-zA-Z0-9]*)*$~', $name)) {
  26. return false;
  27. }
  28. $path = dirname(__DIR__) . "/classes/{$name}.php";
  29. if (is_file($path) && is_readable($path)) {
  30. require_once $path;
  31. } else {
  32. return false;
  33. }
  34. }, true, true);
  35. // vim: ts=4 et ai