lazy loading of services in controllers

This commit is contained in:
zebardy 2019-11-23 15:06:57 +00:00
parent ac210fe092
commit 7cf1430433
7 changed files with 39 additions and 60 deletions

View File

@ -28,7 +28,7 @@ class CalendarApiController extends BaseApiController
{ {
$date = new \DateTime($event['start']); $date = new \DateTime($event['start']);
$date->setTimezone(date_default_timezone_get()); $date->setTimezone(date_default_timezone_get());
if ($event['date_format'] === 'date') if ($event['date_format'] === 'date')
{ {
$date->setTime(23, 59, 59); $date->setTime(23, 59, 59);
@ -41,7 +41,7 @@ class CalendarApiController extends BaseApiController
->setDescription($event['description']) ->setDescription($event['description'])
->setNoTime($event['date_format'] === 'date') ->setNoTime($event['date_format'] === 'date')
->setUseTimezone(true); ->setUseTimezone(true);
$vCalendar->addComponent($vEvent); $vCalendar->addComponent($vEvent);
} }

View File

@ -3,8 +3,6 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use \Grocy\Services\ChoresService; use \Grocy\Services\ChoresService;
use \Grocy\Services\UsersService;
use \Grocy\Services\UserfieldsService;
class ChoresController extends BaseController class ChoresController extends BaseController
{ {
@ -12,23 +10,21 @@ class ChoresController extends BaseController
{ {
parent::__construct($container); parent::__construct($container);
$this->ChoresService = new ChoresService(); $this->ChoresService = new ChoresService();
$this->UserfieldsService = new UserfieldsService();
} }
protected $ChoresService; protected $ChoresService;
protected $UserfieldsService;
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)
{ {
$usersService = new UsersService(); $usersService = $this->getUsersService();
$nextXDays = $usersService->GetUserSettings(GROCY_USER_ID)['chores_due_soon_days']; $nextXDays = $usersService->GetUserSettings(GROCY_USER_ID)['chores_due_soon_days'];
return $this->renderPage($response, 'choresoverview', [ return $this->renderPage($response, 'choresoverview', [
'chores' => $this->getDatabase()->chores()->orderBy('name'), 'chores' => $this->getDatabase()->chores()->orderBy('name'),
'currentChores' => $this->ChoresService->GetCurrent(), 'currentChores' => $this->ChoresService->GetCurrent(),
'nextXDays' => $nextXDays, 'nextXDays' => $nextXDays,
'userfields' => $this->UserfieldsService->GetFields('chores'), 'userfields' => $this->getUserfieldsService()->GetFields('chores'),
'userfieldValues' => $this->UserfieldsService->GetAllValues('chores'), 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('chores'),
'users' => $usersService->GetUsersAsDto() 'users' => $usersService->GetUsersAsDto()
]); ]);
} }
@ -45,8 +41,8 @@ class ChoresController extends BaseController
{ {
return $this->renderPage($response, 'chores', [ return $this->renderPage($response, 'chores', [
'chores' => $this->getDatabase()->chores()->orderBy('name'), 'chores' => $this->getDatabase()->chores()->orderBy('name'),
'userfields' => $this->UserfieldsService->GetFields('chores'), 'userfields' => $this->getUserfieldsService()->GetFields('chores'),
'userfieldValues' => $this->UserfieldsService->GetAllValues('chores') 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('chores')
]); ]);
} }
@ -61,7 +57,7 @@ class ChoresController extends BaseController
public function ChoreEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function ChoreEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
$usersService = new UsersService(); $usersService = getUsersService();
$users = $usersService->GetUsersAsDto(); $users = $usersService->GetUsersAsDto();
if ($args['choreId'] == 'new') if ($args['choreId'] == 'new')
@ -69,7 +65,7 @@ class ChoresController extends BaseController
return $this->renderPage($response, 'choreform', [ return $this->renderPage($response, 'choreform', [
'periodTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_PERIOD_TYPE_'), 'periodTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_PERIOD_TYPE_'),
'mode' => 'create', 'mode' => 'create',
'userfields' => $this->UserfieldsService->GetFields('chores'), 'userfields' => $this->getUserfieldsService()->GetFields('chores'),
'assignmentTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_ASSIGNMENT_TYPE_'), 'assignmentTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_ASSIGNMENT_TYPE_'),
'users' => $users, 'users' => $users,
'products' => $this->getDatabase()->products()->orderBy('name') 'products' => $this->getDatabase()->products()->orderBy('name')
@ -81,7 +77,7 @@ class ChoresController extends BaseController
'chore' => $this->getDatabase()->chores($args['choreId']), 'chore' => $this->getDatabase()->chores($args['choreId']),
'periodTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_PERIOD_TYPE_'), 'periodTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_PERIOD_TYPE_'),
'mode' => 'edit', 'mode' => 'edit',
'userfields' => $this->UserfieldsService->GetFields('chores'), 'userfields' => $this->getUserfieldsService()->GetFields('chores'),
'assignmentTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_ASSIGNMENT_TYPE_'), 'assignmentTypes' => GetClassConstants('\Grocy\Services\ChoresService', 'CHORE_ASSIGNMENT_TYPE_'),
'users' => $users, 'users' => $users,
'products' => $this->getDatabase()->products()->orderBy('name') 'products' => $this->getDatabase()->products()->orderBy('name')

