mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-28 08:14:04 +02:00
Add PropertyModifierParseURL.
Provide a PropertyModifier for the PHP function parse_url().
This commit is contained in:
parent
79482bbf95
commit
7b189ef3b1
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\PropertyModifier;
|
||||||
|
|
||||||
|
use Icinga\Exception\InvalidPropertyException;
|
||||||
|
use Icinga\Module\Director\Hook\PropertyModifierHook;
|
||||||
|
use Icinga\Module\Director\Web\Form\QuickForm;
|
||||||
|
|
||||||
|
class PropertyModifierParseURL extends PropertyModifierHook
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array with possible components that can be returned from URL.
|
||||||
|
*/
|
||||||
|
protected static $components = [
|
||||||
|
'scheme' => PHP_URL_SCHEME,
|
||||||
|
'host' => PHP_URL_HOST,
|
||||||
|
'port' => PHP_URL_PORT,
|
||||||
|
'path' => PHP_URL_PATH,
|
||||||
|
'query' => PHP_URL_QUERY,
|
||||||
|
'fragment' => PHP_URL_FRAGMENT,
|
||||||
|
];
|
||||||
|
|
||||||
|
public static function addSettingsFormFields(QuickForm $form)
|
||||||
|
{
|
||||||
|
$form->addElement('select', 'url_component', [
|
||||||
|
'label' => $form->translate('URL component'),
|
||||||
|
'description' => $form->translate('URL component'),
|
||||||
|
'multiOptions' => $form->optionalEnum(static::enumComponents()),
|
||||||
|
'required' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$form->addElement('select', 'on_failure', [
|
||||||
|
'label' => $form->translate('On failure'),
|
||||||
|
'description' => $form->translate(
|
||||||
|
'What should we do if the URL could not get parsed or component not found?'
|
||||||
|
),
|
||||||
|
'multiOptions' => $form->optionalEnum([
|
||||||
|
'null' => $form->translate('Set no value (null)'),
|
||||||
|
'keep' => $form->translate('Keep the property as is'),
|
||||||
|
'fail' => $form->translate('Let the whole import run fail'),
|
||||||
|
]),
|
||||||
|
'required' => true,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function enumComponents()
|
||||||
|
{
|
||||||
|
$components = array_keys(self::$components);
|
||||||
|
return array_combine($components, $components);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'Parse a URL and return its components';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transform($value)
|
||||||
|
{
|
||||||
|
$component = self::$components[$this->getSetting('url_component')];
|
||||||
|
$response = parse_url($value, $component);
|
||||||
|
|
||||||
|
// if component not found $response will be null, false if seriously malformed URL
|
||||||
|
if ($response === null || $response === false) {
|
||||||
|
switch ($this->getSetting('on_failure')) {
|
||||||
|
case 'null':
|
||||||
|
return null;
|
||||||
|
case 'keep':
|
||||||
|
return $value;
|
||||||
|
case 'fail':
|
||||||
|
default:
|
||||||
|
throw new InvalidPropertyException(
|
||||||
|
'Parsing URL "%s" failed.',
|
||||||
|
$value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
1
run.php
1
run.php
@ -53,6 +53,7 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
|
|||||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierUpperCaseFirst');
|
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierUpperCaseFirst');
|
||||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierRejectOrSelect');
|
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierRejectOrSelect');
|
||||||
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierArrayElementByPosition');
|
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierArrayElementByPosition');
|
||||||
|
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierParseURL');
|
||||||
|
|
||||||
$this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
|
$this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
|
||||||
$this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');
|
$this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');
|
||||||
|
@ -0,0 +1,147 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Icinga\Module\Director\PropertyModifier;
|
||||||
|
|
||||||
|
use Icinga\Module\Director\PropertyModifier\PropertyModifierParseURL;
|
||||||
|
use Icinga\Module\Director\Test\BaseTestCase;
|
||||||
|
|
||||||
|
class PropertyModifierParseURLTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
protected static $validurl = 'https://www.icinga.org/path/file.html?foo=bar#section';
|
||||||
|
protected static $invalidurl = 'http:///www.icinga.org/';
|
||||||
|
|
||||||
|
|
||||||
|
public function testModifierDoesNotSupportArraysItself()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$this->assertFalse($modifier->hasArraySupport());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEmptyPropertyReturnsNullOnfailureNull()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'query',
|
||||||
|
'on_failure' => 'null',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertNull($modifier->transform(''));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMissingComponentReturnsNullOnfailureNull()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'query',
|
||||||
|
'on_failure' => 'null',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertNull($modifier->transform('https://www.icinga.org/path/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMissingComponentReturnsPropertyOnfailureKeep()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'query',
|
||||||
|
'on_failure' => 'keep',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals('http://www.icinga.org/path/', $modifier->transform('http://www.icinga.org/path/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Icinga\Exception\InvalidPropertyException
|
||||||
|
*/
|
||||||
|
public function testMissingComponentThrowsExceptionOnfailureFail()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'query',
|
||||||
|
'on_failure' => 'fail',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$modifier->transform('http://www.icinga.org/path/');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testInvalidUrlReturnsNullOnfailureNull()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'host',
|
||||||
|
'on_failure' => 'null',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertNull($modifier->transform(self::$invalidurl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvalidUrlReturnsItselfOnfailureKeep()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'host',
|
||||||
|
'on_failure' => 'keep',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(self::$invalidurl, $modifier->transform(self::$invalidurl));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Icinga\Exception\InvalidPropertyException
|
||||||
|
*/
|
||||||
|
public function testInvalidUrlThrowsExceptionOnfailureFail()
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => 'host',
|
||||||
|
'on_failure' => 'fail',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$modifier->transform(self::$invalidurl);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataURLcomponentProvider
|
||||||
|
*/
|
||||||
|
public function testSuccessfullyParse($component, $result)
|
||||||
|
{
|
||||||
|
$modifier = new PropertyModifierParseURL();
|
||||||
|
$modifier->setSettings([
|
||||||
|
'url_component' => $component,
|
||||||
|
'on_failure' => 'null',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals($result, $modifier->transform(self::$validurl));
|
||||||
|
}
|
||||||
|
public function dataURLcomponentProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'scheme' => [
|
||||||
|
'scheme',
|
||||||
|
'https',
|
||||||
|
],
|
||||||
|
'host' => [
|
||||||
|
'host',
|
||||||
|
'www.icinga.org',
|
||||||
|
],
|
||||||
|
'port' => [
|
||||||
|
'port',
|
||||||
|
'',
|
||||||
|
],
|
||||||
|
'path' => [
|
||||||
|
'path',
|
||||||
|
'/path/file.html',
|
||||||
|
],
|
||||||
|
'query' => [
|
||||||
|
'query',
|
||||||
|
'foo=bar',
|
||||||
|
],
|
||||||
|
'fragment' => [
|
||||||
|
'fragment',
|
||||||
|
'section',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user