Skip to content

Commit 6d0cb9c

Browse files
authored
Merge pull request #362 from bowphp/refactor/code-base
Fix database, validation, add rabbitmq/kafka queue adapter
2 parents 6eb57d8 + a6563aa commit 6d0cb9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1839
-1301
lines changed

.github/workflows/tests.yml

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ jobs:
2525
- name: Checkout code
2626
uses: actions/checkout@v4
2727

28-
- name: Setup MySQL
29-
uses: mirromutth/mysql-action@v1.1
30-
with:
31-
host port: 3306
32-
container port: 3306
33-
character set server: 'utf8mb4'
34-
collation server: 'utf8mb4_general_ci'
35-
mysql version: '5.7'
36-
mysql database: 'test_db'
37-
mysql root password: 'password'
38-
3928
- name: Setup PHP
4029
uses: shivammathur/setup-php@v2
4130
with:
@@ -46,6 +35,30 @@ jobs:
4635
- name: Set Docker containers
4736
run: docker compose up -d
4837

38+
- name: Wait for MySQL to be ready
39+
run: |
40+
echo "Waiting for MySQL to be ready..."
41+
for i in {1..30}; do
42+
if docker exec bowphp_mysql mysqladmin ping -h localhost -u root -ppassword --silent 2>/dev/null; then
43+
echo "MySQL is ready!"
44+
break
45+
fi
46+
echo "Waiting for MySQL... ($i/30)"
47+
sleep 2
48+
done
49+
50+
- name: Wait for PostgreSQL to be ready
51+
run: |
52+
echo "Waiting for PostgreSQL to be ready..."
53+
for i in {1..30}; do
54+
if docker exec bowphp_postgres pg_isready -U postgres --silent 2>/dev/null; then
55+
echo "PostgreSQL is ready!"
56+
break
57+
fi
58+
echo "Waiting for PostgreSQL... ($i/30)"
59+
sleep 2
60+
done
61+
4962
- name: Cache Composer packages
5063
id: composer-cache
5164
uses: actions/cache@v4

docker-compose.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,41 @@ services:
162162
interval: 10s
163163
timeout: 5s
164164
retries: 5
165+
zookeeper:
166+
container_name: bowphp_zookeeper
167+
image: confluentinc/cp-zookeeper:7.5.0
168+
restart: unless-stopped
169+
ports:
170+
- "2181:2181"
171+
environment:
172+
ZOOKEEPER_CLIENT_PORT: 2181
173+
ZOOKEEPER_TICK_TIME: 2000
174+
networks:
175+
- bowphp_network
176+
healthcheck:
177+
test: ["CMD", "nc", "-z", "localhost", "2181"]
178+
interval: 10s
179+
timeout: 5s
180+
retries: 5
181+
kafka:
182+
container_name: bowphp_kafka
183+
image: confluentinc/cp-kafka:7.5.0
184+
restart: unless-stopped
185+
ports:
186+
- "9092:9092"
187+
environment:
188+
KAFKA_BROKER_ID: 1
189+
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
190+
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
191+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
192+
KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
193+
depends_on:
194+
- zookeeper
195+
networks:
196+
- bowphp_network
197+
healthcheck:
198+
test: ["CMD", "nc", "-z", "localhost", "9092"]
199+
interval: 15s
200+
timeout: 10s
201+
retries: 5
165202

src/Application/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function run(): bool
171171

