diff --git a/controllers/CalendarApiController.php b/controllers/CalendarApiController.php index b9c92f7b..054b9376 100644 --- a/controllers/CalendarApiController.php +++ b/controllers/CalendarApiController.php @@ -10,12 +10,29 @@ class CalendarApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->CalendarService = new CalendarService(); - $this->ApiKeyService = new ApiKeyService(); } - protected $CalendarService; - protected $ApiKeyService; + protected $CalendarService = null; + + protected function getCalendarService() + { + if($this->CalendarService == null) + { + $this->CalendarService = new CalendarService(); + } + return $this->CalendarService; + } + + protected $ApiKeyService = null; + + protected function getApiKeyService() + { + if($this->ApiKeyService == null) + { + $this->ApiKeyService = new ApiKeyService(); + } + return $this->ApiKeyService; + } public function Ical(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -23,7 +40,7 @@ class CalendarApiController extends BaseApiController { $vCalendar = new \Eluceo\iCal\Component\Calendar('grocy'); - $events = $this->CalendarService->GetEvents(); + $events = $this->getCalendarService()->GetEvents(); foreach($events as $event) { $date = new \DateTime($event['start']); @@ -60,7 +77,7 @@ class CalendarApiController extends BaseApiController try { return $this->ApiResponse(array( - 'url' => $this->AppContainer->UrlManager->ConstructUrl('/api/calendar/ical?secret=' . $this->ApiKeyService->GetOrCreateApiKey(ApiKeyService::API_KEY_TYPE_SPECIAL_PURPOSE_CALENDAR_ICAL)) + 'url' => $this->AppContainer->UrlManager->ConstructUrl('/api/calendar/ical?secret=' . $this->getApiKeyService()->GetOrCreateApiKey(ApiKeyService::API_KEY_TYPE_SPECIAL_PURPOSE_CALENDAR_ICAL)) )); } catch (\Exception $ex) diff --git a/controllers/CalendarController.php b/controllers/CalendarController.php index 2f893cf2..0576e646 100644 --- a/controllers/CalendarController.php +++ b/controllers/CalendarController.php @@ -9,15 +9,23 @@ class CalendarController extends BaseController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->CalendarService = new CalendarService(); } - protected $CalendarService; + protected $CalendarService = null; + + protected function getCalendarService() + { + if($this->CalendarService == null) + { + $this->CalendarService = new CalendarService(); + } + return $this->CalendarService; + } public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { return $this->renderPage($response, 'calendar', [ - 'fullcalendarEventSources' => $this->CalendarService->GetEvents() + 'fullcalendarEventSources' => $this->getCalendarService()->GetEvents() ]); } } diff --git a/controllers/ChoresApiController.php b/controllers/ChoresApiController.php index 64dcc36a..a98f4e0a 100644 --- a/controllers/ChoresApiController.php +++ b/controllers/ChoresApiController.php @@ -9,10 +9,18 @@ class ChoresApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->ChoresService = new ChoresService(); } - protected $ChoresService; + protected $ChoresService = null; + + protected function getChoresService() + { + if($this->ChoresService == null) + { + $this->ChoresService = new ChoresService(); + } + return $this->ChoresService; + } public function TrackChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -32,7 +40,7 @@ class ChoresApiController extends BaseApiController $doneBy = $requestBody['done_by']; } - $choreExecutionId = $this->ChoresService->TrackChore($args['choreId'], $trackedTime, $doneBy); + $choreExecutionId = $this->getChoresService()->TrackChore($args['choreId'], $trackedTime, $doneBy); return $this->ApiResponse($this->getDatabase()->chores_log($choreExecutionId)); } catch (\Exception $ex) @@ -45,7 +53,7 @@ class ChoresApiController extends BaseApiController { try { - return $this->ApiResponse($this->ChoresService->GetChoreDetails($args['choreId'])); + return $this->ApiResponse($this->getChoresService()->GetChoreDetails($args['choreId'])); } catch (\Exception $ex) { @@ -55,14 +63,14 @@ class ChoresApiController extends BaseApiController public function Current(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - return $this->ApiResponse($this->ChoresService->GetCurrent()); + return $this->ApiResponse($this->getChoresService()->GetCurrent()); } public function UndoChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { try { - $this->ApiResponse($this->ChoresService->UndoChoreExecution($args['executionId'])); + $this->ApiResponse($this->getChoresService()->UndoChoreExecution($args['executionId'])); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -88,12 +96,12 @@ class ChoresApiController extends BaseApiController $chores = $this->getDatabase()->chores(); foreach ($chores as $chore) { - $this->ChoresService->CalculateNextExecutionAssignment($chore->id); + $this->getChoresService()->CalculateNextExecutionAssignment($chore->id); } } else { - $this->ChoresService->CalculateNextExecutionAssignment($choreId); + $this->getChoresService()->CalculateNextExecutionAssignment($choreId); } return $this->EmptyApiResponse($response); diff --git a/controllers/ChoresController.php b/controllers/ChoresController.php index 23c81fc8..7f58df95 100644 --- a/controllers/ChoresController.php +++ b/controllers/ChoresController.php @@ -9,10 +9,18 @@ class ChoresController extends BaseController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->ChoresService = new ChoresService(); } - protected $ChoresService; + protected $ChoresService = null; + + protected function getChoresService() + { + if($this->ChoresService == null) + { + $this->ChoresService = new ChoresService(); + } + return $this->ChoresService; + } public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -21,7 +29,7 @@ class ChoresController extends BaseController return $this->renderPage($response, 'choresoverview', [ 'chores' => $this->getDatabase()->chores()->orderBy('name'), - 'currentChores' => $this->ChoresService->GetCurrent(), + 'currentChores' => $this->getChoresService()->GetCurrent(), 'nextXDays' => $nextXDays, 'userfields' => $this->getUserfieldsService()->GetFields('chores'), 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('chores'), diff --git a/controllers/FilesApiController.php b/controllers/FilesApiController.php index 91504a49..d4597d55 100644 --- a/controllers/FilesApiController.php +++ b/controllers/FilesApiController.php @@ -9,10 +9,18 @@ class FilesApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->FilesService = new FilesService(); } - protected $FilesService; + protected $FilesService = null; + + protected function getFilesService() + { + if($this->FilesService == null) + { + $this->FilesService = new FilesService(); + } + return $this->FilesService; + } public function UploadFile(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -28,7 +36,7 @@ class FilesApiController extends BaseApiController } $data = $request->getBody()->getContents(); - file_put_contents($this->FilesService->GetFilePath($args['group'], $fileName), $data); + file_put_contents($this->getFilesService()->GetFilePath($args['group'], $fileName), $data); return $this->EmptyApiResponse($response); } @@ -71,11 +79,11 @@ class FilesApiController extends BaseApiController $bestFitWidth = $request->getQueryParams()['best_fit_width']; } - $filePath = $this->FilesService->DownscaleImage($args['group'], $fileName, $bestFitHeight, $bestFitWidth); + $filePath = $this->getFilesService()->DownscaleImage($args['group'], $fileName, $bestFitHeight, $bestFitWidth); } else { - $filePath = $this->FilesService->GetFilePath($args['group'], $fileName); + $filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName); } if (file_exists($filePath)) @@ -109,7 +117,7 @@ class FilesApiController extends BaseApiController throw new \Exception('Invalid filename'); } - $filePath = $this->FilesService->GetFilePath($args['group'], $fileName); + $filePath = $this->getFilesService->GetFilePath($args['group'], $fileName); if (file_exists($filePath)) { unlink($filePath); diff --git a/controllers/LoginController.php b/controllers/LoginController.php index 909453b2..100041f1 100644 --- a/controllers/LoginController.php +++ b/controllers/LoginController.php @@ -10,15 +10,8 @@ class LoginController extends BaseController { public function __construct(\Slim\Container $container, string $sessionCookieName) { - #$fp = fopen('/config/data/sql.log', 'a'); - #$time_start = microtime(true); parent::__construct($container); - #fwrite($fp, "£££ Login controller - parent construstor time : " . round((microtime(true) - $time_start),6) . "\n"); - #$this->SessionService = SessionService::getInstance(); - #fwrite($fp, "£££ Login controller - got session service instance : " . round((microtime(true) - $time_start),6) . "\n"); $this->SessionCookieName = $sessionCookieName; - #fwrite($fp, "£££ Login controller - construction time : " . round((microtime(true) - $time_start),6) . "\n"); - #fclose($fp); } protected $SessionService = null; @@ -35,9 +28,6 @@ class LoginController extends BaseController public function ProcessLogin(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - #$fp = fopen('/config/data/sql.log', 'a'); - #fwrite($fp, "£££ Login controller - ProcessLogin called\n"); - #fclose($fp); $postParams = $request->getParsedBody(); if (isset($postParams['username']) && isset($postParams['password'])) { diff --git a/controllers/OpenApiController.php b/controllers/OpenApiController.php index 10c7a42a..b927eb43 100644 --- a/controllers/OpenApiController.php +++ b/controllers/OpenApiController.php @@ -10,10 +10,18 @@ class OpenApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->ApiKeyService = new ApiKeyService(); } - protected $ApiKeyService; + protected $ApiKeyService = null; + + protected function getApiKeyService() + { + if($this->ApiKeyService == null) + { + $this->ApiKeyService = new ApiKeyService(); + } + return $this->ApiKeyService; + } public function DocumentationUi(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -42,8 +50,8 @@ class OpenApiController extends BaseApiController public function CreateNewApiKey(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - $newApiKey = $this->ApiKeyService->CreateApiKey(); - $newApiKeyId = $this->ApiKeyService->GetApiKeyId($newApiKey); + $newApiKey = $this->getApiKeyService()->CreateApiKey(); + $newApiKeyId = $this->getApiKeyService()->GetApiKeyId($newApiKey); return $response->withRedirect($this->AppContainer->UrlManager->ConstructUrl("/manageapikeys?CreatedApiKeyId=$newApiKeyId")); } } diff --git a/controllers/RecipesApiController.php b/controllers/RecipesApiController.php index 272f4ba4..ef634bcb 100644 --- a/controllers/RecipesApiController.php +++ b/controllers/RecipesApiController.php @@ -9,10 +9,18 @@ class RecipesApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->RecipesService = new RecipesService(); } - protected $RecipesService; + protected $RecipesService = null; + + protected function getRecipesService() + { + if($this->RecipesService == null) + { + $this->RecipesService = new RecipesService(); + } + return $this->RecipesService; + } public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -24,7 +32,7 @@ class RecipesApiController extends BaseApiController $excludedProductIds = $requestBody['excludedProductIds']; } - $this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds); + $this->getRecipesService()->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds); return $this->EmptyApiResponse($response); } @@ -32,7 +40,7 @@ class RecipesApiController extends BaseApiController { try { - $this->RecipesService->ConsumeRecipe($args['recipeId']); + $this->getRecipesService()->ConsumeRecipe($args['recipeId']); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -47,10 +55,10 @@ class RecipesApiController extends BaseApiController { if(!isset($args['recipeId'])) { - return $this->ApiResponse($this->RecipesService->GetRecipesResolved()); + return $this->ApiResponse($this->getRecipesService()->GetRecipesResolved()); } - $recipeResolved = FindObjectInArrayByPropertyValue($this->RecipesService->GetRecipesResolved(), 'recipe_id', $args['recipeId']); + $recipeResolved = FindObjectInArrayByPropertyValue($this->getRecipesService()->GetRecipesResolved(), 'recipe_id', $args['recipeId']); if(!$recipeResolved) { throw new \Exception('Recipe does not exist'); diff --git a/controllers/RecipesController.php b/controllers/RecipesController.php index 35071b9b..44fee529 100644 --- a/controllers/RecipesController.php +++ b/controllers/RecipesController.php @@ -10,12 +10,18 @@ class RecipesController extends BaseController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->RecipesService = new RecipesService(); - $this->UserfieldsService = new UserfieldsService(); } - protected $RecipesService; - protected $UserfieldsService; + protected $RecipesService = null; + + protected function getRecipesService() + { + if($this->RecipesService == null) + { + $this->RecipesService = new RecipesService(); + } + return $this->RecipesService; + } public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -27,7 +33,7 @@ class RecipesController extends BaseController { $recipes = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name'); } - $recipesResolved = $this->RecipesService->GetRecipesResolved(); + $recipesResolved = $this->getRecipesService()->GetRecipesResolved(); $selectedRecipe = null; $selectedRecipePositionsResolved = null; @@ -69,8 +75,8 @@ class RecipesController extends BaseController 'includedRecipeIdsAbsolute' => $includedRecipeIdsAbsolute, 'selectedRecipeTotalCosts' => FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->costs, 'selectedRecipeTotalCalories' => FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->calories, - 'userfields' => $this->UserfieldsService->GetFields('recipes'), - 'userfieldValues' => $this->UserfieldsService->GetAllValues('recipes'), + 'userfields' => $this->getUserfieldsService()->GetFields('recipes'), + 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('recipes'), 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved() ]); } @@ -94,11 +100,11 @@ class RecipesController extends BaseController 'mode' => 'edit', 'products' => $this->getDatabase()->products(), 'quantityunits' => $this->getDatabase()->quantity_units(), - 'recipePositionsResolved' => $this->RecipesService->GetRecipesPosResolved(), - 'recipesResolved' => $this->RecipesService->GetRecipesResolved(), + 'recipePositionsResolved' => $this->getRecipesService()->GetRecipesPosResolved(), + 'recipesResolved' => $this->getRecipesService()->GetRecipesResolved(), 'recipes' => $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name'), 'recipeNestings' => $this->getDatabase()->recipes_nestings()->where('recipe_id', $recipeId), - 'userfields' => $this->UserfieldsService->GetFields('recipes'), + 'userfields' => $this->getUserfieldsService()->GetFields('recipes'), 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved() ]); } @@ -157,7 +163,7 @@ class RecipesController extends BaseController 'fullcalendarEventSources' => $events, 'recipes' => $recipes, 'internalRecipes' => $this->getDatabase()->recipes()->whereNot('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(), - 'recipesResolved' => $this->RecipesService->GetRecipesResolved() + 'recipesResolved' => $this->getRecipesService()->GetRecipesResolved() ]); } } diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index fdef2236..eebc4f1c 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -9,16 +9,24 @@ class StockApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $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 ProductDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { try { - return $this->ApiResponse($this->StockService->GetProductDetails($args['productId'])); + return $this->ApiResponse($this->getStockService()->GetProductDetails($args['productId'])); } catch (\Exception $ex) { @@ -30,8 +38,8 @@ class StockApiController extends BaseApiController { try { - $productId = $this->StockService->GetProductIdFromBarcode($args['barcode']); - return $this->ApiResponse($this->StockService->GetProductDetails($productId)); + $productId = $this->getStockService()->GetProductIdFromBarcode($args['barcode']); + return $this->ApiResponse($this->getStockService()->GetProductDetails($productId)); } catch (\Exception $ex) { @@ -43,7 +51,7 @@ class StockApiController extends BaseApiController { try { - return $this->ApiResponse($this->StockService->GetProductPriceHistory($args['productId'])); + return $this->ApiResponse($this->getStockService()->GetProductPriceHistory($args['productId'])); } catch (\Exception $ex) { @@ -91,7 +99,7 @@ class StockApiController extends BaseApiController $transactionType = $requestBody['transactiontype']; } - $bookingId = $this->StockService->AddProduct($args['productId'], $requestBody['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price, $locationId); + $bookingId = $this->getStockService()->AddProduct($args['productId'], $requestBody['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price, $locationId); return $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); } catch (\Exception $ex) @@ -104,7 +112,7 @@ class StockApiController extends BaseApiController { try { - $args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); + $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']); return $this->AddProduct($request, $response, $args); } catch (\Exception $ex) @@ -159,7 +167,7 @@ class StockApiController extends BaseApiController $recipeId = $requestBody['recipe_id']; } - $bookingId = $this->StockService->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId); + $bookingId = $this->getStockService()->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId); $result = $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); } catch (\Exception $ex) @@ -175,7 +183,7 @@ class StockApiController extends BaseApiController { try { - $args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); + $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']); return $this->ConsumeProduct($request, $response, $args); } catch (\Exception $ex) @@ -218,7 +226,7 @@ class StockApiController extends BaseApiController $price = $requestBody['price']; } - $bookingId = $this->StockService->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price); + $bookingId = $this->getStockService()->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price); return $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); } catch (\Exception $ex) @@ -231,7 +239,7 @@ class StockApiController extends BaseApiController { try { - $args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); + $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']); return $this->InventoryProduct($request, $response, $args); } catch (\Exception $ex) @@ -262,7 +270,7 @@ class StockApiController extends BaseApiController $specificStockEntryId = $requestBody['stock_entry_id']; } - $bookingId = $this->StockService->OpenProduct($args['productId'], $requestBody['amount'], $specificStockEntryId); + $bookingId = $this->getStockService()->OpenProduct($args['productId'], $requestBody['amount'], $specificStockEntryId); return $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); } catch (\Exception $ex) @@ -275,7 +283,7 @@ class StockApiController extends BaseApiController { try { - $args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); + $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']); return $this->OpenProduct($request, $response, $args); } catch (\Exception $ex) @@ -286,7 +294,7 @@ class StockApiController extends BaseApiController public function CurrentStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - return $this->ApiResponse($this->StockService->GetCurrentStock()); + return $this->ApiResponse($this->getStockService()->GetCurrentStock()); } public function CurrentVolatilStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) @@ -297,9 +305,9 @@ class StockApiController extends BaseApiController $nextXDays = $request->getQueryParams()['expiring_days']; } - $expiringProducts = $this->StockService->GetExpiringProducts($nextXDays, true); - $expiredProducts = $this->StockService->GetExpiringProducts(-1); - $missingProducts = $this->StockService->GetMissingProducts(); + $expiringProducts = $this->getStockService()->GetExpiringProducts($nextXDays, true); + $expiredProducts = $this->getStockService()->GetExpiringProducts(-1); + $missingProducts = $this->getStockService()->GetMissingProducts(); return $this->ApiResponse(array( 'expiring_products' => $expiringProducts, 'expired_products' => $expiredProducts, @@ -319,7 +327,7 @@ class StockApiController extends BaseApiController $listId = intval($requestBody['list_id']); } - $this->StockService->AddMissingProductsToShoppingList($listId); + $this->getStockService()->AddMissingProductsToShoppingList($listId); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -340,7 +348,7 @@ class StockApiController extends BaseApiController $listId = intval($requestBody['list_id']); } - $this->StockService->ClearShoppingList($listId); + $this->getStockService()->ClearShoppingList($listId); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -382,7 +390,7 @@ class StockApiController extends BaseApiController throw new \Exception("No product id was supplied"); } - $this->StockService->AddProductToShoppingList($productId, $amount, $note, $listId); + $this->getStockService()->AddProductToShoppingList($productId, $amount, $note, $listId); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -418,7 +426,7 @@ class StockApiController extends BaseApiController throw new \Exception("No product id was supplied"); } - $this->StockService->RemoveProductFromShoppingList($productId, $amount, $listId); + $this->getStockService()->RemoveProductFromShoppingList($productId, $amount, $listId); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -437,7 +445,7 @@ class StockApiController extends BaseApiController $addFoundProduct = true; } - return $this->ApiResponse($this->StockService->ExternalBarcodeLookup($args['barcode'], $addFoundProduct)); + return $this->ApiResponse($this->getStockService()->ExternalBarcodeLookup($args['barcode'], $addFoundProduct)); } catch (\Exception $ex) { @@ -460,7 +468,7 @@ class StockApiController extends BaseApiController public function ProductStockEntries(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - return $this->ApiResponse($this->StockService->GetProductStockEntries($args['productId'])); + return $this->ApiResponse($this->getStockService()->GetProductStockEntries($args['productId'])); } public function StockBooking(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) diff --git a/controllers/StockController.php b/controllers/StockController.php index 2df0b998..780cb210 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -10,11 +10,7 @@ class StockController extends BaseController public function __construct(\Slim\Container $container) { - #$fp = fopen('/www/data/sql.log', 'a'); - #fwrite($fp, "!!!constructing StockController\n"); - #fclose($fp); parent::__construct($container); - #$this->StockService = new StockService(); } protected $StockService = null; @@ -57,15 +53,10 @@ class StockController extends BaseController public function Consume(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - #$fp = fopen('/www/data/sql.log', 'a'); - #fwrite($fp, "???executing consume stock"); - #$time_start = microtime(true); $result = $this->renderPage($response, 'consume', [ 'products' => $this->getDatabase()->products()->orderBy('name'), 'recipes' => $this->getDatabase()->recipes()->orderBy('name') ]); - #fwrite($fp, "???Total execution time in seconds: " . round((microtime(true) - $time_start),6) . "\n"); - #fclose($fp); return $result; } diff --git a/controllers/SystemApiController.php b/controllers/SystemApiController.php index 8f5468f2..3ec023d5 100644 --- a/controllers/SystemApiController.php +++ b/controllers/SystemApiController.php @@ -2,35 +2,19 @@ namespace Grocy\Controllers; -#use \Grocy\Services\DatabaseService; -#use \Grocy\Services\ApplicationService; - class SystemApiController extends BaseApiController { public function __construct(\Slim\Container $container) { parent::__construct($container); - #$this->ApplicationService = ApplicationService::getInstance(); } - #protected $DatabaseService; - #protected $ApplicationService; - public function GetDbChangedTime(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - #$fp = fopen('/config/data/sql.log', 'a'); - #fwrite($fp, "---- getting db changed time ----\n"); - #$time_start = microtime(true); $response = $this->ApiResponse(array( 'changed_time' => $this->getDatabaseService()->GetDbChangedTime() )); - #fwrite($fp, "----Total execution time in seconds: " . round((microtime(true) - $time_start),4) . "\n"); - #fwrite($fp, "---- time obtained ----\n"); - #fclose($fp); return $response; - #return $this->ApiResponse(array( - # 'changed_time' => $this->getDatabaseService()->GetDbChangedTime() - #)); } public function LogMissingLocalization(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) diff --git a/controllers/SystemController.php b/controllers/SystemController.php index 5990c68a..072e3a1f 100644 --- a/controllers/SystemController.php +++ b/controllers/SystemController.php @@ -2,18 +2,15 @@ namespace Grocy\Controllers; -#use \Grocy\Services\ApplicationService; use \Grocy\Services\DatabaseMigrationService; use \Grocy\Services\DemoDataGeneratorService; class SystemController extends BaseController { - #protected $ApplicationService; public function __construct(\Slim\Container $container) { parent::__construct($container); - #$this->ApplicationService = ApplicationService::getInstance(); } public function Root(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) diff --git a/controllers/TasksApiController.php b/controllers/TasksApiController.php index af4edda1..5488f6dd 100644 --- a/controllers/TasksApiController.php +++ b/controllers/TasksApiController.php @@ -9,14 +9,22 @@ class TasksApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->TasksService = new TasksService(); } - protected $TasksService; + protected $TasksService = null; + + protected function getTasksService() + { + if($this->TasksService == null) + { + $this->TasksService = new TasksService(); + } + return $this->TasksService; + } public function Current(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { - return $this->ApiResponse($this->TasksService->GetCurrent()); + return $this->ApiResponse($this->getTasksService()->GetCurrent()); } public function MarkTaskAsCompleted(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) @@ -31,7 +39,7 @@ class TasksApiController extends BaseApiController $doneTime = $requestBody['done_time']; } - $this->TasksService->MarkTaskAsCompleted($args['taskId'], $doneTime); + $this->getTasksService()->MarkTaskAsCompleted($args['taskId'], $doneTime); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -44,7 +52,7 @@ class TasksApiController extends BaseApiController { try { - $this->TasksService->UndoTask($args['taskId']); + $this->getTasksService()->UndoTask($args['taskId']); return $this->EmptyApiResponse($response); } catch (\Exception $ex) diff --git a/controllers/TasksController.php b/controllers/TasksController.php index 152c3b9a..4bad0903 100644 --- a/controllers/TasksController.php +++ b/controllers/TasksController.php @@ -9,10 +9,18 @@ class TasksController extends BaseController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->TasksService = new TasksService(); } - protected $TasksService; + protected $TasksService = null; + + protected function getTasksService() + { + if($this->TasksService == null) + { + $this->TasksService = new TasksService(); + } + return $this->TasksService; + } public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { @@ -22,7 +30,7 @@ class TasksController extends BaseController } else { - $tasks = $this->TasksService->GetCurrent(); + $tasks = $this->getTasksService()->GetCurrent(); } $usersService = $this->getUsersService(); diff --git a/controllers/UsersApiController.php b/controllers/UsersApiController.php index e4d1cf63..24ef96fc 100644 --- a/controllers/UsersApiController.php +++ b/controllers/UsersApiController.php @@ -9,16 +9,24 @@ class UsersApiController extends BaseApiController public function __construct(\Slim\Container $container) { parent::__construct($container); - $this->UsersService = new UsersService(); } - protected $UsersService; + protected $UsersService = null; + + protected function getUsersService() + { + if($this->UsersService == null) + { + $this->UsersService = new UsersService(); + } + return $this->UsersService; + } public function GetUsers(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { try { - return $this->ApiResponse($this->UsersService->GetUsersAsDto()); + return $this->ApiResponse($this->getUsersService()->GetUsersAsDto()); } catch (\Exception $ex) { @@ -37,7 +45,7 @@ class UsersApiController extends BaseApiController throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)'); } - $this->UsersService->CreateUser($requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']); + $this->getUsersService()->CreateUser($requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -50,7 +58,7 @@ class UsersApiController extends BaseApiController { try { - $this->UsersService->DeleteUser($args['userId']); + $this->getUsersService()->DeleteUser($args['userId']); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -65,7 +73,7 @@ class UsersApiController extends BaseApiController try { - $this->UsersService->EditUser($args['userId'], $requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']); + $this->getUsersService()->EditUser($args['userId'], $requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']); return $this->EmptyApiResponse($response); } catch (\Exception $ex) @@ -78,7 +86,7 @@ class UsersApiController extends BaseApiController { try { - $value = $this->UsersService->GetUserSetting(GROCY_USER_ID, $args['settingKey']); + $value = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, $args['settingKey']); return $this->ApiResponse(array('value' => $value)); } catch (\Exception $ex) @@ -93,7 +101,7 @@ class UsersApiController extends BaseApiController { $requestBody = $request->getParsedBody(); - $value = $this->UsersService->SetUserSetting(GROCY_USER_ID, $args['settingKey'], $requestBody['value']); + $value = $this->getUsersService()->SetUserSetting(GROCY_USER_ID, $args['settingKey'], $requestBody['value']); return $this->EmptyApiResponse($response); } catch (\Exception $ex)