From 28f4d5c1be95ac363cf04173a4c62676d30f6a35 Mon Sep 17 00:00:00 2001 From: Terry Lin Date: Sat, 4 Jul 2020 12:47:15 +0800 Subject: [PATCH] Add autoloader.php --- autoload.php | 47 +++++++++++++++++++++++++++++++++ src/Psr17/Utils/SuperGlobal.php | 6 ++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 autoload.php diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..5117577 --- /dev/null +++ b/autoload.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +/** + * Register to PSR-4 autoloader. + * + * @return void + */ +function psr_http_register() +{ + spl_autoload_register('psr_http_autoload', true, false); +} + +/** + * PSR-4 autoloader. + * + * @param string $className + * + * @return void + */ +function psr_http_autoload($className) +{ + $prefix = 'Shieldon\\'; + $dir = __DIR__ . '/src'; + + if (0 === strpos($className, $prefix . 'Psr')) { + $parts = explode('\\', substr($className, strlen($prefix))); + $filepath = $dir . '/' . implode('/', $parts) . '.php'; + + echo $filepath . '
'; + + if (is_file($filepath)) { + require $filepath; + } + } +} + +psr_http_register(); \ No newline at end of file diff --git a/src/Psr17/Utils/SuperGlobal.php b/src/Psr17/Utils/SuperGlobal.php index 274bfc5..4023efe 100644 --- a/src/Psr17/Utils/SuperGlobal.php +++ b/src/Psr17/Utils/SuperGlobal.php @@ -95,7 +95,11 @@ public static function mockCliEnvironment(array $server = []): void 'SERVER_PROTOCOL' => 'HTTP/1.1', ]; - $_SERVER = array_merge($defaultServerParams, $server); + if (defined('NO_MOCK_ENV')) { + $defaultServerParams = []; + } + + $_SERVER = array_merge($defaultServerParams, $_SERVER, $server); } // @codeCoverageIgnoreEnd