172172
// We launch the search of the method that arrived in the query
173173
// then start checking the url of the request
174-
if (!$route->match($this->request->path())) {
174+
if (!$route->match($this->request->path(), $this->request->domain())) {
175175
continue;
176176
}
177177

src/Console/Command/WorkerCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class WorkerCommand extends AbstractCommand
1717
*/
1818
public function run(?string $connection = null): void
1919
{
20-
$tries = (int)$this->arg->getParameter('--tries', 3);
20+
$tries = (int) $this->arg->getParameter('--tries', 3);
2121
$default = $this->arg->getParameter('--queue', "default");
22-
$memory = (int)$this->arg->getParameter('--memory', 126);
23-
$timout = (int)$this->arg->getParameter('--timout', 60);
24-
$sleep = (int)$this->arg->getParameter('--sleep', 60);
22+
$memory = (int) $this->arg->getParameter('--memory', 126);
23+
$timout = (int) $this->arg->getParameter('--timout', 3);
24+
$sleep = (int) $this->arg->getParameter('--sleep', 60);
2525

2626
$queue = app("queue");
2727

src/Database/QueryBuilder.php

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -877,31 +877,39 @@ private function aggregate($aggregate, $column): mixed
877877
*/
878878
private function bind(PDOStatement $pdo_statement, array $bindings = []): void
879879
{
880-
foreach ($bindings as $key => $value) {
881-
if (is_null($value) || strtolower((string) $value) === 'null') {
882-
$key_binding = ':' . $key;
883-
$pdo_statement->bindValue($key_binding, $value, PDO::PARAM_NULL);
884-
unset($bindings[$key]);
880+
// Detect if the SQL uses positional or named placeholders
881+
$sql = $pdo_statement->queryString;
882+
$uses_named = strpos($sql, ':') !== false;
883+
884+
if ($uses_named) {
885+
// Named placeholders
886+
foreach ($bindings as $key => $value) {
887+
$param = PDO::PARAM_STR;
888+
if (is_null($value) || strtolower((string) $value) === 'null') {
889+
$param = PDO::PARAM_NULL;
890+
} elseif (is_int($value)) {
891+
$param = PDO::PARAM_INT;
892+
} elseif (is_resource($value)) {
893+
$param = PDO::PARAM_LOB;
894+
}
895+
$key_binding = is_string($key) ? ":$key" : $key + 1;
896+
$pdo_statement->bindValue($key_binding, $value, $param);
885897
}
886-
}
887-
888-
foreach ($bindings as $key => $value) {
889-
$param = PDO::PARAM_STR;
890-
891-
if (is_int($value)) {
892-
$value = (int) $value;
893-
$param = PDO::PARAM_INT;
894-
} elseif (is_float($value)) {
895-
$value = (float) $value;
896-
} elseif (is_double($value)) {
897-
$value = (float) $value;
898-
} elseif (is_resource($value)) {
899-
$param = PDO::PARAM_LOB;
898+
} else {
899+
// Positional placeholders
900+
$i = 1;
901+
foreach ($bindings as $value) {
902+
$param = PDO::PARAM_STR;
903+
if (is_null($value) || strtolower((string) $value) === 'null') {
904+
$param = PDO::PARAM_NULL;
905+
} elseif (is_int($value)) {
906+
$param = PDO::PARAM_INT;
907+
} elseif (is_resource($value)) {
908+
$param = PDO::PARAM_LOB;
909+
}
910+
$pdo_statement->bindValue($i, $value, $param);
911+
$i++;
900912
}
901-
902-
// Bind by value with native pdo statement object
903-
$key_binding = is_string($key) ? ":" . $key : $key + 1;
904-
$pdo_statement->bindValue($key_binding, $value, $param);
905913
}
906914
}
907915

src/Event/EventQueueTask.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public function __construct(
1818
private EventListener|EventShouldQueue $event,
1919
private mixed $payload = null,
2020
) {
21-
parent::__construct();
2221
}
2322

2423
/**

src/Http/Request.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ public function hostname(): string
278278
return $_SERVER['HTTP_HOST'];
279279
}
280280

281+
/**
282+
* Get the domain of the server.
283+
*
284+
* @return string
285+
*/
286+
public function domain(): string
287+
{
288+
$part = explode(':', $this->hostname() ?? '');
289+
290+
return $part[0] ?? 'unknown';
291+
}
292+
281293
/**
282294
* Get uri send by client.
283295
*
@@ -356,15 +368,13 @@ public function file(string $key): UploadedFile|Collection|null
356368
$collect = [];
357369

358370
foreach ($files['name'] as $key => $name) {
359-
$collect[] = new UploadedFile(
360-
[
371+
$collect[] = new UploadedFile([
361372
'name' => $name,
362373
'type' => $files['type'][$key],
363374
'size' => $files['size'][$key],
364375
'error' => $files['error'][$key],
365376
'tmp_name' => $files['tmp_name'][$key],
366-
]
367-
);
377+
]);
368378
}
369379

370380
return new Collection($collect);
@@ -417,11 +427,7 @@ public function isAjax(): bool
417427

418428
$content_type = $this->getHeader("content-type");
419429

420-
if ($content_type && str_contains($content_type, "application/json")) {
421-
return true;
422-
}
423-
424-
return false;
430+
return $content_type && str_contains($content_type, "application/json");
425431
}
426432

427433
public function wantsJson(): bool

src/Mail/MailQueueTask.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function __construct(
2727
array $data,
2828
Envelop $envelop
2929
) {
30-
parent::__construct();
31-
3230
$this->bags = [
3331
"view" => $view,
3432
"data" => $data,

src/Notifier/NotifierQueueTask.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public function __construct(
2525
Model $context,
2626
Notifier $notifier,
2727
) {
28-
parent::__construct();
29-
3028
$this->bags = [
3129
"notifier" => $notifier,
3230
"context" => $context,

0 commit comments

Comments
 (0)