2017-06-11 03:28:44 +00:00
|
|
|
<?php
|
|
|
|
|
2017-11-18 01:01:42 +00:00
|
|
|
namespace Pterodactyl\Repositories;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
use InvalidArgumentException;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\RepositoryInterface;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
abstract class Repository implements RepositoryInterface
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
protected array $columns = ['*'];
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
protected Model $model;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
protected bool $withFresh = true;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Repository constructor.
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(protected Application $app)
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2018-05-13 14:50:56 +00:00
|
|
|
$this->initializeModel($this->model());
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the model backing this repository.
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
abstract public function model(): string;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Return the model being used for this repository.
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getModel(): Model
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
return $this->model;
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Setup column selection functionality.
|
|
|
|
*
|
2018-01-05 04:49:50 +00:00
|
|
|
* @param array|string $columns
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function setColumns($columns = ['*']): self
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->columns = is_array($columns) ? $columns : func_get_args();
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
return $clone;
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Return the columns to be selected in the repository call.
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function getColumns(): array
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
return $this->columns;
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Stop repository update functions from returning a fresh
|
|
|
|
* model when changes are committed.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function withoutFreshModel(): self
|
2018-01-05 04:49:50 +00:00
|
|
|
{
|
|
|
|
return $this->setFreshModel(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a fresh model with a repository updates a model.
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function withFreshModel(): self
|
2018-01-05 04:49:50 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
return $this->setFreshModel();
|
2018-01-05 04:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Set whether the repository should return a fresh model
|
2018-01-05 04:49:50 +00:00
|
|
|
* when changes are committed.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function setFreshModel(bool $fresh = true): self
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
$clone = clone $this;
|
2018-01-05 04:49:50 +00:00
|
|
|
$clone->withFresh = $fresh;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
return $clone;
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
2018-01-05 04:49:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the provided model and make it accessible to the rest of the repository.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function initializeModel(string ...$model): mixed
|
2018-01-05 04:49:50 +00:00
|
|
|
{
|
|
|
|
switch (count($model)) {
|
|
|
|
case 1:
|
|
|
|
return $this->model = $this->app->make($model[0]);
|
|
|
|
case 2:
|
|
|
|
return $this->model = call_user_func([$this->app->make($model[0]), $model[1]]);
|
|
|
|
default:
|
2018-05-13 14:50:56 +00:00
|
|
|
throw new InvalidArgumentException('Model must be a FQDN or an array with a count of two.');
|
2018-01-05 04:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|