Compare commits

...

10 Commits

Author SHA1 Message Date
Jérémy Dufraisse
00b18b134d feat(composer): allow php ^7.2.5 2023-03-23 11:43:30 +01:00
Jérémy Dufraisse
7b1cd11017 feat(AppTest): test themes and plugins url 2023-03-21 12:40:54 +01:00
Jérémy Dufraisse
02601c54a3 feat(AppTest): define test on url rewrite 2023-03-21 12:04:41 +01:00
Jérémy Dufraisse
d4711dddd1 fix(AppTest): add error handlers on exit 2023-03-21 08:55:54 +01:00
Jérémy Dufraisse
1f89a43499 feat(AppTest): next test to usr phpunit 2023-03-20 17:11:41 +01:00
Jérémy Dufraisse
43dd441473 fix(AppTest) :make it works 2023-03-20 16:15:18 +01:00
Jérémy Dufraisse
d8e64eeaa9 Merge branch 'feat/seacms-app-test-common' 2023-03-20 08:52:23 +01:00
Jérémy Dufraisse
b6a2a22389 feat(test/commen): add helpers for SERVER manipulating 2023-03-20 08:51:54 +01:00
Jérémy Dufraisse
efad1f8b92 fix(Test/Common): to be able to define all possible $_SERVER 2023-03-20 08:46:02 +01:00
Jérémy Dufraisse
fdd6f50d46 feat(Test/common): create 2023-03-19 20:18:57 +01:00
7 changed files with 563 additions and 24 deletions

View File

@ -6,6 +6,7 @@ use SeaCMS\Api\LateApiAware;
use SeaCMS\Api\JsonResponse; use SeaCMS\Api\JsonResponse;
use SeaCMS\App\MdResponse; use SeaCMS\App\MdResponse;
use SeaCMS\App\TestInterface; use SeaCMS\App\TestInterface;
use SeaCMS\App\TestOnPageRenderingInterface;
/** /**
* A plugin for SeaCMS-app. * A plugin for SeaCMS-app.
@ -24,6 +25,12 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
*/ */
protected $triggerTest; protected $triggerTest;
/**
* define if test output should be defined onPageRendering
* @var bool
*/
protected $triggerTestOnPageRendering;
/** /**
* define test * define test
* @var TestInterface * @var TestInterface
@ -39,6 +46,7 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
{ {
parent::__construct($pico); parent::__construct($pico);
$this->triggerTest = !empty($testRunner) && ($testRunner instanceof TestInterface); $this->triggerTest = !empty($testRunner) && ($testRunner instanceof TestInterface);
$this->triggerTestOnPageRendering = !empty($testRunner) && ($testRunner instanceof TestOnPageRenderingInterface);
$this->testRunner = ($this->triggerTest) ? $testRunner : null; $this->testRunner = ($this->triggerTest) ? $testRunner : null;
} }
@ -173,4 +181,17 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
} }
} }
/**
* Triggered before Pico renders the page
*
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
*/
public function onPageRendering(&$templateName, array &$twigVariables)
{
if ($this->triggerTestOnPageRendering){
$this->testRunner->runOnPageRendering($this,$templateName,$twigVariables);
}
}
} }

View File

