forked from RTippin/messenger-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
426 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/storage/*.key | ||
/vendor | ||
/.idea | ||
|
||
/storage/messenger-responses.json | ||
|
||
/public/css | ||
/public/mix-manifest.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class RefreshApiResponsesCommand extends Command | ||
{ | ||
/** | ||
* Git endpoint where the most recent generated responses are stored. | ||
*/ | ||
const GIT_ENDPOINT = 'https://raw.githubusercontent.com/RTippin/messenger/master/docs/generated/responses.json'; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'messenger:get:api'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Download and store a fresh copy of all API responses generated from the messenger core.'; | ||
|
||
/** | ||
* Store the responses file. | ||
*/ | ||
public function handle() | ||
{ | ||
file_put_contents(storage_path('messenger-responses.json'), file_get_contents(self::GIT_ENDPOINT)); | ||
|
||
$this->info("messenger-responses.json stored in public!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Routing\Router; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class ApiExplorerController extends Controller | ||
{ | ||
/** | ||
* @param Router $router | ||
* @return JsonResponse | ||
*/ | ||
public function getRoutes(Router $router): JsonResponse | ||
{ | ||
$routes = collect($router->getRoutes()) | ||
->map(fn ($route) => $this->getRouteInformation($route)) | ||
->filter(fn ($route) => $this->routeIsMessenger($route)) | ||
->sortBy('name') | ||
->values() | ||
->toArray(); | ||
|
||
return new JsonResponse([ | ||
'html' => view('explorer.routes')->with('routes', $routes)->render(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $route | ||
* @return JsonResponse | ||
*/ | ||
public function getRouteResponses(string $route): JsonResponse | ||
{ | ||
$file = storage_path('messenger-responses.json'); | ||
|
||
if (! file_exists($file)) { | ||
throw new NotFoundHttpException("The messenger-responses.json file was not found. Please run the command 'php artisan messenger:get:api' to download it."); | ||
} | ||
|
||
$responses = json_decode(file_get_contents($file), true); | ||
|
||
if (! array_key_exists($route, $responses)) { | ||
throw new NotFoundHttpException("The route name { $route } was not found."); | ||
} | ||
|
||
$details = view('explorer.route-details') | ||
->with('uri', $responses[$route]['uri']) | ||
->with('methods', $responses[$route]['methods']) | ||
->with('query', $responses[$route]['query']) | ||
->with('name', $route) | ||
->render(); | ||
|
||
$verb = explode('|', $responses[$route]['methods'])[0]; | ||
|
||
$data = view('explorer.route-responses') | ||
->with('data', $responses[$route][$verb]) | ||
->with('verb', $verb) | ||
->render(); | ||
|
||
return new JsonResponse([ | ||
'details' => $details, | ||
'data' => $data, | ||
]); | ||
} | ||
|
||
/** | ||
* @param Route $route | ||
* @return array | ||
*/ | ||
private function getRouteInformation(Route $route): array | ||
{ | ||
return [ | ||
'method' => implode('|', $route->methods()), | ||
'uri' => $route->uri(), | ||
'name' => $route->getName(), | ||
]; | ||
} | ||
|
||
/** | ||
* @param array $route | ||
* @return bool | ||
*/ | ||
private function routeIsMessenger(array $route): bool | ||
{ | ||
return Str::contains($route['name'], 'api.messenger'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.