Add ViewDefinition API

- Add initial ViewDefinition entity for list-view.
- Add ViewDefinitionHandler.
- Add ViewDefinitionDataProvider.
- Add ViewDefinitionProviderInterface.
- Add ViewDefinitionsHandlerTest.

Signed-off-by: Dillon-Brown <dillon.brown@salesagility.com>
This commit is contained in:
Dillon-Brown 2020-05-11 15:58:50 +01:00
parent be1e0ca5cf
commit 4fd431d97d
5 changed files with 445 additions and 0 deletions

View file

@ -0,0 +1,110 @@
<?php
namespace SuiteCRM\Core\Legacy;
use App\Entity\ViewDefinition;
use App\Service\ModuleNameMapperInterface;
use App\Service\ViewDefinitionsProviderInterface;
use BeanFactory;
use Exception;
use InvalidArgumentException;
use ListViewFacade;
/**
* Class ViewDefinitions
*/
class ViewDefinitionsHandler extends LegacyHandler implements ViewDefinitionsProviderInterface
{
public const HANDLER_KEY = 'view-definitions';
/**
* @var array
*/
protected static $listViewColumnInterface = [
'fieldname' => '',
'width' => '',
'label' => '',
'link' => false,
'default' => false,
'module' => '',
'id' => '',
'sortable' => false
];
/**
* @inheritDoc
*/
public function getHandlerKey(): string
{
return self::HANDLER_KEY;
}
/**
* @var ModuleNameMapperInterface
*/
protected $moduleNameMapper;
/**
* SystemConfigHandler constructor.
* @param string $projectDir
* @param string $legacyDir
* @param string $legacySessionName
* @param string $defaultSessionName
* @param LegacyScopeState $legacyScopeState
* @param ModuleNameMapperInterface $moduleNameMapper
*/
public function __construct(
string $projectDir,
string $legacyDir,
string $legacySessionName,
string $defaultSessionName,
LegacyScopeState $legacyScopeState,
ModuleNameMapperInterface $moduleNameMapper
) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState);
$this->moduleNameMapper = $moduleNameMapper;
}
/**
* @param string $moduleName
* @return ViewDefinition
* @throws Exception
*/
public function getListViewDef(string $moduleName): ViewDefinition
{
$this->init();
/* @noinspection PhpIncludeInspection */
include_once 'include/ListView/ListViewFacade.php';
$moduleName = $this->validateModuleName($moduleName);
$displayColumns = ListViewFacade::getDisplayColumns($moduleName);
$data = [];
foreach ($displayColumns as $key => $column) {
$column = array_merge(self::$listViewColumnInterface, $column);
$column['fieldname'] = $key;
$data[] = $column;
}
$viewDef = new ViewDefinition();
$viewDef->setId($moduleName);
$viewDef->setListView($data);
$this->close();
return $viewDef;
}
/**
* @param $moduleName
* @return string
*/
private function validateModuleName($moduleName): string
{
$moduleName = $this->moduleNameMapper->toLegacy($moduleName);
if (!$this->moduleNameMapper->isValidModule($moduleName)) {
throw new InvalidArgumentException('Invalid module name: ' . $moduleName);
}
return $moduleName;
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\ViewDefinition;
use App\Service\ViewDefinitionsProviderInterface;
/**
* Class ViewDefinitionItemDataProvider
* @package App\DataProvider
*/
class ViewDefinitionItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
/**
* @var ViewDefinitionsProviderInterface
*/
protected $viewDefHandler;
/**
* ViewDefinitionItemDataProvider constructor.
* @param ViewDefinitionsProviderInterface $viewDefHandler
*/
public function __construct(ViewDefinitionsProviderInterface $viewDefHandler)
{
$this->viewDefHandler = $viewDefHandler;
}
/**
* Define supported resources
* @param string $resourceClass
* @param string|null $operationName
* @param array $context
* @return bool
*/
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return ViewDefinition::class === $resourceClass;
}
/**
* @param string $resourceClass
* @param array|int|string $id
* @param string|null $operationName
* @param array $context
* @return ViewDefinition|null
*/
public function getItem(
string $resourceClass,
$id,
string $operationName = null,
array $context = []
): ?ViewDefinition {
return $this->viewDefHandler->getListViewDef($id);
}
}