@ -5,7 +5,7 @@
"prefer-stable": true, "prefer-stable": true,
"minimum-stability": "dev", "minimum-stability": "dev",
"require": { "require": {
"php": "^8.0", "php": "^7.2.5||^8.0",
"ext-dom": "*", "ext-dom": "*",
"ext-filter": "*", "ext-filter": "*",
"ext-json": "*", "ext-json": "*",
@ -20,7 +20,7 @@
"config": { "config": {
"optimize-autoloader": true, "optimize-autoloader": true,
"platform": { "platform": {
"php": "8.0.0" "php": "7.2.5"
}, },
"platform-check": true, "platform-check": true,
"sort-packages": true, "sort-packages": true,
@ -36,6 +36,7 @@
"autoload": { "autoload": {
"classmap": ["App.php","SeacmsAppPlugin.php"], "classmap": ["App.php","SeacmsAppPlugin.php"],
"psr-4": { "psr-4": {
"SeaCMS\\App\\Test\\": "tests",
"SeaCMS\\App\\": "src" "SeaCMS\\App\\": "src"
} }
}, },

36
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "458f87f6f405c7142436c0a12d01ea99", "content-hash": "8c6acb90dd7429741534e63b22acbbf9",
"packages": [ "packages": [
{ {
"name": "erusev/parsedown", "name": "erusev/parsedown",
@ -192,7 +192,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.defis.info/SeaCMS/pico", "url": "https://git.defis.info/SeaCMS/pico",
"reference": "0187138622d9c8776be3324ed904abd6371c256f" "reference": "4342526f6d60fe72c87f7c2fd02c0b9bf4513db5"
}, },
"require": { "require": {
"erusev/parsedown": "^2.0.0-beta-1", "erusev/parsedown": "^2.0.0-beta-1",
@ -266,7 +266,7 @@
"issues": "https://github.com/picocms/Pico/issues", "issues": "https://github.com/picocms/Pico/issues",
"source": "https://github.com/picocms/Pico" "source": "https://github.com/picocms/Pico"
}, },
"time": "2023-03-10T23:35:40+00:00" "time": "2023-03-21T07:53:30+00:00"
}, },
{ {
"name": "seacms/composer-plugin", "name": "seacms/composer-plugin",
@ -274,23 +274,29 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.defis.info/SeaCMS/composer-plugin", "url": "https://git.defis.info/SeaCMS/composer-plugin",
"reference": "3a1a8da8100369347fc69e4eb96a9b7c7b552fad" "reference": "adc22cdee66306b9392435bf5407cf455b508608"
}, },
"require": { "require": {
"composer-plugin-api": "^2.3", "composer-plugin-api": "^2.3",
"picocms/pico": "*",
"topfloor/composer-cleanup-vcs-dirs": "^1.1" "topfloor/composer-cleanup-vcs-dirs": "^1.1"
}, },
"replace": { "replace": {
"symfony/finder": "*" "symfony/finder": "*"
}, },
"require-dev": {
"phpunit/phpunit": "^8.5"
},
"default-branch": true, "default-branch": true,
"type": "composer-plugin", "type": "composer-plugin",
"extra": { "extra": {
"class": "Seacms\\ComposerInstaller\\Plugin" "class": "Seacms\\ComposerInstaller\\Plugin",
"plugin-modifies-downloads": true
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Seacms\\ComposerInstaller\\": "src" "Seacms\\ComposerInstaller\\": "src/installer/",
"Seacms\\Command\\": "src/commands/"
} }
}, },
"license": [ "license": [
@ -314,7 +320,7 @@
"issues": "https://git.defis.info/SeaCMS/composer-plugin/issues", "issues": "https://git.defis.info/SeaCMS/composer-plugin/issues",
"source": "https://git.defis.info/SeaCMS/composer-plugin" "source": "https://git.defis.info/SeaCMS/composer-plugin"
}, },
"time": "2023-03-13T18:22:14+00:00" "time": "2023-03-23T10:42:37+00:00"
}, },
{ {
"name": "seacms/seacms-api", "name": "seacms/seacms-api",
@ -322,10 +328,10 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.defis.info/SeaCMS/seacms-api", "url": "https://git.defis.info/SeaCMS/seacms-api",
"reference": "f5b81d351454f2b7fe4c09d462f7802241ca94d1" "reference": "f3574dd6d68d1ef31fa3534c4d9b96aac4ef2bc1"
}, },
"require": { "require": {
"php": "^8.0" "php": "^7.2.5||^8.0"
}, },
"default-branch": true, "default-branch": true,
"type": "pico-plugin", "type": "pico-plugin",
@ -360,7 +366,7 @@
"issues": "https://git.defis.info/SeaCMS/seacms-api/issues", "issues": "https://git.defis.info/SeaCMS/seacms-api/issues",
"source": "https://git.defis.info/SeaCMS/seacms-api" "source": "https://git.defis.info/SeaCMS/seacms-api"
}, },
"time": "2023-03-02T17:19:44+00:00" "time": "2023-03-23T10:38:49+00:00"
}, },
{ {
"name": "seacms/seacms-auth", "name": "seacms/seacms-auth",
@ -368,10 +374,10 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.defis.info/SeaCMS/seacms-auth", "url": "https://git.defis.info/SeaCMS/seacms-auth",
"reference": "eef1cad3ccb4deda8f9fbccf01880e0040be3c97" "reference": "1955005a69850591e5f95732f6ddfa00a88e77dc"
}, },
"require": { "require": {
"php": "^8.0", "php": "^7.2.5||^8.0",
"seacms/seacms-api": "dev-master" "seacms/seacms-api": "dev-master"
}, },
"default-branch": true, "default-branch": true,
@ -404,7 +410,7 @@
"issues": "https://git.defis.info/SeaCMS/seacms-auth/issues", "issues": "https://git.defis.info/SeaCMS/seacms-auth/issues",
"source": "https://git.defis.info/SeaCMS/seacms-auth" "source": "https://git.defis.info/SeaCMS/seacms-auth"
}, },
"time": "2023-03-11T17:16:33+00:00" "time": "2023-03-23T10:39:38+00:00"
}, },
{ {
"name": "symfony/polyfill-ctype", "name": "symfony/polyfill-ctype",
@ -861,7 +867,7 @@
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": "^8.0", "php": "^7.2.5||^8.0",
"ext-dom": "*", "ext-dom": "*",
"ext-filter": "*", "ext-filter": "*",
"ext-json": "*", "ext-json": "*",
@ -870,7 +876,7 @@
}, },
"platform-dev": [], "platform-dev": [],
"platform-overrides": { "platform-overrides": {
"php": "8.0.0" "php": "7.2.5"
}, },
"plugin-api-version": "2.3.0" "plugin-api-version": "2.3.0"
} }

