Ejemplo n.º 1
0
 public function __construct()
 {
     header('access-control-allow-origin: *');
     if ($this->checkMethod($_SERVER['REQUEST_METHOD']) && ($file = $this->getInputFile())) {
         $parser = new Parser($this->getPostValue('from'), $_FILES['input']['name'], $this->getPostValue('title'));
         $this->logs = [];
         $parser->setLogger($this);
         try {
             $dictionary = $parser->parse($file);
             $parserLogs = $this->logs;
             $serializer = new Serializer($this->getPostValue('to') ?? '汎用辞書');
             $this->logs = [];
             $serializer->setLogger($this);
             $outputFile = $serializer->serialize($dictionary);
             $serializerLogs = $this->logs;
             $formData = new xmlhttprequest\FormData();
             $formData->append('output', $outputFile['bytes'], $outputFile['name'], $outputFile['type']);
             if ($parserLogs) {
                 $parserLogsFile = $this->convertLogsToFile($parserLogs, _('構文解析時に1つ以上のロギングが発生しました。'));
                 $formData->append('parser-logs', $parserLogsFile['bytes'], null, $parserLogsFile['type']);
             }
             if ($serializerLogs) {
                 $serializerLogsFile = $this->convertLogsToFile($serializerLogs, _('直列化時に1つ以上のロギングが発生しました。'));
                 $formData->append('serializer-logs', $serializerLogsFile['bytes'], null, $serializerLogsFile['type']);
             }
             header($formData->getContentType());
             echo $formData->encode();
         } catch (SyntaxException $e) {
             $this->responseError(['type' => 'https://github.com/esperecyan/dictionary-api/blob/master/malformed-syntax.md', 'title' => 'Malformed Syntax', 'status' => 400, 'detail' => $e->getMessage()]);
         } catch (SerializeExceptionInterface $e) {
             $this->responseError(['type' => 'https://github.com/esperecyan/dictionary-api/blob/master/serialize-error.md', 'title' => 'Serialize Error', 'status' => 400, 'detail' => $e->getMessage()]);
         } catch (\Throwable $e) {
             $this->responseError(['title' => 'Internal Server Error', 'status' => 500, 'detail' => _('ファイルの変換に失敗しました。')]);
             throw $e;
         }
     }
 }