├── src ├── autoload.php └── Autoloader.php ├── composer.json ├── LICENSE └── README.md /src/autoload.php: -------------------------------------------------------------------------------- 1 | 8 | 9 | This library provides a custom autoloader that aliases legacy Nexmo classes, traits, and interfaces 10 | to their replacements under the Vonage namespace. 11 | 12 | This library is handy if you want to switch over to the newer `vonage/client` or `vonage/client-core` libraries but have 13 | a lot of older code that utilizes the `\Nexmo` namespace. If you are starting a project from scratch, we recommend immediately 14 | using the `\Vonage` namespace. 15 | 16 | Much of this code has been ported from the [`laminas/laminas-zendframework-bridge`](https://github.com/laminas/laminas-zendframework-bridge) project. 17 | 18 | ## Installation 19 | 20 | Run the following to install this library: 21 | 22 | ```bash 23 | $ composer require vonage/nexmo-bridge 24 | ``` 25 | 26 | ## Usage 27 | 28 | There is none! By including this package, the autoloader will be automatically invoked and will do all the work 29 | for you. 30 | 31 | ## Contributing 32 | 33 | This library is actively developed and we love to hear from you! Please feel free to [create an issue][issues] or [open a pull request][pulls] with your questions, comments, suggestions and feedback. 34 | 35 | [issues]: https://github.com/Nexmo/vonage-php-nexmo-bridge/issues 36 | [pulls]: https://github.com/Nexmo/vonage-php-nexmo-bridge/pulls -------------------------------------------------------------------------------- /src/Autoloader.php: -------------------------------------------------------------------------------- 1 | 40 | */ 41 | protected static $namespaces = ['Nexmo\\' => 'Vonage\\']; 42 | 43 | /** 44 | * @var array 45 | */ 46 | protected static $namespacesReversed = ['Vonage\\' => 'Nexmo\\']; 47 | 48 | public static function load() : void 49 | { 50 | $loaded = new ArrayObject([]); 51 | 52 | spl_autoload_register(self::createPrependAutoloader( 53 | static::$namespacesReversed, 54 | self::getClassLoader(), 55 | $loaded 56 | ), true, true); 57 | 58 | spl_autoload_register(self::createAppendAutoloader( 59 | static::$namespaces, 60 | $loaded 61 | )); 62 | } 63 | 64 | /** 65 | * @throws RuntimeException 66 | */ 67 | private static function getClassLoader() : ClassLoader 68 | { 69 | if (getenv('COMPOSER_VENDOR_DIR') !== false && file_exists(getenv('COMPOSER_VENDOR_DIR') . '/autoload.php')) { 70 | return include getenv('COMPOSER_VENDOR_DIR') . '/autoload.php'; 71 | } 72 | 73 | if (file_exists(__DIR__ . '/../../../autoload.php')) { 74 | return include __DIR__ . '/../../../autoload.php'; 75 | } 76 | 77 | if (file_exists(__DIR__ . '/../vendor/autoload.php')) { 78 | return include __DIR__ . '/../vendor/autoload.php'; 79 | } 80 | 81 | throw new RuntimeException('Cannot detect composer autoload. Please run composer install'); 82 | } 83 | 84 | /** 85 | * @param array $namespaces Namespaces to flip between 86 | * @return Closure(string): void 87 | */ 88 | private static function createPrependAutoloader( 89 | array $namespaces, 90 | ClassLoader $classLoader, 91 | ArrayObject $loaded 92 | ) : Closure { 93 | /** 94 | * @param string $class Class name to autoload 95 | * @return void 96 | */ 97 | return static function (string $class) use ($namespaces, $classLoader, $loaded) : void { 98 | if (isset($loaded[$class])) { 99 | return; 100 | } 101 | 102 | $segments = explode('\\', $class); 103 | 104 | $i = 0; 105 | $check = ''; 106 | 107 | while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) { 108 | $check .= $segments[$i] . '\\'; 109 | ++$i; 110 | } 111 | 112 | if ($check === '') { 113 | return; 114 | } 115 | 116 | if ($classLoader->loadClass($class)) { 117 | $legacy = $namespaces[$check] 118 | . strtr(substr($class, strlen($check)), [ 119 | 'Vonage' => 'Nexmo', 120 | ]); 121 | class_alias($class, $legacy); 122 | } 123 | }; 124 | } 125 | 126 | /** 127 | * @param array $namespaces Namespaces to flip between 128 | * @return Closure(string): void 129 | */ 130 | private static function createAppendAutoloader(array $namespaces, ArrayObject $loaded) : Closure 131 | { 132 | /** 133 | * @param string $class Class name to autoload 134 | * @return void 135 | */ 136 | return static function (string $class) use ($namespaces, $loaded) : void { 137 | $segments = explode('\\', $class); 138 | 139 | $i = 0; 140 | $check = ''; 141 | 142 | // We are checking segments of the namespace to match quicker 143 | while (isset($segments[$i + 1], $namespaces[$check . $segments[$i] . '\\'])) { 144 | $check .= $segments[$i] . '\\'; 145 | ++$i; 146 | } 147 | 148 | if ($check === '') { 149 | return; 150 | } 151 | 152 | $alias = $namespaces[$check] 153 | . strtr(substr($class, strlen($check)), [ 154 | 'Nexmo' => 'Vonage', 155 | ]); 156 | 157 | $loaded[$alias] = true; 158 | if (class_exists($alias) || interface_exists($alias) || trait_exists($alias)) { 159 | class_alias($alias, $class); 160 | } 161 | }; 162 | } 163 | } 164 | --------------------------------------------------------------------------------