lazy loading of open api spec

This commit is contained in:
zebardy 2019-11-23 14:38:37 +00:00
parent 577b181e1b
commit 8b9790417d
5 changed files with 44 additions and 19 deletions

View File

@ -8,10 +8,18 @@ class BaseApiController extends BaseController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->OpenApiSpec = json_decode(file_get_contents(__DIR__ . '/../grocy.openapi.json'));
} }
protected $OpenApiSpec; protected $OpenApiSpec = null;
protected function getOpenApispec()
{
if($this->OpenApiSpec == null)
{
$this->OpenApiSpec = json_decode(file_get_contents(__DIR__ . '/../grocy.openapi.json'));
}
return $this->OpenApiSpec;
}
protected function ApiResponse($data) protected function ApiResponse($data)
{ {

View File

@ -166,11 +166,11 @@ class GenericEntityApiController extends BaseApiController
private function IsValidEntity($entity) private function IsValidEntity($entity)
{ {
return in_array($entity, $this->OpenApiSpec->components->internalSchemas->ExposedEntity->enum); return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntity->enum);
} }
private function IsEntityWithPreventedListing($entity) private function IsEntityWithPreventedListing($entity)
{ {
return in_array($entity, $this->OpenApiSpec->components->internalSchemas->ExposedEntitiesPreventListing->enum); return in_array($entity, $this->getOpenApiSpec()->components->internalSchemas->ExposedEntitiesPreventListing->enum);
} }
} }

View File

@ -25,11 +25,11 @@ class OpenApiController extends BaseApiController
$applicationService = $this->getApplicationService; $applicationService = $this->getApplicationService;
$versionInfo = $applicationService->GetInstalledVersion(); $versionInfo = $applicationService->GetInstalledVersion();
$this->OpenApiSpec->info->version = $versionInfo->Version; $this->getOpenApiSpec()->info->version = $versionInfo->Version;
$this->OpenApiSpec->info->description = str_replace('PlaceHolderManageApiKeysUrl', $this->AppContainer->UrlManager->ConstructUrl('/manageapikeys'), $this->OpenApiSpec->info->description); $this->getOpenApiSpec()->info->description = str_replace('PlaceHolderManageApiKeysUrl', $this->AppContainer->UrlManager->ConstructUrl('/manageapikeys'), $this->getOpenApiSpec()->info->description);
$this->OpenApiSpec->servers[0]->url = $this->AppContainer->UrlManager->ConstructUrl('/api'); $this->getOpenApiSpec()->servers[0]->url = $this->AppContainer->UrlManager->ConstructUrl('/api');
return $this->ApiResponse($this->OpenApiSpec); return $this->ApiResponse($this->getOpenApiSpec());
} }
public function ApiKeysList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function ApiKeysList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)

View File

@ -15,10 +15,19 @@ class StockController extends BaseController
#fwrite($fp, "!!!constructing StockController\n"); #fwrite($fp, "!!!constructing StockController\n");
#fclose($fp); #fclose($fp);
parent::__construct($container); parent::__construct($container);
$this->StockService = new StockService(); #$this->StockService = new StockService();
} }
protected $StockService; protected $StockService = null;
protected function getStockService()
{
if($this->StockService == null)
{
$this->StockService = new StockService();
}
return $this->StockService;
}
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -29,9 +38,9 @@ class StockController extends BaseController
'products' => $this->getDatabase()->products()->orderBy('name'), 'products' => $this->getDatabase()->products()->orderBy('name'),
'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name'), 'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name'),
'locations' => $this->getDatabase()->locations()->orderBy('name'), 'locations' => $this->getDatabase()->locations()->orderBy('name'),
'currentStock' => $this->StockService->GetCurrentStock(true), 'currentStock' => $this->getStockService()->GetCurrentStock(true),
'currentStockLocations' => $this->StockService->GetCurrentStockLocations(), 'currentStockLocations' => $this->getStockService()->GetCurrentStockLocations(),
'missingProducts' => $this->StockService->GetMissingProducts(), 'missingProducts' => $this->getStockService()->GetMissingProducts(),
'nextXDays' => $nextXDays, 'nextXDays' => $nextXDays,
'productGroups' => $this->getDatabase()->product_groups()->orderBy('name'), 'productGroups' => $this->getDatabase()->product_groups()->orderBy('name'),
'userfields' => $this->getUserfieldsService()->GetFields('products'), 'userfields' => $this->getUserfieldsService()->GetFields('products'),
@ -81,7 +90,7 @@ class StockController extends BaseController
'listItems' => $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId), 'listItems' => $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId),
'products' => $this->getDatabase()->products()->orderBy('name'), 'products' => $this->getDatabase()->products()->orderBy('name'),
'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name'), 'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name'),
'missingProducts' => $this->StockService->GetMissingProducts(), 'missingProducts' => $this->getStockService()->GetMissingProducts(),
'productGroups' => $this->getDatabase()->product_groups()->orderBy('name'), 'productGroups' => $this->getDatabase()->product_groups()->orderBy('name'),
'shoppingLists' => $this->getDatabase()->shopping_lists()->orderBy('name'), 'shoppingLists' => $this->getDatabase()->shopping_lists()->orderBy('name'),
'selectedShoppingListId' => $listId, 'selectedShoppingListId' => $listId,
@ -289,7 +298,7 @@ class StockController extends BaseController
'products' => $this->getDatabase()->products()->orderBy('name'), 'products' => $this->getDatabase()->products()->orderBy('name'),
'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name'), 'quantityunits' => $this->getDatabase()->quantity_units()->orderBy('name'),
'locations' => $this->getDatabase()->locations()->orderBy('name'), 'locations' => $this->getDatabase()->locations()->orderBy('name'),
'currentStockLocationContent' => $this->StockService->GetCurrentStockLocationContent() 'currentStockLocationContent' => $this->getStockService()->GetCurrentStockLocationContent()
]); ]);
} }

View File

@ -18,11 +18,19 @@ class UserfieldsService extends BaseService
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->OpenApiSpec = json_decode(file_get_contents(__DIR__ . '/../grocy.openapi.json'));
} }
protected $OpenApiSpec; protected $OpenApiSpec = null;
protected function getOpenApispec()
{
if($this->OpenApiSpec == null)
{
$this->OpenApiSpec = json_decode(file_get_contents(__DIR__ . '/../grocy.openapi.json'));
}
return $this->OpenApiSpec;
}
public function GetFields($entity) public function GetFields($entity)
{ {
if (!$this->IsValidEntity($entity)) if (!$this->IsValidEntity($entity))
@ -109,7 +117,7 @@ class UserfieldsService extends BaseService
public function GetEntities() public function GetEntities()
{ {
$exposedDefaultEntities = $this->OpenApiSpec->components->internalSchemas->ExposedEntity->enum; $exposedDefaultEntities = $this->getOpenApiSpec()->components->internalSchemas->ExposedEntity->enum;
$userentities = array(); $userentities = array();
foreach ($this->Database->userentities()->orderBy('name') as $userentity) foreach ($this->Database->userentities()->orderBy('name') as $userentity)