Skip to content

Commit 402aa31

Browse files
author
Alexey Filatev
committed
Add ability to retrieve site hosting properties
1 parent 35e0a4f commit 402aa31

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

src/PleskX/Api/Operator.php

100644100755
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ protected function _delete($field, $value, $deleteMethodName = 'del')
6262
* @param string $infoTag
6363
* @param string|null $field
6464
* @param integer|string|null $value
65+
* @param callable|null $filter
6566
* @return mixed
6667
*/
67-
protected function _getItems($structClass, $infoTag, $field = null, $value = null)
68+
protected function _getItems($structClass, $infoTag, $field = null, $value = null, callable $filter = null)
6869
{
6970
$packet = $this->_client->getPacket();
7071
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
@@ -80,6 +81,9 @@ protected function _getItems($structClass, $infoTag, $field = null, $value = nul
8081

8182
$items = [];
8283
foreach ($response->xpath('//result') as $xmlResult) {
84+
if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) {
85+
continue;
86+
}
8387
$items[] = new $structClass($xmlResult->data->$infoTag);
8488
}
8589

src/PleskX/Api/Operator/Site.php

100644100755
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class Site extends \PleskX\Api\Operator
88
{
9+
const PROPERTIES_HOSTING = 'hosting';
910

1011
/**
1112
* @param array $properties
@@ -18,9 +19,22 @@ public function create(array $properties)
1819

1920
$infoGeneral = $info->addChild('gen_setup');
2021
foreach ($properties as $name => $value) {
22+
if (!is_scalar($value)) {
23+
continue;
24+
}
2125
$infoGeneral->addChild($name, $value);
2226
}
2327

28+
// set hosting properties
29+
if (isset($properties[static::PROPERTIES_HOSTING]) && is_array($properties[static::PROPERTIES_HOSTING])) {
30+
$hostingNode = $info->addChild('hosting')->addChild('vrt_hst');
31+
foreach ($properties[static::PROPERTIES_HOSTING] as $name => $value) {
32+
$propertyNode = $hostingNode->addChild('property');
33+
$propertyNode->addChild('name', $name);
34+
$propertyNode->addChild('value', $value);
35+
}
36+
}
37+
2438
$response = $this->_client->request($packet);
2539
return new Struct\Info($response);
2640
}
@@ -46,6 +60,19 @@ public function get($field, $value)
4660
return reset($items);
4761
}
4862

63+
/**
64+
* @param string $field
65+
* @param integer|string $value
66+
* @return Struct\HostingInfo|null
67+
*/
68+
public function getHosting($field, $value)
69+
{
70+
$items = $this->_getItems(Struct\HostingInfo::class, 'hosting', $field, $value, function ($node) {
71+
return isset($node->vrt_hst);
72+
});
73+
return empty($items) ? null : reset($items);
74+
}
75+
4976
/**
5077
* @return Struct\GeneralInfo[]
5178
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3+
4+
namespace PleskX\Api\Struct\Site;
5+
6+
class HostingInfo extends \PleskX\Api\Struct
7+
{
8+
/** @var array */
9+
public $properties = [];
10+
11+
/** @var string */
12+
public $ipAddress;
13+
14+
public function __construct($apiResponse)
15+
{
16+
foreach ($apiResponse->vrt_hst->property as $property) {
17+
$this->properties[(string)$property->name] = (string)$property->value;
18+
}
19+
$this->_initScalarProperties($apiResponse->vrt_hst, [
20+
'ip_address',
21+
]);
22+
}
23+
}

tests/SiteTest.php

100644100755
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public static function tearDownAfterClass()
2020
static::$_client->webspace()->delete('id', static::$_webspace->id);
2121
}
2222

23-
private function _createSite($name)
23+
private function _createSite($name, array $properties = [])
2424
{
25-
return static::$_client->site()->create([
25+
$properties = array_merge([
2626
'name' => $name,
2727
'webspace-id' => static::$_webspace->id,
28-
]);
28+
], $properties);
29+
return static::$_client->site()->create($properties);
2930
}
3031

3132
public function testCreate()
@@ -56,6 +57,32 @@ public function testGet()
5657
static::$_client->site()->delete('id', $site->id);
5758
}
5859

60+
public function testGetHostingWoHosting()
61+
{
62+
$site = $this->_createSite('addon.dom');
63+
64+
$siteHosting = static::$_client->site()->getHosting('id', $site->id);
65+
$this->assertNull($siteHosting);
66+
67+
static::$_client->site()->delete('id', $site->id);
68+
}
69+
70+
public function testGetHostingWithHosting()
71+
{
72+
$properties = [
73+
'hosting' => [
74+
'www_root' => 'addon.dom'
75+
]
76+
];
77+
$site = $this->_createSite('addon.dom', $properties);
78+
79+
$siteHosting = static::$_client->site()->getHosting('id', $site->id);
80+
$this->assertArrayHasKey('www_root', $siteHosting->properties);
81+
$this->assertEquals('addon.dom', basename($siteHosting->properties['www_root']));
82+
83+
static::$_client->site()->delete('id', $site->id);
84+
}
85+
5986
public function testGetAll()
6087
{
6188
$site = $this->_createSite('addon.dom');

0 commit comments

Comments
 (0)