2017-07-01 20:29:49 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
2017-08-12 20:29:01 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Pterodactyl\Repository\Repository;
|
2017-08-12 20:29:01 +00:00
|
|
|
use Illuminate\Database\Query\Expression;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\RepositoryInterface;
|
2017-07-08 19:07:51 +00:00
|
|
|
use Pterodactyl\Exceptions\Model\DataValidationException;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-07-01 20:29:49 +00:00
|
|
|
|
|
|
|
abstract class EloquentRepository extends Repository implements RepositoryInterface
|
|
|
|
{
|
|
|
|
/**
|
2017-07-08 19:07:51 +00:00
|
|
|
* {@inheritdoc}
|
2017-07-01 20:29:49 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function getBuilder()
|
|
|
|
{
|
|
|
|
return $this->getModel()->newQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-08 19:07:51 +00:00
|
|
|
* {@inheritdoc}
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param bool $force
|
2017-07-08 19:07:51 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Model|bool
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2017-07-08 19:07:51 +00:00
|
|
|
public function create(array $fields, $validate = true, $force = false)
|
2017-07-01 20:29:49 +00:00
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::boolean($validate, 'Second argument passed to create must be boolean, recieved %s.');
|
|
|
|
Assert::boolean($force, 'Third argument passed to create must be boolean, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$instance = $this->getBuilder()->newModelInstance();
|
|
|
|
|
|
|
|
if ($force) {
|
|
|
|
$instance->forceFill($fields);
|
|
|
|
} else {
|
|
|
|
$instance->fill($fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $validate) {
|
|
|
|
$saved = $instance->skipValidation()->save();
|
|
|
|
} else {
|
|
|
|
if (! $saved = $instance->save()) {
|
|
|
|
throw new DataValidationException($instance->getValidator());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($this->withFresh) ? $instance->fresh() : $saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-08 19:07:51 +00:00
|
|
|
* {@inheritdoc}
|
2017-07-01 20:29:49 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function find($id)
|
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::numeric($id, 'First argument passed to find must be numeric, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$instance = $this->getBuilder()->find($id, $this->getColumns());
|
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:07:51 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2017-07-08 20:04:59 +00:00
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
|
|
|
public function findWhere(array $fields)
|
2017-07-01 20:29:49 +00:00
|
|
|
{
|
2017-07-08 20:04:59 +00:00
|
|
|
return $this->getBuilder()->where($fields)->get($this->getColumns());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function findFirstWhere(array $fields)
|
|
|
|
{
|
|
|
|
$instance = $this->getBuilder()->where($fields)->first($this->getColumns());
|
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
2017-07-01 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}.
|
|
|
|
*/
|
|
|
|
public function findCountWhere(array $fields)
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->where($fields)->count($this->getColumns());
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
2017-07-08 19:07:51 +00:00
|
|
|
* {@inheritdoc}
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
|
|
|
public function delete($id, $destroy = false)
|
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::numeric($id, 'First argument passed to delete must be numeric, received %s.');
|
|
|
|
Assert::boolean($destroy, 'Second argument passed to delete must be boolean, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-08-09 02:21:10 +00:00
|
|
|
$instance = $this->getBuilder()->where($this->getModel()->getKeyName(), $id);
|
|
|
|
|
|
|
|
return ($destroy) ? $instance->forceDelete() : $instance->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function deleteWhere(array $attributes, $force = false)
|
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::boolean($force, 'Second argument passed to deleteWhere must be boolean, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-08-09 02:21:10 +00:00
|
|
|
$instance = $this->getBuilder()->where($attributes);
|
2017-07-01 20:29:49 +00:00
|
|
|
|
2017-08-09 02:21:10 +00:00
|
|
|
return ($force) ? $instance->forceDelete() : $instance->delete();
|
2017-07-01 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-08 19:07:51 +00:00
|
|
|
* {@inheritdoc}
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2017-07-08 19:07:51 +00:00
|
|
|
public function update($id, array $fields, $validate = true, $force = false)
|
2017-07-01 20:29:49 +00:00
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::numeric($id, 'First argument passed to update must be numeric, received %s.');
|
|
|
|
Assert::boolean($validate, 'Third argument passed to update must be boolean, received %s.');
|
|
|
|
Assert::boolean($force, 'Fourth argument passed to update must be boolean, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$instance = $this->getBuilder()->where('id', $id)->first();
|
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
throw new RecordNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($force) {
|
|
|
|
$instance->forceFill($fields);
|
|
|
|
} else {
|
|
|
|
$instance->fill($fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $validate) {
|
|
|
|
$saved = $instance->skipValidation()->save();
|
|
|
|
} else {
|
|
|
|
if (! $saved = $instance->save()) {
|
|
|
|
throw new DataValidationException($instance->getValidator());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:17:07 +00:00
|
|
|
return ($this->withFresh) ? $instance->fresh() : $saved;
|
2017-07-01 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function updateWhereIn($column, array $values, array $fields)
|
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::stringNotEmpty($column, 'First argument passed to updateWhereIn must be a non-empty string, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
return $this->getBuilder()->whereIn($column, $values)->update($fields);
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:07:51 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function massUpdate(array $where, array $fields)
|
2017-07-01 20:29:49 +00:00
|
|
|
{
|
|
|
|
// TODO: Implement massUpdate() method.
|
|
|
|
}
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->get($this->getColumns());
|
|
|
|
}
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function insert(array $data)
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->insert($data);
|
|
|
|
}
|
2017-07-25 02:34:10 +00:00
|
|
|
|
2017-08-06 02:10:32 +00:00
|
|
|
/**
|
|
|
|
* Insert multiple records into the database and ignore duplicates.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param array $values
|
2017-08-06 02:10:32 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function insertIgnore(array $values)
|
|
|
|
{
|
|
|
|
if (empty($values)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! is_array(reset($values))) {
|
|
|
|
$values = [$values];
|
|
|
|
} else {
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
ksort($value);
|
|
|
|
$values[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$bindings = array_values(array_filter(array_flatten($values, 1), function ($binding) {
|
|
|
|
return ! $binding instanceof Expression;
|
|
|
|
}));
|
|
|
|
|
|
|
|
$grammar = $this->getBuilder()->toBase()->getGrammar();
|
|
|
|
$table = $grammar->wrapTable($this->getModel()->getTable());
|
|
|
|
$columns = $grammar->columnize(array_keys(reset($values)));
|
|
|
|
|
|
|
|
$parameters = collect($values)->map(function ($record) use ($grammar) {
|
|
|
|
return sprintf('(%s)', $grammar->parameterize($record));
|
|
|
|
})->implode(', ');
|
|
|
|
|
|
|
|
$statement = "insert ignore into $table ($columns) values $parameters";
|
|
|
|
|
|
|
|
return $this->getBuilder()->getConnection()->statement($statement, $bindings);
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:34:10 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
* @return bool|\Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function updateOrCreate(array $where, array $fields, $validate = true, $force = false)
|
|
|
|
{
|
2017-08-16 03:21:47 +00:00
|
|
|
Assert::boolean($validate, 'Third argument passed to updateOrCreate must be boolean, received %s.');
|
|
|
|
Assert::boolean($force, 'Fourth argument passed to updateOrCreate must be boolean, received %s.');
|
2017-08-12 20:29:01 +00:00
|
|
|
|
2017-07-25 02:34:10 +00:00
|
|
|
$instance = $this->withColumns('id')->findWhere($where)->first();
|
|
|
|
|
|
|
|
if (! $instance) {
|
|
|
|
return $this->create(array_merge($where, $fields), $validate, $force);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->update($instance->id, $fields, $validate, $force);
|
|
|
|
}
|
2017-07-01 20:29:49 +00:00
|
|
|
}
|