Skip to content

Commit

Permalink
Merge pull request #48 from Raistlfiren/root
Browse files Browse the repository at this point in the history
Added root controller for api route
  • Loading branch information
Raistlfiren authored Apr 12, 2017
2 parents 0824397 + 227ea53 commit 5fb2f6a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
67 changes: 67 additions & 0 deletions src/Controllers/RootAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Bolt\Extension\Bolt\JsonApi\Action;

use Bolt\Config;
use Bolt\Extension\Bolt\JsonApi\Response\ApiResponse;
use Symfony\Component\HttpFoundation\Request;

/**
* Class RootAction
*
* @package Bolt\Extension\Bolt\JsonApi\Action
*/
class RootAction
{
/** @var Config $config */
protected $boltConfig;

/** @var \Bolt\Extension\Bolt\JsonApi\Config\Config $extensionConfig */
protected $extensionConfig;

/** @var string $boltVersion */
protected $boltVersion;

/** @var string $jsonAPIVersion */
protected $jsonAPIVersion;

/**
* MenuAction constructor.
*
* @param Config $boltConfig
* @param \Bolt\Extension\Bolt\JsonApi\Config\Config $extensionConfig
* @param string $boltVersion
* @param string $jsonAPIVersion
*/
public function __construct(Config $boltConfig, \Bolt\Extension\Bolt\JsonApi\Config\Config $extensionConfig, $boltVersion, $jsonAPIVersion)
{
$this->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);
}
}
22 changes: 22 additions & 0 deletions src/Provider/APIProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +18,7 @@
use Silex\Application;
use Silex\ServiceProviderInterface;
use Bolt\Storage\Query\ContentQueryParser;
use Bolt\Extension\Bolt\JsonApi\Action\RootAction;


/**
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5fb2f6a

Please sign in to comment.