1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenSkill\Datatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Contracts\View\Factory; |
7
|
|
|
use OpenSkill\Datatable\Composers\ColumnComposer; |
8
|
|
|
use OpenSkill\Datatable\Providers\Provider; |
9
|
|
|
use OpenSkill\Datatable\Versions\VersionEngine; |
10
|
|
|
use OpenSkill\Datatable\Views\DatatableView; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Datatable |
15
|
|
|
* @package OpenSkill\Datatable |
16
|
|
|
* |
17
|
|
|
* Main class for all data table related operations. |
18
|
|
|
*/ |
19
|
|
|
class Datatable |
20
|
|
|
{ |
21
|
|
|
/** @var VersionEngine */ |
22
|
|
|
private $versionEngine; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Factory |
26
|
|
|
*/ |
27
|
|
|
private $viewFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Repository |
31
|
|
|
*/ |
32
|
|
|
private $configRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Datatable constructor. |
36
|
|
|
* @param VersionEngine $versionEngine The version engine that determines the correct version |
37
|
|
|
* @param Factory $viewFactory The factory used to handle the view generation |
38
|
|
|
* @param Repository $configRepository The repository responsible to get config values |
39
|
|
|
*/ |
40
|
|
|
public function __construct(VersionEngine $versionEngine, Factory $viewFactory, Repository $configRepository) |
41
|
|
|
{ |
42
|
|
|
$this->versionEngine = $versionEngine; |
43
|
|
|
$this->viewFactory = $viewFactory; |
44
|
|
|
$this->configRepository = $configRepository; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Will create a new DataComposer with the given provider as implementation. |
49
|
|
|
* |
50
|
|
|
* @param Provider $provider The provider for the underlying data. |
51
|
|
|
* |
52
|
|
|
* @return ColumnComposer |
53
|
|
|
*/ |
54
|
|
|
public function make(Provider $provider) |
55
|
|
|
{ |
56
|
|
|
$composer = new ColumnComposer($provider, $this->versionEngine, $this->viewFactory, $this->configRepository); |
57
|
|
|
return $composer; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Will return a default DatatableView with no columns defined, but with the view and the version prepared. |
62
|
|
|
* The user is responsible to populate the view with the wished columns, because they can not be derived from |
63
|
|
|
* the server side column configuration. |
64
|
|
|
* |
65
|
|
|
* @param string $tableView the name of the table view to render |
66
|
|
|
* @param string $scriptView the name of the script view to render |
67
|
|
|
* |
68
|
|
|
* @return DatatableView the view to work with |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function view($tableView = null, $scriptView = null) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if (is_null($tableView)) { |
73
|
|
|
$tableView = $this->versionEngine->getVersion()->tableView(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_null($scriptView)) { |
77
|
|
|
$scriptView = $this->versionEngine->getVersion()->scriptView(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new DatatableView($tableView, $scriptView, $this->viewFactory, $this->configRepository, []); |
81
|
|
|
} |
82
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.