StatusPreviewController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Drupal\statusmessage\Controller;
  3. require_once(DRUPAL_ROOT .'/vendor/autoload.php');
  4. use Drupal\Core\Controller\ControllerBase;
  5. use Drupal\Core\Render\Markup;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use GuzzleHttp\Client;
  9. use Drupal\statusmessage\MarkupGenerator;
  10. /**
  11. * Class StatusPreviewController.
  12. *
  13. * @package Drupal\statusmessage\Controller
  14. */
  15. class StatusPreviewController extends ControllerBase {
  16. protected $httpClient;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static function create(ContainerInterface $container) {
  21. return new static(
  22. $container->get('http_client'));
  23. }
  24. /**
  25. * Constructor.
  26. */
  27. public function __construct(Client $http_client) {
  28. $this->httpClient = $http_client;
  29. }
  30. /**
  31. * Generate.
  32. *
  33. * @return string
  34. * Return Hello string.
  35. */
  36. public function generate($url) {
  37. if ($url == 'build') {
  38. $url = \Drupal::request()->get('data');
  39. $generator = new MarkupGenerator();
  40. if (!strpos($url, 'http://')) {
  41. $url = 'http://' . $url;
  42. }
  43. if ($generator->parseMarkup($url)) {
  44. $preview = $generator->generatePreview();
  45. $response = new Response();
  46. $response->setContent(\GuzzleHttp\json_encode(array('data' => $preview)));
  47. $response->headers->set('Content-Type', 'application/json');
  48. return $response;
  49. }
  50. // $contents = file_get_contents('http://' . $url);
  51. // $response = new Response();
  52. // $this->dom = new \DOMDocument;
  53. // $this->dom->loadHTML($contents);
  54. //
  55. // $xpath = new \DomXpath($this->dom);
  56. //
  57. // $anchorAttributes = $this->getAnchorNodeNames();
  58. // $imgAttributes = $this->getImgNodeNames();
  59. // $imgLogos = $this->searchDom('img', 'logo');
  60. // $anchorLogos = $this->searchDom('a', 'logo');
  61. //
  62. }
  63. return false;
  64. }
  65. // private function getAnchorNodeNames() {
  66. // if ($this->dom) {
  67. // $names = array();
  68. // $attrXpath = new \DomXpath($this->dom);
  69. //
  70. // $nodes = $attrXpath->query('//a/@*');
  71. // $i = 0;
  72. // foreach ($nodes as $node) {
  73. // $names[$i] = new \stdClass();
  74. // $names[$i]->name = $node->nodeName;
  75. // $names[$i]->value = $node->nodeValue;
  76. // $i++;
  77. // }
  78. //
  79. // return $names;
  80. // }
  81. // }
  82. //
  83. // private function getImgNodeNames() {
  84. // if ($this->dom) {
  85. // $names = array();
  86. // $attrXpath = new \DomXpath($this->dom);
  87. //
  88. // $nodes = $attrXpath->query('//img/@*');
  89. // $i = 0;
  90. // foreach ($nodes as $node) {
  91. // $names[$i] = new \stdClass();
  92. // $names[$i]->name = $node->nodeName;
  93. // $names[$i]->value = $node->nodeValue;
  94. // $i++;
  95. // }
  96. //
  97. // return $names;
  98. // }
  99. // }
  100. //
  101. // private function searchDom($tag, $string) {
  102. //
  103. // if ($this->dom) {
  104. //
  105. // $results = array();
  106. // $tags = $this->dom->getElementsByTagName($tag);
  107. //
  108. //
  109. // for ($i = 0; $i < $tags->length; $i++) {
  110. // $results[$i] = new \stdClass();
  111. //
  112. // $src = $tags->item($i)->getAttribute('src');
  113. // if (strpos($src, 'logo')) {
  114. // $results[$i]->src = $src;
  115. // }
  116. //
  117. // $href = $tags->item($i)->getAttribute('href');
  118. // if (strpos($href, 'logo')) {
  119. // $results[$i]->href = $href;
  120. // }
  121. // }
  122. //
  123. // return $results;
  124. // }
  125. // }
  126. }