1 <?php
2 /**
3 * ArangoDB PHP Core Client: Autoloader
4 *
5 * @author Frank Mayer
6 * @copyright Copyright 2013-2015, FRANKMAYER.NET, Athens, Greece
7 */
8
9 namespace frankmayer\ArangoDbPhpCore;
10
11
12 /**
13 * Handles automatic loading of missing class files
14 * The autoloader can be nested with other autoloaders. It will only
15 * process classes from its own namespace and ignore all others.
16 *
17 * @package frankmayer\ArangoDbPhpCore
18 */
19 class Autoloader
20 {
21 /**
22 * Root directory for library files
23 *
24 * @var string
25 */
26 private static $rootDir = null;
27
28 /**
29 * Class file extension
30 */
31 const EXTENSION = '.php';
32
33 /**
34 * Initialise the autoloader
35 *
36 * @throws Exception
37 * @return void
38 *
39 * @codeCoverageIgnore
40 */
41 public static function init()
42 {
43 self::checkEnvironment();
44
45 spl_autoload_register(__NAMESPACE__ . '\Autoloader::load');
46
47 self::$rootDir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
48 }
49
50 /**
51 * Handle loading of an unknown class
52 *
53 * This will only handle class from its own namespace and ignore all others.<br>
54 * This allows multiple autoloaders to be used in a nested fashion.
55 *
56 * @param string $className - The name of class to be loaded
57 *
58 * @return void
59 */
60 public static function load($className)
61 {
62 $className = str_replace(__NAMESPACE__, '', $className);
63 $className = str_replace("\\", DIRECTORY_SEPARATOR, $className);
64
65 if (file_exists(self::$rootDir . $className . self::EXTENSION)) {
66 require_once self::$rootDir . $className . self::EXTENSION;
67 }
68 }
69
70 /**
71 * Check the runtime environment
72 *
73 * This will check whether the runtime environment is compatible with this library
74 *
75 * @throws ClientException
76 * @return void
77 *
78 * @codeCoverageIgnore
79 */
80 private static function checkEnvironment()
81 {
82 if (version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION, "5.4.0", "<")) {
83 throw new ClientException('Incompatible PHP environment. Expecting PHP 5.4 or higher');
84 }
85 }
86 }
87