mirror of
https://github.com/grocy/grocy.git
synced 2026-04-05 20:36:15 +02:00
Clean up and tuen all services into singletons
This commit is contained in:
parent
58364493c9
commit
ce75d6bca8
|
|
@ -17,7 +17,7 @@ class BatteriesApiController extends BaseApiController
|
|||
{
|
||||
if($this->BatteriesService == null)
|
||||
{
|
||||
$this->BatteriesService = new BatteriesService();
|
||||
$this->BatteriesService = BatteriesService::getInstance();
|
||||
}
|
||||
return $this->BatteriesService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class BatteriesController extends BaseController
|
|||
{
|
||||
if($this->BatteriesService == null)
|
||||
{
|
||||
$this->BatteriesService = new BatteriesService();
|
||||
$this->BatteriesService = BatteriesService::getInstance();
|
||||
}
|
||||
return $this->BatteriesService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class CalendarApiController extends BaseApiController
|
|||
{
|
||||
if($this->CalendarService == null)
|
||||
{
|
||||
$this->CalendarService = new CalendarService();
|
||||
$this->CalendarService = CalendarService::getInstance();
|
||||
}
|
||||
return $this->CalendarService;
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ class CalendarApiController extends BaseApiController
|
|||
{
|
||||
if($this->ApiKeyService == null)
|
||||
{
|
||||
$this->ApiKeyService = new ApiKeyService();
|
||||
$this->ApiKeyService = ApiKeyService::getInstance();
|
||||
}
|
||||
return $this->ApiKeyService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class CalendarController extends BaseController
|
|||
{
|
||||
if($this->CalendarService == null)
|
||||
{
|
||||
$this->CalendarService = new CalendarService();
|
||||
$this->CalendarService = CalendarService::getInstance();
|
||||
}
|
||||
return $this->CalendarService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class ChoresApiController extends BaseApiController
|
|||
{
|
||||
if($this->ChoresService == null)
|
||||
{
|
||||
$this->ChoresService = new ChoresService();
|
||||
$this->ChoresService = ChoresService::getInstance();
|
||||
}
|
||||
return $this->ChoresService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class ChoresController extends BaseController
|
|||
{
|
||||
if($this->ChoresService == null)
|
||||
{
|
||||
$this->ChoresService = new ChoresService();
|
||||
$this->ChoresService = ChoresService::getInstance();
|
||||
}
|
||||
return $this->ChoresService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class FilesApiController extends BaseApiController
|
|||
{
|
||||
if($this->FilesService == null)
|
||||
{
|
||||
$this->FilesService = new FilesService();
|
||||
$this->FilesService = FilesService::getInstance();
|
||||
}
|
||||
return $this->FilesService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class OpenApiController extends BaseApiController
|
|||
{
|
||||
if($this->ApiKeyService == null)
|
||||
{
|
||||
$this->ApiKeyService = new ApiKeyService();
|
||||
$this->ApiKeyService = ApiKeyService::getInstance();
|
||||
}
|
||||
return $this->ApiKeyService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class RecipesApiController extends BaseApiController
|
|||
{
|
||||
if($this->RecipesService == null)
|
||||
{
|
||||
$this->RecipesService = new RecipesService();
|
||||
$this->RecipesService = RecipesService::getInstance();
|
||||
}
|
||||
return $this->RecipesService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class RecipesController extends BaseController
|
|||
{
|
||||
if($this->RecipesService == null)
|
||||
{
|
||||
$this->RecipesService = new RecipesService();
|
||||
$this->RecipesService = RecipesService::getInstance();
|
||||
}
|
||||
return $this->RecipesService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class StockApiController extends BaseApiController
|
|||
{
|
||||
if($this->StockService == null)
|
||||
{
|
||||
$this->StockService = new StockService();
|
||||
$this->StockService = StockService::getInstance();
|
||||
}
|
||||
return $this->StockService;
|
||||
}
|
||||
|
|
@ -127,10 +127,6 @@ class StockApiController extends BaseApiController
|
|||
|
||||
$result = null;
|
||||
|
||||
#$fp = fopen('/www/data/sql.log', 'a');
|
||||
#fwrite($fp, "???executing api consume product");
|
||||
#$time_start = microtime(true);
|
||||
|
||||
try
|
||||
{
|
||||
if ($requestBody === null)
|
||||
|
|
@ -174,8 +170,6 @@ class StockApiController extends BaseApiController
|
|||
{
|
||||
$result = $this->GenericErrorResponse($response, $ex->getMessage());
|
||||
}
|
||||
#fwrite($fp, "???API Consume product - Total execution time in seconds: " . round((microtime(true) - $time_start),6) . "\n");
|
||||
#fclose($fp);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class StockController extends BaseController
|
|||
{
|
||||
if($this->StockService == null)
|
||||
{
|
||||
$this->StockService = new StockService();
|
||||
$this->StockService = StockService::getInstance();
|
||||
}
|
||||
return $this->StockService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,12 @@ class SystemController extends BaseController
|
|||
public function Root(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
|
||||
{
|
||||
// Schema migration is done here
|
||||
$databaseMigrationService = new DatabaseMigrationService();
|
||||
$databaseMigrationService = DatabaseMigrationService::getInstance();
|
||||
$databaseMigrationService->MigrateDatabase();
|
||||
|
||||
if (GROCY_IS_DEMO_INSTALL)
|
||||
{
|
||||
$demoDataGeneratorService = new DemoDataGeneratorService();
|
||||
$demoDataGeneratorService = DemoDataGeneratorService::getInstance();
|
||||
$demoDataGeneratorService->PopulateDemoData();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class TasksApiController extends BaseApiController
|
|||
{
|
||||
if($this->TasksService == null)
|
||||
{
|
||||
$this->TasksService = new TasksService();
|
||||
$this->TasksService = TasksService::getInstance();
|
||||
}
|
||||
return $this->TasksService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class TasksController extends BaseController
|
|||
{
|
||||
if($this->TasksService == null)
|
||||
{
|
||||
$this->TasksService = new TasksService();
|
||||
$this->TasksService = TasksService::getInstance();
|
||||
}
|
||||
return $this->TasksService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class UsersApiController extends BaseApiController
|
|||
{
|
||||
if($this->UsersService == null)
|
||||
{
|
||||
$this->UsersService = new UsersService();
|
||||
$this->UsersService = UsersService::getInstance();
|
||||
}
|
||||
return $this->UsersService;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,6 @@ namespace Grocy\Services;
|
|||
class ApplicationService extends BaseService
|
||||
{
|
||||
private $InstalledVersion;
|
||||
private static $instance = null;
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance == null)
|
||||
{
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
#private function __construct()
|
||||
#{
|
||||
|
|
|
|||
|
|
@ -10,6 +10,18 @@ class BaseService
|
|||
public function __construct() {
|
||||
}
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance == null)
|
||||
{
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
protected function getDatabaseService()
|
||||
{
|
||||
return DatabaseService::getInstance();
|
||||
|
|
|
|||
|
|
@ -14,25 +14,40 @@ class CalendarService extends BaseService
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->StockService = new StockService();
|
||||
$this->TasksService = new TasksService();
|
||||
$this->ChoresService = new ChoresService();
|
||||
$this->BatteriesService = new BatteriesService();
|
||||
$this->UrlManager = new UrlManager(GROCY_BASE_URL);
|
||||
}
|
||||
|
||||
protected $StockService;
|
||||
protected $TasksService;
|
||||
protected $ChoresService;
|
||||
protected $BatteriesService;
|
||||
protected $UrlManager;
|
||||
protected function getStockservice()
|
||||
{
|
||||
return StockService::getInstance();
|
||||
}
|
||||
|
||||
protected function getTasksService()
|
||||
{
|
||||
return TasksService::getInstance();
|
||||
}
|
||||
|
||||
protected function getChoresService()
|
||||
{
|
||||
return ChoresService::getInstance();
|
||||
}
|
||||
|
||||
protected function getBatteriesService()
|
||||
{
|
||||
return BatteriesService::getInstance();
|
||||
}
|
||||
|
||||
protected function getUsersService()
|
||||
{
|
||||
return UsersService::getInstance();
|
||||
}
|
||||
|
||||
public function GetEvents()
|
||||
{
|
||||
$products = $this->getDatabase()->products();
|
||||
$titlePrefix = $this->getLocalizationService()->__t('Product expires') . ': ';
|
||||
$stockEvents = array();
|
||||
foreach($this->StockService->GetCurrentStock() as $currentStockEntry)
|
||||
foreach($this->getStockService()->GetCurrentStock() as $currentStockEntry)
|
||||
{
|
||||
if ($currentStockEntry->amount > 0)
|
||||
{
|
||||
|
|
@ -46,7 +61,7 @@ class CalendarService extends BaseService
|
|||
|
||||
$titlePrefix = $this->getLocalizationService()->__t('Task due') . ': ';
|
||||
$taskEvents = array();
|
||||
foreach($this->TasksService->GetCurrent() as $currentTaskEntry)
|
||||
foreach($this->getTasksService()->GetCurrent() as $currentTaskEntry)
|
||||
{
|
||||
$taskEvents[] = array(
|
||||
'title' => $titlePrefix . $currentTaskEntry->name,
|
||||
|
|
@ -55,8 +70,7 @@ class CalendarService extends BaseService
|
|||
);
|
||||
}
|
||||
|
||||
$usersService = new UsersService();
|
||||
$users = $usersService->GetUsersAsDto();
|
||||
$users = $this->getUsersService()->GetUsersAsDto();
|
||||
|
||||
$chores = $this->getDatabase()->chores();
|
||||
$titlePrefix = $this->getLocalizationService()->__t('Chore due') . ': ';
|
||||
|
|
@ -81,7 +95,7 @@ class CalendarService extends BaseService
|
|||
$batteries = $this->getDatabase()->batteries();
|
||||
$titlePrefix = $this->getLocalizationService()->__t('Battery charge cycle due') . ': ';
|
||||
$batteryEvents = array();
|
||||
foreach($this->BatteriesService->GetCurrent() as $currentBatteryEntry)
|
||||
foreach($this->getBatteriesService()->GetCurrent() as $currentBatteryEntry)
|
||||
{
|
||||
$batteryEvents[] = array(
|
||||
'title' => $titlePrefix . FindObjectInArrayByPropertyValue($batteries, 'id', $currentBatteryEntry->battery_id)->name,
|
||||
|
|
|
|||
|
|
@ -21,10 +21,17 @@ class ChoresService extends BaseService
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->StockService = new StockService();
|
||||
}
|
||||
|
||||
protected $StockService;
|
||||
protected function getStockService()
|
||||
{
|
||||
return StockService::getInstance();
|
||||
}
|
||||
|
||||
protected function getUsersService()
|
||||
{
|
||||
return UsersService::getInstance();
|
||||
}
|
||||
|
||||
public function GetCurrent()
|
||||
{
|
||||
|
|
@ -38,9 +45,8 @@ class ChoresService extends BaseService
|
|||
{
|
||||
throw new \Exception('Chore does not exist');
|
||||
}
|
||||
|
||||
$usersService = new UsersService();
|
||||
$users = $usersService->GetUsersAsDto();
|
||||
|
||||
$users = $this->getUsersService()->GetUsersAsDto();
|
||||
|
||||
$chore = $this->getDatabase()->chores($choreId);
|
||||
$choreTrackedCount = $this->getDatabase()->chores_log()->where('chore_id = :1 AND undone = 0', $choreId)->count();
|
||||
|
|
@ -101,7 +107,7 @@ class ChoresService extends BaseService
|
|||
|
||||
if ($chore->consume_product_on_execution == 1 && !empty($chore->product_id))
|
||||
{
|
||||
$this->StockService->ConsumeProduct($chore->product_id, $chore->product_amount, false, StockService::TRANSACTION_TYPE_CONSUME);
|
||||
$this->getStockService()->ConsumeProduct($chore->product_id, $chore->product_amount, false, StockService::TRANSACTION_TYPE_CONSUME);
|
||||
}
|
||||
|
||||
return $lastInsertId;
|
||||
|
|
@ -140,8 +146,7 @@ class ChoresService extends BaseService
|
|||
$lastChoreLogRow = $this->getDatabase()->chores_log()->where('chore_id = :1 AND tracked_time = :2 AND undone = 0', $choreId, $choreLastTrackedTime)->fetch();
|
||||
$lastDoneByUserId = $lastChoreLogRow->done_by_user_id;
|
||||
|
||||
$usersService = new UsersService();
|
||||
$users = $usersService->GetUsersAsDto();
|
||||
$users = $this->getUsersService()->GetUsersAsDto();
|
||||
$assignedUsers = array();
|
||||
foreach ($users as $user)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,39 +4,6 @@ namespace Grocy\Services;
|
|||
|
||||
use \Grocy\Services\ApplicationService;
|
||||
|
||||
class PDOWrap
|
||||
{
|
||||
private $instance = null;
|
||||
public function __construct(){
|
||||
$pars = func_get_args();
|
||||
$this->instance = is_object($obj='PDO')?$obj:new $obj(
|
||||
$pars[0],
|
||||
null,
|
||||
null,
|
||||
array(\PDO::ATTR_PERSISTENT => true)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __call($name,$pars){
|
||||
$result = null;
|
||||
|
||||
#$fp = fopen('/www/data/sql.log', 'a');
|
||||
#fwrite($fp, "PDO::".$name." called with arguments:- ".implode( ", ", $pars)."\n");
|
||||
#$time_start = microtime(true);
|
||||
if(in_array($name, array("exec","query")))
|
||||
{
|
||||
#fwrite($fp, array_values($pars)[0] . "\n");
|
||||
$result = call_user_func_array([$this->instance,$name],$pars);
|
||||
}else{
|
||||
$result = call_user_func_array([$this->instance,$name],$pars);
|
||||
}
|
||||
#fwrite($fp, "Total execution time in seconds: " . round((microtime(true) - $time_start),6) . "\n");
|
||||
#fclose($fp);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
class DatabaseService
|
||||
{
|
||||
private static $instance = null;
|
||||
|
|
@ -51,12 +18,6 @@ class DatabaseService
|
|||
return self::$instance;
|
||||
}
|
||||
|
||||
#private static function __construct()
|
||||
#{
|
||||
# parent::__construct();
|
||||
#}
|
||||
|
||||
|
||||
private function GetDbFilePath()
|
||||
{
|
||||
if (GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease')
|
||||
|
|
@ -67,7 +28,6 @@ class DatabaseService
|
|||
return GROCY_DATAPATH . '/grocy.db';
|
||||
}
|
||||
|
||||
#private $DbConnectionRaw;
|
||||
private static $DbConnectionRaw = null;
|
||||
/**
|
||||
* @return \PDO
|
||||
|
|
@ -75,25 +35,15 @@ class DatabaseService
|
|||
public function GetDbConnectionRaw()
|
||||
{
|
||||
if (self::$DbConnectionRaw == null)
|
||||
#if ($this->DbConnectionRaw == null)
|
||||
{
|
||||
#$fp = fopen('/www/data/sql.log', 'a');
|
||||
#fwrite($fp, "+++Creating new PDO object\n");
|
||||
#$time_start = microtime(true);
|
||||
$pdo = new PDOWrap('sqlite:' . $this->GetDbFilePath());
|
||||
$pdo = new \PDO('sqlite:' . $this->GetDbFilePath());
|
||||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||
self::$DbConnectionRaw = $pdo;
|
||||
#$this->DbConnectionRaw = $pdo;
|
||||
#fwrite($fp, "+++Total execution time in seconds: " . round((microtime(true) - $time_start),6) . "\n");
|
||||
#fwrite($fp, "+++object created\n");
|
||||
#fclose($fp);
|
||||
}
|
||||
|
||||
return self::$DbConnectionRaw;
|
||||
#return $this->DbConnectionRaw;
|
||||
}
|
||||
|
||||
#private $DbConnection;
|
||||
private static $DbConnection = null;
|
||||
/**
|
||||
* @return \LessQL\Database
|
||||
|
|
@ -101,20 +51,11 @@ class DatabaseService
|
|||
public function GetDbConnection()
|
||||
{
|
||||
if (self::$DbConnection == null)
|
||||
#if ($this->DbConnection == null)
|
||||
{
|
||||
#$fp = fopen('/www/data/sql.log', 'a');
|
||||
#fwrite($fp, "---creating new LessQL::Database object\n");
|
||||
#$time_start = microtime(true);
|
||||
self::$DbConnection = new \LessQL\Database($this->GetDbConnectionRaw());
|
||||
#$this->DbConnection = new \LessQL\Database($this->GetDbConnectionRaw());
|
||||
#fwrite($fp, "---Total execution time in seconds: " . round((microtime(true) - $time_start),6) . "\n");
|
||||
#fwrite($fp, "---object created\n");
|
||||
#fclose($fp);
|
||||
}
|
||||
|
||||
return self::$DbConnection;
|
||||
#return $this->DbConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -13,10 +13,12 @@ class RecipesService extends BaseService
|
|||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->StockService = new StockService();
|
||||
}
|
||||
|
||||
protected $StockService;
|
||||
protected function getStockService()
|
||||
{
|
||||
return StockService::getInstance();
|
||||
}
|
||||
|
||||
public function GetRecipesPosResolved()
|
||||
{
|
||||
|
|
@ -72,7 +74,7 @@ class RecipesService extends BaseService
|
|||
{
|
||||
if ($recipePosition->only_check_single_unit_in_stock == 0)
|
||||
{
|
||||
$this->StockService->ConsumeProduct($recipePosition->product_id, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId);
|
||||
$this->getStockService()->ConsumeProduct($recipePosition->product_id, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,17 +4,6 @@ namespace Grocy\Services;
|
|||
|
||||
class SessionService extends BaseService
|
||||
{
|
||||
private static $instance = null;
|
||||
|
||||
public function getInstance()
|
||||
{
|
||||
if (self::$instance == null)
|
||||
{
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user