Skip to content

Commit

Permalink
Created dashboard services
Browse files Browse the repository at this point in the history
- `/apigility/api/dashboard`:
  - authentication
  - DB adapters
  - modules (with REST and RPC service names)
- `/apigility/api/settings-dashboard`:
  - authentication
  - content negotiation
  - DB adapters
  • Loading branch information
weierophinney committed May 5, 2014
1 parent 91eb039 commit f8ba42c
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 9 deletions.
50 changes: 49 additions & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
'ZF\Apigility\Admin\Controller\FsPermissions' => 'ZF\Apigility\Admin\Controller\FsPermissionsController',
),
'factories' => array(
'ZF\Apigility\Admin\Controller\Dashboard' => 'ZF\Apigility\Admin\Controller\DashboardControllerFactory',
'ZF\Apigility\Admin\Controller\Documentation' => 'ZF\Apigility\Admin\Controller\DocumentationControllerFactory',
'ZF\Apigility\Admin\Controller\Filters' => 'ZF\Apigility\Admin\Controller\FiltersControllerFactory',
'ZF\Apigility\Admin\Controller\Hydrators' => 'ZF\Apigility\Admin\Controller\HydratorsControllerFactory',
'ZF\Apigility\Admin\Controller\Validators' => 'ZF\Apigility\Admin\Controller\ValidatorsControllerFactory',
'ZF\Apigility\Admin\Controller\InputFilter' => 'ZF\Apigility\Admin\Controller\InputFilterControllerFactory',
'ZF\Apigility\Admin\Controller\SettingsDashboard' => 'ZF\Apigility\Admin\Controller\DashboardControllerFactory',
'ZF\Apigility\Admin\Controller\Validators' => 'ZF\Apigility\Admin\Controller\ValidatorsControllerFactory',
),
),

