Skip to content

Commit

Permalink
UHF-7126: Made request argument optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Oct 20, 2022
1 parent 6b51e23 commit 0bf4cd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
22 changes: 14 additions & 8 deletions src/ProxyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\helfi_proxy\Selector\AbsoluteUriAttributeSelector;
use Drupal\helfi_proxy\Selector\AttributeSelector;
use Drupal\helfi_proxy\Selector\MultiValueAttributeSelector;
use Drupal\helfi_proxy\Selector\SelectorInterface;
use Drupal\helfi_proxy\Selector\SelectorRepositoryTrait;
use Drupal\helfi_proxy\Selector\StringValue;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -49,7 +49,7 @@ public function getConfig(string $key, mixed $defaultValue = NULL) : mixed {
/**
* {@inheritdoc}
*/
public function processHtml(string $html, Request $request, array $selectors = []) : string {
public function processHtml(string $html, Request $request = NULL, array $selectors = []) : string {
$dom = new \DOMDocument();
$previousXmlErrorBehavior = libxml_use_internal_errors(TRUE);
$encoding = '<?xml encoding="utf-8" ?>';
Expand All @@ -66,8 +66,8 @@ public function processHtml(string $html, Request $request, array $selectors = [
$value = $this
->getAttributeValue(
$selector,
$request,
$row->getAttribute($selector->attribute),
$request
);

if (!$value) {
Expand All @@ -86,11 +86,14 @@ public function processHtml(string $html, Request $request, array $selectors = [
/**
* {@inheritdoc}
*/
public function processValue(string $value, Request $request, array $selectors = []) : string {
public function processValue(string $value, Request $request = NULL, array $selectors = []) : string {
$value = trim($value);

if (!$selectors) {
$selectors[] = new StringValue();
}
foreach ($selectors as $selector) {
$value = $this->getAttributeValue($selector, $request, $value);
$value = $this->getAttributeValue($selector, $value, $request);
}
return $value;
}
Expand All @@ -100,15 +103,15 @@ public function processValue(string $value, Request $request, array $selectors =
*
* @param \Drupal\helfi_proxy\Selector\SelectorInterface $selector
* The selector.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
* @param string|null $value
* The value.
* @param null|\Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return string|null
* The attribute value.
*/
private function getAttributeValue(SelectorInterface $selector, Request $request, ?string $value) : ? string {
private function getAttributeValue(SelectorInterface $selector, ?string $value, Request $request = NULL) : ? string {
// Skip if value is being served from CDN already.
if (!$value || $this->isCdnAddress($value)) {
return $value;
Expand All @@ -117,6 +120,9 @@ private function getAttributeValue(SelectorInterface $selector, Request $request
// Certain elements might be absolute URLs already (such as og:image:url).
// Make sure locally hosted files are always served from correct domain.
if ($selector instanceof AbsoluteUriAttributeSelector) {
if (!$request) {
throw new \LogicException('Missing required Request $request.');
}
$parts = parse_url($value);

if (empty($parts['path'])) {
Expand Down
8 changes: 4 additions & 4 deletions src/ProxyManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ interface ProxyManagerInterface {
*
* @param string $html
* The html to manipulate.
* @param \Symfony\Component\HttpFoundation\Request $request
* @param null|\Symfony\Component\HttpFoundation\Request $request
* The request.
* @param array $selectors
* The selectors.
*
* @return \Symfony\Component\HttpFoundation\Response
* The manipulated response.
*/
public function processHtml(string $html, Request $request, array $selectors = []) : string;
public function processHtml(string $html, Request $request = NULL, array $selectors = []) : string;

/**
* Processes a string value.
*
* @param string $value
* The value.
* @param \Symfony\Component\HttpFoundation\Request $request
* @param null|\Symfony\Component\HttpFoundation\Request $request
* The request.
* @param array $selectors
* The selectors.
*
* @return string
* The processed value.
*/
public function processValue(string $value, Request $request, array $selectors = []) : string;
public function processValue(string $value, Request $request = NULL, array $selectors = []) : string;

/**
* Whether the proxy is configured or not.
Expand Down
5 changes: 1 addition & 4 deletions tests/src/Kernel/ProxyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,8 @@ public function testAttributeSelector() : void {
*/
public function testStringValueSelector() : void {
$this->setAssetPath('test-assets');
$request = $this->createRequest();

$processed = $this->proxyManager()->processValue('/core/modules/system/test.js', $request, [
new StringValue(),
]);
$processed = $this->proxyManager()->processValue('/core/modules/system/test.js');
$this->assertEquals('/test-assets/core/modules/system/test.js', $processed);
}

Expand Down

0 comments on commit 0bf4cd3

Please sign in to comment.