From 227ea53c09b34c755802f51474cfd85e8a72630a Mon Sep 17 00:00:00 2001 From: Aaron Valandra Date: Wed, 12 Apr 2017 10:25:57 -0500 Subject: [PATCH] Added root controller to display version of Bolt and extension on /json or API is active if debug is not set. --- src/Controllers/ContentController.php | 3 ++ src/Controllers/RootAction.php | 67 +++++++++++++++++++++++++++ src/Provider/APIProvider.php | 22 +++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/Controllers/RootAction.php diff --git a/src/Controllers/ContentController.php b/src/Controllers/ContentController.php index 7631629..7478682 100644 --- a/src/Controllers/ContentController.php +++ b/src/Controllers/ContentController.php @@ -42,6 +42,9 @@ public function connect(Application $app) */ $ctr = $app['controllers_factory']; + $ctr->get('/', [$app['jsonapi.action.root'], 'handle']) + ->bind('jsonapi'); + $ctr->get('/menu', [$app['jsonapi.action.menu'], 'handle']) ->bind('jsonapi.menu'); diff --git a/src/Controllers/RootAction.php b/src/Controllers/RootAction.php new file mode 100644 index 0000000..1f0fe8d --- /dev/null +++ b/src/Controllers/RootAction.php @@ -0,0 +1,67 @@ +boltConfig = $boltConfig; + $this->extensionConfig = $extensionConfig; + $this->boltVersion = $boltVersion; + $this->jsonAPIVersion = $jsonAPIVersion; + } + + /** + * @param Request $request + * + * @return ApiResponse + */ + public function handle(Request $request) + { + $data = 'API is active.'; + $debug = $this->boltConfig->getConfig()['general']['debug']; + + if ($debug) { + $data = [ + 'versions' => [ + 'bolt' => $this->boltVersion, + 'jsonapi' => $this->jsonAPIVersion + ] + ]; + } + + return new ApiResponse([ + 'data' => $data, + ], $this->extensionConfig); + } +} diff --git a/src/Provider/APIProvider.php b/src/Provider/APIProvider.php index f3cb903..02b9bb8 100644 --- a/src/Provider/APIProvider.php +++ b/src/Provider/APIProvider.php @@ -2,6 +2,8 @@ namespace Bolt\Extension\Bolt\JsonApi\Provider; +use Bolt\Composer\Package; +use Bolt\Composer\PackageCollection; use Bolt\Extension\Bolt\JsonApi\Action\ContentListAction; use Bolt\Extension\Bolt\JsonApi\Action\MenuAction; use Bolt\Extension\Bolt\JsonApi\Action\SearchAction; @@ -16,6 +18,7 @@ use Silex\Application; use Silex\ServiceProviderInterface; use Bolt\Storage\Query\ContentQueryParser; +use Bolt\Extension\Bolt\JsonApi\Action\RootAction; /** @@ -142,6 +145,25 @@ function ($app) { } ); + $app['jsonapi.action.root'] = $app->share( + function ($app) { + $boltVersion = $app['bolt_version']; + /** @var PackageCollection $jsonapiVersion */ + $jsonAPIVersion = $app['extend.manager']->getAllPackages(); + /** @var Package $jsonapiVersion */ + $jsonAPIVersion = $jsonAPIVersion->get('bolt/jsonapi'); + //Get exact version + $jsonAPIVersion = $jsonAPIVersion->jsonSerialize()['version']; + + return new RootAction( + $app['config'], + $app['jsonapi.config'], + $boltVersion, + $jsonAPIVersion + ); + } + ); + $app['jsonapi.action.taxonomy'] = $app->share( function ($app) { return new TaxonomyAction(