2017-06-11 03:28:44 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-06-11 03:28:44 +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-06-11 03:28:44 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-18 01:01:42 +00:00
|
|
|
namespace Pterodactyl\Repositories;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
use Pterodactyl\Contracts\Repository\RepositoryInterface;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
abstract class Repository implements RepositoryInterface
|
|
|
|
{
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* @var \Illuminate\Foundation\Application
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
protected $app;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
protected $columns = ['*'];
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* @var mixed
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* @var bool
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
protected $withFresh = true;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Repository constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Foundation\Application $application
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function __construct(Application $application)
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->app = $application;
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->setModel($this->model());
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* 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
|
2017-07-01 20:29:49 +00:00
|
|
|
* @return mixed
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
protected function setModel($model)
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
if (is_array($model)) {
|
|
|
|
if (count($model) !== 2) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
printf('setModel expected exactly 2 parameters, %d received.', count($model))
|
|
|
|
);
|
|
|
|
}
|
2017-06-11 03:28:44 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
return $this->model = call_user_func(
|
2017-08-22 03:10:48 +00:00
|
|
|
$model[1],
|
|
|
|
$this->app->make($model[0])
|
2017-06-11 03:28:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
return $this->model = $this->app->make($model);
|
2017-06-11 03:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* @return mixed
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
abstract public function model();
|
2017-06-11 03:28:44 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Return the model being used for this repository.
|
|
|
|
*
|
|
|
|
* @return mixed
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function getModel()
|
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.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param array $columns
|
2017-07-01 20:29:49 +00:00
|
|
|
* @return $this
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function withColumns($columns = ['*'])
|
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.
|
|
|
|
*
|
|
|
|
* @return array
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function getColumns()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Set repository to not return a fresh record from the DB when completed.
|
|
|
|
*
|
|
|
|
* @return $this
|
2017-06-11 03:28:44 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function withoutFresh()
|
2017-06-11 03:28:44 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->withFresh = false;
|
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
|
|
|
}
|
|
|
|
}
|