DataTableManipulator
6 years ago
ApiRenderer.php
6 years ago
CORSHandler.php
6 years ago
DataTableGenericFilter.php
6 years ago
DataTableManipulator.php
6 years ago
DataTablePostProcessor.php
6 years ago
DocumentationGenerator.php
6 years ago
Inconsistencies.php
6 years ago
Proxy.php
6 years ago
Request.php
6 years ago
ResponseBuilder.php
6 years ago
ApiRenderer.php
153 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\API; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Common; |
| 13 | use Piwik\DataTable\Renderer; |
| 14 | use Piwik\DataTable; |
| 15 | use Piwik\Piwik; |
| 16 | use Piwik\Plugin; |
| 17 | use Piwik\SettingsServer; |
| 18 | |
| 19 | /** |
| 20 | * API renderer |
| 21 | */ |
| 22 | abstract class ApiRenderer |
| 23 | { |
| 24 | protected $request; |
| 25 | |
| 26 | protected $hideIdSubDataTable; |
| 27 | |
| 28 | final public function __construct($request) |
| 29 | { |
| 30 | $this->request = $request; |
| 31 | $this->init(); |
| 32 | } |
| 33 | |
| 34 | protected function init() |
| 35 | { |
| 36 | $this->hideIdSubDataTable = Common::getRequestVar('hideIdSubDatable', false, 'int', $this->request); |
| 37 | } |
| 38 | |
| 39 | protected function shouldSendBacktrace() |
| 40 | { |
| 41 | return Common::isPhpCliMode() && SettingsServer::isArchivePhpTriggered(); |
| 42 | } |
| 43 | |
| 44 | abstract public function sendHeader(); |
| 45 | |
| 46 | public function renderSuccess($message) |
| 47 | { |
| 48 | return 'Success:' . $message; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param $message |
| 53 | * @param Exception|\Throwable $exception |
| 54 | * @return mixed |
| 55 | */ |
| 56 | public function renderException($message, $exception) |
| 57 | { |
| 58 | return $message; |
| 59 | } |
| 60 | |
| 61 | public function renderScalar($scalar) |
| 62 | { |
| 63 | $dataTable = new DataTable\Simple(); |
| 64 | $dataTable->addRowsFromArray(array($scalar)); |
| 65 | return $this->renderDataTable($dataTable); |
| 66 | } |
| 67 | |
| 68 | public function renderDataTable($dataTable) |
| 69 | { |
| 70 | $renderer = $this->buildDataTableRenderer($dataTable); |
| 71 | return $renderer->render(); |
| 72 | } |
| 73 | |
| 74 | public function renderArray($array) |
| 75 | { |
| 76 | $renderer = $this->buildDataTableRenderer($array); |
| 77 | return $renderer->render(); |
| 78 | } |
| 79 | |
| 80 | public function renderObject($object) |
| 81 | { |
| 82 | $exception = new Exception('The API cannot handle this data structure.'); |
| 83 | return $this->renderException($exception->getMessage(), $exception); |
| 84 | } |
| 85 | |
| 86 | public function renderResource($resource) |
| 87 | { |
| 88 | $exception = new Exception('The API cannot handle this data structure.'); |
| 89 | return $this->renderException($exception->getMessage(), $exception); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param $dataTable |
| 94 | * @return Renderer |
| 95 | */ |
| 96 | protected function buildDataTableRenderer($dataTable) |
| 97 | { |
| 98 | $format = self::getFormatFromClass(get_class($this)); |
| 99 | if ($format == 'json2') { |
| 100 | $format = 'json'; |
| 101 | } |
| 102 | |
| 103 | $idSite = Common::getRequestVar('idSite', 0, 'int', $this->request); |
| 104 | |
| 105 | if (empty($idSite)) { |
| 106 | $idSite = 'all'; |
| 107 | } |
| 108 | |
| 109 | $renderer = Renderer::factory($format); |
| 110 | $renderer->setTable($dataTable); |
| 111 | $renderer->setIdSite($idSite); |
| 112 | $renderer->setRenderSubTables(Common::getRequestVar('expanded', false, 'int', $this->request)); |
| 113 | $renderer->setHideIdSubDatableFromResponse($this->hideIdSubDataTable); |
| 114 | |
| 115 | return $renderer; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param string $format |
| 120 | * @param array $request |
| 121 | * @return ApiRenderer |
| 122 | * @throws Exception |
| 123 | */ |
| 124 | public static function factory($format, $request) |
| 125 | { |
| 126 | $formatToCheck = '\\' . ucfirst(strtolower($format)); |
| 127 | |
| 128 | $rendererClassnames = Plugin\Manager::getInstance()->findMultipleComponents('Renderer', 'Piwik\\API\\ApiRenderer'); |
| 129 | |
| 130 | foreach ($rendererClassnames as $klassName) { |
| 131 | if (Common::stringEndsWith($klassName, $formatToCheck)) { |
| 132 | return new $klassName($request); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | $availableRenderers = array(); |
| 137 | foreach ($rendererClassnames as $rendererClassname) { |
| 138 | $availableRenderers[] = self::getFormatFromClass($rendererClassname); |
| 139 | } |
| 140 | |
| 141 | $availableRenderers = implode(', ', $availableRenderers); |
| 142 | Common::sendHeader('Content-Type: text/plain; charset=utf-8'); |
| 143 | throw new Exception(Piwik::translate('General_ExceptionInvalidRendererFormat', array($format, $availableRenderers))); |
| 144 | } |
| 145 | |
| 146 | private static function getFormatFromClass($klassname) |
| 147 | { |
| 148 | $klass = explode('\\', $klassname); |
| 149 | |
| 150 | return strtolower(end($klass)); |
| 151 | } |
| 152 | } |
| 153 |