Skip to content

Commit c71a556

Browse files
committed
BaseModel no more containerinterface parameter needed in static methods
1 parent 4d1235b commit c71a556

File tree

7 files changed

+72
-51
lines changed

7 files changed

+72
-51
lines changed

app/App.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class App extends ContainerAwareObject
8080
*/
8181
protected $blocked_ips = [];
8282

83+
public static ?App $instance = null;
84+
8385
/**
8486
* class constructor
8587
*/
@@ -169,6 +171,8 @@ public function __construct() {
169171
$debugbar = $this->getDebugbar();
170172
$debugbar['time']->stopMeasure('app_construct');
171173
}
174+
175+
App::$instance = $this;
172176
} catch (Exception $e) {
173177
$response = new Response(
174178
'Critical: ' . $e->getMessage(),
@@ -459,4 +463,14 @@ public function __call(string $name, mixed $arguments): mixed
459463

460464
throw new InvalidValueException("Method \"{$name}\" not found in class\"" . get_class($this) . "\"!", 1);
461465
}
466+
467+
/**
468+
* get current app instance
469+
*
470+
* @return App
471+
*/
472+
public static function getInstance() : ?App
473+
{
474+
return App::$instance;
475+
}
462476
}

app/base/abstracts/Models/BaseModel.php

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace App\Base\Abstracts\Models;
1515

