mirror of
https://github.com/grocy/grocy.git
synced 2025-08-17 19:16:37 +00:00
Convert services to singletons and use lazy loading to improve performance (#479)
* use singletons to reduce need to recreate the same objects * unable to make the constructor private * comment out debug printing to log file * correct typo of treating self() as a var instead of a function * utilise Localisation service as a singleton * fix errent line that should have been commented * remove phpinfo * correct mistake in stock controller * try storing app in apcu * serialise inside the app closures * get timings for db-changed-time * get timings for db-changed-time * store localisation service in apcu * stor translations in apcu instead of localisation service (due to database connection) * correct syntax error * forgot to uncomment instance map * correct indentation and variable out of scope * more timings for app execution time * try apc caching for views * correct scope for Pot variable * remove additional fopen * correct timings for app build time * correct timings for app object build time * correct timings for app route build time * get timings for routing timings * get more in depth timings for routing loading * fix more in depth timings for routing loading * start investigating session auth middleware creation * start investigating session auth middleware creation * start investigating Login controller time * start investigating Login controller time * in depth look at Logincontroller timings * comment out debug printing * lazily obtain valus for page rendering * correct syntax error * correct scope of variable * correct visibiity of methds inherited from BaseController * missing use for Userfieldsservice * lazy loading of open api spec * lazy loading of users service * lazy loading of batteries service * lazy loading of services in controllers * lazy loading of services in services * correct mistake * fix userservice * fix userservice * fix userfieldservice * fix chores service * fix calendar service * remove Dockerfile used for development * Remove docker compose file used for development * Clean up app.php * remove last diff * Clean up base controller * Clean up controllers * lean up middleware * Clean up and tuen all services into singletons * remove debug from routes.php * remove acpu from localisation * Complete removal of acpu from localisation * fixes for things broken * More fixes following merge * Fix for start up bug. Re factoring singleton code had brroken due to scope of clas var. * fix bug where getUsersService is declared twice * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * bug fixes following merge * Fix all the not working things... * Deleted off-topic files * Deleted off-topic files Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
@@ -3,47 +3,38 @@
|
||||
namespace Grocy\Controllers;
|
||||
|
||||
use \Grocy\Services\RecipesService;
|
||||
use \Grocy\Services\StockService;
|
||||
use \Grocy\Services\UserfieldsService;
|
||||
|
||||
class RecipesController extends BaseController
|
||||
{
|
||||
public function __construct(\DI\Container $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
$this->RecipesService = new RecipesService();
|
||||
$this->StockService = new StockService();
|
||||
$this->UserfieldsService = new UserfieldsService();
|
||||
}
|
||||
|
||||
protected $RecipesService;
|
||||
protected $StockService;
|
||||
protected $UserfieldsService;
|
||||
|
||||
public function Overview(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
{
|
||||
$recipes = $this->Database->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name');
|
||||
$recipesResolved = $this->RecipesService->GetRecipesResolved();
|
||||
$recipes = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name');
|
||||
$recipesResolved = $this->getRecipesService()->GetRecipesResolved();
|
||||
|
||||
$selectedRecipe = null;
|
||||
$selectedRecipePositionsResolved = null;
|
||||
if (isset($request->getQueryParams()['recipe']))
|
||||
{
|
||||
$selectedRecipe = $this->Database->recipes($request->getQueryParams()['recipe']);
|
||||
$selectedRecipePositionsResolved = $this->Database->recipes_pos_resolved()->where('recipe_id = :1 AND is_nested_recipe_pos = 0', $request->getQueryParams()['recipe'])->orderBy('ingredient_group', 'ASC', 'product_group', 'ASC');
|
||||
$selectedRecipe = $this->getDatabase()->recipes($request->getQueryParams()['recipe']);
|
||||
$selectedRecipePositionsResolved = $this->getDatabase()->recipes_pos_resolved()->where('recipe_id = :1 AND is_nested_recipe_pos = 0', $request->getQueryParams()['recipe'])->orderBy('ingredient_group', 'ASC', 'product_group', 'ASC');
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($recipes as $recipe)
|
||||
{
|
||||
$selectedRecipe = $recipe;
|
||||
$selectedRecipePositionsResolved = $this->Database->recipes_pos_resolved()->where('recipe_id = :1 AND is_nested_recipe_pos = 0', $recipe->id)->orderBy('ingredient_group', 'ASC', 'product_group', 'ASC');
|
||||
$selectedRecipePositionsResolved = $this->getDatabase()->recipes_pos_resolved()->where('recipe_id = :1 AND is_nested_recipe_pos = 0', $recipe->id)->orderBy('ingredient_group', 'ASC', 'product_group', 'ASC');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$selectedRecipeSubRecipes = $this->Database->recipes()->where('id IN (SELECT includes_recipe_id FROM recipes_nestings_resolved WHERE recipe_id = :1 AND includes_recipe_id != :1)', $selectedRecipe->id)->orderBy('name')->fetchAll();
|
||||
$selectedRecipeSubRecipesPositions = $this->Database->recipes_pos_resolved()->where('recipe_id = :1', $selectedRecipe->id)->orderBy('ingredient_group', 'ASC', 'product_group', 'ASC')->fetchAll();
|
||||
$selectedRecipeSubRecipes = $this->getDatabase()->recipes()->where('id IN (SELECT includes_recipe_id FROM recipes_nestings_resolved WHERE recipe_id = :1 AND includes_recipe_id != :1)', $selectedRecipe->id)->orderBy('name')->fetchAll();
|
||||
$selectedRecipeSubRecipesPositions = $this->getDatabase()->recipes_pos_resolved()->where('recipe_id = :1', $selectedRecipe->id)->orderBy('ingredient_group', 'ASC', 'product_group', 'ASC')->fetchAll();
|
||||
|
||||
$includedRecipeIdsAbsolute = array();
|
||||
$includedRecipeIdsAbsolute[] = $selectedRecipe->id;
|
||||
@@ -52,22 +43,22 @@ class RecipesController extends BaseController
|
||||
$includedRecipeIdsAbsolute[] = $subRecipe->id;
|
||||
}
|
||||
|
||||
return $this->View->render($response, 'recipes', [
|
||||
return $this->renderPage($response, 'recipes', [
|
||||
'recipes' => $recipes,
|
||||
'recipesResolved' => $recipesResolved,
|
||||
'recipePositionsResolved' => $this->Database->recipes_pos_resolved()->where('recipe_type', RecipesService::RECIPE_TYPE_NORMAL),
|
||||
'recipePositionsResolved' => $this->getDatabase()->recipes_pos_resolved()->where('recipe_type', RecipesService::RECIPE_TYPE_NORMAL),
|
||||
'selectedRecipe' => $selectedRecipe,
|
||||
'selectedRecipePositionsResolved' => $selectedRecipePositionsResolved,
|
||||
'products' => $this->Database->products(),
|
||||
'quantityUnits' => $this->Database->quantity_units(),
|
||||
'products' => $this->getDatabase()->products(),
|
||||
'quantityUnits' => $this->getDatabase()->quantity_units(),
|
||||
'selectedRecipeSubRecipes' => $selectedRecipeSubRecipes,
|
||||
'selectedRecipeSubRecipesPositions' => $selectedRecipeSubRecipesPositions,
|
||||
'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'),
|
||||
'quantityUnitConversionsResolved' => $this->Database->quantity_unit_conversions_resolved()
|
||||
'userfields' => $this->getUserfieldsService()->GetFields('recipes'),
|
||||
'userfieldValues' => $this->getUserfieldsService()->GetAllValues('recipes'),
|
||||
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -76,26 +67,26 @@ class RecipesController extends BaseController
|
||||
$recipeId = $args['recipeId'];
|
||||
if ($recipeId == 'new')
|
||||
{
|
||||
$newRecipe = $this->Database->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->createRow(array(
|
||||
'name' => $this->LocalizationService->__t('New recipe')
|
||||
$newRecipe = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->createRow(array(
|
||||
'name' => $this->getLocalizationService()->__t('New recipe')
|
||||
));
|
||||
$newRecipe->save();
|
||||
|
||||
$recipeId = $this->Database->lastInsertId();
|
||||
$recipeId = $this->getDatabase()->lastInsertId();
|
||||
}
|
||||
|
||||
return $this->View->render($response, 'recipeform', [
|
||||
'recipe' => $this->Database->recipes($recipeId),
|
||||
'recipePositions' => $this->Database->recipes_pos()->where('recipe_id', $recipeId),
|
||||
|
||||
return $this->renderPage($response, 'recipeform', [
|
||||
'recipe' => $this->getDatabase()->recipes($recipeId),
|
||||
'recipePositions' => $this->getDatabase()->recipes_pos()->where('recipe_id', $recipeId),
|
||||
'mode' => 'edit',
|
||||
'products' => $this->Database->products()->orderBy('name'),
|
||||
'quantityunits' => $this->Database->quantity_units(),
|
||||
'recipePositionsResolved' => $this->RecipesService->GetRecipesPosResolved(),
|
||||
'recipesResolved' => $this->RecipesService->GetRecipesResolved(),
|
||||
'recipes' => $this->Database->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name'),
|
||||
'recipeNestings' => $this->Database->recipes_nestings()->where('recipe_id', $recipeId),
|
||||
'userfields' => $this->UserfieldsService->GetFields('recipes'),
|
||||
'quantityUnitConversionsResolved' => $this->Database->quantity_unit_conversions_resolved()
|
||||
'products' => $this->getDatabase()->products()->orderBy('name'),
|
||||
'quantityunits' => $this->getDatabase()->quantity_units(),
|
||||
'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->getUserfieldsService()->GetFields('recipes'),
|
||||
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -103,39 +94,39 @@ class RecipesController extends BaseController
|
||||
{
|
||||
if ($args['recipePosId'] == 'new')
|
||||
{
|
||||
return $this->View->render($response, 'recipeposform', [
|
||||
return $this->renderPage($response, 'recipeposform', [
|
||||
'mode' => 'create',
|
||||
'recipe' => $this->Database->recipes($args['recipeId']),
|
||||
'recipe' => $this->getDatabase()->recipes($args['recipeId']),
|
||||
'recipePos' => new \stdClass(),
|
||||
'products' => $this->Database->products()->orderBy('name'),
|
||||
'quantityUnits' => $this->Database->quantity_units()->orderBy('name'),
|
||||
'quantityUnitConversionsResolved' => $this->Database->quantity_unit_conversions_resolved()
|
||||
'products' => $this->getDatabase()->products()->orderBy('name'),
|
||||
'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name'),
|
||||
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
|
||||
]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->View->render($response, 'recipeposform', [
|
||||
return $this->renderPage($response, 'recipeposform', [
|
||||
'mode' => 'edit',
|
||||
'recipe' => $this->Database->recipes($args['recipeId']),
|
||||
'recipePos' => $this->Database->recipes_pos($args['recipePosId']),
|
||||
'products' => $this->Database->products()->orderBy('name'),
|
||||
'quantityUnits' => $this->Database->quantity_units()->orderBy('name'),
|
||||
'quantityUnitConversionsResolved' => $this->Database->quantity_unit_conversions_resolved()
|
||||
'recipe' => $this->getDatabase()->recipes($args['recipeId']),
|
||||
'recipePos' => $this->getDatabase()->recipes_pos($args['recipePosId']),
|
||||
'products' => $this->getDatabase()->products()->orderBy('name'),
|
||||
'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name'),
|
||||
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function RecipesSettings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
{
|
||||
return $this->View->render($response, 'recipessettings');
|
||||
return $this->renderPage($response, 'recipessettings');
|
||||
}
|
||||
|
||||
public function MealPlan(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
||||
{
|
||||
$recipes = $this->Database->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll();
|
||||
$recipes = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll();
|
||||
|
||||
$events = array();
|
||||
foreach($this->Database->meal_plan() as $mealPlanEntry)
|
||||
foreach($this->getDatabase()->meal_plan() as $mealPlanEntry)
|
||||
{
|
||||
$recipe = FindObjectInArrayByPropertyValue($recipes, 'id', $mealPlanEntry['recipe_id']);
|
||||
$title = '';
|
||||
@@ -147,7 +138,7 @@ class RecipesController extends BaseController
|
||||
$productDetails = null;
|
||||
if ($mealPlanEntry['product_id'] !== null)
|
||||
{
|
||||
$productDetails = $this->StockService->GetProductDetails($mealPlanEntry['product_id']);
|
||||
$productDetails = $this->getStockService()->GetProductDetails($mealPlanEntry['product_id']);
|
||||
}
|
||||
|
||||
$events[] = array(
|
||||
@@ -162,14 +153,14 @@ class RecipesController extends BaseController
|
||||
);
|
||||
}
|
||||
|
||||
return $this->View->render($response, 'mealplan', [
|
||||
return $this->renderPage($response, 'mealplan', [
|
||||
'fullcalendarEventSources' => $events,
|
||||
'recipes' => $recipes,
|
||||
'internalRecipes' => $this->Database->recipes()->whereNot('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(),
|
||||
'recipesResolved' => $this->RecipesService->GetRecipesResolved(),
|
||||
'products' => $this->Database->products()->orderBy('name'),
|
||||
'quantityUnits' => $this->Database->quantity_units()->orderBy('name'),
|
||||
'quantityUnitConversionsResolved' => $this->Database->quantity_unit_conversions_resolved()
|
||||
'internalRecipes' => $this->getDatabase()->recipes()->whereNot('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(),
|
||||
'recipesResolved' => $this->getRecipesService()->GetRecipesResolved(),
|
||||
'products' => $this->getDatabase()->products()->orderBy('name'),
|
||||
'quantityUnits' => $this->getDatabase()->quantity_units()->orderBy('name'),
|
||||
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user