View file

@ -0,0 +1,198 @@
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* routePrefix="/metadata",
* itemOperations={
* "get"
* },
* collectionOperations={
* "get"
* },
* graphql={
* "item_query",
* }
* )
*/
class ViewDefinition
{
/**
* The module
*
* @var string
*
* @ApiProperty(
* identifier=true,
* attributes={
* "openapi_context"={
* "type"="string",
* "description"="The module.",
* "example"="Accounts"
* }
* },
*
* )
*/
protected $id;
/**
* Detail View metadata
*
* @var array
*
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="array",
* "description"="The detail-view metadata",
* },
* }
* )
*/
public $detailView;
/**
* Edit View metadata
*
* @var array
*
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="array",
* "description"="The edit-view metadata",
* },
* }
* )
*/
public $editView;
/**
* List View metadata
*
* @var array
*
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="array",
* "description"="The list-view metadata",
* },
* }
* )
*/
public $listView;
/**
* Search metadata
*
* @var array
*
* @ApiProperty(
* attributes={
* "openapi_context"={
* "type"="array",
* "description"="The search metadata",
* },
* }
* )
*/
public $search;
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @param string $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* Get Detail View metadata
* @return array
*/
public function getDetailView(): ?array
{
return $this->detailView;
}
/**
* Set Detail view metadata
* @param array $detailView
*/
public function setDetailView(array $detailView): void
{
$this->detailView = $detailView;
}
/**
* Get EditView Metadata
* @return array
*/
public function getEditView(): ?array
{
return $this->editView;
}
/**
* Set EditView Metadata
* @param array $editView
*/
public function setEditView(array $editView): void
{
$this->editView = $editView;
}
/**
* Get List View Metadata
* @return array
*/
public function getListView(): ?array
{
return $this->listView;
}
/**
* Set List View metadata
* @param array $listView
*/
public function setListView(array $listView): void
{
$this->listView = $listView;
}
/**
* Get Search Metadata
* @return array
*/
public function getSearch(): ?array
{
return $this->search;
}
/**
* Set Search Metadata
* @param array $search
* @return ViewDefinition
*/
public function setSearch(array $search): ViewDefinition
{
$this->search = $search;
return $this;
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace App\Service;
use App\Entity\ViewDefinition;
interface ViewDefinitionsProviderInterface
{
/**
* Get list-view defs
* @param string $moduleName
* @return ViewDefinition
*/
public function getListViewDef(string $moduleName): ViewDefinition;
}

View file

@ -0,0 +1,64 @@
<?php
namespace App\Tests;
use App\Entity\ViewDefinition;
use AspectMock\Test;
use Codeception\Test\Unit;
use Exception;
use SuiteCRM\Core\Legacy\ActionNameMapperHandler;
use SuiteCRM\Core\Legacy\ModuleNameMapperHandler;
use SuiteCRM\Core\Legacy\ModuleRegistryHandler;
use SuiteCRM\Core\Legacy\ViewDefinitionsHandler;
use SuiteCRM\Core\Legacy\RouteConverterHandler;
final class ViewDefinitionsHandlerTest extends Unit
{
/**
* @var UnitTester
*/
protected $tester;
/**
* @var ViewDefinition
*/
protected $viewDefinition;
/**
* @var ViewDefinitionsHandler
*/
private $viewDefinitionHandler;
/**
* @throws Exception
* @noinspection StaticClosureCanBeUsedInspection
*/
protected function _before(): void
{
$projectDir = $this->tester->getProjectDir();
$legacyDir = $this->tester->getLegacyDir();
$legacySessionName = $this->tester->getLegacySessionName();
$defaultSessionName = $this->tester->getDefaultSessionName();
$legacyScope = $this->tester->getLegacyScope();
$moduleNameMapper = new ModuleNameMapperHandler(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScope
);
$this->viewDefinitionHandler = new ViewDefinitionsHandler(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScope,
$moduleNameMapper
);
$this->viewDefinition = $this->viewDefinitionHandler->getListViewDef('Accounts');
}
}