1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\SmokerBundle\Response\Handler; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Goutte\Client; |
7
|
|
|
use LAG\SmokerBundle\Contracts\Requirements\Mapping\MappingResolverInterface; |
8
|
|
|
use LAG\SmokerBundle\Exception\Exception; |
9
|
|
|
use LAG\SmokerBundle\Url\Registry\UrlProviderRegistry; |
10
|
|
|
use LAG\SmokerBundle\Url\Requirements\Registry\RequirementsProviderRegistry; |
11
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
13
|
|
|
|
14
|
|
|
class HtmlHandler extends AbstractHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var MappingResolverInterface |
18
|
|
|
*/ |
19
|
|
|
private $mappingResolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityManagerInterface |
23
|
|
|
*/ |
24
|
|
|
private $entityManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var UrlProviderRegistry |
28
|
|
|
*/ |
29
|
|
|
private $urlProviderRegistry; |
30
|
|
|
/** |
31
|
|
|
* @var RequirementsProviderRegistry |
32
|
|
|
*/ |
33
|
|
|
private $requirementsProviderRegistry; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
MappingResolverInterface $mappingResolver, |
37
|
|
|
EntityManagerInterface $entityManager, |
38
|
|
|
UrlProviderRegistry $urlProviderRegistry, |
39
|
|
|
RequirementsProviderRegistry $requirementsProviderRegistry, |
40
|
|
|
array $configuration = [] |
41
|
|
|
) { |
42
|
|
|
parent::__construct($configuration); |
43
|
|
|
|
44
|
|
|
$this->mappingResolver = $mappingResolver; |
45
|
|
|
$this->entityManager = $entityManager; |
46
|
|
|
$this->configuration = $configuration; |
47
|
|
|
$this->urlProviderRegistry = $urlProviderRegistry; |
48
|
|
|
$this->requirementsProviderRegistry = $requirementsProviderRegistry; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function handle(string $routeName, Crawler $crawler, Client $client, array $options = []): void |
52
|
|
|
{ |
53
|
|
|
$configuration = $this->getConfiguration($routeName); |
54
|
|
|
$mapping = $this->mappingResolver->resolve($routeName); |
55
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
56
|
|
|
|
57
|
|
|
if (null === $mapping) { |
|
|
|
|
58
|
|
|
throw new Exception('No mapping found for the html response handler for the route "'.$routeName.'"'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($configuration as $selector => $content) { |
62
|
|
|
preg_match('#\{\{(.*?)\}\}#', $content, $match); |
63
|
|
|
$isDynamicString = 1 < count($match); |
64
|
|
|
|
65
|
|
|
if ($isDynamicString) { |
66
|
|
|
$identifiers = $options['_identifiers']; |
67
|
|
|
|
68
|
|
|
foreach ($this->requirementsProviderRegistry->all() as $requirementsProvider) { |
69
|
|
|
if (!$requirementsProvider->supports($routeName)) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$entities = $requirementsProvider->getDataProvider()->getData($mapping['entity'], [ |
74
|
|
|
'where' => $identifiers, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
foreach ($entities as $entity) { |
78
|
|
|
$entity = $entity[0]; |
79
|
|
|
preg_match('#\{\{(.*?)\}\}#', $content, $match); |
80
|
|
|
$property = trim($match[1]); |
81
|
|
|
$valueToFind = $accessor->getValue($entity, $property); |
82
|
|
|
$found = false; |
83
|
|
|
|
84
|
|
|
$crawler->filter($selector)->each(function (Crawler $node) use ($valueToFind, &$found) { |
85
|
|
|
if (false !== strpos($node->text(), $valueToFind)) { |
86
|
|
|
$found = true; |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
|
|
if (false === strpos($crawler->filter($selector)->text(), $content)) { |
93
|
|
|
throw new Exception(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the unique name of the response handler. |
101
|
|
|
*/ |
102
|
|
|
public function getName(): string |
103
|
|
|
{ |
104
|
|
|
return 'html'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function isDynamicString(string $content) |
108
|
|
|
{ |
109
|
|
|
if ('{{' !== substr($content, 0, 2)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ('}}' !== substr($content, -2)) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|