Expand Down Expand Up @@ -75,6 +77,26 @@
),
'may_terminate' => false,
'child_routes' => array(
'dashboard' => array(
'type' => 'literal',
'options' => array(
'route' => '/dashboard',
'defaults' => array(
'controller' => 'ZF\Apigility\Admin\Controller\Dashboard',
'action' => 'dashboard',
),
),
),
'settings-dashboard' => array(
'type' => 'literal',
'options' => array(
'route' => '/settings-dashboard',
'defaults' => array(
'controller' => 'ZF\Apigility\Admin\Controller\SettingsDashboard',
'action' => 'settingsDashboard',
),
),
),
'cache-enabled' => array(
'type' => 'literal',
'options' => array(
Expand Down Expand Up @@ -346,6 +368,7 @@
'ZF\Apigility\Admin\Controller\Authorization' => 'HalJson',
'ZF\Apigility\Admin\Controller\CacheEnabled' => 'Json',
'ZF\Apigility\Admin\Controller\ContentNegotiation' => 'HalJson',
'ZF\Apigility\Admin\Controller\Dashboard' => 'HalJson',
'ZF\Apigility\Admin\Controller\DbAdapter' => 'HalJson',
'ZF\Apigility\Admin\Controller\Documentation' => 'HalJson',
'ZF\Apigility\Admin\Controller\Filters' => 'Json',
Expand All @@ -359,6 +382,7 @@
'ZF\Apigility\Admin\Controller\OAuth2Authentication' => 'HalJson',
'ZF\Apigility\Admin\Controller\RestService' => 'HalJson',
'ZF\Apigility\Admin\Controller\RpcService' => 'HalJson',
'ZF\Apigility\Admin\Controller\SettingsDashboard' => 'HalJson',
'ZF\Apigility\Admin\Controller\Source' => 'Json',
'ZF\Apigility\Admin\Controller\Validators' => 'Json',
'ZF\Apigility\Admin\Controller\Versioning' => 'Json',
Expand All @@ -380,6 +404,10 @@
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\Dashboard' => array(
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\DbAdapter' => array(
'application/json',
'application/*+json',
Expand Down Expand Up @@ -424,6 +452,10 @@
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\SettingsDashboard' => array(
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\Source' => array(
'application/json',
'application/*+json',
Expand Down Expand Up @@ -458,6 +490,10 @@
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\Dashboard' => array(
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\DbAdapter' => array(
'application/json',
'application/*+json',
Expand Down Expand Up @@ -495,6 +531,10 @@
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\SettingsDashboard' => array(
'application/json',
'application/*+json',
),
'ZF\Apigility\Admin\Controller\Source' => array(
'application/json',
),
Expand Down Expand Up @@ -703,6 +743,10 @@
'http_methods' => array('GET', 'PATCH'),
'route_name' => 'zf-apigility/api/config',
),
'ZF\Apigility\Admin\Controller\Dashboard' => array(
'http_methods' => array('GET'),
'route_name' => 'zf-apigility/api/dashboard',
),
'ZF\Apigility\Admin\Controller\Documentation' => array(
'http_methods' => array('GET', 'PATCH', 'PUT', 'DELETE'),
'route_name' => 'zf-apigility/api/rest-service/rest-doc',
Expand Down Expand Up @@ -743,6 +787,10 @@
'http_methods' => array('GET', 'POST', 'PATCH', 'DELETE'),
'route_name' => 'zf-apigility/api/authentication/oauth2',
),
'ZF\Apigility\Admin\Controller\SettingsDashboard' => array(
'http_methods' => array('GET'),
'route_name' => 'zf-apigility/api/settings-dashboard',
),
'ZF\Apigility\Admin\Controller\Source' => array(
'http_methods' => array('GET'),
'route_name' => 'zf-apigility/api/source',
Expand Down
170 changes: 170 additions & 0 deletions src/Controller/DashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZF\Apigility\Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use ZF\Apigility\Admin\Model\AuthenticationEntity;
use ZF\Apigility\Admin\Model\AuthenticationModel;
use ZF\Apigility\Admin\Model\ContentNegotiationModel;
use ZF\Apigility\Admin\Model\DbAdapterModel;
use ZF\Apigility\Admin\Model\ModuleModel;
use ZF\Apigility\Admin\Model\RestServiceModelFactory;
use ZF\Apigility\Admin\Model\RpcServiceModelFactory;
use ZF\ContentNegotiation\ViewModel;
use ZF\Hal\Entity;
use ZF\Hal\Collection;
use ZF\Hal\Link\Link;

class DashboardController extends AbstractActionController
{
protected $authentication;

protected $contentNegotiation;

protected $dbAdapters;

protected $modules;

protected $restServicesFactory;

protected $rpcServicesFactory;

public function __construct(
AuthenticationModel $authentication,
ContentNegotiationModel $contentNegotiation,
DbAdapterModel $dbAdapters,
ModuleModel $modules,
RestServiceModelFactory $restServicesFactory,
RpcServiceModelFactory $rpcServicesFactory
) {
$this->authentication = $authentication;
$this->contentNegotiation = $contentNegotiation;
$this->dbAdapters = $dbAdapters;
$this->modules = $modules;
$this->restServicesFactory = $restServicesFactory;
$this->rpcServicesFactory = $rpcServicesFactory;
}

public function dashboardAction()
{
$authentication = $this->authentication->fetch();
if ($authentication) {
$authenticationEntity = $authentication;
$authentication = new Entity($authentication, null);
$authentication->getLinks()->add(Link::factory(array(
'rel' => 'self',
'route' => $this->getRouteForEntity($authenticationEntity),
)));
}

$dbAdapters = new Collection($this->dbAdapters->fetchAll());
$dbAdapters->setCollectionRoute('zf-apigility/api/db-adapter');

$modules = $this->modules->getModules();
$map = function ($value) {
return $value->serviceName;
};
foreach ($modules as $module) {
$name = $module->getName();
$version = $module->getLatestVersion();

$rest = $this->restServicesFactory->factory($name)->fetchAll($version);
$rest = array_map($map, $rest);

$rpc = $this->rpcServicesFactory->factory($name)->fetchAll($version);
$rpc = array_map($map, $rpc);

$module->exchangeArray(array(
'rest' => $rest,
'rpc' => $rpc,
));
}

$modulesCollection = new Collection($modules);
$modulesCollection->setCollectionRoute('zf-apigility/api/module');

$dashboard = array(
'authentication' => $authentication,
'db_adapter' => $dbAdapters,
'module' => $modulesCollection,
);

$entity = new Entity($dashboard, 'dashboard');
$links = $entity->getLinks();
$links->add(Link::factory(array(
'rel' => 'self',
'route' => array(
'name' => 'zf-apigility/api/dashboard',
),
)));

return new ViewModel(array('payload' => $entity));
}

public function settingsDashboardAction()
{
$authentication = $this->authentication->fetch();
if ($authentication) {
$authenticationEntity = $authentication;
$authentication = new Entity($authentication, null);
$authentication->getLinks()->add(Link::factory(array(
'rel' => 'self',
'route' => $this->getRouteForEntity($authenticationEntity),
)));
}

$dbAdapters = new Collection($this->dbAdapters->fetchAll());
$dbAdapters->setCollectionRoute('zf-apigility/api/db-adapter');

$contentNegotiation = new Collection($this->contentNegotiation->fetchAll());
$contentNegotiation->setCollectionRoute('zf-apigility/api/content-negotiation');

$dashboard = array(
'authentication' => $authentication,
'content_negotiation' => $contentNegotiation,
'db_adapter' => $dbAdapters,
);

$entity = new Entity($dashboard, 'settings-dashboard');
$links = $entity->getLinks();
$links->add(Link::factory(array(
'rel' => 'self',
'route' => array(
'name' => 'zf-apigility/api/settings-dashboard',
),
)));

return new ViewModel(array('payload' => $entity));
}

/**
* Determine the route to use for a given entity
*
* Copied from AuthenticationController
*
* @param AuthenticationEntity $entity
* @return string
*/
protected function getRouteForEntity(AuthenticationEntity $entity)
{
$baseRoute = 'zf-apigility/api/authentication';

if ($entity->isBasic()) {
return $baseRoute . '/http-basic';
}

if ($entity->isDigest()) {
return $baseRoute . '/http-digest';
}

if ($entity->isOAuth2()) {
return $baseRoute . '/oauth2';
}

return $baseRoute;
}
}
23 changes: 23 additions & 0 deletions src/Controller/DashboardControllerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZF\Apigility\Admin\Controller;

class DashboardControllerFactory
{
public function __invoke($controllers)
{
$services = $controllers->getServiceLocator();
return new DashboardController(
$services->get('ZF\Apigility\Admin\Model\AuthenticationModel'),
$services->get('ZF\Apigility\Admin\Model\ContentNegotiationModel'),
$services->get('ZF\Apigility\Admin\Model\DbAdapterModel'),
$services->get('ZF\Apigility\Admin\Model\ModuleModel'),
$services->get('ZF\Apigility\Admin\Model\RestServiceModelFactory'),
$services->get('ZF\Apigility\Admin\Model\RpcServiceModelFactory')
);
}
}
17 changes: 9 additions & 8 deletions src/Model/ModuleEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use InvalidArgumentException;
use ReflectionClass;
use ZF\Hal\Collection;

class ModuleEntity
{
Expand Down Expand Up @@ -160,20 +161,20 @@ public function exchangeArray(array $data)
$this->isVendor = (bool) $value;
break;
case 'rest':
if (!is_array($value)) {
throw new InvalidArgumentException(
'REST services must be an array; received "%s"',
if (!is_array($value) && ! $value instanceof Collection) {
throw new InvalidArgumentException(sprintf(
'REST services must be an array or ZF\Hal\Collection; received "%s"',
(is_object($value) ? get_class($value) : gettype($value))
);
));
}
$this->restServices = $value;
break;
case 'rpc':
if (!is_array($value)) {
throw new InvalidArgumentException(
'RPC services must be an array; received "%s"',
if (!is_array($value) && ! $value instanceof Collection) {
throw new InvalidArgumentException(sprintf(
'RPC services must be an array or ZF\Hal\Collection; received "%s"',
(is_object($value) ? get_class($value) : gettype($value))
);
));
}
$this->rpcServices = $value;
break;
Expand Down

0 comments on commit f8ba42c

Please sign in to comment.