Browse Source

Simplify php activerecords (#4606)

* Simplify php-activerecord

* Simplify php-eloquent

* Simplify php-laravel-query-builder

* Change require_once for require
and delete commented code

* Small change for travis
Joan Miquel 6 years ago
parent
commit
d415bb2a9a

+ 16 - 26
frameworks/PHP/php/dborm.php

@@ -1,47 +1,37 @@
 <?php
 <?php
 // Set content type
 // Set content type
-header("Content-type: application/json");
+header('Content-type: application/json');
 
 
 // Database connection
 // Database connection
 // http://www.php.net/manual/en/ref.pdo-mysql.php
 // http://www.php.net/manual/en/ref.pdo-mysql.php
 // $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
 // $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
 
 
 # inclue the ActiveRecord library
 # inclue the ActiveRecord library
-require_once 'vendor/php-activerecord/php-activerecord/ActiveRecord.php';
+require 'vendor/php-activerecord/php-activerecord/ActiveRecord.php';
 
 
 ActiveRecord\Connection::$PDO_OPTIONS[PDO::ATTR_PERSISTENT] = true;
 ActiveRecord\Connection::$PDO_OPTIONS[PDO::ATTR_PERSISTENT] = true;
-ActiveRecord\Config::initialize(function($cfg)
-{
-  $cfg->set_model_directory('models');
-  $cfg->set_connections(array('development' =>
-    'mysql://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world'));
+ActiveRecord\Config::initialize(function ($cfg) {
+    $cfg->set_model_directory('models');
+    $cfg->set_connections(['development' =>
+        'mysql://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world']);
 });
 });
 
 
+if (! isset($_GET['queries'])) {
+    echo json_encode(World::find_by_id(mt_rand(1, 10000))->to_array());
+    return;
+}
+
 // Read number of queries to run from URL parameter
 // Read number of queries to run from URL parameter
 $query_count = 1;
 $query_count = 1;
-$query_param = isset($_GET['queries']);
-if ($query_param && $_GET['queries'] > 0) {
+if ($_GET['queries'] > 1) {
     $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
     $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
 }
 }
-
 // Create an array with the response string.
 // Create an array with the response string.
-$arr = array();
-
+$arr = [];
 // For each query, store the result set values in the response array
 // For each query, store the result set values in the response array
-$query_counter = $query_count;
-while (0 < $query_counter--) {
-  // Choose a random row
-  // http://www.php.net/mt_rand
-  $id = mt_rand(1, 10000);
-
-  $world = World::find_by_id($id);
-  
-  // Store result in array.
-  $arr[] = $world->to_array();
-}
-
-if ($query_count === 1 && !$query_param) {
-    $arr = $arr[0];
+while (0 < $query_count--) {
+    // Store result in array.
+    $arr[] = World::find_by_id(mt_rand(1, 10000))->to_array();
 }
 }
 
 
 // Use the PHP standard JSON encoder.
 // Use the PHP standard JSON encoder.

+ 13 - 23
frameworks/PHP/php/eloquent/db-eloquent.php

@@ -1,37 +1,27 @@
 <?php
 <?php
+// Set content type
+header('Content-type: application/json');
+
+require __DIR__.'/boot-eloquent.php';
 
 
-require_once __DIR__ . '/boot-eloquent.php';
+if (! isset($_GET['queries'])) {
+    echo json_encode(World::find(mt_rand(1, 10000))->toArray());
+    return;
+}
 
 
 // Read number of queries to run from URL parameter
 // Read number of queries to run from URL parameter
 $query_count = 1;
 $query_count = 1;
-$query_param = isset($_GET['queries']);
-if ($query_param && $_GET['queries'] > 0) {
+if ($_GET['queries'] > 1) {
     $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
     $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
 }
 }
-
 // Create an array with the response string.
 // Create an array with the response string.
-$arr = array();
-
+$arr = [];
 // For each query, store the result set values in the response array
 // For each query, store the result set values in the response array
-$query_counter = $query_count;
-while (0 < $query_counter--) {
-    // Choose a random row
-    // http://www.php.net/mt_rand
-    $id = mt_rand(1, 10000);
-
-    $world = World::find($id);
-
+while (0 < $query_count--) {
     // Store result in array.
     // Store result in array.
-    $arr[] = $world->toArray();
-}
-
-if ($query_count === 1 && !$query_param) {
-    $arr = $arr[0];
+    $arr[] = World::find(mt_rand(1, 10000))->toArray();
 }
 }
 
 
-// Set content type
-header("Content-type: application/json");
-
 // Use the PHP standard JSON encoder.
 // Use the PHP standard JSON encoder.
 // http://www.php.net/manual/en/function.json-encode.php
 // http://www.php.net/manual/en/function.json-encode.php
-echo json_encode($arr);
+echo json_encode($arr);

+ 15 - 23
frameworks/PHP/php/eloquent/db-laravel-query-builder.php

@@ -1,39 +1,31 @@
 <?php
 <?php
+// Set content type
+header('Content-type: application/json');
 
 
-require_once __DIR__ . '/init-capsule.php';
+require __DIR__.'/init-capsule.php';
 
 
 use Illuminate\Database\Capsule\Manager as Capsule;
 use Illuminate\Database\Capsule\Manager as Capsule;
 
 
+if (! isset($_GET['queries'])) {
+    echo json_encode(
+        Capsule::table('World')->where('id', mt_rand(1, 10000))->get()->first()
+    );
+    return;
+}
+
 // Read number of queries to run from URL parameter
 // Read number of queries to run from URL parameter
 $query_count = 1;
 $query_count = 1;
-$query_param = isset($_GET['queries']);
-if ($query_param && $_GET['queries'] > 0) {
+if ($_GET['queries'] > 1) {
     $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
     $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
 }
 }
-
 // Create an array with the response string.
 // Create an array with the response string.
-$arr = array();
-
+$arr = [];
 // For each query, store the result set values in the response array
 // For each query, store the result set values in the response array
-$query_counter = $query_count;
-while (0 < $query_counter--) {
-    // Choose a random row
-    // http://www.php.net/mt_rand
-    $id = mt_rand(1, 10000);
-
-    $world = Capsule::table('World')->where('id',$id)->get()->first();
-
+while (0 < $query_count--) {
     // Store result in array.
     // Store result in array.
-    $arr[] = $world;
-}
-
-if ($query_count === 1 && !$query_param) {
-    $arr = $arr[0];
+    $arr[] = Capsule::table('World')->where('id', mt_rand(1, 10000))->get()->first();
 }
 }
 
 
-// Set content type
-header("Content-type: application/json");
-
 // Use the PHP standard JSON encoder.
 // Use the PHP standard JSON encoder.
 // http://www.php.net/manual/en/function.json-encode.php
 // http://www.php.net/manual/en/function.json-encode.php
-echo json_encode($arr);
+echo json_encode($arr);