Skip to content

Commit

Permalink
Process input request with a psr7 library.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed Mar 24, 2022
1 parent 1d699ec commit 697e8ac
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 53 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"psr/log": ">=1.0.0",
"psr/container": ">=1.0.0",
"pimple/pimple": "^3.0",
"nyholm/psr7": "^1.5",
"nyholm/psr7-server": "^1.0",
"jaxon-php/jaxon-utils": "^1.0"
},
"require-dev": {
Expand Down
23 changes: 11 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<phpunit bootstrap="vendor/autoload.php">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/logs/html" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="build/logs/html" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<testsuites>
<testsuite name="Jaxon Core">
<directory>tests</directory>
<directory>tests/Registration</directory>
<directory>tests/RequestFactory</directory>
</testsuite>
</testsuites>
<logging/>
</phpunit>
20 changes: 17 additions & 3 deletions src/Di/Traits/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use Jaxon\Ui\Dialogs\DialogFacade;
use Jaxon\Ui\Pagination\Paginator;
use Jaxon\Utils\Translation\Translator;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ServerRequestInterface;

trait RequestTrait
{
Expand All @@ -28,9 +31,20 @@ trait RequestTrait
*/
private function registerRequests()
{
// The server request
$this->set(ServerRequestInterface::class, function() {
$xRequestFactory = new Psr17Factory();
$xRequestCreator = new ServerRequestCreator(
$xRequestFactory, // ServerRequestFactory
$xRequestFactory, // UriFactory
$xRequestFactory, // UploadedFileFactory
$xRequestFactory // StreamFactory
);
return $xRequestCreator->fromGlobals();
});
// Argument Manager
$this->set(ArgumentManager::class, function($c) {
return new ArgumentManager($c->g(ConfigManager::class), $c->g(Translator::class));
return new ArgumentManager($c->g(ServerRequestInterface::class), $c->g(ConfigManager::class), $c->g(Translator::class));
});
// Callback Manager
$this->set(CallbackManager::class, function() {
Expand All @@ -40,8 +54,8 @@ private function registerRequests()
$this->set(RequestHandler::class, function($c) {
return new RequestHandler($c->g(Container::class), $c->g(ConfigManager::class),
$c->g(ArgumentManager::class), $c->g(PluginManager::class), $c->g(ResponseManager::class),
$c->g(CallbackManager::class), $c->g(UploadHandler::class), $c->g(DataBagPlugin::class),
$c->g(Translator::class));
$c->g(CallbackManager::class), $c->g(ServerRequestInterface::class), $c->g(UploadHandler::class),
$c->g(DataBagPlugin::class), $c->g(Translator::class));
});
// Request Factory
$this->set(Factory::class, function($c) {
Expand Down
5 changes: 4 additions & 1 deletion src/Plugin/Contract/RequestHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jaxon\Plugin\Contract;

use Jaxon\Request\Target;
use Psr\Http\Message\ServerRequestInterface;

interface RequestHandlerInterface
{
Expand All @@ -19,9 +20,11 @@ public function getTarget(): ?Target;
* Called by the <Jaxon\Plugin\PluginManager> when a request has been received to determine
* if the request is targeted to this request plugin.
*
* @param ServerRequestInterface $xRequest
*
* @return bool
*/
public static function canProcessRequest(): bool;
public static function canProcessRequest(ServerRequestInterface $xRequest): bool;

/**
* Process the current request
Expand Down
12 changes: 7 additions & 5 deletions src/Request/Handler/ArgumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Jaxon\Config\ConfigManager;
use Jaxon\Utils\Translation\Translator;
use Jaxon\Exception\RequestException;
use Psr\Http\Message\ServerRequestInterface;

use function strcasecmp;
use function is_numeric;
Expand Down Expand Up @@ -88,25 +89,26 @@ class ArgumentManager
*
* Get and decode the arguments of the HTTP request
*
* @param ServerRequestInterface $xRequest
* @param ConfigManager $xConfigManager
* @param Translator $xTranslator
*/
public function __construct(ConfigManager $xConfigManager, Translator $xTranslator)
public function __construct(ServerRequestInterface $xRequest, ConfigManager $xConfigManager, Translator $xTranslator)
{
$this->xConfigManager = $xConfigManager;
$this->xTranslator = $xTranslator;
$this->aArgs = [];
$this->nMethod = self::METHOD_UNKNOWN;

if(isset($_POST['jxnargs']))
if(is_array(($aBody = $xRequest->getParsedBody())) && isset($aBody['jxnargs']))
{
$this->nMethod = self::METHOD_POST;
$this->aArgs = $_POST['jxnargs'];
$this->aArgs = $aBody['jxnargs'];
}
elseif(isset($_GET['jxnargs']))
elseif(is_array(($aParams = $xRequest->getQueryParams())) && isset($aParams['jxnargs']))
{
$this->nMethod = self::METHOD_GET;
$this->aArgs = $_GET['jxnargs'];
$this->aArgs = $aParams['jxnargs'];
}
}

Expand Down
33 changes: 15 additions & 18 deletions src/Request/Handler/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
use Jaxon\Response\ResponseManager;
use Jaxon\Utils\Translation\Translator;
use Psr\Http\Message\ServerRequestInterface;

use Exception;

Expand Down Expand Up @@ -104,46 +105,42 @@ class RequestHandler
*/
private $xRequestPlugin = null;

/**
* @var ServerRequestInterface
*/
private $xRequest;

/**
* The constructor
*
* @param Container $di
* @param ConfigManager $xConfigManager
* @param ArgumentManager $xArgument
* @param ArgumentManager $xArgumentManager
* @param PluginManager $xPluginManager
* @param ResponseManager $xResponseManager
* @param CallbackManager $xCallbackManager
* @param ServerRequestInterface $xRequest
* @param UploadHandler|null $xUploadHandler
* @param DataBagPlugin $xDataBagPlugin
* @param Translator $xTranslator
*/
public function __construct(Container $di, ConfigManager $xConfigManager, ArgumentManager $xArgument,
public function __construct(Container $di, ConfigManager $xConfigManager, ArgumentManager $xArgumentManager,
PluginManager $xPluginManager, ResponseManager $xResponseManager, CallbackManager $xCallbackManager,
?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin, Translator $xTranslator)
ServerRequestInterface $xRequest, ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin,
Translator $xTranslator)
{
$this->di = $di;
$this->xConfigManager = $xConfigManager;
$this->xPluginManager = $xPluginManager;
$this->xResponseManager = $xResponseManager;
$this->xArgumentManager = $xArgument;
$this->xArgumentManager = $xArgumentManager;
$this->xCallbackManager = $xCallbackManager;
$this->xRequest = $xRequest;
$this->xUploadHandler = $xUploadHandler;
$this->xDataBagPlugin = $xDataBagPlugin;
$this->xTranslator = $xTranslator;
}

/**
* Return the method that was used to send the arguments from the client
*
* The method is one of: ArgumentManager::METHOD_UNKNOWN, ArgumentManager::METHOD_GET, ArgumentManager::METHOD_POST.
*
* @return int
*/
public function getRequestMethod(): int
{
return $this->xArgumentManager->getRequestMethod();
}

/**
* Return the array of arguments that were extracted and parsed from the GET or POST data
*
Expand Down Expand Up @@ -275,9 +272,9 @@ public function canProcessRequest(): bool
// Find a plugin to process the request
foreach($this->xPluginManager->getRequestHandlers() as $sClassName)
{
if($sClassName::canProcessRequest())
if($sClassName::canProcessRequest($this->xRequest))
{
$this->xRequestPlugin = $this->di->get($sClassName);
$this->xRequestPlugin = $this->di->g($sClassName);
return true;
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/Request/Plugin/CallableClass/CallableClassPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Jaxon\Utils\Translation\Translator;
use Jaxon\Exception\RequestException;
use Jaxon\Exception\SetupException;
use Psr\Http\Message\ServerRequestInterface;

use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -298,23 +299,23 @@ public function getScript(): string
/**
* @inheritDoc
*/
public static function canProcessRequest(): bool
public static function canProcessRequest(ServerRequestInterface $xRequest): bool
{
if(isset($_POST['jxncls']))
if(is_array(($aBody = $xRequest->getParsedBody())) && isset($aBody['jxncls']))
{
self::$sRequestedClass = trim($_POST['jxncls']);
self::$sRequestedClass = trim($aBody['jxncls']);
}
elseif(isset($_GET['jxncls']))
elseif(is_array(($aParams = $xRequest->getQueryParams())) && isset($aParams['jxncls']))
{
self::$sRequestedClass = trim($_GET['jxncls']);
self::$sRequestedClass = trim($aParams['jxncls']);
}
if(isset($_POST['jxnmthd']))
if(is_array(($aBody = $xRequest->getParsedBody())) && isset($aBody['jxnmthd']))
{
self::$sRequestedMethod = trim($_POST['jxnmthd']);
self::$sRequestedMethod = trim($aBody['jxnmthd']);
}
elseif(isset($_GET['jxnmthd']))
elseif(is_array(($aParams = $xRequest->getQueryParams())) && isset($aParams['jxnmthd']))
{
self::$sRequestedMethod = trim($_GET['jxnmthd']);
self::$sRequestedMethod = trim($aParams['jxnmthd']);
}
return (self::$sRequestedClass !== '' && self::$sRequestedMethod !== '');
}
Expand Down
11 changes: 6 additions & 5 deletions src/Request/Plugin/CallableFunction/CallableFunctionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Jaxon\Utils\Translation\Translator;
use Jaxon\Exception\RequestException;
use Jaxon\Exception\SetupException;
use Psr\Http\Message\ServerRequestInterface;

use function array_keys;
use function implode;
Expand Down Expand Up @@ -232,15 +233,15 @@ public function getScript(): string
/**
* @inheritDoc
*/
public static function canProcessRequest(): bool
public static function canProcessRequest(ServerRequestInterface $xRequest): bool
{
if(isset($_POST['jxnfun']))
if(is_array(($aBody = $xRequest->getParsedBody())) && isset($aBody['jxnfun']))
{
self::$sRequestedFunction = trim($_POST['jxnfun']);
self::$sRequestedFunction = trim($aBody['jxnfun']);
}
elseif(isset($_GET['jxnfun']))
elseif(is_array(($aParams = $xRequest->getQueryParams())) && isset($aParams['jxnfun']))
{
self::$sRequestedFunction = trim($_GET['jxnfun']);
self::$sRequestedFunction = trim($aParams['jxnfun']);
}
return (self::$sRequestedFunction !== '');
}
Expand Down

0 comments on commit 697e8ac

Please sign in to comment.