Skip to content

Commit 0c00b43

Browse files
committed
added composer patches support, updated collections and base models, fixes
1 parent c7ca6e0 commit 0c00b43

File tree

7 files changed

+107
-20
lines changed

7 files changed

+107
-20
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.phpdoc
33
composer.lock
44
package-lock.json
5+
patches.lock.json
56
docs
67
vendor
78
node_modules/
@@ -45,4 +46,4 @@ assets/rsa_private.key
4546
assets/rsa_private.csr
4647
assets/rsa_private.pem
4748
docker/database/data
48-
docker/elasticsearch/data
49+
docker/elasticsearch/data

app/base/abstracts/Models/BaseCollection.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ class BaseCollection extends ContainerAwareObject implements ArrayAccess, Iterat
4949
*/
5050
protected ?Result $stmt = null;
5151

52-
/**
53-
* @var array objects cache
54-
*/
55-
protected static array $loadedObjects = [];
56-
5752

5853
public function __construct(
5954
protected ContainerInterface $container,
@@ -153,7 +148,7 @@ public function addOrder($order = []) : static
153148
}
154149
}
155150
} else {
156-
$this->stmt = $this->getSelect()->orderBy('id');
151+
$this->stmt = $this->getSelect()->orderBy($this->getContainer()->call([$this->className, 'getKeyField']));
157152
}
158153

159154
return $this;
@@ -215,7 +210,6 @@ public function load() : static
215210
$this->items = [];
216211
foreach($this->getContainer()->call([$this->className, 'hydrateStatementResult'], ['stmt' => $this->getSelect()]) as $item) {
217212
$this->items[$item->getId()] = $item;
218-
static::$loadedObjects[$this->getTableName()][$item->getId()] = $item;
219213
}
220214

221215
if (getenv('DEBUG')) {
@@ -246,7 +240,6 @@ public function getFirst()
246240
return null;
247241
}
248242

249-
static::$loadedObjects[$this->getTableName()][$item->getId()] = $item;
250243
return $item;
251244
}
252245

@@ -408,7 +401,6 @@ public function paginate(Request $request, $page_size = self::ITEMS_PER_PAGE): a
408401
$this->items = [];
409402
foreach ($this->getContainer()->call([$this->className, 'hydrateStatementResult'], ['stmt' => $this->getSelect()->limit($page_size, $start)]) as $item) {
410403
$this->items[$item->getId()] = $item;
411-
static::$loadedObjects[$this->getTableName()][$item->getId()] = $item;
412404
}
413405

414406
$out = ['items' => $this->getItems(), 'page' => $page, 'total' => $total];

app/base/abstracts/Models/BaseModel.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use App\Base\Abstracts\ContainerAwareObject;
3030
use App\Base\Exceptions\InvalidValueException;
3131
use Exception;
32+
use League\Plates\Template\Func;
3233

3334
/**
3435
* A wrapper for LessQL Row
@@ -61,6 +62,8 @@ abstract class BaseModel extends ContainerAwareObject implements ArrayAccess, It
6162
*/
6263
protected static array $loadedObjects = [];
6364

65+
protected static string|array $keyField = 'id';
66+
6467
/**
6568
* {@inheritdocs}
6669
*
@@ -224,7 +227,7 @@ protected static function getModelBasicWhere($condition = [], $order = []): Resu
224227
}
225228
}
226229
} else {
227-
$stmt = $stmt->orderBy('id');
230+
$stmt = $stmt->orderBy(static::getKeyField());
228231
}
229232

230233
return $stmt;
@@ -241,6 +244,33 @@ public static function defaultTableName(): string
241244
return strtolower(static::pascalCaseToSnakeCase(array_pop($path)));
242245
}
243246

247+
public static Function getKeyField() : string|array
248+
{
249+
return static::$keyField;
250+
}
251+
252+
protected static function loadedObjectsIdentifier($id)
253+
{
254+
if (is_array(static::getKeyField())) {
255+
return implode("|", array_map(function($column, $value) {
256+
return $column.':'.$value;
257+
}, static::getKeyField(), $id));
258+
}
259+
260+
return static::getKeyField().':'.$id;
261+
}
262+
263+
public function getKeyFieldValue() : mixed
264+
{
265+
if (!is_array(static::getKeyField())) {
266+
return $this->getData(static::getKeyField());
267+
}
268+
269+
return array_combine(static::getKeyField(), array_map(function($column) {
270+
return $this->getData($column);
271+
}, static::getKeyField()));
272+
}
273+
244274
/**
245275
* fills empty model with data
246276
*
@@ -340,14 +370,21 @@ public static function load($id, $reset = false): BaseModel
340370
$debugbar['time']->startMeasure($measure_key);
341371
}
342372

343-
if (isset(static::$loadedObjects[static::defaultTableName()][$id]) && !$reset) {
373+
if (isset(static::$loadedObjects[static::defaultTableName()][static::loadedObjectsIdentifier($id)]) && !$reset) {
344374
if (getenv('DEBUG')) {
345375
$debugbar['time']->stopMeasure($measure_key);
346376
}
347-
return static::$loadedObjects[static::defaultTableName()][$id];
377+
return static::$loadedObjects[static::defaultTableName()][static::loadedObjectsIdentifier($id)];
378+
}
379+
380+
$keyField = static::getKeyField();
381+
if (is_array($keyField)) {
382+
$condition = array_combine($keyField, $id);
383+
} else {
384+
$condition = [$keyField => $id];
348385
}
349386

350-
$object = $container->call([static::class, 'loadByCondition'], ['condition' => ['id' => $id]]);
387+
$object = $container->call([static::class, 'loadByCondition'], ['condition' => $condition]);
351388

352389
if (getenv('DEBUG')) {
353390
$debugbar['time']->stopMeasure($measure_key);
@@ -383,13 +420,13 @@ public static function loadByCondition(array $condition): ?BaseModel
383420
throw new BasicException('Model not found');
384421
}
385422

386-
static::$loadedObjects[static::defaultTableName()][$db_row->id] = $container->make(static::class, ['db_row' => $db_row]);
423+
static::$loadedObjects[static::defaultTableName()][static::loadedObjectsIdentifier($db_row->id)] = $container->make(static::class, ['db_row' => $db_row]);
387424

388425
if (getenv('DEBUG')) {
389426
$debugbar['time']->stopMeasure($measure_key);
390427
}
391428

392-
return static::$loadedObjects[static::defaultTableName()][$db_row->id];
429+
return static::$loadedObjects[static::defaultTableName()][static::loadedObjectsIdentifier($db_row->id)];
393430
}
394431

395432
/**

app/base/tools/Plates/SiteBase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class SiteBase implements ExtensionInterface
3939
*/
4040
protected Engine $engine;
4141

