forked from EC-CUBE/eccube-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEccubeApiEvent.php
More file actions
92 lines (82 loc) · 3.21 KB
/
EccubeApiEvent.php
File metadata and controls
92 lines (82 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
* This file is part of the EccubeApi
*
* Copyright (C) 2016 LOCKON CO.,LTD. All Rights Reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\EccubeApi;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\DomCrawler\Crawler;
class EccubeApiEvent
{
/** @var \Eccube\Application $app */
private $app;
public function __construct($app)
{
$this->app = $app;
}
/**
* 全体に対して適用させるイベント
*
* @param GetResponseEvent $event
*/
public function onAppRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->getMethod() === "OPTIONS") {
$response = new Response();
// https://developer.mozilla.org/ja/docs/HTTP_access_control#Requests_with_credentials
$response->headers->set("Access-Control-Allow-Origin","*");
$response->headers->set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
$response->headers->set("Access-Control-Allow-Headers","Content-Type");
$response->setStatusCode(204);
}
//accepting JSON
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
}
/**
* 全体のレスポンスに対して適用させるイベント
*
* @param FilterResponseEvent $event
*/
public function onAppResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
$response->headers->set("Access-Control-Allow-Origin","*");
$response->headers->set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
$response->headers->set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
}
/**
* 管理画面に APIクライアント一覧へのボタンを表示するイベント
*
* @param FilterResponseEvent $event
*/
public function onRouteAdminMemberResponse(FilterResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
if ($response->isRedirection()) {
return;
}
$html = $response->getContent();
$crawler = new Crawler($html);
$oldElement= $crawler->filter('#common_button_box__insert_button');
$oldHtml= $oldElement->html();
$newHtml= $oldHtml.'<button class="btn btn-primary btn-block btn-lg" onclick="window.location.href=\'api\';">APIクライアント一覧</button>';
$html = $crawler->html();
$html =str_replace($oldHtml, $newHtml, $html);
$response->setContent($html);
$event->setResponse($response);
}
}