misc_pterodactyl-panel/app/Repositories/Repository.php

125 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2017-09-26 02:43:01 +00:00
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
2017-11-18 01:01:42 +00:00
namespace Pterodactyl\Repositories;
use Illuminate\Foundation\Application;
use Pterodactyl\Contracts\Repository\RepositoryInterface;
abstract class Repository implements RepositoryInterface
{
/**
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* @var array
*/
protected $columns = ['*'];
/**
* @var mixed
*/
protected $model;
/**
* @var bool
*/
protected $withFresh = true;
/**
* Repository constructor.
*
* @param \Illuminate\Foundation\Application $application
*/
public function __construct(Application $application)
{
$this->app = $application;
$this->setModel($this->model());
}
/**
* Take the provided model and make it accessible to the rest of the repository.
*
2017-08-22 03:10:48 +00:00
* @param string|array $model
* @return mixed
*/
protected function setModel($model)
{
if (is_array($model)) {
if (count($model) !== 2) {
throw new \InvalidArgumentException(
printf('setModel expected exactly 2 parameters, %d received.', count($model))
);
}
return $this->model = call_user_func(
2017-08-22 03:10:48 +00:00
$model[1],
$this->app->make($model[0])
);
}
return $this->model = $this->app->make($model);
}
/**
* @return mixed
*/
abstract public function model();
/**
* Return the model being used for this repository.
*
* @return mixed
*/
public function getModel()
{
return $this->model;
}
/**
* Setup column selection functionality.
*
2017-08-22 03:10:48 +00:00
* @param array $columns
* @return $this
*/
public function withColumns($columns = ['*'])
{
$clone = clone $this;
$clone->columns = is_array($columns) ? $columns : func_get_args();
return $clone;
}
/**
* Return the columns to be selected in the repository call.
*
* @return array
*/
public function getColumns()
{
return $this->columns;
}
/**
* Set repository to not return a fresh record from the DB when completed.
*
* @return $this
*/
public function withoutFresh()
{
$clone = clone $this;
$clone->withFresh = false;
return $clone;
}
}