Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Phalcon installation and code base update #6443

Merged
merged 6 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions frameworks/PHP/phalcon/app/collections/FortunesCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Phalcon\Incubator\MongoDB\Mvc\Collection;

class FortunesCollection extends Collection
{
public $message;

public function onConstruct(): void
{
$this->setSource('fortune');
$this->useImplicitObjectIds(false);
}
}
14 changes: 14 additions & 0 deletions frameworks/PHP/phalcon/app/collections/WorldsCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Phalcon\Incubator\MongoDB\Mvc\Collection;

class WorldsCollection extends Collection
{
public $randomNumber;

public function onConstruct(): void
{
$this->setSource('world');
$this->useImplicitObjectIds(false);
}
}
19 changes: 11 additions & 8 deletions frameworks/PHP/phalcon/app/config/config.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<?php

return new \Phalcon\Config(array(
'database' => array(
use Phalcon\Config;

return new Config([
'database' => [
'adapter' => 'Mysql',
'host' => 'tfb-database',
'username' => 'benchmarkdbuser',
'password' => 'benchmarkdbpass',
'name' => 'hello_world',
'persistent' => true,
),
'mongodb' => array(
],
'mongodb' => [
'url' => 'mongodb://tfb-database:27017',
'db' => 'hello_world'
),
'application' => array(
],
'application' => [
'controllersDir' => APP_PATH . '/app/controllers/',
'modelsDir' => APP_PATH . '/app/models/',
'collectionsDir' => APP_PATH . '/app/collections/',
'viewsDir' => APP_PATH . '/app/views/',
'baseUri' => '/',
)
));
]
]);
46 changes: 24 additions & 22 deletions frameworks/PHP/phalcon/app/config/routes.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
<?php

$router = new Phalcon\Mvc\Router(false);
use Phalcon\Mvc\Router;

$router->add('/json', array(
$router = new Router(false);

$router->addGet('/json', [
'controller' => 'bench',
'action' => 'json',
));
]);

$router->add('/db', array(
$router->addGet('/db', [
'controller' => 'bench',
'action' => 'db',
));
]);

$router->add('/queries', array(
$router->addGet('/queries', [
'controller' => 'bench',
'action' => 'queries',
));
]);

$router->add('/fortunes', array(
$router->addGet('/fortunes', [
'controller' => 'bench',
'action' => 'fortunes',
));
]);

$router->add('/update', array(
$router->addGet('/update', [
'controller' => 'bench',
'action' => 'update',
));
]);

$router->add('/plaintext', array(
$router->addGet('/plaintext', [
'controller' => 'bench',
'action' => 'plaintext',
));
]);

$router->add('/mongodb/db', array(
'controller' => 'mongobench',
$router->addGet('/mongodb/db', [
'controller' => 'mongo',
'action' => 'db',
));
]);

$router->add('/mongodb/queries', array(
'controller' => 'mongobench',
$router->addGet('/mongodb/queries', [
'controller' => 'mongo',
'action' => 'queries',
));
]);

$router->add('/mongodb/fortunes', array(
'controller' => 'mongobench',
$router->addGet('/mongodb/fortunes', [
'controller' => 'mongo',
'action' => 'fortunes',
));
]);

return $router;
58 changes: 28 additions & 30 deletions frameworks/PHP/phalcon/app/controllers/BenchController.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
<?php

use Phalcon\Mvc\View,
Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Http\ResponseInterface;

