2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Abstracts\Http;
|
|
|
|
|
2020-11-06 00:43:46 +03:00
|
|
|
use App\Abstracts\Http\Response;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Traits\Jobs;
|
2020-11-18 13:19:24 +03:00
|
|
|
use App\Traits\Permissions;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Traits\Relationships;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
|
use Illuminate\Pagination\Paginator;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
|
|
|
|
|
|
abstract class Controller extends BaseController
|
|
|
|
{
|
2020-11-18 13:19:24 +03:00
|
|
|
use AuthorizesRequests, Jobs, Permissions, Relationships, ValidatesRequests;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new controller instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2020-11-18 13:19:24 +03:00
|
|
|
$this->assignPermissionsToController();
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a pagination collection.
|
|
|
|
*
|
|
|
|
* @param array|Collection $items
|
|
|
|
* @param int $perPage
|
|
|
|
* @param int $page
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return LengthAwarePaginator
|
|
|
|
*/
|
|
|
|
public function paginate($items, $perPage = 15, $page = null, $options = [])
|
|
|
|
{
|
|
|
|
$perPage = $perPage ?: request('limit', setting('default.list_limit', '25'));
|
|
|
|
|
|
|
|
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
|
|
|
|
|
|
|
|
$items = $items instanceof Collection ? $items : Collection::make($items);
|
|
|
|
|
|
|
|
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
|
|
|
|
}
|
2020-11-06 00:43:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a response based on request type like HTML, JSON, or anything else.
|
|
|
|
*
|
|
|
|
* @param string $view
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function response($view, $data = [])
|
|
|
|
{
|
|
|
|
$class_name = str_replace('Controllers', 'Responses', (new \ReflectionClass($this))->getName());
|
|
|
|
|
|
|
|
if (class_exists($class_name)) {
|
|
|
|
$response = new $class_name($view, $data);
|
|
|
|
} else {
|
|
|
|
$response = new class($view, $data) extends Response {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|