1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Block; |
15
|
|
|
|
16
|
|
|
use SimpleThings\EntityAudit\AuditReader; |
17
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
18
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
19
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
20
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
24
|
|
|
use Twig\Environment; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Thomas Rabaix <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class AuditBlockService extends AbstractBlockService |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var AuditReader |
33
|
|
|
*/ |
34
|
|
|
protected $auditReader; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* NEXT_MAJOR: Allow only Environment|EngineInterface for argument 1 and AuditReader for argument 2. |
38
|
|
|
* |
39
|
|
|
* @param Environment|EngineInterface|string $templatingOrDeprecatedName |
40
|
|
|
* @param EngineInterface|AuditReader $templatingOrAuditReader |
41
|
|
|
*/ |
42
|
|
|
public function __construct($templatingOrDeprecatedName, object $templatingOrAuditReader, ?AuditReader $auditReader = null) |
43
|
|
|
{ |
44
|
|
|
if ($templatingOrAuditReader instanceof EngineInterface) { |
45
|
|
|
@trigger_error(sprintf( |
|
|
|
|
46
|
|
|
'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x' |
47
|
|
|
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.', |
48
|
|
|
EngineInterface::class, |
49
|
|
|
__METHOD__, |
50
|
|
|
AuditReader::class |
51
|
|
|
), E_USER_DEPRECATED); |
52
|
|
|
|
53
|
|
|
if (null === $auditReader) { |
54
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
55
|
|
|
'Passing null as argument 3 to %s() is not allowed when %s is passed as argument 2.' |
56
|
|
|
.' You must pass an instance of %s instead.', |
57
|
|
|
__METHOD__, |
58
|
|
|
EngineInterface::class, |
59
|
|
|
AuditReader::class |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
parent::__construct($templatingOrDeprecatedName, $templatingOrAuditReader); |
64
|
|
|
|
65
|
|
|
$this->auditReader = $auditReader; |
66
|
|
|
} elseif ($templatingOrAuditReader instanceof AuditReader) { |
67
|
|
|
if (!$templatingOrDeprecatedName instanceof Environment |
68
|
|
|
&& !$templatingOrDeprecatedName instanceof EngineInterface |
69
|
|
|
) { |
70
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
71
|
|
|
'Argument 1 passed to %s() must be either an instance of %s or preferably %s, %s given.', |
72
|
|
|
__METHOD__, |
73
|
|
|
EngineInterface::class, |
74
|
|
|
Environment::class, |
75
|
|
|
\is_object($templatingOrDeprecatedName) |
76
|
|
|
? 'instance of '.\get_class($templatingOrDeprecatedName) |
77
|
|
|
: \gettype($templatingOrDeprecatedName) |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
parent::__construct($templatingOrDeprecatedName); |
82
|
|
|
|
83
|
|
|
$this->auditReader = $templatingOrAuditReader; |
84
|
|
|
} else { |
85
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
86
|
|
|
'Argument 2 passed to %s() must be either an instance of %s or preferably %s, instance of %s given.', |
87
|
|
|
__METHOD__, |
88
|
|
|
EngineInterface::class, |
89
|
|
|
AuditReader::class, |
90
|
|
|
\get_class($templatingOrAuditReader) |
91
|
|
|
)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function execute(BlockContextInterface $blockContext, ?Response $response = null) |
96
|
|
|
{ |
97
|
|
|
$revisions = []; |
98
|
|
|
|
99
|
|
|
foreach ($this->auditReader->findRevisionHistory($blockContext->getSetting('limit'), 0) as $revision) { |
100
|
|
|
$revisions[] = [ |
101
|
|
|
'revision' => $revision, |
102
|
|
|
'entities' => $this->auditReader->findEntitiesChangedAtRevision($revision->getRev()), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->renderResponse($blockContext->getTemplate(), [ |
107
|
|
|
'block' => $blockContext->getBlock(), |
108
|
|
|
'settings' => $blockContext->getSettings(), |
109
|
|
|
'revisions' => $revisions, |
110
|
|
|
], $response); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getName() |
118
|
|
|
{ |
119
|
|
|
return 'Audit List'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
123
|
|
|
{ |
124
|
|
|
$resolver->setDefaults([ |
125
|
|
|
'limit' => 10, |
126
|
|
|
'template' => '@SonataDoctrineORMAdmin/Block/block_audit.html.twig', |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: