Skip to content

Commit

Permalink
add extensions and list views endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Feb 27, 2018
1 parent 9886f36 commit 1c81e1b
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$env = get_api_env();
$requestUri = trim(get_virtual_path(), '/');

$reservedNames = ['server'];
$reservedNames = ['server', 'interfaces', 'extensions', 'list_views'];
if ($requestUri && !empty($env) && $env !== '_' && !in_array($env, $reservedNames)) {
$configFilePath = sprintf('%s/api.%s.php', $configPath, $env);
if (!file_exists($configFilePath)) {
Expand Down
39 changes: 39 additions & 0 deletions src/core/Directus/Services/AbstractAddOnsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Directus\Services;

abstract class AbstractAddOnsController extends AbstractService
{
protected $basePath = null;

protected function all($basePath, array $params = [])
{
$addOns = [];

if (!file_exists($basePath)) {
return ['data' => $addOns];
}

$filePaths = find_directories($basePath);
foreach ($filePaths as $path) {
$path .= '/meta.json';

if (!file_exists($path)) {
continue;
}

$addOnsPath = trim(substr($path, strlen($basePath)), '/');
$data = ['id' => basename(dirname($addOnsPath))];

$meta = @json_decode(file_get_contents($path), true);
if ($meta) {
unset($meta['id']);
$data = array_merge($data, $meta);
}

$addOns[] = $data;
}

return ['data' => $addOns];
}
}
21 changes: 21 additions & 0 deletions src/core/Directus/Services/ExtensionsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Directus\Services;

use Directus\Application\Container;

class ExtensionsService extends AbstractAddOnsController
{
public function __construct(Container $container)
{
parent::__construct($container);

$basePath = $this->container->get('path_base');
$this->basePath = $basePath . '/public/core/extensions';
}

public function findAll(array $params = [])
{
return $this->all($this->basePath, $params);
}
}
44 changes: 11 additions & 33 deletions src/core/Directus/Services/InterfacesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,20 @@

namespace Directus\Services;

class InterfacesService extends AbstractService
use Directus\Application\Container;

class InterfacesService extends AbstractAddOnsController
{
public function findAll(array $params = [])
public function __construct(Container $container)
{
$basePath = $this->container->get('path_base');
$interfacesPath = $basePath . '/public/core/interfaces';
$interfaces = [];

if (!file_exists($interfacesPath)) {
return $interfaces;
}

$filePaths = find_directories($interfacesPath);
foreach ($filePaths as $path) {
$path .= '/meta.json';

if (!file_exists($path)) {
continue;
}

$interfacePath = trim(substr($path, strlen($interfacesPath)), '/');
$data = ['id' => basename(dirname($interfacePath))];
parent::__construct($container);

$meta = @json_decode(file_get_contents($path), true);
if ($meta) {
unset($meta['id']);
$data = array_merge($data, $meta);
}

$data = [
'data' => $data
];

$interfaces[] = $data;
}
$basePath = $this->container->get('path_base');
$this->basePath = $basePath . '/public/core/interfaces';
}

return $interfaces;
public function findAll(array $params = [])
{
return $this->all($this->basePath, $params);
}
}
21 changes: 21 additions & 0 deletions src/core/Directus/Services/ListViewsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Directus\Services;

use Directus\Application\Container;

class ListViewsService extends AbstractAddOnsController
{
public function __construct(Container $container)
{
parent::__construct($container);

$basePath = $this->container->get('path_base');
$this->basePath = $basePath . '/public/core/list_views';
}

public function findAll(array $params = [])
{
return $this->all($this->basePath, $params);
}
}
30 changes: 30 additions & 0 deletions src/endpoints/Extensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Directus\Api\Routes;

use Directus\Application\Application;
use Directus\Application\Http\Request;
use Directus\Application\Http\Response;
use Directus\Application\Route;
use Directus\Services\ExtensionsService;

class Extensions extends Route
{
/**
* @param Application $app
*/
public function __invoke(Application $app)
{
create_ping_route($app);

$app->get('', [$this, 'all']);
}

public function all(Request $request, Response $response)
{
$service = new ExtensionsService($this->container);
$responseData = $service->findAll();

return $this->responseWithData($request, $response, $responseData);
}
}
30 changes: 30 additions & 0 deletions src/endpoints/ListViews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Directus\Api\Routes;

use Directus\Application\Application;
use Directus\Application\Http\Request;
use Directus\Application\Http\Response;
use Directus\Application\Route;
use Directus\Services\ListViewsService;

class ListViews extends Route
{
/**
* @param Application $app
*/
public function __invoke(Application $app)
{
create_ping_route($app);

$app->get('', [$this, 'all']);
}

public function all(Request $request, Response $response)
{
$service = new ListViewsService($this->container);
$responseData = $service->findAll();

return $this->responseWithData($request, $response, $responseData);
}
}
2 changes: 2 additions & 0 deletions src/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// TODO: Customized Method not allowed error
// =============================================================================

$app->group('/extensions', \Directus\Api\Routes\Extensions::class);
$app->group('/interfaces', \Directus\Api\Routes\Interfaces::class);
$app->group('/list_views', \Directus\Api\Routes\ListViews::class);
$app->group('/server', \Directus\Api\Routes\Server::class);

$app->group('/{env}', function () {
Expand Down

0 comments on commit 1c81e1b

Please sign in to comment.