class BenchController extends \Phalcon\Mvc\Controller
class BenchController extends Controller
{

public function initialize()
public function initialize(): void
{
// views must be renderd explicitly. safes processing time when not needed.
// views must be rendered explicitly. safes processing time when not needed.
$this->view->setRenderLevel(View::LEVEL_LAYOUT);
}

public function jsonAction()
public function jsonAction(): ResponseInterface
{
return $this->response->setJsonContent(array(
return $this->response->setJsonContent([
'message' => 'Hello, World!'
));
]);
}

public function dbAction()
public function dbAction(): ResponseInterface
{
return $this->response->setJsonContent($this->getRandomWorld());
}

public function queriesAction()
public function queriesAction(): ResponseInterface
{

$queries = min(500, max(1, $this->filter->sanitize($this->request->getQuery('queries', null, 1), "int")));

$worlds = array();
$queries = min(500, max(1, $this->request->getQuery('queries', "int", 1)));
$worlds = [];

for ($i = 0; $i < $queries; ++$i) {
$worlds[] = $this->getRandomWorld();
Expand All @@ -38,9 +37,8 @@ public function queriesAction()
return $this->response->setJsonContent($worlds);
}

public function fortunesAction()
public function fortunesAction(): void
{

$fortunes = $this->getFortunesArray();
$fortunes[] = $this->buildFortune();

Expand All @@ -49,13 +47,12 @@ public function fortunesAction()
$this->view->fortunes = $this->sortFortunes($fortunes);
}

public function updateAction()
public function updateAction(): ResponseInterface
{

$queries = $this->request->getQuery('queries', null, 1);
$queries = $this->request->getQuery('queries', "int", 1);
$queries = max(1, min(500, $queries));

$worlds = array();
$worlds = [];

for ($i = 0; $i < $queries; ++$i) {
$world = $this->getRandomWorld();
Expand All @@ -67,11 +64,12 @@ public function updateAction()
return $this->response->setJsonContent($worlds);
}

public function plaintextAction()
public function plaintextAction(): ResponseInterface
{
$this->view->disable();
$this->response->setContentType('text/plain');
$this->response->setContent("Hello, World!");

return $this->response;
}

Expand All @@ -80,7 +78,7 @@ protected function getRandomWorld()
return Worlds::findFirst(mt_rand(1, 10000));
}

protected function getFortunesArray()
protected function getFortunesArray(): array
{
// since the resultset is immutable get an array instead
// so we can add the new fortune
Expand All @@ -89,18 +87,18 @@ protected function getFortunesArray()

protected function buildFortune()
{
return array(
return [
'id' => 0,
'message' => 'Additional fortune added at request time.'
);
];
}

protected function sortFortunes($fortunes)
protected function sortFortunes(array $fortunes): array
{
usort($fortunes,
function($left, $right) {
return $left['message'] <=> $right['message'];
});
usort($fortunes, function ($left, $right) {
return $left['message'] <=> $right['message'];
});

return $fortunes;
}
}
6 changes: 4 additions & 2 deletions frameworks/PHP/phalcon/app/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

class IndexController extends \Phalcon\Mvc\Controller
use Phalcon\Mvc\Controller;

class IndexController extends Controller
{
public function indexAction()
public function indexAction(): void
{
echo "<h1>Wrong controller for this benchmark!</h1>";
}
Expand Down
43 changes: 43 additions & 0 deletions frameworks/PHP/phalcon/app/controllers/MongoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Phalcon\Mvc\View;
use Phalcon\Mvc\Model\Resultset;

class MongoController extends BenchController
{
protected function getRandomWorld()
{
return WorldsCollection::findFirst([
'conditions' => [
'_id' => mt_rand(1, 10000),
],
'projection' => [
//'id' => 0,
],
]);
}

protected function getFortunesArray(): array
{
return FortunesCollection::find()->toArray();
}

protected function buildFortune()
{
$fortune = parent::buildFortune();
$newFortune = new FortunesCollection();
$newFortune->setId($fortune['id']);
$newFortune->message = $fortune['message'];

return $newFortune;
}

protected function sortFortunes(array $fortunes): array
{
usort($fortunes, function ($left, $right) {
return $left->message <=> $right->message;
});

return $fortunes;
}
}
37 changes: 0 additions & 37 deletions frameworks/PHP/phalcon/app/controllers/MongobenchController.php

This file was deleted.

7 changes: 4 additions & 3 deletions frameworks/PHP/phalcon/app/models/Fortunes.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\MetaData;
use Phalcon\Db\Column;

class Fortunes extends \Phalcon\Mvc\Model
class Fortunes extends Model
{
public $id;

Expand All @@ -14,7 +15,7 @@ public function initialize()
$this->setSource('Fortune');
}

public function metaData()
public function metaData(): array
{
return [
// Every column in the mapped table
Expand Down Expand Up @@ -73,4 +74,4 @@ public function metaData()
MetaData::MODELS_EMPTY_STRING_VALUES => [],
];
}
}
}
Loading