42+
/**
43+
* @var Website|null current website cached element
44+
*/
45+
protected static $currentWebsite = null;
46+
4247
/**
4348
* constructor
4449
*
@@ -80,7 +85,10 @@ public function getObject(): SiteBase
8085
*/
8186
public function getCurrentWebsite(): ?Website
8287
{
83-
return $this->getSiteData()->getCurrentWebsite();
88+
if (is_null(static::$currentWebsite)) {
89+
static::$currentWebsite = $this->getSiteData()->getCurrentWebsite();
90+
}
91+
return static::$currentWebsite;
8492
}
8593

8694
/**

app/base/tools/Utils/SiteData.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,22 @@ public function currentServerName(): ?string
6363
* @throws NotFoundException
6464
*/
6565
public function getCurrentWebsite(): ?Website
66-
{
66+
{
6767
if ($this->getAppWebsite() instanceof Website && $this->getAppWebsite()->isLoaded()) {
6868
return $this->getAppWebsite();
6969
}
7070

71+
$container = $this->getContainer();
72+
73+
/** @var DebugBar $debugbar */
74+
$debugbar = $container->get('debugbar');
75+
76+
$measure_key = 'SiteData getCurrentWebsite';
77+
78+
if (getenv('DEBUG')) {
79+
$debugbar['time']->startMeasure($measure_key);
80+
}
81+
7182
$website = null;
7283
if (php_sapi_name() == 'cli-server' || php_sapi_name() == 'cli') {
7384
$website = $this->getContainer()->call([Website::class, 'load'], ['id' => getenv('website_id')]);
@@ -78,6 +89,10 @@ public function getCurrentWebsite(): ?Website
7889
$website = $this->getContainer()->make(Website::class, ['db_row' => $db_row]);
7990
}
8091

92+
if (getenv('DEBUG')) {
93+
$debugbar['time']->stopMeasure($measure_key);
94+
}
95+
8196
if ($website instanceof Website) {
8297
// register into container
8398
$this->getContainer()->set(Website::class, $website);

composer.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"spatie/array-to-xml": "^2.11",
4444
"elasticsearch/elasticsearch": "~7.0",
4545
"mathiasverraes/classfunctions": "^1.1@dev",
46-
"phpgangsta/googleauthenticator": "dev-master"
46+
"phpgangsta/googleauthenticator": "dev-master",
47+
"cweagans/composer-patches": "dev-main"
4748
},
4849
"autoload": {
4950
"files": [
@@ -72,5 +73,18 @@
7273
},
7374
"require-dev": {
7475
"maximebf/debugbar": "^1.15"
75-
}
76+
},
77+
"config": {
78+
"allow-plugins": {
79+
"cweagans/composer-patches": true
80+
}
81+
},
82+
"extra": {
83+
"composer-exit-on-patch-failure": true,
84+
"patches": {
85+
"maximebf/debugbar": {
86+
"fixes sql queries not shown due to js errors": "patches/debugbar_sql.patch"
87+
}
88+
}
89+
}
7690
}

patches/debugbar_sql.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--- a/src/DebugBar/Resources/widgets/sqlqueries/widget.js 2024-04-13 14:49:54.902665978 +0200
2+
+++ b/src/DebugBar/Resources/widgets/sqlqueries/widget.js 2024-04-13 14:50:43.998929879 +0200
3+
@@ -70,10 +70,14 @@
4+
var filters = [], self = this;
5+
6+
this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, stmt) {
7+
- if (stmt.type === 'transaction') {
8+
+ try {
9+
+ if (stmt.type === 'transaction') {
10+
+ $('<strong />').addClass(csscls('sql')).addClass(csscls('name')).text(stmt.sql).appendTo(li);
11+
+ } else {
12+
+ $('<code />').addClass(csscls('sql')).html(PhpDebugBar.Widgets.highlight(stmt.sql, 'sql')).appendTo(li);
13+
+ }
14+
+ } catch (Exception) {
15+
$('<strong />').addClass(csscls('sql')).addClass(csscls('name')).text(stmt.sql).appendTo(li);
16+
- } else {
17+
- $('<code />').addClass(csscls('sql')).html(PhpDebugBar.Widgets.highlight(stmt.sql, 'sql')).appendTo(li);
18+
}
19+
if (stmt.width_percent) {
20+
$('<div />').addClass(csscls('bg-measure')).append(

0 commit comments

Comments
 (0)