View File

@ -2,14 +2,11 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use \Grocy\Services\UserfieldsService;
class EquipmentController extends BaseController class EquipmentController extends BaseController
{ {
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->UserfieldsService = new UserfieldsService();
} }
protected $UserfieldsService; protected $UserfieldsService;
@ -18,8 +15,8 @@ class EquipmentController extends BaseController
{ {
return $this->renderPage($response, 'equipment', [ return $this->renderPage($response, 'equipment', [
'equipment' => $this->getDatabase()->equipment()->orderBy('name'), 'equipment' => $this->getDatabase()->equipment()->orderBy('name'),
'userfields' => $this->UserfieldsService->GetFields('equipment'), 'userfields' => $this->getUserfieldsService()->GetFields('equipment'),
'userfieldValues' => $this->UserfieldsService->GetAllValues('equipment') 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('equipment')
]); ]);
} }
@ -29,7 +26,7 @@ class EquipmentController extends BaseController
{ {
return $this->renderPage($response, 'equipmentform', [ return $this->renderPage($response, 'equipmentform', [
'mode' => 'create', 'mode' => 'create',
'userfields' => $this->UserfieldsService->GetFields('equipment') 'userfields' => $this->getUserfieldsService()->GetFields('equipment')
]); ]);
} }
else else
@ -37,7 +34,7 @@ class EquipmentController extends BaseController
return $this->renderPage($response, 'equipmentform', [ return $this->renderPage($response, 'equipmentform', [
'equipment' => $this->getDatabase()->equipment($args['equipmentId']), 'equipment' => $this->getDatabase()->equipment($args['equipmentId']),
'mode' => 'edit', 'mode' => 'edit',
'userfields' => $this->UserfieldsService->GetFields('equipment') 'userfields' => $this->getUserfieldsService()->GetFields('equipment')
]); ]);
} }
} }

View File

@ -2,18 +2,13 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use \Grocy\Services\UserfieldsService;
class GenericEntityApiController extends BaseApiController class GenericEntityApiController extends BaseApiController
{ {
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->UserfieldsService = new UserfieldsService();
} }
protected $UserfieldsService;
public function GetObjects(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function GetObjects(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity'])) if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity']))
@ -136,7 +131,7 @@ class GenericEntityApiController extends BaseApiController
{ {
try try
{ {
return $this->ApiResponse($this->UserfieldsService->GetValues($args['entity'], $args['objectId'])); return $this->ApiResponse($this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']));
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -155,7 +150,7 @@ class GenericEntityApiController extends BaseApiController
throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)'); throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)');
} }
$this->UserfieldsService->SetValues($args['entity'], $args['objectId'], $requestBody); $this->getUserfieldsService()->SetValues($args['entity'], $args['objectId'], $requestBody);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)

View File

