/** * Implements module logic for given hook * * @param \AppserverIo\Psr\HttpMessage\RequestInterface $request A request object * @param \AppserverIo\Psr\HttpMessage\ResponseInterface $response A response object * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance * @param int $hook The current hook to process logic for * * @return bool * @throws \AppserverIo\Server\Exceptions\ModuleException */ public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook) { // In php an interface is, by definition, a fixed contract. It is immutable. // So we have to declare the right ones afterwards... /** * @var $request \AppserverIo\Psr\HttpMessage\RequestInterface */ /** * @var $request \AppserverIo\Psr\HttpMessage\ResponseInterface */ // if false hook is coming do nothing if (ModuleHooks::REQUEST_POST !== $hook) { return; } // set member ref for request context $this->requestContext = $requestContext; // We have to throw a ModuleException on failure, so surround the body with a try...catch block try { $requestUrl = $requestContext->getServerVar(ServerVars::HTTP_HOST) . $requestContext->getServerVar(ServerVars::X_REQUEST_URI); if (!isset($this->rules[$requestUrl])) { // Reset the $serverBackreferences array to avoid mixups of different requests $this->serverBackreferences = array(); // Resolve all used backreferences which are NOT linked to the query string. // We will resolve query string related backreferences separately as we are not able to cache them // as easily as, say, the URI // We also have to resolve all the changes rules in front of us made, so build up the backreferences // IN the loop. $this->fillContextBackreferences(); $this->fillHeaderBackreferences($request); // Get the rules as the array they are within the config. // We have to also collect any volatile rules which might be set on request base. // We might not even get anything, so prepare our rules accordingly $volatileRewrites = array(); if ($requestContext->hasModuleVar(ModuleVars::VOLATILE_REWRITES)) { $volatileRewrites = $requestContext->getModuleVar(ModuleVars::VOLATILE_REWRITES); } // Build up the complete ruleset, volatile rules up front $rules = array_merge($volatileRewrites, $this->configuredRules); $this->rules[$requestUrl] = array(); // Only act if we got something if (is_array($rules)) { // Convert the rules to our internally used objects foreach ($rules as $rule) { // Add the rule as a Rule object $rule = new Rule($rule['condition'], $rule['target'], $rule['flag']); $rule->resolve($this->serverBackreferences); $this->rules[$requestUrl][] = $rule; } } } // Iterate over all rules, resolve vars and apply the rule (if needed) foreach ($this->rules[$requestUrl] as $rule) { // Check if the rule matches, and if, apply the rule if ($rule->matches()) { // Apply the rule. If apply() returns false this means this was the last rule to process if ($rule->apply($requestContext, $response, $this->serverBackreferences) === false) { break; } } } } catch (\Exception $e) { // Re-throw as a ModuleException throw new ModuleException($e); } }
/** * Used to open up the parent's sortFlags() method for testing * * @param string $flagString The unsorted string of flags * * @return array */ public function sortFlags($flagString) { return parent::sortFlags($flagString); }
/** * Test for the getTarget() method * * @return void */ public function testGetTarget() { $rule = new Rule('^_Resources/.*', '/index.php', ''); $this->assertEquals('/index.php', $rule->getTarget()); }