From f40eebdd69b51597d892cede8077767e62ab403d Mon Sep 17 00:00:00 2001 From: Aaron Belz Date: Mon, 26 Oct 2020 04:16:20 -0400 Subject: [PATCH 01/28] Add Guzzle version 7 support --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c9a450e..44ae289 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "require": { "php": ">=5.6.4", - "guzzlehttp/guzzle": "~6.0" + "guzzlehttp/guzzle": "^6.0|^7.0" }, "require-dev": { "mockery/mockery": "0.9.*", From 596a17b174e80caea80dff3ddc92238b4ac7faca Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 13:15:32 +0800 Subject: [PATCH 02/28] Refactor classes --- src/Actions/ManagesAllocations.php | 48 ---- src/Actions/ManagesEggs.php | 34 --- src/Actions/ManagesLocations.php | 70 ----- src/Actions/ManagesNests.php | 33 --- src/Actions/ManagesNodes.php | 70 ----- src/Actions/ManagesServers.php | 234 --------------- src/Actions/ManagesUsers.php | 83 ------ src/Actions/UsesServers.php | 72 ----- src/Exceptions/AccessDeniedHttpException.php | 18 ++ src/Http.php | 283 +++++++++++++++++++ src/MakesHttpRequests.php | 159 ----------- src/Managers/LocationManager.php | 73 +++++ src/Managers/Manager.php | 30 ++ src/Pterodactyl.php | 129 ++------- 14 files changed, 423 insertions(+), 913 deletions(-) delete mode 100644 src/Actions/ManagesAllocations.php delete mode 100644 src/Actions/ManagesEggs.php delete mode 100644 src/Actions/ManagesLocations.php delete mode 100644 src/Actions/ManagesNests.php delete mode 100644 src/Actions/ManagesNodes.php delete mode 100644 src/Actions/ManagesServers.php delete mode 100644 src/Actions/ManagesUsers.php delete mode 100644 src/Actions/UsesServers.php create mode 100644 src/Exceptions/AccessDeniedHttpException.php create mode 100644 src/Http.php delete mode 100644 src/MakesHttpRequests.php create mode 100644 src/Managers/LocationManager.php create mode 100644 src/Managers/Manager.php diff --git a/src/Actions/ManagesAllocations.php b/src/Actions/ManagesAllocations.php deleted file mode 100644 index ae53af8..0000000 --- a/src/Actions/ManagesAllocations.php +++ /dev/null @@ -1,48 +0,0 @@ -get("api/application/nodes/$nodeId".'/allocations?page='.$page); - } - - /** - * Create new allocation(s). - * - * @param int $nodeId - * @param array $data - * - * @return void - */ - public function createAllocation(int $nodeId, array $data) - { - return $this->post("api/application/nodes/$nodeId/allocations", $data); - } - - /** - * Delete the given allocation. - * - * @param int $nodeId - * @param int $allocationId - * @param array $data - * - * @return void - */ - public function deleteAllocation(int $nodeId, int $allocationId) - { - return $this->delete("api/application/nodes/$nodeId/allocations/$allocationId"); - } -} diff --git a/src/Actions/ManagesEggs.php b/src/Actions/ManagesEggs.php deleted file mode 100644 index 8eca706..0000000 --- a/src/Actions/ManagesEggs.php +++ /dev/null @@ -1,34 +0,0 @@ -get("api/application/nests/$nestId/eggs"); - } - - /** - * Get a egg instance. - * - * @param int $nestId - * @param int $eggId - * @param array $includes - * - * @return Egg - */ - public function egg(int $nestId, int $eggId, array $includes = []) - { - return $this->get("api/application/nests/$nestId/eggs/$eggId".$this->include($includes)); - } -} diff --git a/src/Actions/ManagesLocations.php b/src/Actions/ManagesLocations.php deleted file mode 100644 index 30b9230..0000000 --- a/src/Actions/ManagesLocations.php +++ /dev/null @@ -1,70 +0,0 @@ -get('api/application/locations?page='.$page); - } - - /** - * Get a location instance. - * - * @param int $locationId - * @param array $includes - * - * @return Location - */ - public function location(int $locationId, array $includes = []) - { - return $this->get("api/application/locations/$locationId".$this->include($includes)); - } - - /** - * Create a new location. - * - * @param array $data - * - * @return Location - */ - public function createLocation(array $data) - { - return $this->post('api/application/locations', $data); - } - - /** - * Update a specified location. - * - * @param int $locationId - * @param array $data - * - * @return Location - */ - public function updateLocation(int $locationId, array $data) - { - return $this->patch("api/application/locations/$locationId", $data); - } - - /** - * Delete the given location. - * - * @param int $locationId - * - * @return void - */ - public function deleteLocation(int $locationId) - { - return $this->delete("api/application/locations/$locationId"); - } -} diff --git a/src/Actions/ManagesNests.php b/src/Actions/ManagesNests.php deleted file mode 100644 index ce82c2f..0000000 --- a/src/Actions/ManagesNests.php +++ /dev/null @@ -1,33 +0,0 @@ -get('api/application/nests?page='.$page); - } - - /** - * Get a nest instance. - * - * @param int $nestId - * @param array $includes - * - * @return Nest - */ - public function nest(int $nestId, array $includes = []) - { - return $this->get("api/application/nests/$nestId".$this->include($includes)); - } -} diff --git a/src/Actions/ManagesNodes.php b/src/Actions/ManagesNodes.php deleted file mode 100644 index 7c2600e..0000000 --- a/src/Actions/ManagesNodes.php +++ /dev/null @@ -1,70 +0,0 @@ -get('api/application/nodes?page='.$page); - } - - /** - * Get a node instance. - * - * @param int $nodeId - * @param array $includes - * - * @return Node - */ - public function node(int $nodeId, array $includes = []) - { - return $this->get("api/application/nodes/$nodeId".$this->include($includes)); - } - - /** - * Create a new node. - * - * @param array $data - * - * @return Node - */ - public function createNode(array $data) - { - return $this->post('api/application/nodes', $data); - } - - /** - * Update a specified node. - * - * @param int $nodeId - * @param array $data - * - * @return Node - */ - public function updateNode(int $nodeId, array $data) - { - return $this->patch("api/application/nodes/$nodeId", $data); - } - - /** - * Delete the given node. - * - * @param int $nodeId - * - * @return void - */ - public function deleteNode(int $nodeId) - { - return $this->delete("api/application/nodes/$nodeId"); - } -} diff --git a/src/Actions/ManagesServers.php b/src/Actions/ManagesServers.php deleted file mode 100644 index bf27caa..0000000 --- a/src/Actions/ManagesServers.php +++ /dev/null @@ -1,234 +0,0 @@ -get('api/application/servers?page='.$page); - } - - /** - * Get a server instance. - * - * @param int $serverId - * @param array $includes - * - * @return Server - */ - public function server(int $serverId, array $includes = ['allocations']) - { - return $this->get("api/application/servers/$serverId".$this->include($includes)); - } - - /** - * Get a server instance by external id. - * - * @param string $externalId - * @param array $includes - * - * @return Server - */ - public function serverEx(string $externalId, array $includes = ['allocations']) - { - return $this->get("api/application/servers/external/$externalId".$this->include($includes)); - } - - /** - * Create a new server. - * - * @param array $data - * - * @return Server - */ - public function createServer(array $data) - { - return $this->post('api/application/servers', $data); - } - - /** - * Delete the given server. - * - * @param int $serverId - * - * @return void - */ - public function deleteServer(int $serverId) - { - return $this->delete("api/application/servers/$serverId"); - } - - /** - * Force delete the given server. - * - * @param int $serverId - * - * @return void - */ - public function forceDeleteServer(int $serverId) - { - return $this->delete("api/application/servers/$serverId/force"); - } - - /** - * Update details of the given server. - * - * @param int $serverId - * @param array $data - * - * @return Server - */ - public function updateServerDetails(int $serverId, array $data) - { - return $this->patch("api/application/servers/$serverId/details", $data); - } - - /** - * Update build configuration of the given server. - * - * @param int $serverId - * @param array $data - * - * @return Server - */ - public function updateServerBuild(int $serverId, array $data) - { - return $this->patch("api/application/servers/$serverId/build", $data); - } - - /** - * Update startup parameters of the given server. - * - * @param int $serverId - * @param array $data - * - * @return Server - */ - public function updateServerStartup(int $serverId, array $data) - { - return $this->patch("api/application/servers/$serverId/startup", $data); - } - - /** - * Suspend the given server. - * - * @param int $serverId - * - * @return void - */ - public function suspendServer(int $serverId) - { - return $this->post("api/application/servers/$serverId/suspend"); - } - - /** - * Unsuspend the given server. - * - * @param int $serverId - * - * @return void - */ - public function unsuspendServer(int $serverId) - { - return $this->post("api/application/servers/$serverId/unsuspend"); - } - - /** - * Reinstall the given server. - * - * @param int $serverId - * - * @return void - */ - public function reinstallServer(int $serverId) - { - return $this->post("api/application/servers/$serverId/reinstall"); - } - - /** - * Rebuild the given server. - * - * @param int $serverId - * - * @return void - */ - public function rebuildServer(int $serverId) - { - return $this->post("api/application/servers/$serverId/rebuild"); - } - - /** - * Get a collection of databases of a server. - * - * @param int $serverId - * - * @return ServerDatabase[] - */ - public function serverDatabases(int $serverId) - { - return $this->get("api/application/servers/$serverId/databases"); - } - - /** - * Get a database instance of a server. - * - * @param int $serverId - * @param int $databaseId - * - * @return ServerDatabase - */ - public function serverDatabase(int $serverId, int $databaseId) - { - return $this->get("api/application/servers/$serverId/databases/$databaseId"); - } - - /** - * Create a database for a server. - * - * @param int $serverId - * @param array $data - * - * @return ServerDatabase - */ - public function createServerDatabase(int $serverId, array $data) - { - return $this->post("api/application/servers/$serverId/databases/", $data); - } - - /** - * Reset the password of a server's database. - * - * @param int $serverId - * @param int $databaseId - * - * @return void - */ - public function resetServerDatabasePassword(int $serverId, int $databaseId) - { - return $this->post("api/application/servers/$serverId/databases/$databaseId/reset-password"); - } - - /** - * Delete the given database of a server. - * - * @param int $serverId - * @param int $databaseId - * - * @return void - */ - public function deleteServerDatabase(int $serverId, int $databaseId) - { - return $this->delete("api/application/servers/$serverId/databases/$databaseId"); - } -} diff --git a/src/Actions/ManagesUsers.php b/src/Actions/ManagesUsers.php deleted file mode 100644 index 6456269..0000000 --- a/src/Actions/ManagesUsers.php +++ /dev/null @@ -1,83 +0,0 @@ -get('api/application/users?page='.$page); - } - - /** - * Get a user instance. - * - * @param int $userId - * @param array $includes - * - * @return User - */ - public function user(int $userId, array $includes = []) - { - return $this->get("api/application/users/$userId".$this->include($includes)); - } - - /** - * Get a user instance by external id. - * - * @param int $userExternalId - * @param array $includes - * - * @return User - */ - public function userEx(int $userExternalId, array $includes = []) - { - return $this->get("api/application/users/external/$userExternalId".$this->include($includes)); - } - - /** - * Create a new user. - * - * @param array $data - * - * @return User - */ - public function createUser(array $data) - { - return $this->post('api/application/users', $data); - } - - /** - * Update a specified user. - * - * @param int $userId - * @param array $data - * - * @return User - */ - public function updateUser(int $userId, array $data) - { - return $this->patch("api/application/users/$userId", $data); - } - - /** - * Delete the given user. - * - * @param int $userId - * - * @return void - */ - public function deleteUser(int $userId) - { - return $this->delete("api/application/users/$userId"); - } -} diff --git a/src/Actions/UsesServers.php b/src/Actions/UsesServers.php deleted file mode 100644 index 2aa1c27..0000000 --- a/src/Actions/UsesServers.php +++ /dev/null @@ -1,72 +0,0 @@ -get('api/client?page='.$page); - } - - /** - * Gets the details of a given server. - * - * @param string $serverIdentifier - * @param array $includes - * - * @return Server[] - */ - public function getServer(string $serverIdentifier, array $includes = []) - { - return $this->get("api/client/servers/$serverIdentifier".$this->include($includes)); - } - - /** - * Toggle the power on a given server. - * - * @param string $serverIdentifier - * @param string $action - * - * @return void - */ - public function powerServer(string $serverIdentifier, string $action) - { - return $this->post("api/client/servers/$serverIdentifier/power", ['signal'=>"$action"]); - } - - /** - * Send a command to a given server. - * - * @param string $serverIdentifier - * @param string $command - * - * @return void - */ - public function commandServer(string $serverIdentifier, string $command) - { - return $this->post("api/client/servers/$serverIdentifier/command", ['command'=>"$command"]); - } - - /** - * Get the utilization of a given server. - * - * @param string $serverIdentifier - * - * @return Stats[] - */ - public function utilizationServer(string $serverIdentifier) - { - return $this->get("api/client/servers/$serverIdentifier/utilization"); - } -} diff --git a/src/Exceptions/AccessDeniedHttpException.php b/src/Exceptions/AccessDeniedHttpException.php new file mode 100644 index 0000000..a8b1029 --- /dev/null +++ b/src/Exceptions/AccessDeniedHttpException.php @@ -0,0 +1,18 @@ +pterodactyl = $pterodactyl; + + $this->baseUri = $this->pterodactyl->baseUri; + + $this->apiKey = $this->pterodactyl->apiKey; + + $this->guzzle = $guzzle ?: new Client([ + 'base_uri' => $this->baseUri, + 'http_errors' => false, + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + ]); + + return $this; + } + + /** + * Make a GET request and return the response. + * + * @param string $uri + * + * @return mixed + */ + public function get($uri, array $query = []) + { + return $this->request('GET', $uri, [], $query); + } + + /** + * Make a POST request and return the response. + * + * @param string $uri + * @param array $query + * @param array $payload + * + * @return mixed + */ + public function post($uri, array $query = [], array $payload = []) + { + return $this->request('POST', $uri, $query, $payload); + } + + /** + * Make a PUT request and return the response. + * + * @param string $uri + * @param array $query + * @param array $payload + * + * @return mixed + */ + public function put($uri, array $query = [], array $payload = []) + { + return $this->request('PUT', $uri, $query, $payload); + } + + /** + * Make a PATCH request and return the response. + * + * @param string $uri + * @param array $query + * @param array $payload + * + * @return mixed + */ + public function patch($uri, array $query = [], array $payload = []) + { + return $this->request('PATCH', $uri, $query, $payload); + } + + /** + * Make a DELETE request and return the response. + * + * @param string $uri + * @param array $query + * @param array $payload + * + * @return mixed + */ + public function delete($uri, array $query = [], array $payload = []) + { + return $this->request('DELETE', $uri, $query, $payload); + } + + /** + * Make request and return the response. + * + * @param string $method + * @param string $uri + * @param array $query + * @param array $payload + * + * @return mixed + */ + public function request($method, $uri, array $query = [], array $payload = []) + { + $url = $this->baseUri.$uri; + + $body = json_encode($payload); + + $token = $this->apiKey; + + $options['query'] = $query; + $options['body'] = $body; + $options['debug'] = false; + $options['headers']['Authorization'] = 'Bearer '.$token; + + $response = $this->guzzle->request($method, $uri, $options); + + if ($response->getStatusCode() != 200 && $response->getStatusCode() != 204 && $response->getStatusCode() != 201) { + return $this->handleRequestError($response); + } + + $responseBody = (string) $response->getBody(); + + return $this->transform(json_decode($responseBody, true)) ?: $responseBody; + } + + /** + * Transform response data to resources. + * + * @param array $response + * + * @return mixed + */ + protected function transform($response) + { + if (empty($response['object'])) { + return $response; + } + + switch ($response['object']) { + case 'list': + $response['data'] = array_map(function ($object) { + return $this->transform($object); + }, $response['data']); + + // Rename to use the collection resource. + $response['object'] = 'collection'; + break; + } + + if (isset($response['attributes']['relationships'])) { + $response['attributes']['relationships'] = array_map(function ($object) { + return $this->transform($object); + }, $response['attributes']['relationships']); + } + + $class = '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($response['object']); + + $resource = class_exists($class) ? new $class($response, $this->pterodactyl) : $response; + + return $resource; + } + + /** + * @param \Psr\Http\Message\ResponseInterface $response + * + * @return void + */ + private function handleRequestError(ResponseInterface $response) + { + switch($response->getStatusCode()) { + case 400: + throw new FailedActionException((string) $response->getBody()); + case 403: + throw new AccessDeniedHttpException(); + case 404: + throw new NotFoundException(); + case 422: + throw new ValidationException(json_decode((string) $response->getBody(), true)); + default: + throw new \Exception((string) $response->getBody()); + } + } + + /** + * Retry the callback or fail after x seconds. + * + * @param int $timeout + * @param callable $callback + * + * @return mixed + */ + public function retry($timeout, $callback) + { + $start = time(); + + beginning: + + $output = $callback(); + + if ($output) { + return $output; + } + + if (time() - $start < $timeout) { + sleep(5); + + goto beginning; + } + + throw new TimeoutException($output); + } + + /** + * Set a new timeout. + * + * @param int $timeout + * + * @return $this + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + + return $this; + } + + /** + * Get the timeout. + * + * @return int + */ + public function getTimeout() + { + return $this->timeout; + } +} diff --git a/src/MakesHttpRequests.php b/src/MakesHttpRequests.php deleted file mode 100644 index 54f3f6f..0000000 --- a/src/MakesHttpRequests.php +++ /dev/null @@ -1,159 +0,0 @@ -request('GET', $uri); - } - - /** - * Make a POST request to Pterodactyl servers and return the response. - * - * @param string $uri - * @param array $payload - * - * @return mixed - */ - private function post($uri, array $payload = []) - { - return $this->request('POST', $uri, $payload); - } - - /** - * Make a PUT request to Pterodactyl servers and return the response. - * - * @param string $uri - * @param array $payload - * - * @return mixed - */ - private function put($uri, array $payload = []) - { - return $this->request('PUT', $uri, $payload); - } - - /** - * Make a PATCH request to Pterodactyl servers and return the response. - * - * @param string $uri - * @param array $payload - * - * @return mixed - */ - private function patch($uri, array $payload = []) - { - return $this->request('PATCH', $uri, $payload); - } - - /** - * Make a DELETE request to Pterodactyl servers and return the response. - * - * @param string $uri - * @param array $payload - * - * @return mixed - */ - private function delete($uri, array $payload = []) - { - return $this->request('DELETE', $uri, $payload); - } - - /** - * Make request to Pterodactyl servers and return the response. - * - * @param string $verb - * @param string $uri - * @param array $payload - * - * @return mixed - */ - private function request($verb, $uri, array $payload = []) - { - $url = $this->baseUri.$uri; - - $body = json_encode($payload); - - $token = $this->apiKey; - - $options['body'] = $body; - $options['debug'] = false; - $options['headers']['Authorization'] = 'Bearer '.$token; - - $response = $this->guzzle->request($verb, $uri, $options); - - if ($response->getStatusCode() != 200 && $response->getStatusCode() != 204 && $response->getStatusCode() != 201) { - return $this->handleRequestError($response); - } - - $responseBody = (string) $response->getBody(); - - return $this->transform(json_decode($responseBody, true)) ?: $responseBody; - } - - /** - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return void - */ - private function handleRequestError(ResponseInterface $response) - { - if ($response->getStatusCode() == 422) { - throw new ValidationException(json_decode((string) $response->getBody(), true)); - } - - if ($response->getStatusCode() == 404) { - throw new NotFoundException(); - } - - if ($response->getStatusCode() == 400) { - throw new FailedActionException((string) $response->getBody()); - } - - throw new \Exception((string) $response->getBody()); - } - - /** - * Retry the callback or fail after x seconds. - * - * @param int $timeout - * @param callable $callback - * - * @return mixed - */ - public function retry($timeout, $callback) - { - $start = time(); - - beginning: - - $output = $callback(); - - if ($output) { - return $output; - } - - if (time() - $start < $timeout) { - sleep(5); - - goto beginning; - } - - throw new TimeoutException($output); - } -} diff --git a/src/Managers/LocationManager.php b/src/Managers/LocationManager.php new file mode 100644 index 0000000..81507f9 --- /dev/null +++ b/src/Managers/LocationManager.php @@ -0,0 +1,73 @@ +http->get('api/application/locations', array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a location instance. + * + * @param int $locationId + * @param array $includes + * + * @return Location + */ + public function get(int $locationId, array $query = []) + { + return $this->http->get("api/application/locations/$locationId", $query); + } + + /** + * Create a new location. + * + * @param array $data + * + * @return Location + */ + public function create(array $data) + { + return $this->http->post('api/application/locations', [], $data); + } + + /** + * Update a specified location. + * + * @param int $locationId + * @param array $data + * + * @return Location + */ + public function update(int $locationId, array $data) + { + return $this->http->patch("api/application/locations/$locationId", [], $data); + } + + /** + * Delete the given location. + * + * @param int $locationId + * + * @return void + */ + public function delete(int $locationId) + { + return $this->http->delete("api/application/locations/$locationId"); + } +} diff --git a/src/Managers/Manager.php b/src/Managers/Manager.php new file mode 100644 index 0000000..7256be8 --- /dev/null +++ b/src/Managers/Manager.php @@ -0,0 +1,30 @@ +pterodactyl = $pterodactyl; + + $this->http = $this->pterodactyl->http; + } +} diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index 021a2db..49a0527 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -2,47 +2,39 @@ namespace HCGCloud\Pterodactyl; -use GuzzleHttp\Client as HttpClient; +use GuzzleHttp\Client as Client; + +use HCGCloud\Pterodactyl\Managers\LocationManager; class Pterodactyl { - use MakesHttpRequests; - use Actions\ManagesUsers; - use Actions\ManagesServers; - use Actions\ManagesAllocations; - use Actions\UsesServers; - use Actions\ManagesNodes; - use Actions\ManagesNests; - use Actions\ManagesEggs; - use Actions\ManagesLocations; - /** - * The Pterodactyl API Key. + * The Pterodactyl base uri. * * @var string */ - public $apiKey; + public $baseUri; /** - * The Pterodactyl Base Uri. + * The Pterodactyl API key. * * @var string */ - public $baseUri; + public $apiKey; /** - * The Guzzle HTTP Client instance. + * The Http client. * - * @var \GuzzleHttp\Client + * @var Http */ - public $guzzle; - + public $http; + /** - * Number of seconds a request is retried. + * Location manager. * - * @var int + * @var LocationManager */ - public $timeout = 30; + public $locations; /** * Create a new Pterodactyl instance. @@ -52,97 +44,14 @@ class Pterodactyl * * @return void */ - public function __construct($apiKey, $baseUri, HttpClient $guzzle = null) + public function __construct($baseUri, $apiKey, Client $guzzle = null) { - $this->apiKey = $apiKey; - $this->baseUri = $baseUri; - $this->guzzle = $guzzle ?: new HttpClient([ - 'base_uri' => $this->baseUri, - 'http_errors' => false, - 'headers' => [ - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', - ], - ]); - } - - /** - * Transform response data to resources. - * - * @param array $response - * - * @return mixed - */ - protected function transform($response) - { - if (empty($response['object'])) { - return $response; - } - - switch ($response['object']) { - case 'list': - $data = array_map(function ($object) { - return $this->transform($object); - }, $response['data']); - - if (isset($response['meta'])) { - return [ - 'data' => $data, - 'meta' => $response['meta'], - ]; - } - - return $data; - } - - if (isset($response['attributes']['relationships'])) { - $response['attributes']['relationships'] = array_map(function ($object) { - return $this->transform($object); - }, $response['attributes']['relationships']); - } - - $class = '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($response['object']); - - $resource = class_exists($class) ? new $class($response['attributes'], $this) : $response['attributes']; - - return $resource; - } - - /** - * Transform include array to url parameter. - * - * @param array $includes - * - * @return string - */ - protected function include($includes) - { - return empty($includes) ? '' : '?include='.implode(',', $includes); - } - - /** - * Set a new timeout. - * - * @param int $timeout - * - * @return $this - */ - public function setTimeout($timeout) - { - $this->timeout = $timeout; - - return $this; - } + $this->apiKey = $apiKey; + + $this->http = new Http($this, $guzzle); - /** - * Get the timeout. - * - * @return int - */ - public function getTimeout() - { - return $this->timeout; + $this->locations = new LocationManager($this); } } From 2d380b1a12326ef395df5e0f2c50b8213f1135d7 Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 13:16:11 +0800 Subject: [PATCH 03/28] Refactor resources --- src/Resources/Allocation.php | 33 ---- src/Resources/Egg.php | 75 -------- src/Resources/Location.php | 39 +--- src/Resources/Nest.php | 70 -------- src/Resources/Node.php | 189 ------------------- src/Resources/Resource.php | 5 +- src/Resources/Server.php | 300 ------------------------------- src/Resources/ServerDatabase.php | 74 -------- src/Resources/Stats.php | 26 --- src/Resources/User.php | 104 ----------- 10 files changed, 5 insertions(+), 910 deletions(-) diff --git a/src/Resources/Allocation.php b/src/Resources/Allocation.php index dfe11b2..ed7384e 100644 --- a/src/Resources/Allocation.php +++ b/src/Resources/Allocation.php @@ -4,38 +4,5 @@ class Allocation extends Resource { - /** - * The id of the allocation. - * - * @var int - */ - public $id; - /** - * The ip address of the allocation. - * - * @var string - */ - public $ip; - - /** - * The ip alias of the allocation. - * - * @var string - */ - public $alias; - - /** - * The port of the allocation. - * - * @var int - */ - public $port; - - /** - * The assign status of the allocation. - * - * @var bool - */ - public $assigned; } diff --git a/src/Resources/Egg.php b/src/Resources/Egg.php index 13cfb83..980cc22 100644 --- a/src/Resources/Egg.php +++ b/src/Resources/Egg.php @@ -4,80 +4,5 @@ class Egg extends Resource { - /** - * The id of the egg. - * - * @var int - */ - public $id; - /** - * The uuid of the egg. - * - * @var string - */ - public $uuid; - - /** - * The nest of the egg. - * - * @var int - */ - public $nest; - - /** - * The author of the egg. - * - * @var string - */ - public $author; - - /** - * The description of the egg. - * - * @var string - */ - public $description; - - /** - * The docker image url of the egg. - * - * @var string - */ - public $dockerImage; - - /** - * The config of the egg. - * - * @var array - */ - public $config = []; - - /** - * The startup of the egg. - * - * @var string - */ - public $startup; - - /** - * The script of the egg. - * - * @var array - */ - public $script = []; - - /** - * The date/time the egg was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the egg was updated. - * - * @var string - */ - public $updatedAt; } diff --git a/src/Resources/Location.php b/src/Resources/Location.php index c0966c9..3a61564 100644 --- a/src/Resources/Location.php +++ b/src/Resources/Location.php @@ -4,41 +4,6 @@ class Location extends Resource { - /** - * The id of the location. - * - * @var int - */ - public $id; - - /** - * The short name of the location. - * - * @var string - */ - public $short; - - /** - * The long name of the location. - * - * @var string - */ - public $long; - - /** - * The date/time the location was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the location was updated. - * - * @var string - */ - public $updatedAt; - /** * Delete the given location. * @@ -46,7 +11,7 @@ class Location extends Resource */ public function delete() { - return $this->pterodactyl->deleteLocation($this->id); + return $this->pterodactyl->locations->delete($this->id); } /** @@ -61,6 +26,6 @@ public function update(array $data = []) 'long' => $this->long, ], $data); - return $this->pterodactyl->updateLocation($this->id, $data); + return $this->pterodactyl->locations->update($this->id, $data); } } diff --git a/src/Resources/Nest.php b/src/Resources/Nest.php index 52993d4..ffcee76 100644 --- a/src/Resources/Nest.php +++ b/src/Resources/Nest.php @@ -4,75 +4,5 @@ class Nest extends Resource { - /** - * The id of the nest. - * - * @var int - */ - public $id; - /** - * The uuid of the nest. - * - * @var string - */ - public $uuid; - - /** - * The author of the nest. - * - * @var string - */ - public $author; - - /** - * The name of the nest. - * - * @var string - */ - public $name; - - /** - * The description of the nest. - * - * @var string - */ - public $description; - - /** - * The date/time the nest was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the nest was updated. - * - * @var string - */ - public $updatedAt; - - /** - * Get a collection of eggs in the given nest. - * - * @return Egg[] - */ - public function eggs() - { - return $this->pterodactyl->eggs($this->id); - } - - /** - * Get a egg instance in the given nest. - * - * @param int $eggId - * @param array $includes - * - * @return Egg - */ - public function egg($eggId, array $includes = []) - { - return $this->pterodactyl->egg($this->id, $eggId, $includes); - } } diff --git a/src/Resources/Node.php b/src/Resources/Node.php index 6dc5bde..9899f68 100644 --- a/src/Resources/Node.php +++ b/src/Resources/Node.php @@ -4,194 +4,5 @@ class Node extends Resource { - /** - * The id of the node. - * - * @var int - */ - public $id; - /** - * The node is public or not. - * - * @var int - */ - public $public; - - /** - * The name of the node. - * - * @var string - */ - public $name; - - /** - * The description of the node. - * - * @var string - */ - public $description; - - /** - * The location id of the node. - * - * @var int - */ - public $locationId; - - /** - * The fqdn of the node. - * - * @var string - */ - public $fqdn; - - /** - * The scheme of the node. - * - * @var string - */ - public $scheme; - - /** - * The behind proxy set of the node. - * - * @var boolen - */ - public $behindProxy; - - /** - * The maintenance mode status of the node. - * - * @var string - */ - public $maintenanceMode; - - /** - * The memory of the node. - * - * @var int - */ - public $memory; - - /** - * The memory overallocate of the node. - * - * @var int - */ - public $memoryOverallocate; - - /** - * The disk of the node. - * - * @var int - */ - public $disk; - - /** - * The disk overallocate of the node. - * - * @var int - */ - public $diskOverallocate; - - /** - * The upload size of the node. - * - * @var int - */ - public $uploadSize; - - /** - * The daemon listen port of the node. - * - * @var int - */ - public $daemonListen; - - /** - * The daemon sftp port of the node. - * - * @var int - */ - public $daemonSftp; - - /** - * The daemon base of the node. - * - * @var string - */ - public $daemonBase; - - /** - * The date/time the node was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the node was updated. - * - * @var string - */ - public $updatedAt; - - /** - * Delete the given node. - * - * @return void - */ - public function delete() - { - return $this->pterodactyl->deleteNode($this->id); - } - - /** - * Update the given node. - * - * @param array $data - * - * @return void - */ - public function update(array $data) - { - return $this->pterodactyl->updateNode($this->id, $data); - } - - /** - * Get a collection of allocations of the given node. - * - * @param int $page - * - * @return array - */ - public function allocations(int $page = 1) - { - return $this->pterodactyl->allocations($this->id, $page); - } - - /** - * Create new allocation(s) of the given node. - * - * @param array $data - * - * @return void - */ - public function createAllocation(array $data) - { - return $this->pterodactyl->createAllocation($this->id, $data); - } - - /** - * Delete a allocation of the given node. - * - * @param int $allocationId - * - * @return void - */ - public function deleteAllocation(int $allocationId) - { - return $this->pterodactyl->deleteAllocation($this->id, $allocationId); - } } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index e72f593..ac6ca48 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -33,6 +33,7 @@ public function __construct(array $attributes, $pterodactyl = null) $attributes = isset($attributes['attributes']) ? array_merge($attributes, $attributes['attributes']) : $attributes; + $this->attributes = $attributes; $this->pterodactyl = $pterodactyl; @@ -44,7 +45,7 @@ public function __construct(array $attributes, $pterodactyl = null) * * @return void */ - private function fill() + protected function fill() { foreach ($this->attributes as $key => $value) { $key = $this->camelCase($key); @@ -58,7 +59,7 @@ private function fill() * * @param $key */ - private function camelCase($key) + protected function camelCase($key) { $parts = explode('_', $key); diff --git a/src/Resources/Server.php b/src/Resources/Server.php index 0348578..d8fa345 100644 --- a/src/Resources/Server.php +++ b/src/Resources/Server.php @@ -4,305 +4,5 @@ class Server extends Resource { - /** - * The id of the server. - * - * @var int - */ - public $id; - /** - * The external id of the server. - * - * @var int - */ - public $externalId; - - /** - * The uuid of the server. - * - * @var int - */ - public $uuid; - - /** - * The short uuid of the server. - * - * @var string - */ - public $identifier; - - /** - * The node id of the server. - * - * @var string - */ - public $node; - - /** - * The name of the server. - * - * @var string - */ - public $name; - - /** - * The description of the server. - * - * @var string - */ - public $description; - - /** - * The suspended status of the server. - * - * @var int - */ - public $suspended; - - /** - * The pack id for the server. - * - * @var int - */ - public $pack; - - /** - * The date/time the server was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the server was updated. - * - * @var string - */ - public $updatedAt; - - /** - * The limits of the server. - * - * @var array - */ - public $limits = []; - - /** - * The feature limits of the server. - * - * @var array - */ - public $featureLimits = []; - - /** - * The allocations of the server. - * - * @var array - */ - public $allocations = []; - - /** - * The container of the server. - * - * @var array - */ - public $container = []; - - /** - * Delete the given server. - * - * @return void - */ - public function delete() - { - return $this->pterodactyl->deleteServer($this->id); - } - - /** - * Force delete the given server. - * - * @return void - */ - public function forceDelete() - { - return $this->pterodactyl->forceDeleteServer($this->id); - } - - /** - * Suspend the server. - * - * @return void - */ - public function suspend() - { - return $this->pterodactyl->suspendServer($this->id); - } - - /** - * Unsuspend the server. - * - * @return void - */ - public function unsuspend() - { - return $this->pterodactyl->unsuspendServer($this->id); - } - - /** - * Update details of the server. - * - * @param array $data - * - * @return void - */ - public function updateDetails(array $data = []) - { - $data = array_merge([ - 'name' => $this->name, - 'user' => $this->user, - ], $data); - - return $this->pterodactyl->updateServerDetails($this->id, $data); - } - - /** - * Update build configuration of the server. - * - * @param array $data - * - * @return void - */ - public function updateBuild(array $data = []) - { - $data = array_merge([ - 'allocation' => $this->allocation, - 'limits' => $this->limits, - 'feature_limits' => $this->featureLimits, - ], $data); - - return $this->pterodactyl->updateServerBuild($this->id, $data); - } - - /** - * Update startup parameters of the server. - * - * @param array $data - * - * @return void - */ - public function updateStartup(array $data = []) - { - $data = array_merge([ - 'startup' => $this->container['startup_command'], - 'egg' => $this->egg, - 'image' => $this->container['image'], - 'environment' => $this->container['environment'], - 'skip_scripts' => 0, - ], $data); - - return $this->pterodactyl->updateServerStartup($this->id, $data); - } - - /** - * Reinstall the server. - * - * @return void - */ - public function reinstall() - { - return $this->pterodactyl->reinstallServer($this->id); - } - - /** - * Rebuild the server. - * - * @return void - */ - public function rebuild() - { - return $this->pterodactyl->rebuildServer($this->id); - } - - /** - * Power the server. - * - * @param string $action - * - * @return void - */ - public function power(string $action) - { - return $this->pterodactyl->powerServer($this->identifier, $action); - } - - /** - * Send command to the server. - * - * @param string $command - * - * @return void - */ - public function command(string $command) - { - return $this->pterodactyl->commandServer($this->identifier, $command); - } - - /** - * Get a collection of databases of the server. - * - * @return ServerDatabase[] - */ - public function databases() - { - return $this->pterodactyl->serverDatabases($this->id); - } - - /** - * Get a database instance of the server. - * - * @param int $databaseId - * - * @return ServerDatabase - */ - public function database(int $databaseId) - { - return $this->pterodactyl->serverDatabase($this->id, $databaseId); - } - - /** - * Create a database for the server. - * - * @param array $data - * - * @return ServerDatabase - */ - public function createDatabase(array $data) - { - return $this->pterodactyl->createServerDatabase($this->id, $data); - } - - /** - * Reset a database password of the server. - * - * @param int $databaseId - * - * @return void - */ - public function resetDatabasePassword(int $databaseId) - { - return $this->pterodactyl->resetServerDatabasePassword($this->id, $databaseId); - } - - /** - * Delete a database of the server. - * - * @param int $databaseId - * - * @return void - */ - public function deleteDatabase(int $databaseId) - { - return $this->pterodactyl->deleteServerDatabase($this->id, $databaseId); - } } diff --git a/src/Resources/ServerDatabase.php b/src/Resources/ServerDatabase.php index e20a9bd..c65b3d1 100644 --- a/src/Resources/ServerDatabase.php +++ b/src/Resources/ServerDatabase.php @@ -4,79 +4,5 @@ class ServerDatabase extends Resource { - /** - * The id of the server's database. - * - * @var int - */ - public $id; - /** - * The server id of the server's database. - * - * @var int - */ - public $server; - - /** - * The host id of the server's database. - * - * @var int - */ - public $host; - - /** - * The name of the server's database. - * - * @var string - */ - public $database; - - /** - * The username of the server's database. - * - * @var string - */ - public $username; - - /** - * The remote hostname of the server's database. - * - * @var string - */ - public $remote; - - /** - * The date/time the server was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the server was updated. - * - * @var string - */ - public $updatedAt; - - /** - * Reset password of the given server's database. - * - * @return void - */ - public function resetPassword() - { - return $this->pterodactyl->resetServerDatabasePassword($this->server, $this->id); - } - - /** - * Delete the given database of a server. - * - * @return void - */ - public function delete() - { - return $this->pterodactyl->deleteServerDatabase($this->server, $this->id); - } } diff --git a/src/Resources/Stats.php b/src/Resources/Stats.php index 0daaa8a..936a8d8 100644 --- a/src/Resources/Stats.php +++ b/src/Resources/Stats.php @@ -4,31 +4,5 @@ class Stats extends Resource { - /** - * The cpu of the stats. - * - * @var array - */ - public $cpu = []; - /** - * The disk of the stats. - * - * @var array - */ - public $disk = []; - - /** - * The memory of the stats. - * - * @var array - */ - public $memory = []; - - /** - * The state of the server. - * - * @var string - */ - public $state; } diff --git a/src/Resources/User.php b/src/Resources/User.php index 55e4f2f..00196c5 100644 --- a/src/Resources/User.php +++ b/src/Resources/User.php @@ -4,109 +4,5 @@ class User extends Resource { - /** - * The id of the user. - * - * @var int - */ - public $id; - /** - * The external id of the user. - * - * @var int - */ - public $externalId; - - /** - * The uuid of the user. - * - * @var int - */ - public $uuid; - - /** - * The username of the user. - * - * @var string - */ - public $username; - - /** - * The email of the user. - * - * @var string - */ - public $email; - - /** - * The first name of the user. - * - * @var string - */ - public $firstName; - - /** - * The last name of the user. - * - * @var string - */ - public $lastName; - - /** - * The language of the user. - * - * @var string - */ - public $language; - - /** - * If a user is an admin. - * - * @var int - */ - public $rootAdmin; - - /** - * The date/time the user was created. - * - * @var string - */ - public $createdAt; - - /** - * The date/time the user was updated. - * - * @var string - */ - public $updatedAt; - - /** - * Delete the given user. - * - * @return void - */ - public function delete() - { - return $this->pterodactyl->deleteUser($this->id); - } - - /** - * Update the given user. - * - * @param array $data - * - * @return void - */ - public function update(array $data = []) - { - $data = array_merge([ - 'username' => $this->username, - 'email' => $this->email, - 'first_name' => $this->firstName, - 'last_name' => $this->lastName, - ], $data); - - return $this->pterodactyl->updateUser($this->id, $data); - } } From cc1babe8779750ee07801d917b0022cffaa1ef04 Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 13:16:30 +0800 Subject: [PATCH 04/28] Remove SubUser resource --- src/Resources/SubUser.php | 48 --------------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 src/Resources/SubUser.php diff --git a/src/Resources/SubUser.php b/src/Resources/SubUser.php deleted file mode 100644 index 709b9c6..0000000 --- a/src/Resources/SubUser.php +++ /dev/null @@ -1,48 +0,0 @@ - Date: Wed, 14 Oct 2020 13:16:41 +0800 Subject: [PATCH 05/28] Add new resources --- src/Resources/ApiKey.php | 8 ++ src/Resources/Backup.php | 8 ++ src/Resources/Collection.php | 116 ++++++++++++++++++++++++++++ src/Resources/DatabasePassword.php | 8 ++ src/Resources/Databases.php | 8 ++ src/Resources/EggVariable.php | 8 ++ src/Resources/FileObject.php | 8 ++ src/Resources/RecoveryTokens.php | 8 ++ src/Resources/ScheduleTask.php | 8 ++ src/Resources/ServerSchedule.php | 8 ++ src/Resources/ServerSubuser.php | 8 ++ src/Resources/SignedUrl.php | 8 ++ src/Resources/SystemPermissions.php | 8 ++ 13 files changed, 212 insertions(+) create mode 100644 src/Resources/ApiKey.php create mode 100644 src/Resources/Backup.php create mode 100644 src/Resources/Collection.php create mode 100644 src/Resources/DatabasePassword.php create mode 100644 src/Resources/Databases.php create mode 100644 src/Resources/EggVariable.php create mode 100644 src/Resources/FileObject.php create mode 100644 src/Resources/RecoveryTokens.php create mode 100644 src/Resources/ScheduleTask.php create mode 100644 src/Resources/ServerSchedule.php create mode 100644 src/Resources/ServerSubuser.php create mode 100644 src/Resources/SignedUrl.php create mode 100644 src/Resources/SystemPermissions.php diff --git a/src/Resources/ApiKey.php b/src/Resources/ApiKey.php new file mode 100644 index 0000000..35d4a80 --- /dev/null +++ b/src/Resources/ApiKey.php @@ -0,0 +1,8 @@ +attributes = $attributes; + + $this->pterodactyl = $pterodactyl; + } + + /** + * Convert resource to array. + * + * @return Array + */ + public function toArray() + { + return $this->all(); + } + + /** + * Get meta data of resource. + * + * @return Array + */ + public function meta() + { + return $this->attributes['meta']; + } + + public function all() + { + return $this->attributes['data']; + } + + public function get($offset) + { + return $this->attributes['data'][$offset]; + } + + public function set($offset, $value) + { + $this->attributes['data'][$offset] = $value; + } + + public function has($offset) + { + return isset($this->attributes['data'][$offset]); + } + + public function forget($offset) + { + unset($this->attributes['data'][$offset]); + } + + public function offsetGet($offset) + { + return $this->offsetExists($offset) ? $this->get($offset) : null; + } + + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + public function offsetUnset($offset) + { + if ($this->offsetExists($offset)) { + $this->forget($offset); + } + } + + public function offsetExists($offset) + { + return $this->has($offset); + } + + public function jsonSerialize() + { + return $this->attributes['data']; + } + + public function serialize() + { + return serialize($this->attributes['data']); + } + + public function unserialize($serialized) + { + return $this->attributes = unserialize($serialized); + } + + public function toJson($option = JSON_UNESCAPED_UNICODE) + { + return json_encode($this->all(), $option); + } +} diff --git a/src/Resources/DatabasePassword.php b/src/Resources/DatabasePassword.php new file mode 100644 index 0000000..33684f5 --- /dev/null +++ b/src/Resources/DatabasePassword.php @@ -0,0 +1,8 @@ + Date: Wed, 14 Oct 2020 15:39:08 +0800 Subject: [PATCH 06/28] Add apiType and update structure --- src/Exceptions/InvaildApiTypeException.php | 18 +++ src/Http.php | 13 +- .../{ => Application}/LocationManager.php | 22 +-- src/Managers/Application/UserManager.php | 90 +++++++++++++ src/Managers/Client/AccountManager.php | 20 +++ src/Pterodactyl.php | 51 ++++++- src/Resources/Allocation.php | 8 -- src/Resources/ApiKey.php | 8 -- src/Resources/Application/Allocation.php | 10 ++ src/Resources/Application/ApiKey.php | 10 ++ src/Resources/Application/Backup.php | 10 ++ .../Application/DatabasePassword.php | 10 ++ src/Resources/Application/Databases.php | 10 ++ src/Resources/Application/Egg.php | 10 ++ src/Resources/Application/EggVariable.php | 10 ++ src/Resources/Application/FileObject.php | 10 ++ src/Resources/{ => Application}/Location.php | 8 +- src/Resources/Application/Nest.php | 10 ++ src/Resources/Application/Node.php | 10 ++ src/Resources/Application/RecoveryTokens.php | 10 ++ src/Resources/Application/ScheduleTask.php | 10 ++ src/Resources/Application/Server.php | 10 ++ src/Resources/Application/ServerDatabase.php | 10 ++ src/Resources/Application/ServerSchedule.php | 10 ++ src/Resources/Application/ServerSubuser.php | 10 ++ src/Resources/Application/SignedUrl.php | 10 ++ src/Resources/Application/Stats.php | 10 ++ .../Application/SystemPermissions.php | 10 ++ src/Resources/Application/User.php | 45 +++++++ src/Resources/Backup.php | 8 -- src/Resources/Collection.php | 7 +- src/Resources/DatabasePassword.php | 8 -- src/Resources/Databases.php | 8 -- src/Resources/Egg.php | 8 -- src/Resources/EggVariable.php | 8 -- src/Resources/FileObject.php | 8 -- src/Resources/Nest.php | 8 -- src/Resources/Node.php | 8 -- src/Resources/RecoveryTokens.php | 8 -- src/Resources/Resource.php | 125 ++++++++++++++---- src/Resources/ScheduleTask.php | 8 -- src/Resources/Server.php | 8 -- src/Resources/ServerDatabase.php | 8 -- src/Resources/ServerSchedule.php | 8 -- src/Resources/ServerSubuser.php | 8 -- src/Resources/SignedUrl.php | 8 -- src/Resources/Stats.php | 8 -- src/Resources/SystemPermissions.php | 8 -- src/Resources/User.php | 8 -- 49 files changed, 540 insertions(+), 209 deletions(-) create mode 100644 src/Exceptions/InvaildApiTypeException.php rename src/Managers/{ => Application}/LocationManager.php (61%) create mode 100644 src/Managers/Application/UserManager.php create mode 100644 src/Managers/Client/AccountManager.php delete mode 100644 src/Resources/Allocation.php delete mode 100644 src/Resources/ApiKey.php create mode 100644 src/Resources/Application/Allocation.php create mode 100644 src/Resources/Application/ApiKey.php create mode 100644 src/Resources/Application/Backup.php create mode 100644 src/Resources/Application/DatabasePassword.php create mode 100644 src/Resources/Application/Databases.php create mode 100644 src/Resources/Application/Egg.php create mode 100644 src/Resources/Application/EggVariable.php create mode 100644 src/Resources/Application/FileObject.php rename src/Resources/{ => Application}/Location.php (75%) create mode 100644 src/Resources/Application/Nest.php create mode 100644 src/Resources/Application/Node.php create mode 100644 src/Resources/Application/RecoveryTokens.php create mode 100644 src/Resources/Application/ScheduleTask.php create mode 100644 src/Resources/Application/Server.php create mode 100644 src/Resources/Application/ServerDatabase.php create mode 100644 src/Resources/Application/ServerSchedule.php create mode 100644 src/Resources/Application/ServerSubuser.php create mode 100644 src/Resources/Application/SignedUrl.php create mode 100644 src/Resources/Application/Stats.php create mode 100644 src/Resources/Application/SystemPermissions.php create mode 100644 src/Resources/Application/User.php delete mode 100644 src/Resources/Backup.php delete mode 100644 src/Resources/DatabasePassword.php delete mode 100644 src/Resources/Databases.php delete mode 100644 src/Resources/Egg.php delete mode 100644 src/Resources/EggVariable.php delete mode 100644 src/Resources/FileObject.php delete mode 100644 src/Resources/Nest.php delete mode 100644 src/Resources/Node.php delete mode 100644 src/Resources/RecoveryTokens.php delete mode 100644 src/Resources/ScheduleTask.php delete mode 100644 src/Resources/Server.php delete mode 100644 src/Resources/ServerDatabase.php delete mode 100644 src/Resources/ServerSchedule.php delete mode 100644 src/Resources/ServerSubuser.php delete mode 100644 src/Resources/SignedUrl.php delete mode 100644 src/Resources/Stats.php delete mode 100644 src/Resources/SystemPermissions.php delete mode 100644 src/Resources/User.php diff --git a/src/Exceptions/InvaildApiTypeException.php b/src/Exceptions/InvaildApiTypeException.php new file mode 100644 index 0000000..6937ddb --- /dev/null +++ b/src/Exceptions/InvaildApiTypeException.php @@ -0,0 +1,18 @@ +apiKey = $this->pterodactyl->apiKey; + $this->apiType = $this->pterodactyl->apiType; + $this->guzzle = $guzzle ?: new Client([ 'base_uri' => $this->baseUri, 'http_errors' => false, @@ -148,7 +157,7 @@ public function delete($uri, array $query = [], array $payload = []) */ public function request($method, $uri, array $query = [], array $payload = []) { - $url = $this->baseUri.$uri; + $uri = $this->baseUri . '/api/' . $this->apiType . '/' . $uri; $body = json_encode($payload); @@ -200,7 +209,7 @@ protected function transform($response) }, $response['attributes']['relationships']); } - $class = '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($response['object']); + $class = '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.ucwords($response['object']); $resource = class_exists($class) ? new $class($response, $this->pterodactyl) : $response; diff --git a/src/Managers/LocationManager.php b/src/Managers/Application/LocationManager.php similarity index 61% rename from src/Managers/LocationManager.php rename to src/Managers/Application/LocationManager.php index 81507f9..ea2caac 100644 --- a/src/Managers/LocationManager.php +++ b/src/Managers/Application/LocationManager.php @@ -1,22 +1,26 @@ http->get('api/application/locations', array_merge([ + return $this->http->get('locations', array_merge([ 'page' => $page ], $query)); } @@ -31,7 +35,7 @@ public function all(int $page = 1, array $query = []) */ public function get(int $locationId, array $query = []) { - return $this->http->get("api/application/locations/$locationId", $query); + return $this->http->get("locations/$locationId", $query); } /** @@ -43,7 +47,7 @@ public function get(int $locationId, array $query = []) */ public function create(array $data) { - return $this->http->post('api/application/locations', [], $data); + return $this->http->post('locations', [], $data); } /** @@ -56,7 +60,7 @@ public function create(array $data) */ public function update(int $locationId, array $data) { - return $this->http->patch("api/application/locations/$locationId", [], $data); + return $this->http->patch("locations/$locationId", [], $data); } /** @@ -68,6 +72,6 @@ public function update(int $locationId, array $data) */ public function delete(int $locationId) { - return $this->http->delete("api/application/locations/$locationId"); + return $this->http->delete("locations/$locationId"); } } diff --git a/src/Managers/Application/UserManager.php b/src/Managers/Application/UserManager.php new file mode 100644 index 0000000..042998a --- /dev/null +++ b/src/Managers/Application/UserManager.php @@ -0,0 +1,90 @@ +http->get('users', array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a user instance by user id. + * + * @param int $userId + * @param array $includes + * + * @return User + */ + public function get(int $userId, array $query = []) + { + return $this->http->get("users/$userId", $query); + } + + /** + * Get a user instance by user id. + * + * @param string $externalId + * @param array $includes + * + * @return User + */ + public function getByExternalid(string $externalId, array $query = []) + { + return $this->http->get("users/external/$externalId", $query); + } + + /** + * Create a new user. + * + * @param array $data + * + * @return User + */ + public function create(array $data) + { + return $this->http->post('users', [], $data); + } + + /** + * Update a specified user. + * + * @param int $userId + * @param array $data + * + * @return User + */ + public function update(int $userId, array $data) + { + return $this->http->patch("users/$userId", [], $data); + } + + /** + * Delete the given user. + * + * @param int $userId + * + * @return void + */ + public function delete(int $userId) + { + return $this->http->delete("users/$userId"); + } +} diff --git a/src/Managers/Client/AccountManager.php b/src/Managers/Client/AccountManager.php new file mode 100644 index 0000000..a0d3013 --- /dev/null +++ b/src/Managers/Client/AccountManager.php @@ -0,0 +1,20 @@ +http->get('api/client/account'); + } +} diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index 49a0527..996b95d 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -4,7 +4,11 @@ use GuzzleHttp\Client as Client; -use HCGCloud\Pterodactyl\Managers\LocationManager; +use HCGCloud\Pterodactyl\Exceptions\InvaildApiTypeException; + +use HCGCloud\Pterodactyl\Managers\Application\UserManager as ApplicationUserManager; +use HCGCloud\Pterodactyl\Managers\Application\LocationManager as ApplicationLocationManager; +use HCGCloud\Pterodactyl\Managers\Client\AccountManager as ClientAccountManager; class Pterodactyl { @@ -22,19 +26,40 @@ class Pterodactyl */ public $apiKey; + /** + * The API type of the API key. + * + * @var string + */ + public $apiType; + /** * The Http client. * - * @var Http + * @var Client */ public $http; + /** + * Account Manager. + * + * @var ClientAccountManager + */ + public $account; + /** * Location manager. * - * @var LocationManager + * @var ApplicationLocationManager */ public $locations; + + /** + * User Manager. + * + * @var ApplicationUserManager + */ + public $users; /** * Create a new Pterodactyl instance. @@ -44,14 +69,28 @@ class Pterodactyl * * @return void */ - public function __construct($baseUri, $apiKey, Client $guzzle = null) + public function __construct($baseUri, $apiKey, $apiType = 'application', Client $guzzle = null) { $this->baseUri = $baseUri; $this->apiKey = $apiKey; - + + if(!in_array($apiType, ['application', 'client'], true)) { + throw new InvaildApiTypeException(); + } + $this->apiType = $apiType; + $this->http = new Http($this, $guzzle); - $this->locations = new LocationManager($this); + $that = $this; + switch($this->apiType) { + case 'application': + $this->locations = new ApplicationLocationManager($that); + $this->users = new ApplicationUserManager($that); + break; + case 'client': + $this->account = new ClientAccountManager($that); + break; + } } } diff --git a/src/Resources/Allocation.php b/src/Resources/Allocation.php deleted file mode 100644 index ed7384e..0000000 --- a/src/Resources/Allocation.php +++ /dev/null @@ -1,8 +0,0 @@ -update(array_merge([ + 'email' => $this->email, + 'username' => $this->username, + 'first_name' => $this->first_name, + 'last_name' => $this->last_name], $this->getChangedData() + )); + } + + /** + * Update the user. + * + * @param array $data + * @return void + */ + public function update(array $data) + { + $this->pterodactyl->users->update($this->id, $data); + } + + /** + * Delete the user. + * + * @return void + */ + public function delete() + { + $this->pterodactyl->users->delete($this->id); + } +} diff --git a/src/Resources/Backup.php b/src/Resources/Backup.php deleted file mode 100644 index 47dbdab..0000000 --- a/src/Resources/Backup.php +++ /dev/null @@ -1,8 +0,0 @@ -attributes = unserialize($serialized); - } - - public function toJson($option = JSON_UNESCAPED_UNICODE) - { - return json_encode($this->all(), $option); + return $this->attributes['data'] = unserialize($serialized); } } diff --git a/src/Resources/DatabasePassword.php b/src/Resources/DatabasePassword.php deleted file mode 100644 index 33684f5..0000000 --- a/src/Resources/DatabasePassword.php +++ /dev/null @@ -1,8 +0,0 @@ -attributes = $attributes; + $this->origin = $this->attributes = $attributes; + $this->pterodactyl = $pterodactyl; + } - $this->fill(); + public function getChangedData() + { + $data = array_diff($this->attributes, $this->origin); + + $this->origin = $this->attributes; + + return $data; } - /** - * Fill the resource with the array of attributes. - * - * @return void - */ - protected function fill() + public function __set($key, $value) { - foreach ($this->attributes as $key => $value) { - $key = $this->camelCase($key); + $this->attributes[$key] = $value; + } - $this->{$key} = $value; - } + public function __get($key) + { + return $this->attributes[$key]; + } + + public function __isset($key) + { + return isset($this->attributes[$key]); + } + + public function __unset($key) + { + unset($this->attributes[$key]); } /** - * Convert the key name to camel case. + * Convert resource to array. * - * @param $key + * @return Array */ - protected function camelCase($key) + public function toArray() { - $parts = explode('_', $key); + return $this->all(); + } - foreach ($parts as $i => $part) { - if ($i !== 0) { - $parts[$i] = ucfirst($part); - } + public function all() + { + return $this->attributes; + } + + public function get($offset) + { + return $this->attributes[$offset]; + } + + public function set($offset, $value) + { + $this->attributes[$offset] = $value; + } + + public function has($offset) + { + return isset($this->attributes[$offset]); + } + + public function forget($offset) + { + unset($this->attributes[$offset]); + } + + public function offsetGet($offset) + { + return $this->offsetExists($offset) ? $this->get($offset) : null; + } + + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + public function offsetUnset($offset) + { + if ($this->offsetExists($offset)) { + $this->forget($offset); } + } + + public function offsetExists($offset) + { + return $this->has($offset); + } - return str_replace(' ', '', implode(' ', $parts)); + public function jsonSerialize() + { + return $this->attributes; + } + + public function serialize() + { + return serialize($this->attributes); + } + + public function unserialize($serialized) + { + return $this->attributes = unserialize($serialized); } } diff --git a/src/Resources/ScheduleTask.php b/src/Resources/ScheduleTask.php deleted file mode 100644 index 0840614..0000000 --- a/src/Resources/ScheduleTask.php +++ /dev/null @@ -1,8 +0,0 @@ - Date: Wed, 14 Oct 2020 16:03:00 +0800 Subject: [PATCH 07/28] Fix request uri --- src/Managers/Client/AccountManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Managers/Client/AccountManager.php b/src/Managers/Client/AccountManager.php index a0d3013..ed708fd 100644 --- a/src/Managers/Client/AccountManager.php +++ b/src/Managers/Client/AccountManager.php @@ -9,12 +9,12 @@ class AccountManager extends Manager { /** - * Get information of the account + * Get information of the account. * * @return User */ public function details() { - return $this->http->get('api/client/account'); + return $this->http->get('account'); } } From b93016c6f65a53723c2a17806f0229be41d6a506 Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 16:09:12 +0800 Subject: [PATCH 08/28] Add Client/User resource --- src/Resources/Client/User.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/Resources/Client/User.php diff --git a/src/Resources/Client/User.php b/src/Resources/Client/User.php new file mode 100644 index 0000000..e8dfa6f --- /dev/null +++ b/src/Resources/Client/User.php @@ -0,0 +1,10 @@ + Date: Wed, 14 Oct 2020 16:22:13 +0800 Subject: [PATCH 09/28] Fix annotations --- src/Managers/Application/LocationManager.php | 2 +- src/Managers/Application/UserManager.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Managers/Application/LocationManager.php b/src/Managers/Application/LocationManager.php index ea2caac..1c59282 100644 --- a/src/Managers/Application/LocationManager.php +++ b/src/Managers/Application/LocationManager.php @@ -29,7 +29,7 @@ public function paginate(int $page = 1, array $query = []) * Get a location instance. * * @param int $locationId - * @param array $includes + * @param array $query * * @return Location */ diff --git a/src/Managers/Application/UserManager.php b/src/Managers/Application/UserManager.php index 042998a..ebf11bc 100644 --- a/src/Managers/Application/UserManager.php +++ b/src/Managers/Application/UserManager.php @@ -29,7 +29,7 @@ public function paginate(int $page = 1, array $query = []) * Get a user instance by user id. * * @param int $userId - * @param array $includes + * @param array $query * * @return User */ @@ -39,7 +39,7 @@ public function get(int $userId, array $query = []) } /** - * Get a user instance by user id. + * Get a user instance by user external id. * * @param string $externalId * @param array $includes From 7c7eb3f9ef31c7a4ea9831570e424db5b276cd05 Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 16:28:29 +0800 Subject: [PATCH 10/28] Fix class selection for collection instance --- src/Http.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Http.php b/src/Http.php index e47bac7..91646f0 100644 --- a/src/Http.php +++ b/src/Http.php @@ -209,7 +209,9 @@ protected function transform($response) }, $response['attributes']['relationships']); } - $class = '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.ucwords($response['object']); + $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.ucwords($response['object'])) ? + '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.ucwords($response['object']) : + '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($response['object']); $resource = class_exists($class) ? new $class($response, $this->pterodactyl) : $response; From a7a87a19fb9eb7dc94de0a71706633d7890abbed Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 16:38:21 +0800 Subject: [PATCH 11/28] Add Application/NestManager --- src/Managers/Application/Nest/EggManager.php | 42 ++++++++++++++ src/Managers/Application/NestManager.php | 58 ++++++++++++++++++++ src/Pterodactyl.php | 13 ++++- 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/Managers/Application/Nest/EggManager.php create mode 100644 src/Managers/Application/NestManager.php diff --git a/src/Managers/Application/Nest/EggManager.php b/src/Managers/Application/Nest/EggManager.php new file mode 100644 index 0000000..4828d5f --- /dev/null +++ b/src/Managers/Application/Nest/EggManager.php @@ -0,0 +1,42 @@ +http->get("nests/$nestId/eggs", array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a egg instance by id. + * + * @param int $nestId + * @param int $eggId + * @param array $query + * + * @return Egg + */ + public function get(int $nestId, int $eggId, array $query = []) + { + return $this->http->get("nests/$nestId/eggs/$eggId", $query); + } +} diff --git a/src/Managers/Application/NestManager.php b/src/Managers/Application/NestManager.php new file mode 100644 index 0000000..4e15762 --- /dev/null +++ b/src/Managers/Application/NestManager.php @@ -0,0 +1,58 @@ +eggs = new EggManager($pterodactyl); + } + + /** + * Get a paginated collection of nests. + * + * @param int $page + * @param array $query + * + * @return Collection + */ + public function paginate(int $page = 1, array $query = []) + { + return $this->http->get('nests', array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a nest instance by id. + * + * @param int $nestId + * @param array $query + * + * @return Nest + */ + public function get(int $nestId, array $query = []) + { + return $this->http->get("nests/$nestId", $query); + } +} diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index 996b95d..66b6828 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -8,6 +8,7 @@ use HCGCloud\Pterodactyl\Managers\Application\UserManager as ApplicationUserManager; use HCGCloud\Pterodactyl\Managers\Application\LocationManager as ApplicationLocationManager; +use HCGCloud\Pterodactyl\Managers\Application\NestManager as ApplicationNestManager; use HCGCloud\Pterodactyl\Managers\Client\AccountManager as ClientAccountManager; class Pterodactyl @@ -41,7 +42,7 @@ class Pterodactyl public $http; /** - * Account Manager. + * Account manager. * * @var ClientAccountManager */ @@ -55,12 +56,19 @@ class Pterodactyl public $locations; /** - * User Manager. + * User manager. * * @var ApplicationUserManager */ public $users; + /** + * Nest manager. + * + * @var ApplicationNestManager + */ + public $nests; + /** * Create a new Pterodactyl instance. * @@ -87,6 +95,7 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client case 'application': $this->locations = new ApplicationLocationManager($that); $this->users = new ApplicationUserManager($that); + $this->nests = new ApplicationNestManager($that); break; case 'client': $this->account = new ClientAccountManager($that); From d3fef323b52f852d6c164cb84aae5984a7b4f16e Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 14 Oct 2020 17:30:42 +0800 Subject: [PATCH 12/28] Improve eggs management --- src/Managers/Application/Nest/EggManager.php | 42 ----------------- src/Managers/Application/NestManager.php | 47 +++++++++++++------- src/Resources/Application/Nest.php | 28 ++++++++++++ 3 files changed, 59 insertions(+), 58 deletions(-) delete mode 100644 src/Managers/Application/Nest/EggManager.php diff --git a/src/Managers/Application/Nest/EggManager.php b/src/Managers/Application/Nest/EggManager.php deleted file mode 100644 index 4828d5f..0000000 --- a/src/Managers/Application/Nest/EggManager.php +++ /dev/null @@ -1,42 +0,0 @@ -http->get("nests/$nestId/eggs", array_merge([ - 'page' => $page - ], $query)); - } - - /** - * Get a egg instance by id. - * - * @param int $nestId - * @param int $eggId - * @param array $query - * - * @return Egg - */ - public function get(int $nestId, int $eggId, array $query = []) - { - return $this->http->get("nests/$nestId/eggs/$eggId", $query); - } -} diff --git a/src/Managers/Application/NestManager.php b/src/Managers/Application/NestManager.php index 4e15762..bea1d49 100644 --- a/src/Managers/Application/NestManager.php +++ b/src/Managers/Application/NestManager.php @@ -6,28 +6,13 @@ use HCGCloud\Pterodactyl\Managers\Manager; -use HCGCloud\Pterodactyl\Managers\Application\Nest\EggManager; - use HCGCloud\Pterodactyl\Resources\Collection; use HCGCloud\Pterodactyl\Resources\Application\Nest; +use HCGCloud\Pterodactyl\Resources\Application\Egg; class NestManager extends Manager { - - /** - * Eggs manager. - * - * @var EggManager - */ - public $eggs; - - public function __construct(Pterodactyl $pterodactyl) - { - parent::__construct($pterodactyl); - $this->eggs = new EggManager($pterodactyl); - } - /** * Get a paginated collection of nests. * @@ -55,4 +40,34 @@ public function get(int $nestId, array $query = []) { return $this->http->get("nests/$nestId", $query); } + + /** + * Get a paginated collection of eggs. + * + * @param int $nestId + * @param int $page + * @param array $query + * + * @return Collection + */ + public function eggs(int $nestId, int $page = 1, array $query = []) + { + return $this->http->get("nests/$nestId/eggs", array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a egg instance by id. + * + * @param int $nestId + * @param int $eggId + * @param array $query + * + * @return Egg + */ + public function egg(int $nestId, int $eggId, array $query = []) + { + return $this->http->get("nests/$nestId/eggs/$eggId", $query); + } } diff --git a/src/Resources/Application/Nest.php b/src/Resources/Application/Nest.php index f767ea5..1f2a1fc 100644 --- a/src/Resources/Application/Nest.php +++ b/src/Resources/Application/Nest.php @@ -2,9 +2,37 @@ namespace HCGCloud\Pterodactyl\Resources\Application; +use HCGCloud\Pterodactyl\Resources\Collection; + use HCGCloud\Pterodactyl\Resources\Resource; class Nest extends Resource { + /** + * Get a paginated collection of eggs. + * + * @param int $page + * @param array $query + * + * @return Collection + */ + public function eggs(int $page = 1, array $query = []) + { + return $this->pterodactyl->nests->eggs($this->id, $page, $query); + } + + /** + * Get a egg instance by id. + * + * @param int $eggId + * @param array $query + * + * @return Egg + */ + public function egg(int $eggId, array $query = []) + { + return $this->pterodactyl->nests->egg($this->id, $eggId, $query); + } + } From ebf54efe61c85d28870493306a343583dc4006d6 Mon Sep 17 00:00:00 2001 From: TLingC Date: Tue, 20 Oct 2020 12:46:25 +0800 Subject: [PATCH 13/28] Remove unnecessary import --- src/Managers/Application/NestManager.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Managers/Application/NestManager.php b/src/Managers/Application/NestManager.php index bea1d49..4c64763 100644 --- a/src/Managers/Application/NestManager.php +++ b/src/Managers/Application/NestManager.php @@ -2,8 +2,6 @@ namespace HCGCloud\Pterodactyl\Managers\Application; -use HCGCloud\Pterodactyl\Pterodactyl; - use HCGCloud\Pterodactyl\Managers\Manager; use HCGCloud\Pterodactyl\Resources\Collection; From 019b6ae3bd03e8a19e584393c27dccf200458a93 Mon Sep 17 00:00:00 2001 From: TLingC Date: Tue, 20 Oct 2020 13:07:51 +0800 Subject: [PATCH 14/28] Improve annotation --- src/Resources/Application/Location.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Resources/Application/Location.php b/src/Resources/Application/Location.php index 3c09af9..b8525df 100644 --- a/src/Resources/Application/Location.php +++ b/src/Resources/Application/Location.php @@ -18,6 +18,8 @@ public function delete() /** * Update the location. + * + * @param array $data * * @return void */ From 72d9314d6e714b18ec5f6c3345a8c68e6f505b77 Mon Sep 17 00:00:00 2001 From: TLingC Date: Tue, 20 Oct 2020 13:09:50 +0800 Subject: [PATCH 15/28] Add node manager --- src/Managers/Application/NodeManager.php | 133 +++++++++++++++++++++++ src/Pterodactyl.php | 9 ++ src/Resources/Application/Node.php | 57 ++++++++++ 3 files changed, 199 insertions(+) create mode 100644 src/Managers/Application/NodeManager.php diff --git a/src/Managers/Application/NodeManager.php b/src/Managers/Application/NodeManager.php new file mode 100644 index 0000000..6f651f4 --- /dev/null +++ b/src/Managers/Application/NodeManager.php @@ -0,0 +1,133 @@ +http->get('nodes', array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a node instance by id. + * + * @param int $nodeId + * @param array $query + * + * @return Node + */ + public function get(int $nodeId, array $query = []) + { + return $this->http->get("nodes/$nodeId", $query); + } + + /** + * Get node configuration by id. + * + * @param int $nodeId + * @param array $query + * + * @return array + */ + public function getConfiguration(int $nodeId) + { + return $this->http->get("nodes/$nodeId/configuration"); + } + + /** + * Create a new node. + * + * @param array $data + * + * @return Node + */ + public function create(array $data) + { + return $this->http->post('nodes', [], $data); + } + + /** + * Update a specified node. + * + * @param int $nodeId + * @param array $data + * + * @return Node + */ + public function update(int $nodeId, array $data) + { + return $this->http->patch("nodes/$nodeId", [], $data); + } + + /** + * Delete the given node. + * + * @param int $nodeId + * + * @return void + */ + public function delete(int $nodeId) + { + return $this->http->delete("nodes/$nodeId"); + } + + /** + * Get a paginated collection of node allocations. + * + * @param int $nodeId + * @param int $page + * @param array $query + * + * @return Collection + */ + public function allocations(int $nodeId, int $page = 1, array $query = []) + { + return $this->http->get("nodes/$nodeId/allocations", array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Create a new allocation for a node. + * + * @param int $nodeId + * @param array $data + * + * @return User + */ + public function createAllocation(int $nodeId, array $data) + { + return $this->http->post("nodes/$nodeId/allocations", [], $data); + } + + /** + * Delete the given allocation of a node. + * + * @param int $nodeId + * @param int $allocationId + * + * @return void + */ + public function deleteAllocation(int $nodeId, int $allocationId) + { + return $this->http->delete("nodes/$nodeId/allocations/$allocationId"); + } +} diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index 66b6828..e387b8d 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -8,6 +8,7 @@ use HCGCloud\Pterodactyl\Managers\Application\UserManager as ApplicationUserManager; use HCGCloud\Pterodactyl\Managers\Application\LocationManager as ApplicationLocationManager; +use HCGCloud\Pterodactyl\Managers\Application\NodeManager as ApplicationNodeManager; use HCGCloud\Pterodactyl\Managers\Application\NestManager as ApplicationNestManager; use HCGCloud\Pterodactyl\Managers\Client\AccountManager as ClientAccountManager; @@ -69,6 +70,13 @@ class Pterodactyl */ public $nests; + /** + * Node manager. + * + * @var ApplicationNodeManager + */ + public $nodes; + /** * Create a new Pterodactyl instance. * @@ -96,6 +104,7 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client $this->locations = new ApplicationLocationManager($that); $this->users = new ApplicationUserManager($that); $this->nests = new ApplicationNestManager($that); + $this->nodes = new ApplicationNodeManager($that); break; case 'client': $this->account = new ClientAccountManager($that); diff --git a/src/Resources/Application/Node.php b/src/Resources/Application/Node.php index 62a23f4..3f7c1ec 100644 --- a/src/Resources/Application/Node.php +++ b/src/Resources/Application/Node.php @@ -6,5 +6,62 @@ class Node extends Resource { + /** + * Delete the node. + * + * @return void + */ + public function delete() + { + return $this->pterodactyl->nodes->delete($this->id); + } + /** + * Update the node. + * + * @param array $data + * + * @return void + */ + public function update(array $data = []) + { + return $this->pterodactyl->nodes->update($this->id, $data); + } + + /** + * Get a paginated collection of node allocations. + * + * @param int $page + * @param array $query + * + * @return Collection + */ + public function allocations(int $page = 1, array $query = []) + { + return $this->pterodactyl->nodes->allocations($this->id, $page, $query); + } + + /** + * Create a new allocation for a node. + * + * @param array $data + * + * @return User + */ + public function createAllocation(array $data) + { + return $this->pterodactyl->nodes->createAllocation($this->id, $data); + } + + /** + * Delete the given allocation of a node. + * + * @param int $allocationId + * + * @return void + */ + public function deleteAllocation(int $allocationId) + { + return $this->pterodactyl->nodes->deleteAllocation($this->id, $allocationId); + } } From c81d12485db61f472cf351bf1da517d3e5924e49 Mon Sep 17 00:00:00 2001 From: TLingC Date: Tue, 20 Oct 2020 13:12:58 +0800 Subject: [PATCH 16/28] Improve managers initialization --- src/Pterodactyl.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index e387b8d..4ebeedd 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -98,16 +98,15 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client $this->http = new Http($this, $guzzle); - $that = $this; switch($this->apiType) { case 'application': - $this->locations = new ApplicationLocationManager($that); - $this->users = new ApplicationUserManager($that); - $this->nests = new ApplicationNestManager($that); - $this->nodes = new ApplicationNodeManager($that); + $this->locations = new ApplicationLocationManager($this); + $this->users = new ApplicationUserManager($this); + $this->nests = new ApplicationNestManager($this); + $this->nodes = new ApplicationNodeManager($this); break; case 'client': - $this->account = new ClientAccountManager($that); + $this->account = new ClientAccountManager($this); break; } } From 198c00d986c200055b055472a6486932ec891dbb Mon Sep 17 00:00:00 2001 From: TLingC Date: Tue, 20 Oct 2020 13:38:37 +0800 Subject: [PATCH 17/28] Change license --- LICENSE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.md b/LICENSE.md index f1b03a3..eb1d7d7 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,7 +1,7 @@ # MIT License Copyright (c) 2018 FruitBytes\ -Copyright (c) 2019 HCGCloud +Copyright (c) 2020 HCGCloud > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal From efbf193c9ff667eee2803363c98957bf2a963c04 Mon Sep 17 00:00:00 2001 From: TLingC Date: Tue, 20 Oct 2020 13:39:16 +0800 Subject: [PATCH 18/28] Add client permissions function --- src/Managers/Client/AccountManager.php | 11 +++++++++++ .../{Application => Client}/SystemPermissions.php | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) rename src/Resources/{Application => Client}/SystemPermissions.php (64%) diff --git a/src/Managers/Client/AccountManager.php b/src/Managers/Client/AccountManager.php index ed708fd..8beeb1a 100644 --- a/src/Managers/Client/AccountManager.php +++ b/src/Managers/Client/AccountManager.php @@ -5,6 +5,7 @@ use HCGCloud\Pterodactyl\Managers\Manager; use HCGCloud\Pterodactyl\Resources\User; +use HCGCloud\Pterodactyl\Resources\Client\SystemPermissions; class AccountManager extends Manager { @@ -17,4 +18,14 @@ public function details() { return $this->http->get('account'); } + + /** + * Get permissions of the account. + * + * @return SystemPermissions + */ + public function permissions() + { + return $this->http->get('permissions'); + } } diff --git a/src/Resources/Application/SystemPermissions.php b/src/Resources/Client/SystemPermissions.php similarity index 64% rename from src/Resources/Application/SystemPermissions.php rename to src/Resources/Client/SystemPermissions.php index f0ba69c..8431926 100644 --- a/src/Resources/Application/SystemPermissions.php +++ b/src/Resources/Client/SystemPermissions.php @@ -1,6 +1,6 @@ Date: Tue, 20 Oct 2020 13:39:58 +0800 Subject: [PATCH 19/28] Improve resource class name detection --- src/Http.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Http.php b/src/Http.php index 91646f0..a413410 100644 --- a/src/Http.php +++ b/src/Http.php @@ -209,15 +209,35 @@ protected function transform($response) }, $response['attributes']['relationships']); } - $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.ucwords($response['object'])) ? - '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.ucwords($response['object']) : - '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($response['object']); + $object = ucwords($this->camelCase($response['object'])); + + $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.$object) ? + '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.$object : + '\\HCGCloud\\Pterodactyl\\Resources\\'.$object; $resource = class_exists($class) ? new $class($response, $this->pterodactyl) : $response; return $resource; } + /** + * Convert the key name to camel case. + * + * @param $key + */ + private function camelCase($key) + { + $parts = explode('_', $key); + + foreach ($parts as $i => $part) { + if ($i !== 0) { + $parts[$i] = ucfirst($part); + } + } + + return str_replace(' ', '', implode(' ', $parts)); + } + /** * @param \Psr\Http\Message\ResponseInterface $response * From 94ea8ae66f01a948166bb09e3d9938a57a43f7e7 Mon Sep 17 00:00:00 2001 From: TLingC Date: Mon, 2 Nov 2020 09:21:01 +0800 Subject: [PATCH 20/28] Structure improvement --- src/Http.php | 4 +-- src/Managers/{Client => }/AccountManager.php | 4 +-- .../{Application => }/LocationManager.php | 4 +-- src/Managers/Manager.php | 9 +++++ .../{Application => }/NestManager.php | 6 ++-- .../{Application => }/NodeManager.php | 6 ++-- .../{Application => }/UserManager.php | 4 +-- src/Pterodactyl.php | 36 ++++++++----------- .../{Application => }/Allocation.php | 3 +- src/Resources/{Application => }/ApiKey.php | 2 +- src/Resources/{Application => }/Backup.php | 2 +- src/Resources/Client/User.php | 10 ------ .../{Application => }/DatabasePassword.php | 2 +- src/Resources/{Application => }/Databases.php | 2 +- src/Resources/{Application => }/Egg.php | 2 +- .../{Application => }/EggVariable.php | 2 +- .../{Application => }/FileObject.php | 2 +- src/Resources/{Application => }/Location.php | 2 +- src/Resources/{Application => }/Nest.php | 2 +- src/Resources/{Application => }/Node.php | 2 +- .../{Application => }/RecoveryTokens.php | 2 +- .../{Application => }/ScheduleTask.php | 2 +- src/Resources/{Application => }/Server.php | 2 +- .../{Application => }/ServerDatabase.php | 2 +- .../{Application => }/ServerSchedule.php | 2 +- .../{Application => }/ServerSubuser.php | 2 +- src/Resources/{Application => }/SignedUrl.php | 2 +- src/Resources/{Application => }/Stats.php | 2 +- .../{Client => }/SystemPermissions.php | 2 +- src/Resources/{Application => }/User.php | 2 +- 30 files changed, 59 insertions(+), 67 deletions(-) rename src/Managers/{Client => }/AccountManager.php (81%) rename src/Managers/{Application => }/LocationManager.php (93%) rename src/Managers/{Application => }/NestManager.php (90%) rename src/Managers/{Application => }/NodeManager.php (94%) rename src/Managers/{Application => }/UserManager.php (94%) rename src/Resources/{Application => }/Allocation.php (62%) rename src/Resources/{Application => }/ApiKey.php (62%) rename src/Resources/{Application => }/Backup.php (62%) delete mode 100644 src/Resources/Client/User.php rename src/Resources/{Application => }/DatabasePassword.php (64%) rename src/Resources/{Application => }/Databases.php (63%) rename src/Resources/{Application => }/Egg.php (61%) rename src/Resources/{Application => }/EggVariable.php (63%) rename src/Resources/{Application => }/FileObject.php (63%) rename src/Resources/{Application => }/Location.php (92%) rename src/Resources/{Application => }/Nest.php (92%) rename src/Resources/{Application => }/Node.php (96%) rename src/Resources/{Application => }/RecoveryTokens.php (64%) rename src/Resources/{Application => }/ScheduleTask.php (63%) rename src/Resources/{Application => }/Server.php (62%) rename src/Resources/{Application => }/ServerDatabase.php (64%) rename src/Resources/{Application => }/ServerSchedule.php (64%) rename src/Resources/{Application => }/ServerSubuser.php (64%) rename src/Resources/{Application => }/SignedUrl.php (63%) rename src/Resources/{Application => }/Stats.php (61%) rename src/Resources/{Client => }/SystemPermissions.php (67%) rename src/Resources/{Application => }/User.php (93%) diff --git a/src/Http.php b/src/Http.php index a413410..d60a1f6 100644 --- a/src/Http.php +++ b/src/Http.php @@ -211,8 +211,8 @@ protected function transform($response) $object = ucwords($this->camelCase($response['object'])); - $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.$object) ? - '\\HCGCloud\\Pterodactyl\\Resources\\'.ucwords($this->apiType).'\\'.$object : + $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.$object) ? + '\\HCGCloud\\Pterodactyl\\Resources\\'.$object : '\\HCGCloud\\Pterodactyl\\Resources\\'.$object; $resource = class_exists($class) ? new $class($response, $this->pterodactyl) : $response; diff --git a/src/Managers/Client/AccountManager.php b/src/Managers/AccountManager.php similarity index 81% rename from src/Managers/Client/AccountManager.php rename to src/Managers/AccountManager.php index 8beeb1a..5acef6d 100644 --- a/src/Managers/Client/AccountManager.php +++ b/src/Managers/AccountManager.php @@ -1,11 +1,11 @@ pterodactyl = $pterodactyl; $this->http = $this->pterodactyl->http; + + $this->apiType = $this->pterodactyl->apiType; } } diff --git a/src/Managers/Application/NestManager.php b/src/Managers/NestManager.php similarity index 90% rename from src/Managers/Application/NestManager.php rename to src/Managers/NestManager.php index 4c64763..347d345 100644 --- a/src/Managers/Application/NestManager.php +++ b/src/Managers/NestManager.php @@ -1,13 +1,13 @@ http = new Http($this, $guzzle); - switch($this->apiType) { - case 'application': - $this->locations = new ApplicationLocationManager($this); - $this->users = new ApplicationUserManager($this); - $this->nests = new ApplicationNestManager($this); - $this->nodes = new ApplicationNodeManager($this); - break; - case 'client': - $this->account = new ClientAccountManager($this); - break; - } + $this->locations = new LocationManager($this); + $this->users = new UserManager($this); + $this->nests = new NestManager($this); + $this->nodes = new NodeManager($this); + $this->account = new AccountManager($this); } } diff --git a/src/Resources/Application/Allocation.php b/src/Resources/Allocation.php similarity index 62% rename from src/Resources/Application/Allocation.php rename to src/Resources/Allocation.php index a303524..6034a4d 100644 --- a/src/Resources/Application/Allocation.php +++ b/src/Resources/Allocation.php @@ -1,10 +1,9 @@ Date: Sun, 8 Nov 2020 12:03:03 +0800 Subject: [PATCH 21/28] Change allocation and egg to a standalone manager --- src/Managers/Nest/NestEggManager.php | 42 ++++++++++++++++ src/Managers/NestManager.php | 31 ------------ src/Managers/Node/NodeAllocationManager.php | 54 +++++++++++++++++++++ src/Managers/NodeManager.php | 43 ---------------- src/Pterodactyl.php | 18 +++++++ src/Resources/Nest.php | 26 ---------- src/Resources/Node.php | 39 +-------------- 7 files changed, 115 insertions(+), 138 deletions(-) create mode 100644 src/Managers/Nest/NestEggManager.php create mode 100644 src/Managers/Node/NodeAllocationManager.php diff --git a/src/Managers/Nest/NestEggManager.php b/src/Managers/Nest/NestEggManager.php new file mode 100644 index 0000000..6fd3668 --- /dev/null +++ b/src/Managers/Nest/NestEggManager.php @@ -0,0 +1,42 @@ +http->get("nests/$nestId/eggs", array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a egg instance by id. + * + * @param int $nestId + * @param int $eggId + * @param array $query + * + * @return Egg + */ + public function get(int $nestId, int $eggId, array $query = []) + { + return $this->http->get("nests/$nestId/eggs/$eggId", $query); + } +} diff --git a/src/Managers/NestManager.php b/src/Managers/NestManager.php index 347d345..70c4cee 100644 --- a/src/Managers/NestManager.php +++ b/src/Managers/NestManager.php @@ -7,7 +7,6 @@ use HCGCloud\Pterodactyl\Resources\Collection; use HCGCloud\Pterodactyl\Resources\Nest; -use HCGCloud\Pterodactyl\Resources\Egg; class NestManager extends Manager { @@ -38,34 +37,4 @@ public function get(int $nestId, array $query = []) { return $this->http->get("nests/$nestId", $query); } - - /** - * Get a paginated collection of eggs. - * - * @param int $nestId - * @param int $page - * @param array $query - * - * @return Collection - */ - public function eggs(int $nestId, int $page = 1, array $query = []) - { - return $this->http->get("nests/$nestId/eggs", array_merge([ - 'page' => $page - ], $query)); - } - - /** - * Get a egg instance by id. - * - * @param int $nestId - * @param int $eggId - * @param array $query - * - * @return Egg - */ - public function egg(int $nestId, int $eggId, array $query = []) - { - return $this->http->get("nests/$nestId/eggs/$eggId", $query); - } } diff --git a/src/Managers/Node/NodeAllocationManager.php b/src/Managers/Node/NodeAllocationManager.php new file mode 100644 index 0000000..f9471d9 --- /dev/null +++ b/src/Managers/Node/NodeAllocationManager.php @@ -0,0 +1,54 @@ +http->get("nodes/$nodeId/allocations", array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Create a new allocation for a node. + * + * @param int $nodeId + * @param array $data + * + * @return Allocation + */ + public function create(int $nodeId, array $data) + { + return $this->http->post("nodes/$nodeId/allocations", [], $data); + } + + /** + * Delete the given allocation of a node. + * + * @param int $nodeId + * @param int $allocationId + * + * @return void + */ + public function delete(int $nodeId, int $allocationId) + { + return $this->http->delete("nodes/$nodeId/allocations/$allocationId"); + } +} diff --git a/src/Managers/NodeManager.php b/src/Managers/NodeManager.php index dcdf812..87f2168 100644 --- a/src/Managers/NodeManager.php +++ b/src/Managers/NodeManager.php @@ -7,7 +7,6 @@ use HCGCloud\Pterodactyl\Resources\Collection; use HCGCloud\Pterodactyl\Resources\Node; -use HCGCloud\Pterodactyl\Resources\Allocation; class NodeManager extends Manager { @@ -88,46 +87,4 @@ public function delete(int $nodeId) { return $this->http->delete("nodes/$nodeId"); } - - /** - * Get a paginated collection of node allocations. - * - * @param int $nodeId - * @param int $page - * @param array $query - * - * @return Collection - */ - public function allocations(int $nodeId, int $page = 1, array $query = []) - { - return $this->http->get("nodes/$nodeId/allocations", array_merge([ - 'page' => $page - ], $query)); - } - - /** - * Create a new allocation for a node. - * - * @param int $nodeId - * @param array $data - * - * @return User - */ - public function createAllocation(int $nodeId, array $data) - { - return $this->http->post("nodes/$nodeId/allocations", [], $data); - } - - /** - * Delete the given allocation of a node. - * - * @param int $nodeId - * @param int $allocationId - * - * @return void - */ - public function deleteAllocation(int $nodeId, int $allocationId) - { - return $this->http->delete("nodes/$nodeId/allocations/$allocationId"); - } } diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index 66bf34d..b1374a2 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -9,7 +9,9 @@ use HCGCloud\Pterodactyl\Managers\UserManager; use HCGCloud\Pterodactyl\Managers\LocationManager; use HCGCloud\Pterodactyl\Managers\NodeManager; +use HCGCloud\Pterodactyl\Managers\Node\NodeAllocationManager; use HCGCloud\Pterodactyl\Managers\NestManager; +use HCGCloud\Pterodactyl\Managers\Nest\NestEggManager; use HCGCloud\Pterodactyl\Managers\AccountManager; class Pterodactyl @@ -77,6 +79,20 @@ class Pterodactyl */ public $nodes; + /** + * Node allocation manager. + * + * @var NodeAllocationManager + */ + public $node_allocations; + + /** + * Nest egg manager. + * + * @var NestEggManager + */ + public $nest_eggs; + /** * Create a new Pterodactyl instance. * @@ -101,7 +117,9 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client $this->locations = new LocationManager($this); $this->users = new UserManager($this); $this->nests = new NestManager($this); + $this->nest_eggs = new NestEggManager($this); $this->nodes = new NodeManager($this); + $this->node_allocations = new NodeAllocationManager($this); $this->account = new AccountManager($this); } } diff --git a/src/Resources/Nest.php b/src/Resources/Nest.php index 976dee7..49a0f8b 100644 --- a/src/Resources/Nest.php +++ b/src/Resources/Nest.php @@ -9,30 +9,4 @@ class Nest extends Resource { - /** - * Get a paginated collection of eggs. - * - * @param int $page - * @param array $query - * - * @return Collection - */ - public function eggs(int $page = 1, array $query = []) - { - return $this->pterodactyl->nests->eggs($this->id, $page, $query); - } - - /** - * Get a egg instance by id. - * - * @param int $eggId - * @param array $query - * - * @return Egg - */ - public function egg(int $eggId, array $query = []) - { - return $this->pterodactyl->nests->egg($this->id, $eggId, $query); - } - } diff --git a/src/Resources/Node.php b/src/Resources/Node.php index fe6119f..ed94a0d 100644 --- a/src/Resources/Node.php +++ b/src/Resources/Node.php @@ -18,7 +18,7 @@ public function delete() /** * Update the node. - * + * * @param array $data * * @return void @@ -27,41 +27,4 @@ public function update(array $data = []) { return $this->pterodactyl->nodes->update($this->id, $data); } - - /** - * Get a paginated collection of node allocations. - * - * @param int $page - * @param array $query - * - * @return Collection - */ - public function allocations(int $page = 1, array $query = []) - { - return $this->pterodactyl->nodes->allocations($this->id, $page, $query); - } - - /** - * Create a new allocation for a node. - * - * @param array $data - * - * @return User - */ - public function createAllocation(array $data) - { - return $this->pterodactyl->nodes->createAllocation($this->id, $data); - } - - /** - * Delete the given allocation of a node. - * - * @param int $allocationId - * - * @return void - */ - public function deleteAllocation(int $allocationId) - { - return $this->pterodactyl->nodes->deleteAllocation($this->id, $allocationId); - } } From 4ea8bf7e8fc2ea130a5621f7a9352ba4178313f2 Mon Sep 17 00:00:00 2001 From: TLingC Date: Sun, 8 Nov 2020 12:14:08 +0800 Subject: [PATCH 22/28] Add server manager --- src/Managers/ServerManager.php | 164 +++++++++++++++++++++++++++++++++ src/Pterodactyl.php | 9 ++ 2 files changed, 173 insertions(+) create mode 100644 src/Managers/ServerManager.php diff --git a/src/Managers/ServerManager.php b/src/Managers/ServerManager.php new file mode 100644 index 0000000..4d4d910 --- /dev/null +++ b/src/Managers/ServerManager.php @@ -0,0 +1,164 @@ +http->get('servers', array_merge([ + 'page' => $page + ], $query)); + } + + /** + * Get a server instance by id. + * + * @param int $serverId + * @param array $query + * + * @return Server + */ + public function get(int $serverId, array $query = []) + { + return $this->http->get("servers/$serverId", $query); + } + + /** + * Get a server instance by external id. + * + * @param int $externalId + * @param array $query + * + * @return Server + */ + public function getByExternalId(int $externalId, array $query = []) + { + return $this->http->get("servers/external/$externalId", $query); + } + + /** + * Create a new server. + * + * @param array $data + * + * @return Server + */ + public function create(array $data) + { + return $this->http->post('server', [], $data); + } + + /** + * Update details of a specified server. + * + * @param int $serverId + * @param array $data + * + * @return void + */ + public function updateDetails(int $serverId, array $data) + { + return $this->http->patch("servers/$serverId/details", [], $data); + } + + /** + * Update build of a specified server. + * + * @param int $serverId + * @param array $data + * + * @return void + */ + public function updateBuild(int $serverId, array $data) + { + return $this->http->patch("servers/$serverId/build", [], $data); + } + + /** + * Update startup of a specified server. + * + * @param int $serverId + * @param array $data + * + * @return void + */ + public function updateStartup(int $serverId, array $data) + { + return $this->http->patch("servers/$serverId/startup", [], $data); + } + + /** + * Suspend a specified server. + * + * @param int $serverId + * + * @return void + */ + public function suspend(int $serverId) + { + return $this->http->post("servers/$serverId/suspend"); + } + + /** + * Unsuspend a specified server. + * + * @param int $serverId + * + * @return void + */ + public function unsuspend(int $serverId) + { + return $this->http->post("servers/$serverId/unsuspend"); + } + + /** + * Reinstall a specified server. + * + * @param int $serverId + * + * @return void + */ + public function reinstall(int $serverId) + { + return $this->http->post("servers/$serverId/reinstall"); + } + + /** + * Delete the given server. + * + * @param int $serverId + * + * @return void + */ + public function delete(int $serverId) + { + return $this->http->delete("servers/$serverId"); + } + + /** + * Force delete the given server. + * + * @param int $serverId + * + * @return void + */ + public function forceDelete(int $serverId) + { + return $this->http->delete("servers/$serverId/force"); + } +} diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index b1374a2..ba3f418 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -13,6 +13,7 @@ use HCGCloud\Pterodactyl\Managers\NestManager; use HCGCloud\Pterodactyl\Managers\Nest\NestEggManager; use HCGCloud\Pterodactyl\Managers\AccountManager; +use HCGCloud\Pterodactyl\Managers\ServerManager; class Pterodactyl { @@ -93,6 +94,13 @@ class Pterodactyl */ public $nest_eggs; + /** + * Server manager + * + * @var ServerManager + */ + public $servers; + /** * Create a new Pterodactyl instance. * @@ -121,5 +129,6 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client $this->nodes = new NodeManager($this); $this->node_allocations = new NodeAllocationManager($this); $this->account = new AccountManager($this); + $this->servers = new ServerManager($this); } } From f5479dc495ff693d87e317bfe8357e9987085b5b Mon Sep 17 00:00:00 2001 From: TLingC Date: Sun, 8 Nov 2020 04:17:05 +0000 Subject: [PATCH 23/28] Apply fixes from StyleCI --- src/Http.php | 29 ++++++++++----------- src/Managers/AccountManager.php | 6 ++--- src/Managers/LocationManager.php | 7 ++--- src/Managers/Manager.php | 2 +- src/Managers/Nest/NestEggManager.php | 8 +++--- src/Managers/NestManager.php | 7 ++--- src/Managers/Node/NodeAllocationManager.php | 12 ++++----- src/Managers/NodeManager.php | 7 ++--- src/Managers/ServerManager.php | 15 +++++------ src/Managers/UserManager.php | 7 ++--- src/Pterodactyl.php | 22 +++++++--------- src/Resources/Allocation.php | 2 -- src/Resources/ApiKey.php | 3 --- src/Resources/Backup.php | 3 --- src/Resources/Collection.php | 4 +-- src/Resources/DatabasePassword.php | 3 --- src/Resources/Databases.php | 3 --- src/Resources/Egg.php | 3 --- src/Resources/EggVariable.php | 3 --- src/Resources/FileObject.php | 3 --- src/Resources/Location.php | 4 +-- src/Resources/Nest.php | 4 --- src/Resources/Node.php | 2 -- src/Resources/RecoveryTokens.php | 3 --- src/Resources/Resource.php | 7 +++-- src/Resources/ScheduleTask.php | 3 --- src/Resources/Server.php | 3 --- src/Resources/ServerDatabase.php | 3 --- src/Resources/ServerSchedule.php | 3 --- src/Resources/ServerSubuser.php | 3 --- src/Resources/SignedUrl.php | 3 --- src/Resources/Stats.php | 3 --- src/Resources/SystemPermissions.php | 3 --- src/Resources/User.php | 18 ++++++------- 34 files changed, 64 insertions(+), 147 deletions(-) diff --git a/src/Http.php b/src/Http.php index d60a1f6..17d3de1 100644 --- a/src/Http.php +++ b/src/Http.php @@ -8,7 +8,6 @@ use HCGCloud\Pterodactyl\Exceptions\NotFoundException; use HCGCloud\Pterodactyl\Exceptions\TimeoutException; use HCGCloud\Pterodactyl\Exceptions\ValidationException; - use Psr\Http\Message\ResponseInterface; class Http @@ -93,8 +92,8 @@ public function get($uri, array $query = []) * Make a POST request and return the response. * * @param string $uri - * @param array $query - * @param array $payload + * @param array $query + * @param array $payload * * @return mixed */ @@ -107,8 +106,8 @@ public function post($uri, array $query = [], array $payload = []) * Make a PUT request and return the response. * * @param string $uri - * @param array $query - * @param array $payload + * @param array $query + * @param array $payload * * @return mixed */ @@ -121,8 +120,8 @@ public function put($uri, array $query = [], array $payload = []) * Make a PATCH request and return the response. * * @param string $uri - * @param array $query - * @param array $payload + * @param array $query + * @param array $payload * * @return mixed */ @@ -135,8 +134,8 @@ public function patch($uri, array $query = [], array $payload = []) * Make a DELETE request and return the response. * * @param string $uri - * @param array $query - * @param array $payload + * @param array $query + * @param array $payload * * @return mixed */ @@ -150,14 +149,14 @@ public function delete($uri, array $query = [], array $payload = []) * * @param string $method * @param string $uri - * @param array $query - * @param array $payload + * @param array $query + * @param array $payload * * @return mixed */ public function request($method, $uri, array $query = [], array $payload = []) { - $uri = $this->baseUri . '/api/' . $this->apiType . '/' . $uri; + $uri = $this->baseUri.'/api/'.$this->apiType.'/'.$uri; $body = json_encode($payload); @@ -211,8 +210,8 @@ protected function transform($response) $object = ucwords($this->camelCase($response['object'])); - $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.$object) ? - '\\HCGCloud\\Pterodactyl\\Resources\\'.$object : + $class = class_exists('\\HCGCloud\\Pterodactyl\\Resources\\'.$object) ? + '\\HCGCloud\\Pterodactyl\\Resources\\'.$object : '\\HCGCloud\\Pterodactyl\\Resources\\'.$object; $resource = class_exists($class) ? new $class($response, $this->pterodactyl) : $response; @@ -245,7 +244,7 @@ private function camelCase($key) */ private function handleRequestError(ResponseInterface $response) { - switch($response->getStatusCode()) { + switch ($response->getStatusCode()) { case 400: throw new FailedActionException((string) $response->getBody()); case 403: diff --git a/src/Managers/AccountManager.php b/src/Managers/AccountManager.php index 5acef6d..d5e4a4c 100644 --- a/src/Managers/AccountManager.php +++ b/src/Managers/AccountManager.php @@ -2,10 +2,8 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Managers\Manager; - -use HCGCloud\Pterodactyl\Resources\User; use HCGCloud\Pterodactyl\Resources\SystemPermissions; +use HCGCloud\Pterodactyl\Resources\User; class AccountManager extends Manager { @@ -21,7 +19,7 @@ public function details() /** * Get permissions of the account. - * + * * @return SystemPermissions */ public function permissions() diff --git a/src/Managers/LocationManager.php b/src/Managers/LocationManager.php index a5bf5ab..25862ae 100644 --- a/src/Managers/LocationManager.php +++ b/src/Managers/LocationManager.php @@ -2,10 +2,7 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Managers\Manager; - use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\Location; class LocationManager extends Manager @@ -13,7 +10,7 @@ class LocationManager extends Manager /** * Get a paginated collection of locations. * - * @param int $page + * @param int $page * @param array $query * * @return Collection @@ -21,7 +18,7 @@ class LocationManager extends Manager public function paginate(int $page = 1, array $query = []) { return $this->http->get('locations', array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } diff --git a/src/Managers/Manager.php b/src/Managers/Manager.php index d54e50a..04a38c9 100644 --- a/src/Managers/Manager.php +++ b/src/Managers/Manager.php @@ -2,8 +2,8 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Pterodactyl; use HCGCloud\Pterodactyl\Http; +use HCGCloud\Pterodactyl\Pterodactyl; class Manager { diff --git a/src/Managers/Nest/NestEggManager.php b/src/Managers/Nest/NestEggManager.php index 6fd3668..8ad89ea 100644 --- a/src/Managers/Nest/NestEggManager.php +++ b/src/Managers/Nest/NestEggManager.php @@ -3,9 +3,7 @@ namespace HCGCloud\Pterodactyl\Managers\Nest; use HCGCloud\Pterodactyl\Managers\Manager; - use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\Egg; class NestEggManager extends Manager @@ -13,8 +11,8 @@ class NestEggManager extends Manager /** * Get a paginated collection of eggs. * - * @param int $nestId - * @param int $page + * @param int $nestId + * @param int $page * @param array $query * * @return Collection @@ -22,7 +20,7 @@ class NestEggManager extends Manager public function paginate(int $nestId, int $page = 1, array $query = []) { return $this->http->get("nests/$nestId/eggs", array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } diff --git a/src/Managers/NestManager.php b/src/Managers/NestManager.php index 70c4cee..2b676fd 100644 --- a/src/Managers/NestManager.php +++ b/src/Managers/NestManager.php @@ -2,10 +2,7 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Managers\Manager; - use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\Nest; class NestManager extends Manager @@ -13,7 +10,7 @@ class NestManager extends Manager /** * Get a paginated collection of nests. * - * @param int $page + * @param int $page * @param array $query * * @return Collection @@ -21,7 +18,7 @@ class NestManager extends Manager public function paginate(int $page = 1, array $query = []) { return $this->http->get('nests', array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } diff --git a/src/Managers/Node/NodeAllocationManager.php b/src/Managers/Node/NodeAllocationManager.php index f9471d9..7158762 100644 --- a/src/Managers/Node/NodeAllocationManager.php +++ b/src/Managers/Node/NodeAllocationManager.php @@ -3,18 +3,16 @@ namespace HCGCloud\Pterodactyl\Managers\Node; use HCGCloud\Pterodactyl\Managers\Manager; - -use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\Allocation; +use HCGCloud\Pterodactyl\Resources\Collection; class NodeAllocationManager extends Manager { /** * Get a paginated collection of node allocations. * - * @param int $nodeId - * @param int $page + * @param int $nodeId + * @param int $page * @param array $query * * @return Collection @@ -22,14 +20,14 @@ class NodeAllocationManager extends Manager public function paginate(int $nodeId, int $page = 1, array $query = []) { return $this->http->get("nodes/$nodeId/allocations", array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } /** * Create a new allocation for a node. * - * @param int $nodeId + * @param int $nodeId * @param array $data * * @return Allocation diff --git a/src/Managers/NodeManager.php b/src/Managers/NodeManager.php index 87f2168..2f7eb0b 100644 --- a/src/Managers/NodeManager.php +++ b/src/Managers/NodeManager.php @@ -2,10 +2,7 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Managers\Manager; - use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\Node; class NodeManager extends Manager @@ -13,7 +10,7 @@ class NodeManager extends Manager /** * Get a paginated collection of nodes. * - * @param int $page + * @param int $page * @param array $query * * @return Collection @@ -21,7 +18,7 @@ class NodeManager extends Manager public function paginate(int $page = 1, array $query = []) { return $this->http->get('nodes', array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } diff --git a/src/Managers/ServerManager.php b/src/Managers/ServerManager.php index 4d4d910..0fc0b82 100644 --- a/src/Managers/ServerManager.php +++ b/src/Managers/ServerManager.php @@ -2,10 +2,7 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Managers\Manager; - use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\Server; class ServerManager extends Manager @@ -13,7 +10,7 @@ class ServerManager extends Manager /** * Get a paginated collection of servers. * - * @param int $page + * @param int $page * @param array $query * * @return Collection @@ -21,7 +18,7 @@ class ServerManager extends Manager public function paginate(int $page = 1, array $query = []) { return $this->http->get('servers', array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } @@ -75,7 +72,7 @@ public function updateDetails(int $serverId, array $data) { return $this->http->patch("servers/$serverId/details", [], $data); } - + /** * Update build of a specified server. * @@ -105,7 +102,7 @@ public function updateStartup(int $serverId, array $data) /** * Suspend a specified server. * - * @param int $serverId + * @param int $serverId * * @return void */ @@ -117,7 +114,7 @@ public function suspend(int $serverId) /** * Unsuspend a specified server. * - * @param int $serverId + * @param int $serverId * * @return void */ @@ -129,7 +126,7 @@ public function unsuspend(int $serverId) /** * Reinstall a specified server. * - * @param int $serverId + * @param int $serverId * * @return void */ diff --git a/src/Managers/UserManager.php b/src/Managers/UserManager.php index 44d742b..cd479e5 100644 --- a/src/Managers/UserManager.php +++ b/src/Managers/UserManager.php @@ -2,10 +2,7 @@ namespace HCGCloud\Pterodactyl\Managers; -use HCGCloud\Pterodactyl\Managers\Manager; - use HCGCloud\Pterodactyl\Resources\Collection; - use HCGCloud\Pterodactyl\Resources\User; class UserManager extends Manager @@ -13,7 +10,7 @@ class UserManager extends Manager /** * Get a paginated collection of users. * - * @param int $page + * @param int $page * @param array $query * * @return Collection @@ -21,7 +18,7 @@ class UserManager extends Manager public function paginate(int $page = 1, array $query = []) { return $this->http->get('users', array_merge([ - 'page' => $page + 'page' => $page, ], $query)); } diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index ba3f418..9d86350 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -3,17 +3,15 @@ namespace HCGCloud\Pterodactyl; use GuzzleHttp\Client as Client; - use HCGCloud\Pterodactyl\Exceptions\InvaildApiTypeException; - -use HCGCloud\Pterodactyl\Managers\UserManager; +use HCGCloud\Pterodactyl\Managers\AccountManager; use HCGCloud\Pterodactyl\Managers\LocationManager; -use HCGCloud\Pterodactyl\Managers\NodeManager; -use HCGCloud\Pterodactyl\Managers\Node\NodeAllocationManager; -use HCGCloud\Pterodactyl\Managers\NestManager; use HCGCloud\Pterodactyl\Managers\Nest\NestEggManager; -use HCGCloud\Pterodactyl\Managers\AccountManager; +use HCGCloud\Pterodactyl\Managers\NestManager; +use HCGCloud\Pterodactyl\Managers\Node\NodeAllocationManager; +use HCGCloud\Pterodactyl\Managers\NodeManager; use HCGCloud\Pterodactyl\Managers\ServerManager; +use HCGCloud\Pterodactyl\Managers\UserManager; class Pterodactyl { @@ -44,7 +42,7 @@ class Pterodactyl * @var Client */ public $http; - + /** * Account manager. * @@ -58,7 +56,7 @@ class Pterodactyl * @var LocationManager */ public $locations; - + /** * User manager. * @@ -82,7 +80,7 @@ class Pterodactyl /** * Node allocation manager. - * + * * @var NodeAllocationManager */ public $node_allocations; @@ -95,7 +93,7 @@ class Pterodactyl public $nest_eggs; /** - * Server manager + * Server manager. * * @var ServerManager */ @@ -115,7 +113,7 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client $this->apiKey = $apiKey; - if(!in_array($apiType, ['application', 'client'], true)) { + if (!in_array($apiType, ['application', 'client'], true)) { throw new InvaildApiTypeException(); } $this->apiType = $apiType; diff --git a/src/Resources/Allocation.php b/src/Resources/Allocation.php index 6034a4d..40b0ff9 100644 --- a/src/Resources/Allocation.php +++ b/src/Resources/Allocation.php @@ -2,8 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Allocation extends Resource { } diff --git a/src/Resources/ApiKey.php b/src/Resources/ApiKey.php index 0d62b1d..6d6615a 100644 --- a/src/Resources/ApiKey.php +++ b/src/Resources/ApiKey.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class ApiKey extends Resource { - } diff --git a/src/Resources/Backup.php b/src/Resources/Backup.php index 747af3a..27ed1bb 100644 --- a/src/Resources/Backup.php +++ b/src/Resources/Backup.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Backup extends Resource { - } diff --git a/src/Resources/Collection.php b/src/Resources/Collection.php index 15e263d..d444345 100644 --- a/src/Resources/Collection.php +++ b/src/Resources/Collection.php @@ -30,7 +30,7 @@ public function __construct(array $attributes, $pterodactyl = null) /** * Convert resource to array. * - * @return Array + * @return array */ public function toArray() { @@ -40,7 +40,7 @@ public function toArray() /** * Get meta data of resource. * - * @return Array + * @return array */ public function meta() { diff --git a/src/Resources/DatabasePassword.php b/src/Resources/DatabasePassword.php index f14187a..c9ef902 100644 --- a/src/Resources/DatabasePassword.php +++ b/src/Resources/DatabasePassword.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class DatabasePassword extends Resource { - } diff --git a/src/Resources/Databases.php b/src/Resources/Databases.php index 099fe0f..2d8598e 100644 --- a/src/Resources/Databases.php +++ b/src/Resources/Databases.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Databases extends Resource { - } diff --git a/src/Resources/Egg.php b/src/Resources/Egg.php index 693f3a4..cad07fc 100644 --- a/src/Resources/Egg.php +++ b/src/Resources/Egg.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Egg extends Resource { - } diff --git a/src/Resources/EggVariable.php b/src/Resources/EggVariable.php index c172e1c..9fa6189 100644 --- a/src/Resources/EggVariable.php +++ b/src/Resources/EggVariable.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class EggVariable extends Resource { - } diff --git a/src/Resources/FileObject.php b/src/Resources/FileObject.php index fa95e3e..135bc4c 100644 --- a/src/Resources/FileObject.php +++ b/src/Resources/FileObject.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class FileObject extends Resource { - } diff --git a/src/Resources/Location.php b/src/Resources/Location.php index 72a4e0a..faaf63b 100644 --- a/src/Resources/Location.php +++ b/src/Resources/Location.php @@ -2,8 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Location extends Resource { /** @@ -18,7 +16,7 @@ public function delete() /** * Update the location. - * + * * @param array $data * * @return void diff --git a/src/Resources/Nest.php b/src/Resources/Nest.php index 49a0f8b..b668672 100644 --- a/src/Resources/Nest.php +++ b/src/Resources/Nest.php @@ -2,11 +2,7 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Collection; - -use HCGCloud\Pterodactyl\Resources\Resource; class Nest extends Resource { - } diff --git a/src/Resources/Node.php b/src/Resources/Node.php index ed94a0d..0a6af2c 100644 --- a/src/Resources/Node.php +++ b/src/Resources/Node.php @@ -2,8 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Node extends Resource { /** diff --git a/src/Resources/RecoveryTokens.php b/src/Resources/RecoveryTokens.php index ec37c56..4cd3f62 100644 --- a/src/Resources/RecoveryTokens.php +++ b/src/Resources/RecoveryTokens.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class RecoveryTokens extends Resource { - } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 2047ca2..c17e35d 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -2,9 +2,8 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Pterodactyl; - use ArrayAccess; +use HCGCloud\Pterodactyl\Pterodactyl; use JsonSerializable; use Serializable; @@ -16,7 +15,7 @@ class Resource implements ArrayAccess, JsonSerializable, Serializable * @var array */ protected $attributes; - + /** * The origin attributes. * @@ -82,7 +81,7 @@ public function __unset($key) /** * Convert resource to array. * - * @return Array + * @return array */ public function toArray() { diff --git a/src/Resources/ScheduleTask.php b/src/Resources/ScheduleTask.php index d2ff3db..083ad3a 100644 --- a/src/Resources/ScheduleTask.php +++ b/src/Resources/ScheduleTask.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class ScheduleTask extends Resource { - } diff --git a/src/Resources/Server.php b/src/Resources/Server.php index f0f6c72..92e4c60 100644 --- a/src/Resources/Server.php +++ b/src/Resources/Server.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Server extends Resource { - } diff --git a/src/Resources/ServerDatabase.php b/src/Resources/ServerDatabase.php index 2333b92..693b86d 100644 --- a/src/Resources/ServerDatabase.php +++ b/src/Resources/ServerDatabase.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class ServerDatabase extends Resource { - } diff --git a/src/Resources/ServerSchedule.php b/src/Resources/ServerSchedule.php index 57c90f7..859a5cb 100644 --- a/src/Resources/ServerSchedule.php +++ b/src/Resources/ServerSchedule.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class ServerSchedule extends Resource { - } diff --git a/src/Resources/ServerSubuser.php b/src/Resources/ServerSubuser.php index c2c56c2..9665e93 100644 --- a/src/Resources/ServerSubuser.php +++ b/src/Resources/ServerSubuser.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class ServerSubuser extends Resource { - } diff --git a/src/Resources/SignedUrl.php b/src/Resources/SignedUrl.php index 156df50..5388c22 100644 --- a/src/Resources/SignedUrl.php +++ b/src/Resources/SignedUrl.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class SignedUrl extends Resource { - } diff --git a/src/Resources/Stats.php b/src/Resources/Stats.php index 0c45352..d72e173 100644 --- a/src/Resources/Stats.php +++ b/src/Resources/Stats.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class Stats extends Resource { - } diff --git a/src/Resources/SystemPermissions.php b/src/Resources/SystemPermissions.php index c76ba6b..49dcbcd 100644 --- a/src/Resources/SystemPermissions.php +++ b/src/Resources/SystemPermissions.php @@ -2,9 +2,6 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class SystemPermissions extends Resource { - } diff --git a/src/Resources/User.php b/src/Resources/User.php index 42cd01f..c81d550 100644 --- a/src/Resources/User.php +++ b/src/Resources/User.php @@ -2,11 +2,8 @@ namespace HCGCloud\Pterodactyl\Resources; -use HCGCloud\Pterodactyl\Resources\Resource; - class User extends Resource { - /** * Save the user. * @@ -14,11 +11,13 @@ class User extends Resource */ public function save() { - return $this->update(array_merge([ - 'email' => $this->email, - 'username' => $this->username, - 'first_name' => $this->first_name, - 'last_name' => $this->last_name], $this->getChangedData() + return $this->update(array_merge( + [ + 'email' => $this->email, + 'username' => $this->username, + 'first_name' => $this->first_name, + 'last_name' => $this->last_name, ], + $this->getChangedData() )); } @@ -26,13 +25,14 @@ public function save() * Update the user. * * @param array $data + * * @return void */ public function update(array $data) { $this->pterodactyl->users->update($this->id, $data); } - + /** * Delete the user. * From b72cf9b5deff5a04d9afcd073788a20606d9de36 Mon Sep 17 00:00:00 2001 From: TLingC Date: Sun, 8 Nov 2020 13:38:54 +0800 Subject: [PATCH 24/28] Add server database manager --- src/Managers/Server/ServerDatabaseManager.php | 79 +++++++++++++++++++ src/Pterodactyl.php | 9 +++ 2 files changed, 88 insertions(+) create mode 100644 src/Managers/Server/ServerDatabaseManager.php diff --git a/src/Managers/Server/ServerDatabaseManager.php b/src/Managers/Server/ServerDatabaseManager.php new file mode 100644 index 0000000..c3d3f34 --- /dev/null +++ b/src/Managers/Server/ServerDatabaseManager.php @@ -0,0 +1,79 @@ +http->get("servers/$serverId/databases", array_merge([ + 'page' => $page, + ], $query)); + } + + /** + * Get a server database instance. + * + * @param mixed $serverId + * @param int $databaseId + * @param array $query + * + * @return ServerDatabase + */ + public function get($serverId, int $databaseId, array $query = []) + { + return $this->http->get("servers/$serverId/databases/$databaseId", $query); + } + + /** + * Create a new database for a server. + * + * @param mixed $serverId + * @param array $data + * + * @return ServerDatabase + */ + public function create($serverId, array $data) + { + return $this->http->post("servers/$serverId/databases", [], $data); + } + + /** + * Reset password for a specified database of a server. + * + * @param mixed $serverId + * @param int $databaseId + * + * @return void + */ + public function resetPassword($serverId, int $databaseId) + { + return $this->http->post("servers/$serverId/databases/$databaseId/reset-password"); + } + + /** + * Delete the given database of a server. + * + * @param mixed $serverId + * @param int $databaseId + * + * @return void + */ + public function delete($serverId, int $databaseId) + { + return $this->http->delete("servers/$serverId/databases/$databaseId"); + } +} diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index 9d86350..a37d2a2 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -11,6 +11,7 @@ use HCGCloud\Pterodactyl\Managers\Node\NodeAllocationManager; use HCGCloud\Pterodactyl\Managers\NodeManager; use HCGCloud\Pterodactyl\Managers\ServerManager; +use HCGCloud\Pterodactyl\Managers\Server\ServerDatabaseManager; use HCGCloud\Pterodactyl\Managers\UserManager; class Pterodactyl @@ -99,6 +100,13 @@ class Pterodactyl */ public $servers; + /** + * Server database manager. + * + * @var ServerDatabaseManager + */ + public $server_databases; + /** * Create a new Pterodactyl instance. * @@ -128,5 +136,6 @@ public function __construct($baseUri, $apiKey, $apiType = 'application', Client $this->node_allocations = new NodeAllocationManager($this); $this->account = new AccountManager($this); $this->servers = new ServerManager($this); + $this->server_databases = new ServerDatabaseManager($this); } } From cf197a910bed1a8c53de9155459b3154da708d95 Mon Sep 17 00:00:00 2001 From: TLingC Date: Sun, 8 Nov 2020 05:39:56 +0000 Subject: [PATCH 25/28] Apply fixes from StyleCI --- src/Managers/Server/ServerDatabaseManager.php | 2 +- src/Pterodactyl.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Managers/Server/ServerDatabaseManager.php b/src/Managers/Server/ServerDatabaseManager.php index c3d3f34..9ffc03d 100644 --- a/src/Managers/Server/ServerDatabaseManager.php +++ b/src/Managers/Server/ServerDatabaseManager.php @@ -3,8 +3,8 @@ namespace HCGCloud\Pterodactyl\Managers\Server; use HCGCloud\Pterodactyl\Managers\Manager; -use HCGCloud\Pterodactyl\Resources\ServerDatabase; use HCGCloud\Pterodactyl\Resources\Collection; +use HCGCloud\Pterodactyl\Resources\ServerDatabase; class ServerDatabaseManager extends Manager { diff --git a/src/Pterodactyl.php b/src/Pterodactyl.php index a37d2a2..6f9f1d0 100644 --- a/src/Pterodactyl.php +++ b/src/Pterodactyl.php @@ -10,8 +10,8 @@ use HCGCloud\Pterodactyl\Managers\NestManager; use HCGCloud\Pterodactyl\Managers\Node\NodeAllocationManager; use HCGCloud\Pterodactyl\Managers\NodeManager; -use HCGCloud\Pterodactyl\Managers\ServerManager; use HCGCloud\Pterodactyl\Managers\Server\ServerDatabaseManager; +use HCGCloud\Pterodactyl\Managers\ServerManager; use HCGCloud\Pterodactyl\Managers\UserManager; class Pterodactyl From ff9c072148ad03a624fdf26f94c88c0c32905d16 Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 18 Nov 2020 14:57:44 +0800 Subject: [PATCH 26/28] Add server apis --- src/Managers/ServerManager.php | 70 +++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/src/Managers/ServerManager.php b/src/Managers/ServerManager.php index 0fc0b82..88e6fa3 100644 --- a/src/Managers/ServerManager.php +++ b/src/Managers/ServerManager.php @@ -4,6 +4,7 @@ use HCGCloud\Pterodactyl\Resources\Collection; use HCGCloud\Pterodactyl\Resources\Server; +use HCGCloud\Pterodactyl\Resources\Stats; class ServerManager extends Manager { @@ -17,20 +18,29 @@ class ServerManager extends Manager */ public function paginate(int $page = 1, array $query = []) { - return $this->http->get('servers', array_merge([ - 'page' => $page, - ], $query)); + switch ($this->apiType) { + case 'application': + return $this->http->get('servers', array_merge([ + 'page' => $page, + ], $query)); + break; + case 'client': + return $this->http->get('', array_merge([ + 'page' => $page, + ], $query)); + break; + } } /** * Get a server instance by id. * - * @param int $serverId + * @param mixed $serverId * @param array $query * * @return Server */ - public function get(int $serverId, array $query = []) + public function get($serverId, array $query = []) { return $this->http->get("servers/$serverId", $query); } @@ -158,4 +168,54 @@ public function forceDelete(int $serverId) { return $this->http->delete("servers/$serverId/force"); } + + /** + * Get information for websocket console of a specified server. + * + * @param string $serverId + * + * @return array + */ + public function websocket(string $serverId) + { + return $this->http->get("servers/$serverId/websocket"); + } + + /** + * Get resource utilization of a specified server. + * + * @param string $serverId + * + * @return Stats + */ + public function resources(string $serverId) + { + return $this->http->get("servers/$serverId/resources"); + } + + /** + * Send a command to a specified server. + * + * @param string $serverId + * @param string $command + * + * @return array + */ + public function command(string $serverId, string $command) + { + return $this->http->post("servers/$serverId/command", [], ['command' => $command]); + } + + /** + * Send a power signal to a specified server. + * + * @param string $serverId + * @param string $signal + * + * @return array + */ + public function power(string $serverId, string $signal) + { + return $this->http->post("servers/$serverId/power", [], ['signal' => $signal]); + } } From 8289288178c5b0185721850506c67ee03e1f5858 Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 18 Nov 2020 06:58:34 +0000 Subject: [PATCH 27/28] Apply fixes from StyleCI --- src/Managers/ServerManager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Managers/ServerManager.php b/src/Managers/ServerManager.php index 88e6fa3..6832a46 100644 --- a/src/Managers/ServerManager.php +++ b/src/Managers/ServerManager.php @@ -35,7 +35,7 @@ public function paginate(int $page = 1, array $query = []) /** * Get a server instance by id. * - * @param mixed $serverId + * @param mixed $serverId * @param array $query * * @return Server @@ -173,7 +173,7 @@ public function forceDelete(int $serverId) * Get information for websocket console of a specified server. * * @param string $serverId - * + * * @return array */ public function websocket(string $serverId) @@ -185,7 +185,7 @@ public function websocket(string $serverId) * Get resource utilization of a specified server. * * @param string $serverId - * + * * @return Stats */ public function resources(string $serverId) From 25fd904d230d8e50cdebf2219fb2665c03b4737e Mon Sep 17 00:00:00 2001 From: TLingC Date: Wed, 25 Nov 2020 09:26:55 +0800 Subject: [PATCH 28/28] fix create server --- src/Managers/ServerManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Managers/ServerManager.php b/src/Managers/ServerManager.php index 6832a46..604c132 100644 --- a/src/Managers/ServerManager.php +++ b/src/Managers/ServerManager.php @@ -67,7 +67,7 @@ public function getByExternalId(int $externalId, array $query = []) */ public function create(array $data) { - return $this->http->post('server', [], $data); + return $this->http->post('servers', [], $data); } /**