Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ApiRenderer.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\API;
  10. use Exception;
  11. use Piwik\Common;
  12. use Piwik\DataTable\Renderer;
  13. use Piwik\DataTable;
  14. use Piwik\Piwik;
  15. use Piwik\Plugin;
  16. /**
  17. * API renderer
  18. */
  19. abstract class ApiRenderer
  20. {
  21. protected $request;
  22. final public function __construct($request)
  23. {
  24. $this->request = $request;
  25. $this->init();
  26. }
  27. protected function init() {}
  28. abstract public function sendHeader();
  29. public function renderSuccess($message)
  30. {
  31. return 'Success:' . $message;
  32. }
  33. public function renderException($message, \Exception $exception)
  34. {
  35. return $message;
  36. }
  37. public function renderScalar($scalar)
  38. {
  39. $dataTable = new DataTable\Simple();
  40. $dataTable->addRowsFromArray(array($scalar));
  41. return $this->renderDataTable($dataTable);
  42. }
  43. public function renderDataTable($dataTable)
  44. {
  45. $renderer = $this->buildDataTableRenderer($dataTable);
  46. return $renderer->render();
  47. }
  48. public function renderArray($array)
  49. {
  50. $renderer = $this->buildDataTableRenderer($array);
  51. return $renderer->render();
  52. }
  53. public function renderObject($object)
  54. {
  55. $exception = new Exception('The API cannot handle this data structure.');
  56. return $this->renderException($exception->getMessage(), $exception);
  57. }
  58. public function renderResource($resource)
  59. {
  60. $exception = new Exception('The API cannot handle this data structure.');
  61. return $this->renderException($exception->getMessage(), $exception);
  62. }
  63. /**
  64. * @param $dataTable
  65. * @return Renderer
  66. */
  67. protected function buildDataTableRenderer($dataTable)
  68. {
  69. $format = self::getFormatFromClass(get_class($this));
  70. if ($format == 'json2') {
  71. $format = 'json';
  72. }
  73. $renderer = Renderer::factory($format);
  74. $renderer->setTable($dataTable);
  75. $renderer->setRenderSubTables(Common::getRequestVar('expanded', false, 'int', $this->request));
  76. $renderer->setHideIdSubDatableFromResponse(Common::getRequestVar('hideIdSubDatable', false, 'int', $this->request));
  77. return $renderer;
  78. }
  79. /**
  80. * @param string $format
  81. * @param array $request
  82. * @return ApiRenderer
  83. * @throws Exception
  84. */
  85. public static function factory($format, $request)
  86. {
  87. $formatToCheck = '\\' . ucfirst(strtolower($format));
  88. $rendererClassnames = Plugin\Manager::getInstance()->findMultipleComponents('Renderer', 'Piwik\\API\\ApiRenderer');
  89. foreach ($rendererClassnames as $klassName) {
  90. if (Common::stringEndsWith($klassName, $formatToCheck)) {
  91. return new $klassName($request);
  92. }
  93. }
  94. $availableRenderers = array();
  95. foreach ($rendererClassnames as $rendererClassname) {
  96. $availableRenderers[] = self::getFormatFromClass($rendererClassname);
  97. }
  98. $availableRenderers = implode(', ', $availableRenderers);
  99. Common::sendHeader('Content-Type: text/plain; charset=utf-8');
  100. throw new Exception(Piwik::translate('General_ExceptionInvalidRendererFormat', array($format, $availableRenderers)));
  101. }
  102. private static function getFormatFromClass($klassname)
  103. {
  104. $klass = explode('\\', $klassname);
  105. return strtolower(end($klass));
  106. }
  107. }