46
src/TestBaseUrl.php Normal file
View File

@ -0,0 +1,46 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeacmsAppPlugin;
use SeaCMS\App\TestException;
use SeaCMS\App\TestOnPageRenderingInterface;
use SeaCMS\App\TestBaseUrlException;
/**
* test base Url
*/
class TestBaseUrl implements TestOnPageRenderingInterface
{
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $templateName file name of the template
* @param array $twigVariables template variables
* @throws TestException
*/
public function runOnPageRendering(SeacmsAppPlugin $plugin, string $templateName, array $twigVariables)
{
$pico = $plugin->getPico();
throw new TestBaseUrlException(
$pico->getBaseUrl(),
$pico->getRootDir(),
$pico->getPluginsDir(),
$pico->getThemesDir(),
$pico->getCurrentPage(),
$twigVariables,
"Testing base url"
);
}
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $output
* @throws TestException
*/
public function run(SeacmsAppPlugin $plugin, string $output){
}
}

View File

@ -0,0 +1,126 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeaCMS\App\TestException;
use Throwable;
/**
* define exception to caught return for tests
*/
class TestBaseUrlException extends TestException
{
/**
* caught baseUrl
* @var string
*/
protected $baseUrl;
/**
* plugin dir
* @var string
*/
protected $pluginDir;
/**
* root dir
* @var string
*/
protected $rootDir;
/**
* theme dir
* @var string
*/
protected $themeDir;
/**
* currentPage
* @var null|array
*/
protected $currentPage;
/**
* twigVariables
* @var null|array
*/
protected $twigVariables;
public function __construct(
string $baseUrl,
string $rootDir,
string $pluginDir,
string $themeDir,
?array $currentPage,
?array $twigVariables,
string $message = "",
int $code=0,
Throwable $th = null
)
{
parent::__construct($message,$code,$th);
$this->baseUrl = $baseUrl;
$this->rootDir = $rootDir;
$this->pluginDir = $pluginDir;
$this->themeDir = $themeDir;
$this->currentPage = $currentPage;
$this->twigVariables = $twigVariables;
}
/**
* get baseUrl
* @return string
*/
public function getBaseUrl(): string
{
return $this->baseUrl;
}
/**
* get pluginDir
* @return string
*/
public function getPluginDir(): string
{
return $this->pluginDir;
}
/**
* get rootDir
* @return string
*/
public function getRootDir(): string
{
return $this->rootDir;
}
/**
* get themeDir
* @return string
*/
public function getThemeDir(): string
{
return $this->themeDir;
}
/**
* get currentPage
* @return null|array
*/
public function getcurrentPage(): ?array
{
return $this->currentPage;
}
/**
* get twigVariables
* @return null|array
*/
public function getTwigVariables(): ?array
{
return $this->twigVariables;
}
}

View File

@ -0,0 +1,24 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeacmsAppPlugin;
use SeaCMS\App\TestException;
use SeaCMS\App\TestInterface;
/**
* define interface for tests
*/
interface TestOnPageRenderingInterface extends TestInterface
{
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $templateName file name of the template
* @param array $twigVariables template variables
* @throws TestException
*/
public function runOnPageRendering(SeacmsAppPlugin $plugin, string $templateName, array $twigVariables);
}

View File

