1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of cwdFancyGridBundle |
4
|
|
|
* |
5
|
|
|
* (c)2017 cwd.at GmbH <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
namespace Cwd\FancyGridBundle\Grid; |
12
|
|
|
|
13
|
|
|
use Cwd\FancyGridBundle\Grid\Exception\InvalidArgumentException; |
14
|
|
|
use Cwd\FancyGridBundle\Grid\Exception\UnexpectedTypeException; |
15
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class GridFactory |
21
|
|
|
* @package Cwd\FancyGridBundle\Grid |
22
|
|
|
* @author Ludwig Ruderstaler <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class GridFactory |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ManagerRegistry |
28
|
|
|
*/ |
29
|
|
|
protected $doctrineRegistry; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TranslatorInterface |
33
|
|
|
*/ |
34
|
|
|
protected $translator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Twig_Environment |
38
|
|
|
*/ |
39
|
|
|
protected $twig; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* GridFactory constructor. |
43
|
|
|
* @param TranslatorInterface $translator |
44
|
|
|
* @param ManagerRegistry $doctrineRegistry |
45
|
|
|
* @param \Twig_environment $twig |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
TranslatorInterface $translator, |
49
|
|
|
ManagerRegistry $doctrineRegistry, |
50
|
|
|
\Twig_Environment $twig |
51
|
|
|
) { |
52
|
|
|
$this->doctrineRegistry = $doctrineRegistry; |
53
|
|
|
$this->translator = $translator; |
54
|
|
|
$this->twig = $twig; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $type |
59
|
|
|
* @param array $options |
60
|
|
|
* @return GridInterface |
61
|
|
|
*/ |
62
|
|
|
public function create($type = 'Cwd\FancyGridBundle\Grid\AbstractGrid', array $options = array()) |
63
|
|
|
{ |
64
|
|
|
if (!is_string($type)) { |
65
|
|
|
throw new UnexpectedTypeException($type, 'string'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$type = $this->getType($type, $options); |
69
|
|
|
$builder = new GridBuilder($this->doctrineRegistry->getManager(), new EventDispatcher(), $options); |
70
|
|
|
|
71
|
|
|
$type->buildGrid($builder, array_merge($type->getOptions(), $options)); |
72
|
|
|
$type->setChildren($builder->children); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
return $type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @param array $options |
81
|
|
|
* @return GridInterface |
82
|
|
|
*/ |
83
|
|
|
public function getType($name, $options) |
84
|
|
|
{ |
85
|
|
|
if (class_exists($name) && in_array('Cwd\FancyGridBundle\Grid\GridInterface', class_implements($name))) { |
86
|
|
|
$type = new $name($this->translator, $options); |
87
|
|
|
$type->setObjectManager($this->doctrineRegistry->getManager()); |
88
|
|
|
$type->setTwig($this->twig); |
89
|
|
|
} else { |
90
|
|
|
throw new InvalidArgumentException(sprintf('Could not load type "%s"', $name)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|