16+
use App\App;
1617
use ArrayAccess;
1718
use DebugBar\DebugBar;
1819
use Degami\Basics\Exceptions\BasicException;
@@ -134,14 +135,14 @@ private function checkDbName(Row $db_row): BaseModel
134135
/**
135136
* returns an array of models, starting from a statement Result
136137
*
137-
* @param ContainerInterface $container
138138
* @param Result $stmt
139139
* @return array
140140
* @throws DependencyException
141141
* @throws NotFoundException
142142
*/
143-
public static function hydrateStatementResult(ContainerInterface $container, Result $stmt): array
143+
public static function hydrateStatementResult(Result $stmt): array
144144
{
145+
$container = App::getInstance()->getContainer();
145146
return array_map(
146147
function ($el) use ($container) {
147148
return $container->make(static::class, ['db_row' => $el]);
@@ -153,25 +154,25 @@ function ($el) use ($container) {
153154
/**
154155
* basic select statement
155156
*
156-
* @param ContainerInterface $container
157157
* @param array $options
158158
* @return PDOStatement
159159
*/
160-
public static function select(ContainerInterface $container, $options = []): PDOStatement
160+
public static function select($options = []): PDOStatement
161161
{
162+
$container = App::getInstance()->getContainer();
162163
return $container->get('db')->select(static::defaultTableName(), $options);
163164
}
164165

165166
/**
166167
* gets basic where statement for model
167168
*
168-
* @param ContainerInterface $container
169169
* @param array $condition
170170
* @param array $order
171171
* @return Result
172172
*/
173-
protected static function getModelBasicWhere(ContainerInterface $container, $condition = [], $order = []): Result
173+
protected static function getModelBasicWhere($condition = [], $order = []): Result
174174
{
175+
$container = App::getInstance()->getContainer();
175176
if ($condition == null) {
176177
$condition = [];
177178
}
@@ -228,15 +229,16 @@ protected static function getModelBasicWhere(ContainerInterface $container, $con
228229
/**
229230
* returns all found items
230231
*
231-
* @param ContainerInterface $container
232232
* @param array $condition
233233
* @param array $order
234234
* @return array
235235
* @throws DependencyException
236236
* @throws NotFoundException
237237
*/
238-
public static function all(ContainerInterface $container, $condition = [], $order = []): array
238+
public static function all($condition = [], $order = []): array
239239
{
240+
$container = App::getInstance()->getContainer();
241+
240242
/** @var DebugBar $debugbar */
241243
$debugbar = $container->get('debugbar');
242244

@@ -246,7 +248,7 @@ public static function all(ContainerInterface $container, $condition = [], $orde
246248
$debugbar['time']->startMeasure($measure_key);
247249
}
248250

249-
$items = static::hydrateStatementResult($container, static::getModelBasicWhere($container, $condition, $order));
251+
$items = static::hydrateStatementResult(static::getModelBasicWhere($condition, $order));
250252

251253
foreach ($items as $item) {
252254
static::$loadedObjects[static::defaultTableName()][$item->id] = $item;
@@ -262,21 +264,19 @@ public static function all(ContainerInterface $container, $condition = [], $orde
262264
/**
263265
* gets total number of elements
264266
*
265-
* @param ContainerInterface $container
266267
* @param array $condition
267268
* @return int
268269
*/
269-
public static function totalNum(ContainerInterface $container, $condition = []): int
270+
public static function totalNum($condition = []): int
270271
{
271-
$stmt = static::getModelBasicWhere($container, $condition);
272+
$stmt = static::getModelBasicWhere($condition);
272273

273274
return $stmt->count();
274275
}
275276

276277
/**
277278
* return subset of found items (useful for paginate)
278279
*
279-
* @param ContainerInterface $container
280280
* @param Request $request
281281
* @param int $page_size
282282
* @param array $condition
@@ -285,8 +285,10 @@ public static function totalNum(ContainerInterface $container, $condition = []):
285285
* @throws DependencyException
286286
* @throws NotFoundException
287287
*/
288-
public static function paginate(ContainerInterface $container, Request $request, $page_size = self::ITEMS_PER_PAGE, $condition = [], $order = []): array
288+
public static function paginate(Request $request, $page_size = self::ITEMS_PER_PAGE, $condition = [], $order = []): array
289289
{
290+
$container = App::getInstance()->getContainer();
291+
290292
/** @var DebugBar $debugbar */
291293
$debugbar = $container->get('debugbar');
292294

@@ -300,8 +302,8 @@ public static function paginate(ContainerInterface $container, Request $request,
300302
$condition = [];
301303
}
302304

303-
$stmt = static::getModelBasicWhere($container, $condition, $order);
304-
$out = static::paginateByStatement($container, $request, $stmt, $page_size);
305+
$stmt = static::getModelBasicWhere($condition, $order);
306+
$out = static::paginateByStatement($request, $stmt, $page_size);
305307

306308
if (getenv('DEBUG')) {
307309
$debugbar['time']->startMeasure($measure_key);
@@ -313,22 +315,21 @@ public static function paginate(ContainerInterface $container, Request $request,
313315
/**
314316
* return subset of found items (useful for paginate)
315317
*
316-
* @param ContainerInterface $container
317318
* @param Request $request
318319
* @param Result $stmt
319320
* @param int $page_size
320321
* @return array
321322
* @throws DependencyException
322323
* @throws NotFoundException
323324
*/
324-
public static function paginateByStatement(ContainerInterface $container, Request $request, Result $stmt, $page_size = self::ITEMS_PER_PAGE): array
325+
public static function paginateByStatement(Request $request, Result $stmt, $page_size = self::ITEMS_PER_PAGE): array
325326
{
326327
$page = $request->get('page') ?? 0;
327328
$start = (int)$page * $page_size;
328329

329330
$total = (clone $stmt)->count();
330331

331-
$items = static::hydrateStatementResult($container, $stmt->limit($page_size, $start));
332+
$items = static::hydrateStatementResult($stmt->limit($page_size, $start));
332333

333334
foreach ($items as $item) {
334335
static::$loadedObjects[static::defaultTableName()][$item->id] = $item;
@@ -340,15 +341,16 @@ public static function paginateByStatement(ContainerInterface $container, Reques
340341
/**
341342
* finds elements
342343
*
343-
* @param ContainerInterface $container
344344
* @param array|string $condition
345345
* @param array $order
346346
* @return array
347347
* @throws DependencyException
348348
* @throws NotFoundException
349349
*/
350-
public static function where(ContainerInterface $container, $condition, $order = []): array
350+
public static function where($condition, $order = []): array
351351
{
352+
$container = App::getInstance()->getContainer();
353+
352354
/** @var DebugBar $debugbar */
353355
$debugbar = $container->get('debugbar');
354356

@@ -358,7 +360,7 @@ public static function where(ContainerInterface $container, $condition, $order =
358360
$debugbar['time']->startMeasure($measure_key);
359361
}
360362

361-
$items = static::hydrateStatementResult($container, static::getModelBasicWhere($container, $condition, $order));
363+
$items = static::hydrateStatementResult(static::getModelBasicWhere($condition, $order));
362364

363365
foreach ($items as $item) {
364366
static::$loadedObjects[static::defaultTableName()][$item->id] = $item;
@@ -464,13 +466,14 @@ public function reset(): BaseModel
464466
/**
465467
* loads model by id
466468
*
467-
* @param ContainerInterface $container
468469
* @param int $id
469470
* @param bool $reset
470471
* @return self
471472
*/
472-
public static function load(ContainerInterface $container, $id, $reset = false): BaseModel
473+
public static function load($id, $reset = false): BaseModel
473474
{
475+
$container = App::getInstance()->getContainer();
476+
474477
/** @var DebugBar $debugbar */
475478
$debugbar = $container->get('debugbar');
476479

@@ -498,15 +501,16 @@ public static function load(ContainerInterface $container, $id, $reset = false):
498501
/**
499502
* loads multiple models by id
500503
*
501-
* @param ContainerInterface $container
502504
* @param array $ids
503505
* @param bool $reset
504506
* @return array
505507
* @throws DependencyException
506508
* @throws NotFoundException
507509
*/
508-
public static function loadMultiple(ContainerInterface $container, array $ids, bool $reset = false): array
510+
public static function loadMultiple(array $ids, bool $reset = false): array
509511
{
512+
$container = App::getInstance()->getContainer();
513+
510514
/** @var DebugBar $debugbar */
511515
$debugbar = $container->get('debugbar');
512516

@@ -532,7 +536,7 @@ public static function loadMultiple(ContainerInterface $container, array $ids, b
532536

533537
$already_loaded = array_filter($already_loaded);
534538

535-
$out = (!empty($ids) ? static::loadMultipleByCondition($container, ['id' => $ids], $reset) : []) +
539+
$out = (!empty($ids) ? static::loadMultipleByCondition(['id' => $ids], $reset) : []) +
536540
(!empty($already_loaded) ? array_intersect_key(static::$loadedObjects[static::defaultTableName()], array_flip($already_loaded)) : []);
537541

538542
if (getenv('DEBUG')) {
@@ -545,15 +549,16 @@ public static function loadMultiple(ContainerInterface $container, array $ids, b
545549
/**
546550
* loads model by condition
547551
*
548-
* @param ContainerInterface $container
549552
* @param array $condition
550553
* @return self|null
551554
* @throws BasicException
552555
* @throws DependencyException
553556
* @throws NotFoundException
554557
*/
555-
public static function loadByCondition(ContainerInterface $container, array $condition): ?BaseModel
558+
public static function loadByCondition(array $condition): ?BaseModel
556559
{
560+
$container = App::getInstance()->getContainer();
561+
557562
/** @var DebugBar $debugbar */
558563
$debugbar = $container->get('debugbar');
559564

@@ -563,7 +568,7 @@ public static function loadByCondition(ContainerInterface $container, array $con
563568
$debugbar['time']->startMeasure($measure_key);
564569
}
565570

566-
$stmt = static::getModelBasicWhere($container, $condition);
571+
$stmt = static::getModelBasicWhere($condition);
567572
$db_row = $stmt->limit(1)->fetch();
568573
if (!$db_row || !$db_row->id) {
569574
throw new BasicException('Model not found');
@@ -581,15 +586,16 @@ public static function loadByCondition(ContainerInterface $container, array $con
581586
/**
582587
* loads multiple models by condition
583588
*
584-
* @param ContainerInterface $container
585589
* @param array $condition
586590
* @param bool $reset
587591
* @return array
588592
* @throws DependencyException
589593
* @throws NotFoundException
590594
*/
591-
public static function loadMultipleByCondition(ContainerInterface $container, array $condition, bool $reset = false): array
595+
public static function loadMultipleByCondition(array $condition, bool $reset = false): array
592596
{
597+
$container = App::getInstance()->getContainer();
598+
593599
/** @var DebugBar $debugbar */
594600
$debugbar = $container->get('debugbar');
595601

@@ -600,7 +606,7 @@ public static function loadMultipleByCondition(ContainerInterface $container, ar
600606
}
601607

602608
$ids = [];
603-
$stmt = static::getModelBasicWhere($container, $condition);
609+
$stmt = static::getModelBasicWhere($condition);
604610
foreach ($stmt->fetchAll() as $db_row) {
605611
$ids[] = intval($db_row->id);
606612
/** @var Result $db_row */
@@ -620,14 +626,15 @@ public static function loadMultipleByCondition(ContainerInterface $container, ar
620626
/**
621627
* gets new empty model
622628
*
623-
* @param ContainerInterface $container
624629
* @param array $initial_data
625630
* @return static
626631
* @throws InvalidValueException
627632
* @throws BasicException
628633
*/
629-
public static function new(ContainerInterface $container, $initial_data = []): BaseModel
634+
public static function new($initial_data = []): BaseModel
630635
{
636+
$container = App::getInstance()->getContainer();
637+
631638
$db_row = $container->get('db')->createRow(static::defaultTableName());
632639
$db_row->setData($initial_data);
633640
return new static($container, $db_row);
@@ -636,17 +643,16 @@ public static function new(ContainerInterface $container, $initial_data = []): B
636643
/**
637644
* loads model by field - value pair
638645
*
639-
* @param ContainerInterface $container
640646
* @param string $field
641647
* @param mixed $value
642648
* @return self
643649
* @throws BasicException
644650
* @throws DependencyException
645651
* @throws NotFoundException
646652
*/
647-
public static function loadBy(ContainerInterface $container, string $field, $value): BaseModel
653+
public static function loadBy(string $field, $value): BaseModel
648654
{
649-
return static::loadByCondition($container, [$field => $value]);
655+
return static::loadByCondition([$field => $value]);
650656
}
651657

652658
/**

app/base/abstracts/Routing/BaseRouter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ public function getBaseUrl(): ?string
315315
$_SERVER['REQUEST_URI']
316316
)
317317
);
318+
318319
if ($parsed) {
319320
return $parsed['scheme'] . '://' . $parsed['host'];
320321
}

app/site/controllers/Admin/Permissions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function loadRolePermission(Role $role_model, Permission $permission_mod
208208
*/
209209
private function addPermission(Role $role_model, Permission $permission_model) : void
210210
{
211-
$pivot_model = RolePermission::new($this->getContainer());
211+
$pivot_model = RolePermission::new();
212212
$pivot_model->setPermissionId($permission_model->getId());
213213
$pivot_model->setRoleId($role_model->getId());
214214
$pivot_model->persist();

app/site/controllers/Admin/TwoFa.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed
248248
protected function getSecret(User $user) : string
249249
{
250250
/** @var User2Fa $user2Fa */
251-
$user2Fa = current(User2Fa::where($this->getContainer(), ['user_id' => $user->getId(), 'website_id' => null]));
251+
$user2Fa = current(User2Fa::where(['user_id' => $user->getId(), 'website_id' => null]));
252252

253253
if (!$user2Fa) {
254-
$user2Fa = User2Fa::new($this->getContainer());
254+
$user2Fa = User2Fa::new();
255255
$user2Fa->setUserId($user->getId());
256256
$user2Fa->setWebsiteId(null);
257257
}

app/site/controllers/Frontend/Users/TwoFa.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed
156156
protected function getSecret(User $user) : string
157157
{
158158
/** @var User2Fa $user2Fa */
159-
$user2Fa = current(User2Fa::where($this->getContainer(), ['user_id' => $user->getId(), 'website_id' => $this->getCurrentWebsiteId()]));
159+
$user2Fa = current(User2Fa::where(['user_id' => $user->getId(), 'website_id' => $this->getCurrentWebsiteId()]));
160160

161161
if (!$user2Fa) {
162-
$user2Fa = User2Fa::new($this->getContainer());
162+
$user2Fa = User2Fa::new();
163163
$user2Fa->setUserId($user->getId());
164164
$user2Fa->setWebsiteId($this->getCurrentWebsiteId());
165165
}

0 commit comments

Comments
 (0)