@ -7,19 +7,334 @@
namespace SeaCMS\App\Test; namespace SeaCMS\App\Test;
use SeaCMS\App;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use SeaCMS\App;
use SeaCMS\App\TestBaseUrl;
use SeaCMS\App\TestBaseUrlException;
use Throwable;
final class AppTest extends TestCase { final class AppTest extends TestCase {
public function testSuccess(): void /**
* register error handlers
*/
public function testInit()
{ {
$app = new App(''); $displayErrors = true;
$output = $app->runPico(); set_error_handler(function(
$this->assertTrue($app instanceof App); int $errno,
string $errstr,
?string $errfile = null,
?int $errline = null,
?array $errcontext = null
) use (&$displayErrors){
if ($displayErrors){
echo "\nError $errstr ($errno)\nIn file $errfile\nLine : $errline\n";
} }
public function testFailure(): void });
register_shutdown_function(function (){
$lastErr = error_get_last();
if (!is_null($lastErr)){
echo "\nAn error occured\n".json_encode($lastErr)."\n";
}
});
$this->assertTrue($displayErrors);
}
/**
* @depends testInit
* @dataProvider apiRewriteProvider
* @covers App::update_SERVERIfNeeded
* @param string $rootFolder
* @param string $filePath
* @param string $shortScriptName
* @param string $queryString
* @param string $waitedBaseUrl
* @param string $waitedPageId
* @param string $waitedPageUrl
* @param string $waitedThemesUrl
* @param string $waitedPluginsUrl
*/
public function testApiRewrite(
string $rootFolder,
string $filePath,
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageUrl,
string $waitedThemesUrl,
string $waitedPluginsUrl,
): void
{ {
$this->assertTrue(false); $this->saveSERVER();
$this->defineServer(
true,
$filePath,
$shortScriptName,
$queryString
);
$app = new App($rootFolder,new TestBaseUrl());
$thrown = false;
$foundTh = null;
try {
$output = $app->runPico();
} catch (TestBaseUrlException $th) {
$thrown = true;
$foundTh = $th;
} catch (Throwable $th){
}
$cwd = getcwd();
$this->revertSERVER();
$this->assertTrue($thrown,"TestBaseUrlException not found");
$this->assertEquals($waitedBaseUrl,$foundTh->getBaseUrl(),"Not same baseUrl");
$this->assertEquals("$cwd/vendor/picocms/plugins/",$foundTh->getPluginDir(),"Not same pluginDir");
$this->assertEquals("$cwd/vendor/picocms/themes/",$foundTh->getThemeDir(),"Not same themeDir");
$this->assertEquals("$cwd/",$foundTh->getRootDir(),"Not same rootDir");
$currentPage = $foundTh->getcurrentPage();
$this->assertIsArray($currentPage,"Current Page should be an array");
$this->assertArrayHasKey('id',$currentPage,"Current Page should be an array with key 'id'");
$this->assertEquals($waitedPageId,$currentPage['id'],"Not waited page's id");
$this->assertArrayHasKey('url',$currentPage,"Current Page should be an array with key 'url'");
$this->assertEquals($waitedPageUrl,$currentPage['url'],"Not waited page's url");
$twigVariables = $foundTh->getTwigVariables();
$this->assertIsArray($twigVariables,"Twigvariables should be an array");
$this->assertArrayHasKey('themes_url',$twigVariables,"Twigvariables should be an array with key 'themes_url'");
$this->assertEquals($waitedThemesUrl,$twigVariables['themes_url'],"Not waited themes_url");
$this->assertArrayHasKey('plugins_url',$twigVariables,"Twigvariables should be an array with key 'plugins_url'");
$this->assertEquals($waitedPluginsUrl,$twigVariables['plugins_url'],"Not waited plugins_url");
}
public function apiRewriteProvider()
{
$data = [];
$this->prepareAPage($data,'','content','index');
$this->prepareAPage($data,'','content','sub/index');
$this->prepareAPage($data,'','content','theme');
$this->prepareAPage($data,'','sites/default','index');
$this->prepareAPage($data,'','sites/default','sub/index');
$this->prepareAPage($data,'','sites/default','theme');
$this->prepareAPage($data,'/sea','content','index');
$this->prepareAPage($data,'/sea','content','sub/index');
$this->prepareAPage($data,'/sea','content','theme');
$this->prepareAPage($data,'/sea','sites/default','index');
$this->prepareAPage($data,'/sea','sites/default','sub/index');
$this->prepareAPage($data,'/sea','sites/default','theme');
// echo "\n";
// foreach($data as $name => $line){
// echo "$name => ".implode(',',$line)."\n";
// }
return $data;
}
protected function prepareAPage(
array &$data,
string $baseScriptName,
string $rootFolder,
string $pageId
)
{
$baseUrl = 'http://localhost'.$baseScriptName.'/';
$isIndex = ($pageId == 'index');
$isSubIndex = (substr($pageId,-strlen('/index')) == '/index');
$pageIdInUrl = $isIndex ? '' : ($isSubIndex ? substr($pageId,0,-strlen('/index')) : $pageId) ;
$queriesStrings = $isIndex ? [['q'=>'','s'=>'','f'=>'']] : [
['q'=>$pageIdInUrl,'s'=>'','f'=>''],
['q'=>'','s'=>$pageIdInUrl,'f'=>''],
['q'=>'','s'=>$pageIdInUrl,'f'=>$pageIdInUrl],
['q'=>$pageIdInUrl,'s'=>$pageIdInUrl,'f'=>''],
['q'=>$pageIdInUrl,'s'=>$pageIdInUrl,'f'=>$pageIdInUrl]
];
foreach ($queriesStrings as $queryData) {
$queryString = empty($queryData['q']) ? '' :'?'.$queryData['q'];
$scriptNameMiddle = empty($queryData['s']) ? '' : $queryData['s'].'/';
foreach (['','index.php'] as $endScriptName) {
$formattedQueryString = str_replace('/','%2F',$queryString);
$pageUrl = empty($queryData['s']) ? (empty($queryData['q']) ? '' : $formattedQueryString) : $queryData['s'];
$canRewriteFromRoot = empty($queryData['s']) && empty($queryData['f']);
if ($rootFolder == 'content' && $canRewriteFromRoot){
$scriptName = "$baseScriptName/$scriptNameMiddle$endScriptName";
$name = $rootFolder.$scriptName.$queryString.'*';
$this->prepareData($data,$name,$rootFolder,'index.php',$scriptName,$queryData['q'],$baseUrl,$pageId,$pageUrl);
}
foreach ([false,true] as $withRewrite) {
if ($withRewrite){
$scriptName = "$baseScriptName/$scriptNameMiddle$endScriptName";
$name = $rootFolder.$scriptName.$queryString.' R';
$waitedUrl = $baseUrl;
} else {
$scriptName = "$baseScriptName/$rootFolder/$scriptNameMiddle$endScriptName";
$name = $rootFolder.$scriptName.$queryString;
$waitedUrl = $baseUrl.$rootFolder.'/';
}
if ($canRewriteFromRoot){
$this->prepareData($data,$name."*",$rootFolder,"index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl);
}
$formattedRootMiddle = empty($pageIdInUrl) ? '' : "/$pageIdInUrl";
$this->prepareData($data,$name,$rootFolder,"$rootFolder$formattedRootMiddle/index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl);
}
}
}
}
protected function prepareData(
array &$data,
string $name,
string $rootFolder,
string $filePath,
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageEndUrl
)
{
$waitedPageUrl = $waitedBaseUrl.$waitedPageEndUrl;
$nbLevels = in_array($shortScriptName,['/','/index.php']) ? 0 : count(explode('/',$rootFolder));
$prefix = implode('/',array_fill(0,$nbLevels,'..'));
if (!empty($prefix)){
$prefix .= '/';
}
$waitedThemesUrl = $waitedBaseUrl.$prefix.'vendor/picocms/themes';
$waitedPluginsUrl = $waitedBaseUrl.$prefix.'vendor/picocms/plugins';
$data[$name] = compact([
'rootFolder',
'filePath',
'shortScriptName',
'queryString',
'waitedBaseUrl',
'waitedPageId',
'waitedPageUrl',
'waitedThemesUrl',
'waitedPluginsUrl'
]);
}
/**
* define $_SERVER because fastcgi not run in CLI
* @param bool $reset reset previous $_SERVER
* @param string $filePath realpath of current script file
* @param string $shortScriptName uri path of current script file
* @param string $queryString wanted query string
* @param string $method
* @param string $pathInfo
*/
public function defineServer(
bool $reset,
string $filePath,
string $shortScriptName,
string $queryString,
string $method = 'GET',
string $pathInfo = ''
)
{
if (is_file($filePath)){
if (!is_array($_SERVER) || $reset){
$new = [];
if (is_array($_SERVER)){
foreach(['SERVER_SOFTWARE','SERVER_PROTOCOL','GATEWAY_INTERFACE'] as $key){
if (array_key_exists($key,$_SERVER)){
$new[$key] = $_SERVER[$key];
}
}
}
$_SERVER = $new;
}
$_SERVER['QUERY_STRING'] = $queryString;
$_SERVER['REQUEST_METHOD'] = in_array($method,['GET','POST','PUT','DELETE','HEAD']) ? $method : 'GET';
// $_SERVER['CONTENT_TYPE'] = 'not defined';
// $_SERVER['CONTENT_LENGTH'] = 'not defined';
$documentRoot = dirname(realpath($filePath));
$fileName = basename(realpath($filePath));
$documentRootShort = (substr($documentRoot,-strlen(DIRECTORY_SEPARATOR)) != DIRECTORY_SEPARATOR)
? $documentRoot
: substr($documentRoot,0,-strlen(DIRECTORY_SEPARATOR));
$documentRootFull = $documentRootShort.DIRECTORY_SEPARATOR;
$scriptName = (basename($shortScriptName) == $fileName)
? $shortScriptName
: $shortScriptName.((empty($shortScriptName) || substr($shortScriptName,-1) == '/') ? '' : '/').$fileName;
$hashPos = strpos($queryString,'#');
$queryStringWithoutHash =
(empty($queryString) || ($hashPos === 0))
? ''
: (
($hashPos === false)
? "?$queryString"
: '?'.sustr($queryString,0,$hashPos+1)
);
$_SERVER['SCRIPT_FILENAME'] = $documentRootFull.$fileName;
$_SERVER['SCRIPT_NAME'] = $scriptName;
$_SERVER['PHP_SELF'] = $scriptName.$pathInfo;
$_SERVER['PATH_INFO'] = $pathInfo;
$_SERVER['ORIG_PATH_INFO'] = $pathInfo;
$_SERVER['PATH_TRANSLATED'] = $documentRootShort.$pathInfo;
if (substr($shortScriptName,-1) == '/'){
if (!empty($pathInfo) && substr($pathInfo,0,1) == '/'){
$_SERVER['DOCUMENT_URI'] = $shortScriptName.substr($pathInfo,1);
} else {
$_SERVER['DOCUMENT_URI'] = $shortScriptName.$pathInfo;
}
} else if (empty($pathInfo) || substr($pathInfo,0,1) == '/'){
$_SERVER['DOCUMENT_URI'] = $shortScriptName.$pathInfo;
} else {
$_SERVER['DOCUMENT_URI'] = $shortScriptName.'/'.$pathInfo;
}
$_SERVER['REQUEST_URI'] = $_SERVER['DOCUMENT_URI'].$queryStringWithoutHash;
$_SERVER['DOCUMENT_ROOT'] = $documentRootShort;
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // forced
$_SERVER['REMOTE_PORT'] = '80'; // forced
$_SERVER['SERVER_ADDR'] = '127.0.0.1'; // forced
$_SERVER['SERVER_ADDR'] = '80'; // forced
$_SERVER['SERVER_NAME'] = 'localhost'; // forced
$_SERVER['HTTPS'] = null; // forced
}
}
/**
* save previous $_SERVER in $GLOBALS if existing
*/
public function saveSERVER()
{
if (!isset($GLOBALS['savedSERVER'])){
$GLOBALS['savedSERVER'] = [
'saved' => false,
'value' => null
];
}
if (!$GLOBALS['savedSERVER']['saved']){
if (!isset($_SERVER)){
$GLOBALS['savedSERVER']['value'] = null;
$GLOBALS['savedSERVER']['saved'] = true;
} else {
$GLOBALS['savedSERVER']['value'] = $_SERVER;
$GLOBALS['savedSERVER']['saved'] = true;
}
}
}
/**
* revert previous $_SERVER in $GLOBALS if existing
*/
public function revertSERVER()
{
if (isset($GLOBALS['savedSERVER']['saved']) && $GLOBALS['savedSERVER']['saved']){
if (is_null($GLOBALS['savedSERVER']['value'])){
unset($_SERVER);
} else {
$_SERVER = $GLOBALS['savedSERVER']['value'];
}
$GLOBALS['savedSERVER']['saved'] = false;
$GLOBALS['savedSERVER']['value'] = null;
}
} }
} }