2017-06-11 03:28:44 +00:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
namespace Pterodactyl\Repository;
|
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.
|
|
|
|
*
|
|
|
|
* @param string|array $model
|
|
|
|
* @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(
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @param array $columns
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|