Path: blob/1.0-develop/app/Repositories/Repository.php
7432 views
<?php12namespace Pterodactyl\Repositories;34use Illuminate\Foundation\Application;5use Illuminate\Database\Eloquent\Model;6use Pterodactyl\Contracts\Repository\RepositoryInterface;78abstract class Repository implements RepositoryInterface9{10protected array $columns = ['*'];1112protected Model $model;1314protected bool $withFresh = true;1516/**17* Repository constructor.18*/19public function __construct(protected Application $app)20{21$this->initializeModel($this->model());22}2324/**25* Return the model backing this repository.26*/27abstract public function model(): string;2829/**30* Return the model being used for this repository.31*/32public function getModel(): Model33{34return $this->model;35}3637/**38* Setup column selection functionality.39*40* @param array|string $columns41*/42public function setColumns($columns = ['*']): self43{44$clone = clone $this;45$clone->columns = is_array($columns) ? $columns : func_get_args();4647return $clone;48}4950/**51* Return the columns to be selected in the repository call.52*/53public function getColumns(): array54{55return $this->columns;56}5758/**59* Stop repository update functions from returning a fresh60* model when changes are committed.61*/62public function withoutFreshModel(): self63{64return $this->setFreshModel(false);65}6667/**68* Return a fresh model with a repository updates a model.69*/70public function withFreshModel(): self71{72return $this->setFreshModel();73}7475/**76* Set whether the repository should return a fresh model77* when changes are committed.78*/79public function setFreshModel(bool $fresh = true): self80{81$clone = clone $this;82$clone->withFresh = $fresh;8384return $clone;85}8687/**88* Take the provided model and make it accessible to the rest of the repository.89*/90protected function initializeModel(string ...$model): mixed91{92switch (count($model)) {93case 1:94return $this->model = $this->app->make($model[0]);95case 2:96return $this->model = call_user_func([$this->app->make($model[0]), $model[1]]);97default:98throw new \InvalidArgumentException('Model must be a FQDN or an array with a count of two.');99}100}101}102103104