@ -2,23 +2,18 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use \Grocy\Services\UserfieldsService;
class GenericEntityController extends BaseController class GenericEntityController extends BaseController
{ {
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->UserfieldsService = new UserfieldsService();
} }
protected $UserfieldsService;
public function UserfieldsList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function UserfieldsList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->renderPage($response, 'userfields', [ return $this->renderPage($response, 'userfields', [
'userfields' => $this->UserfieldsService->GetAllFields(), 'userfields' => $this->getUserfieldsService()->GetAllFields(),
'entities' => $this->UserfieldsService->GetEntities() 'entities' => $this->getUserfieldsService()->GetEntities()
]); ]);
} }
@ -36,8 +31,8 @@ class GenericEntityController extends BaseController
return $this->renderPage($response, 'userobjects', [ return $this->renderPage($response, 'userobjects', [
'userentity' => $userentity, 'userentity' => $userentity,
'userobjects' => $this->getDatabase()->userobjects()->where('userentity_id = :1', $userentity->id), 'userobjects' => $this->getDatabase()->userobjects()->where('userentity_id = :1', $userentity->id),
'userfields' => $this->UserfieldsService->GetFields('userentity-' . $args['userentityName']), 'userfields' => $this->getUserfieldsService()->GetFields('userentity-' . $args['userentityName']),
'userfieldValues' => $this->UserfieldsService->GetAllValues('userentity-' . $args['userentityName']) 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('userentity-' . $args['userentityName'])
]); ]);
} }
@ -47,17 +42,17 @@ class GenericEntityController extends BaseController
{ {
return $this->renderPage($response, 'userfieldform', [ return $this->renderPage($response, 'userfieldform', [
'mode' => 'create', 'mode' => 'create',
'userfieldTypes' => $this->UserfieldsService->GetFieldTypes(), 'userfieldTypes' => $this->getUserfieldsService()->GetFieldTypes(),
'entities' => $this->UserfieldsService->GetEntities() 'entities' => $this->getUserfieldsService()->GetEntities()
]); ]);
} }
else else
{ {
return $this->renderPage($response, 'userfieldform', [ return $this->renderPage($response, 'userfieldform', [
'mode' => 'edit', 'mode' => 'edit',
'userfield' => $this->UserfieldsService->GetField($args['userfieldId']), 'userfield' => $this->getUserfieldsService()->GetField($args['userfieldId']),
'userfieldTypes' => $this->UserfieldsService->GetFieldTypes(), 'userfieldTypes' => $this->getUserfieldsService()->GetFieldTypes(),
'entities' => $this->UserfieldsService->GetEntities() 'entities' => $this->getUserfieldsService()->GetEntities()
]); ]);
} }
} }
@ -88,7 +83,7 @@ class GenericEntityController extends BaseController
return $this->renderPage($response, 'userobjectform', [ return $this->renderPage($response, 'userobjectform', [
'userentity' => $userentity, 'userentity' => $userentity,
'mode' => 'create', 'mode' => 'create',
'userfields' => $this->UserfieldsService->GetFields('userentity-' . $args['userentityName']) 'userfields' => $this->getUserfieldsService()->GetFields('userentity-' . $args['userentityName'])
]); ]);
} }
else else
@ -97,7 +92,7 @@ class GenericEntityController extends BaseController
'userentity' => $userentity, 'userentity' => $userentity,
'mode' => 'edit', 'mode' => 'edit',
'userobject' => $this->getDatabase()->userobjects($args['userobjectId']), 'userobject' => $this->getDatabase()->userobjects($args['userobjectId']),
'userfields' => $this->UserfieldsService->GetFields('userentity-' . $args['userentityName']) 'userfields' => $this->getUserfieldsService()->GetFields('userentity-' . $args['userentityName'])
]); ]);
} }
} }

View File

