1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Pfilsx\DataGrid\Config\ConfigurationContainerInterface; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Pfilsx\DataGrid\DataGridServiceContainer; |
10
|
|
|
use Pfilsx\DataGrid\Extension\DependencyInjection\DependencyInjectionExtension; |
11
|
|
|
use Pfilsx\DataGrid\Grid\Providers\DataProvider; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
class DataGridFactory implements DataGridFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DataGridServiceContainer |
18
|
|
|
*/ |
19
|
|
|
protected $container; |
20
|
|
|
/** |
21
|
|
|
* @var Request |
22
|
|
|
*/ |
23
|
|
|
protected $request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ConfigurationContainerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $defaultConfiguration; |
29
|
|
|
/** |
30
|
|
|
* @var DependencyInjectionExtension |
31
|
|
|
*/ |
32
|
|
|
protected $extension; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $types; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function __construct(DataGridServiceContainer $container, ConfigurationContainerInterface $configs, DependencyInjectionExtension $extension) |
40
|
|
|
{ |
41
|
|
|
$this->container = $container; |
42
|
|
|
$this->request = $container->getRequest()->getCurrentRequest(); |
43
|
|
|
$this->defaultConfiguration = $configs; |
44
|
|
|
$this->extension = $extension; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
public function createGrid(string $gridType, $dataSource, array $params = []): DataGridInterface |
49
|
|
|
{ |
50
|
|
|
if (!is_subclass_of($gridType, AbstractGridType::class)) { |
51
|
|
|
throw new InvalidArgumentException('Expected subclass of ' . AbstractGridType::class); |
52
|
|
|
} |
53
|
|
|
$provider = DataProvider::create($dataSource, $this->container->getDoctrine()); |
54
|
|
|
|
55
|
|
|
/** @var AbstractGridType $type */ |
56
|
|
|
$gridType = $this->getType($gridType); |
57
|
|
|
$gridType->setParams($params); |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
return new DataGrid($gridType, $provider, $this->defaultConfiguration, $this->container); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @return AbstractGridType |
66
|
|
|
*/ |
67
|
|
|
private function getType(string $name) |
68
|
|
|
{ |
69
|
|
|
if (!isset($this->types[$name])) { |
70
|
|
|
$type = null; |
71
|
|
|
|
72
|
|
|
if ($this->extension->hasType($name)) { |
73
|
|
|
$type = $this->extension->getType($name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!$type) { |
77
|
|
|
// Support fully-qualified class names |
78
|
|
|
if (!class_exists($name)) { |
79
|
|
|
throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not exist.', $name)); |
80
|
|
|
} |
81
|
|
|
if (!is_subclass_of($name, AbstractGridType::class)) { |
82
|
|
|
throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not extend "Pfilsx\DataGrid\AbstractGridType class".', $name)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$type = new $name(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->types[$name] = $type; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->types[$name]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|