Skip to content

Commit

Permalink
[php] Webman update to PHP 8.2 (#7707)
Browse files Browse the repository at this point in the history
  • Loading branch information
joanhey authored Nov 16, 2022
1 parent e6eee12 commit dd09759
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 51 deletions.
35 changes: 11 additions & 24 deletions frameworks/PHP/webman/app/controller/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function json($request)

public function db($request)
{
$statement = Db::$statement;
$statement = Db::$random;
$statement->execute([\mt_rand(1, 10000)]);

return new Response(200, [
Expand All @@ -43,13 +43,13 @@ public function fortunes($request)

$fortune->execute();

$arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
$arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
$arr[0] = 'Additional fortune added at request time.';
\asort($arr);

$html = '';
foreach ($arr as $id => $message) {
$message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
$message = \htmlspecialchars($message, \ENT_QUOTES, 'UTF-8');
$html .= "<tr><td>$id</td><td>$message</td></tr>";
}

Expand All @@ -61,7 +61,7 @@ public function fortunes($request)

public function queries($request, $q = 1)
{
$statement = Db::$statement;
$statement = Db::$random;

$query_count = 1;
if ((int) $q > 1) {
Expand All @@ -83,41 +83,28 @@ public function queries($request, $q = 1)
public function updates($request, $q = 1)
{
$random = Db::$random;
$update = Db::$update;

$query_count = 1;
if ((int) $q > 1) {
$query_count = \min($q, 500);
}

$arr = [];
$worlds = [];

while ($query_count--) {
$id = \mt_rand(1, 10000);
$random->execute([$id]);

//$random->fetchColumn(); //
//$world = ['id' => $id, 'randomNumber' => \mt_rand(1, 10000)]; //
$random->execute([\mt_rand(1, 10000)]);
$world = $random->fetch();
$world['randomNumber'] = \mt_rand(1, 10000);

$world = ['id' => $id, 'randomNumber' => $random->fetchColumn()];
$update->execute(
[$world['randomNumber'] = mt_rand(1, 10000), $id]
);

$arr[] = $world;
$worlds[] = $world;
}

/*$pdo = Db::$pdo;
$pdo->beginTransaction();
foreach($arr as $world) {
$update->execute([$world['randomNumber'], $world['id']]);
}
$pdo->commit();*/
Db::update($worlds);

return new Response(200, [
'Content-Type' => 'application/json',
'Date' => Date::$date
], \json_encode($arr));
], \json_encode($worlds));
}


Expand Down
56 changes: 35 additions & 21 deletions frameworks/PHP/webman/support/bootstrap/db/Raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,17 @@
*/
class Raw implements Bootstrap
{
/**
* @var \PDO
*/
public static $pdo;

/**
* @var PDOStatement
*/
public static $statement;
public static PDO $pdo;

/**
* @var PDOStatement
*/
public static $fortune;
public static PDOStatement $fortune;

/**
* @var PDOStatement
*/
public static $random;
public static PDOStatement $random;

/**
* @var PDOStatement
* @var PDOStatement[]
*/
public static $update;
public static array $update;

/**
* @param Worker $worker
Expand All @@ -61,10 +48,37 @@ public static function start($worker)
[PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false]
);
self::$statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
self::$fortune = $pdo->prepare('SELECT id,message FROM Fortune');
self::$random = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
self::$update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
self::$fortune = $pdo->prepare('SELECT id,message FROM Fortune');
self::$pdo = $pdo;
}

/**
* Postgres bulk update
*
* @param array $worlds
* @return void
*/
public static function update(array $worlds)
{
$rows = count($worlds);

if (!isset(self::$update[$rows])) {
$sql = 'UPDATE world SET randomNumber = CASE id'
. str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $rows)
. 'END WHERE id IN ('
. str_repeat('?::INTEGER,', $rows - 1) . '?::INTEGER)';

self::$update[$rows] = self::$pdo->prepare($sql);
}

$val = [];
$keys = [];
foreach ($worlds as $world) {
$val[] = $keys[] = $world['id'];
$val[] = $world['randomNumber'];
}

self::$update[$rows]->execute([...$val, ...$keys]);
}
}
13 changes: 7 additions & 6 deletions frameworks/PHP/webman/webman.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ FROM ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
RUN apt-get update -yqq > /dev/null && \
apt-get install -yqq php8.1-cli php8.1-pgsql php8.1-xml > /dev/null
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \
apt-get update -yqq > /dev/null && apt-get upgrade -yqq > /dev/null

RUN apt-get install -yqq php8.2-cli php8.2-pgsql php8.2-xml > /dev/null

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

RUN apt-get update -yqq && apt-get install -y php-pear php8.1-dev libevent-dev git
RUN pecl install event-3.0.8 > /dev/null && echo "extension=event.so" > /etc/php/8.1/cli/conf.d/event.ini
RUN apt-get update -yqq && apt-get install -y php-pear php8.2-dev libevent-dev git > /dev/null
RUN pecl install event-3.0.8 > /dev/null && echo "extension=event.so" > /etc/php/8.2/cli/conf.d/event.ini

COPY php.ini /etc/php/8.1/cli/php.ini
COPY php.ini /etc/php/8.2/cli/php.ini

ADD ./ /webman
WORKDIR /webman
Expand Down

0 comments on commit dd09759

Please sign in to comment.