Github.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * GitHub
  4. *
  5. * This file is part of Grav MediaEmbed plugin.
  6. *
  7. * Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
  8. * http://benjamin-regler.de/license/
  9. */
  10. namespace Grav\Plugin\MediaEmbed\Services;
  11. use Grav\Common\Debugger;
  12. use Grav\Plugin\MediaEmbed\OEmbed\OEmbedRich;
  13. /**
  14. * GitHub
  15. */
  16. class GitHub extends OEmbedRich
  17. {
  18. public function getOEmbed()
  19. {
  20. if ($this->oembed) {
  21. return $this->oembed;
  22. }
  23. $endpoint = $this->format($this->config->get('endpoint', ''));
  24. if (!$endpoint) {
  25. return [];
  26. }
  27. $response = \Requests::get($endpoint);
  28. if (!$response->success) {
  29. $response->throw_for_status();
  30. }
  31. $json = json_decode($response->body, true);
  32. $this->oembed = [
  33. 'type' => 'rich',
  34. 'title' => $json['files'],
  35. 'description' => $json['description'],
  36. 'author_name' => $json['owner'],
  37. 'author_url' => 'https://github.com/' . $json['owner'],
  38. 'provider' => 'GitHub',
  39. 'provider_url' => 'https://gist.github.com/',
  40. 'url' => 'https://gist.github.com/' . $this->embedCode,
  41. 'html' => $json['div'],
  42. ];
  43. $this->config->join('assets', [$json['stylesheet']]);
  44. return $this->oembed;
  45. }
  46. }