|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\SmokerBundle\Response\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Goutte\Client; |
|
7
|
|
|
use LAG\SmokerBundle\Exception\Exception; |
|
8
|
|
|
use LAG\SmokerBundle\Url\Registry\UrlProviderRegistry; |
|
9
|
|
|
use LAG\SmokerBundle\Url\Requirements\Mapping\MappingResolverInterface; |
|
10
|
|
|
use LAG\SmokerBundle\Url\Requirements\Registry\RequirementsProviderRegistry; |
|
11
|
|
|
use Symfony\Component\BrowserKit\Response; |
|
12
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
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($this->getMappingName($routeName), $routeName); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
foreach ($configuration as $selector => $content) { |
|
57
|
|
|
if ($this->isDynamicString($content)) { |
|
58
|
|
|
/** @var Response $response */ |
|
59
|
|
|
$response = $client->getResponse(); |
|
|
|
|
|
|
60
|
|
|
$requirements = []; |
|
61
|
|
|
//$response->getHeader(); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->requirementsProviderRegistry->all() as $requirementsProvider) { |
|
64
|
|
|
if (!$requirementsProvider->supports($routeName)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
$requirements = array_merge($requirements, $requirementsProvider->getRequirements($routeName)); |
|
68
|
|
|
} |
|
69
|
|
|
$criteria = []; |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
foreach ($requirements as $requirement) { |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
var_dump($mapping, $client->getResponse()); |
|
|
|
|
|
|
79
|
|
|
die; |
|
|
|
|
|
|
80
|
|
|
// $provider = $this->registry->get('default'); |
|
81
|
|
|
// $provider->getRequirements($routeName, [ |
|
82
|
|
|
// 'where' => '', |
|
83
|
|
|
// ]); |
|
84
|
|
|
} else { |
|
85
|
|
|
if (false === strpos($crawler->filter($selector)->text(), $content)) { |
|
86
|
|
|
throw new Exception(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the unique name of the response handler. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getName(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return 'html'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function isDynamicString(string $content) |
|
103
|
|
|
{ |
|
104
|
|
|
if ('{{' !== substr($content, 0, 2)) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ('}}' !== substr($content, -2)) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|