1 <?php
2
3 /**
4 * ArangoDB PHP client: autoloader
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9 */
10
11 namespace triagens\ArangoDb;
12
13 /**
14 * Handles automatic loading of missing class files.
15 *
16 * The autoloader can be nested with other autoloaders. It will only
17 * process classes from its own namespace and ignore all others.<br>
18 * <br>
19 *
20 * @package triagens\ArangoDb
21 * @since 0.2
22 */
23 class Autoloader
24 {
25 /**
26 * Directory with library files
27 *
28 * @var string
29 */
30 private static $libDir;
31
32 /**
33 * Class file extension
34 */
35 const EXTENSION = '.php';
36
37 /**
38 * Initialise the autoloader
39 *
40 * @throws Exception
41 * @return void
42 */
43 public static function init()
44 {
45 self::checkEnvironment();
46
47 self::$libDir = __DIR__ . DIRECTORY_SEPARATOR;
48
49 spl_autoload_register(__NAMESPACE__ . '\Autoloader::load');
50 }
51
52 /**
53 * Handle loading of an unknown class
54 *
55 * This will only handle class from its own namespace and ignore all others.
56 *
57 * This allows multiple autoloaders to be used in a nested fashion.
58 *
59 * @param string $className - name of class to be loaded
60 *
61 * @return void
62 */
63 public static function load($className)
64 {
65 $namespace = __NAMESPACE__ . '\\';
66 $length = strlen($namespace);
67
68 // if (substr($className, 0, $length) !== $namespace) {
69 if (0 !== strpos($className, $namespace)) {
70 return;
71 }
72
73 // init() must have been called before
74 assert(self::$libDir !== null);
75
76 require self::$libDir . substr($className, $length) . self::EXTENSION;
77 }
78
79 /**
80 * Check the runtime environment
81 *
82 * This will check whether the runtime environment is compatible with the
83 * Arango PHP client.
84 *
85 * @throws ClientException
86 * @return void
87 */
88 private static function checkEnvironment()
89 {
90 list($major, $minor) = explode('.', phpversion());
91
92 if ((int) $major < 5 || ((int) $major === 5 && (int) $minor < 5)) {
93 throw new ClientException('Incompatible PHP environment. Expecting PHP 5.5 or higher');
94 }
95 }
96 }
97