@ -23,7 +23,7 @@ class RecipesApiController extends BaseApiController
{ {
$excludedProductIds = $requestBody['excludedProductIds']; $excludedProductIds = $requestBody['excludedProductIds'];
} }
$this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds); $this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
@ -44,7 +44,7 @@ class RecipesApiController extends BaseApiController
public function GetRecipeFulfillment(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function GetRecipeFulfillment(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try
{ {
if(!isset($args['recipeId'])) if(!isset($args['recipeId']))
{ {
return $this->ApiResponse($this->RecipesService->GetRecipesResolved()); return $this->ApiResponse($this->RecipesService->GetRecipesResolved());
@ -59,7 +59,7 @@ class RecipesApiController extends BaseApiController
{ {
return $this->ApiResponse($recipeResolved); return $this->ApiResponse($recipeResolved);
} }
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
return $this->GenericErrorResponse($response, $ex->getMessage()); return $this->GenericErrorResponse($response, $ex->getMessage());

View File

@ -3,8 +3,6 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use \Grocy\Services\TasksService; use \Grocy\Services\TasksService;
use \Grocy\Services\UsersService;
use \Grocy\Services\UserfieldsService;
class TasksController extends BaseController class TasksController extends BaseController
{ {
@ -12,11 +10,9 @@ class TasksController extends BaseController
{ {
parent::__construct($container); parent::__construct($container);
$this->TasksService = new TasksService(); $this->TasksService = new TasksService();
$this->UserfieldsService = new UserfieldsService();
} }
protected $TasksService; protected $TasksService;
protected $UserfieldsService;
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,7 +25,7 @@ class TasksController extends BaseController
$tasks = $this->TasksService->GetCurrent(); $tasks = $this->TasksService->GetCurrent();
} }
$usersService = new UsersService(); $usersService = $this->getUsersService();
$nextXDays = $usersService->GetUserSettings(GROCY_USER_ID)['tasks_due_soon_days']; $nextXDays = $usersService->GetUserSettings(GROCY_USER_ID)['tasks_due_soon_days'];
return $this->renderPage($response, 'tasks', [ return $this->renderPage($response, 'tasks', [
@ -37,8 +33,8 @@ class TasksController extends BaseController
'nextXDays' => $nextXDays, 'nextXDays' => $nextXDays,
'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'), 'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'),
'users' => $this->getDatabase()->users(), 'users' => $this->getDatabase()->users(),
'userfields' => $this->UserfieldsService->GetFields('tasks'), 'userfields' => $this->getUserfieldsService()->GetFields('tasks'),
'userfieldValues' => $this->UserfieldsService->GetAllValues('tasks') 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('tasks')
]); ]);
} }
@ -50,7 +46,7 @@ class TasksController extends BaseController
'mode' => 'create', 'mode' => 'create',
'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'), 'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'),
'users' => $this->getDatabase()->users()->orderBy('username'), 'users' => $this->getDatabase()->users()->orderBy('username'),
'userfields' => $this->UserfieldsService->GetFields('tasks') 'userfields' => $this->getUserfieldsService()->GetFields('tasks')
]); ]);
} }
else else
@ -60,7 +56,7 @@ class TasksController extends BaseController
'mode' => 'edit', 'mode' => 'edit',
'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'), 'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'),
'users' => $this->getDatabase()->users()->orderBy('username'), 'users' => $this->getDatabase()->users()->orderBy('username'),
'userfields' => $this->UserfieldsService->GetFields('tasks') 'userfields' => $this->getUserfieldsService()->GetFields('tasks')
]); ]);
} }
} }
@ -69,8 +65,8 @@ class TasksController extends BaseController
{ {
return $this->renderPage($response, 'taskcategories', [ return $this->renderPage($response, 'taskcategories', [
'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'), 'taskCategories' => $this->getDatabase()->task_categories()->orderBy('name'),
'userfields' => $this->UserfieldsService->GetFields('task_categories'), 'userfields' => $this->getUserfieldsService()->GetFields('task_categories'),
'userfieldValues' => $this->UserfieldsService->GetAllValues('task_categories') 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('task_categories')
]); ]);
} }
@ -80,7 +76,7 @@ class TasksController extends BaseController
{ {
return $this->renderPage($response, 'taskcategoryform', [ return $this->renderPage($response, 'taskcategoryform', [
'mode' => 'create', 'mode' => 'create',
'userfields' => $this->UserfieldsService->GetFields('task_categories') 'userfields' => $this->getUserfieldsService()->GetFields('task_categories')
]); ]);
} }
else else
@ -88,7 +84,7 @@ class TasksController extends BaseController
return $this->renderPage($response, 'taskcategoryform', [ return $this->renderPage($response, 'taskcategoryform', [
'category' => $this->getDatabase()->task_categories($args['categoryId']), 'category' => $this->getDatabase()->task_categories($args['categoryId']),
'mode' => 'edit', 'mode' => 'edit',
'userfields' => $this->UserfieldsService->GetFields('task_categories') 'userfields' => $this->getUserfieldsService()->GetFields('task_categories')
]); ]);
} }
} }