Complete move from old repository to new repository structure!
This commit is contained in:
parent
2cabb61b54
commit
72735c24f7
27 changed files with 964 additions and 730 deletions
|
@ -29,16 +29,47 @@ use Illuminate\Foundation\Application;
|
|||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
class BaseRepository implements BaseRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Foundation\Application
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $accessServer;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $accessToken;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
||||
*/
|
||||
protected $nodeRepository;
|
||||
|
||||
/**
|
||||
* BaseRepository constructor.
|
||||
*
|
||||
* @param \Illuminate\Foundation\Application $app
|
||||
* @param \Illuminate\Contracts\Config\Repository $config
|
||||
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
|
||||
*/
|
||||
public function __construct(
|
||||
Application $app,
|
||||
ConfigRepository $config,
|
||||
|
@ -49,44 +80,70 @@ class BaseRepository implements BaseRepositoryInterface
|
|||
$this->nodeRepository = $nodeRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setNode($id)
|
||||
{
|
||||
// @todo accept a model
|
||||
Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.');
|
||||
|
||||
$this->node = $this->nodeRepository->find($id);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNode()
|
||||
{
|
||||
return $this->node;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAccessServer($server = null)
|
||||
{
|
||||
Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.');
|
||||
|
||||
$this->accessServer = $server;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAccessServer()
|
||||
{
|
||||
return $this->accessServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAccessToken($token = null)
|
||||
{
|
||||
Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.');
|
||||
|
||||
$this->accessToken = $token;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAccessToken()
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
public function getHttpClient($headers = [])
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHttpClient(array $headers = [])
|
||||
{
|
||||
if (! is_null($this->accessServer)) {
|
||||
$headers['X-Access-Server'] = $this->getAccessServer();
|
||||
|
|
45
app/Repositories/Daemon/CommandRepository.php
Normal file
45
app/Repositories/Daemon/CommandRepository.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Repositories\Daemon;
|
||||
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
|
||||
|
||||
class CommandRepository extends BaseRepository implements CommandRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function send($command)
|
||||
{
|
||||
Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.');
|
||||
|
||||
return $this->getHttpClient()->request('POST', '/server/command', [
|
||||
'json' => [
|
||||
'command' => $command,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
126
app/Repositories/Daemon/FileRepository.php
Normal file
126
app/Repositories/Daemon/FileRepository.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Repositories\Daemon;
|
||||
|
||||
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
class FileRepository extends BaseRepository implements FileRepositoryInterface
|
||||
{
|
||||
public function getFileStat($path)
|
||||
{
|
||||
Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.');
|
||||
|
||||
$file = pathinfo($path);
|
||||
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
|
||||
|
||||
$response = $this->getHttpClient()->request('GET', sprintf(
|
||||
'/server/file/stat/%s',
|
||||
rawurlencode($file['dirname'] . $file['basename'])
|
||||
));
|
||||
|
||||
return json_decode($response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getContent($path)
|
||||
{
|
||||
Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.');
|
||||
|
||||
$file = pathinfo($path);
|
||||
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
|
||||
|
||||
$response = $this->getHttpClient()->request('GET', sprintf(
|
||||
'/server/file/f/%s',
|
||||
rawurlencode($file['dirname'] . $file['basename'])
|
||||
));
|
||||
|
||||
return json_decode($response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function putContent($path, $content)
|
||||
{
|
||||
Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.');
|
||||
Assert::string($content, 'Second argument passed to putContent must be a string, received %s.');
|
||||
|
||||
$file = pathinfo($path);
|
||||
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
|
||||
|
||||
return $this->getHttpClient()->request('POST', '/server/file/save', [
|
||||
'json' => [
|
||||
'path' => rawurlencode($file['dirname'] . $file['basename']),
|
||||
'content' => $content,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDirectory($path)
|
||||
{
|
||||
Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.');
|
||||
|
||||
$response = $this->getHttpClient()->request('GET', sprintf(
|
||||
'/server/directory/%s',
|
||||
rawurlencode($path)
|
||||
));
|
||||
|
||||
$contents = json_decode($response->getBody());
|
||||
$files = [];
|
||||
$folders = [];
|
||||
|
||||
foreach ($contents as $value) {
|
||||
if ($value->directory) {
|
||||
array_push($folders, [
|
||||
'entry' => $value->name,
|
||||
'directory' => trim($path, '/'),
|
||||
'size' => null,
|
||||
'date' => strtotime($value->modified),
|
||||
'mime' => $value->mime,
|
||||
]);
|
||||
} elseif ($value->file) {
|
||||
array_push($files, [
|
||||
'entry' => $value->name,
|
||||
'directory' => trim($path, '/'),
|
||||
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
|
||||
'size' => human_readable($value->size),
|
||||
'date' => strtotime($value->modified),
|
||||
'mime' => $value->mime,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'files' => $files,
|
||||
'folders' => $folders,
|
||||
];
|
||||
}
|
||||
}
|
55
app/Repositories/Daemon/PowerRepository.php
Normal file
55
app/Repositories/Daemon/PowerRepository.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Repositories\Daemon;
|
||||
|
||||
use Webmozart\Assert\Assert;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException;
|
||||
|
||||
class PowerRepository extends BaseRepository implements PowerRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sendSignal($signal)
|
||||
{
|
||||
Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.');
|
||||
|
||||
switch ($signal) {
|
||||
case self::SIGNAL_START:
|
||||
case self::SIGNAL_STOP:
|
||||
case self::SIGNAL_RESTART:
|
||||
case self::SIGNAL_KILL:
|
||||
return $this->getHttpClient()->request('PUT', '/server/power', [
|
||||
'json' => [
|
||||
'action' => $signal,
|
||||
],
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@ namespace Pterodactyl\Repositories\Daemon;
|
|||
use Pterodactyl\Services\Servers\EnvironmentService;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface as DatabaseServerRepositoryInterface;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
class ServerRepository extends BaseRepository implements ServerRepositoryInterface
|
||||
{
|
||||
|
@ -35,8 +36,11 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create($id, $overrides = [], $start = false)
|
||||
public function create($id, array $overrides = [], $start = false)
|
||||
{
|
||||
Assert::numeric($id, 'First argument passed to create must be numeric, received %s.');
|
||||
Assert::boolean($start, 'Third argument passed to create must be boolean, received %s.');
|
||||
|
||||
$repository = $this->app->make(DatabaseServerRepositoryInterface::class);
|
||||
$environment = $this->app->make(EnvironmentService::class);
|
||||
|
||||
|
@ -89,6 +93,8 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa
|
|||
*/
|
||||
public function setSubuserKey($key, array $permissions)
|
||||
{
|
||||
Assert::stringNotEmpty($key, 'First argument passed to setSubuserKey must be a non-empty string, received %s.');
|
||||
|
||||
return $this->getHttpClient()->request('PATCH', '/server', [
|
||||
'json' => [
|
||||
'keys' => [
|
||||
|
@ -113,6 +119,8 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa
|
|||
*/
|
||||
public function reinstall($data = null)
|
||||
{
|
||||
Assert::nullOrIsArray($data, 'First argument passed to reinstall must be null or an array, received %s.');
|
||||
|
||||
if (is_null($data)) {
|
||||
return $this->getHttpClient()->request('POST', '/server/reinstall');
|
||||
}
|
||||
|
|
Reference in a new issue