使用微型框架Flight来处理MongoDB和csv文件
我改编了下面的程序,使其能够处理MongoDB。
使用微型框架Flight处理MariaDB和csv文件。
创建下一个路径。
http://本地主机:9000/数据库
http://本地主机:9000/逗号分隔值
http://本地主机:9000/导出
http://本地主机:9000/导入
1) 如果我只能提供一种选项,那么可以将”index.php”翻译为”主页.php”。
<?php
// -----------------------------------------------------------------
require 'flight/Flight.php';
require 'app/views/greeting.php';
Flight::route('/',array('Greeting','hello'));
Flight::route('/mongodb',array('Greeting','mongodb'));
Flight::route('/csv',array('Greeting','csv'));
Flight::route('/export',array('Greeting','export'));
Flight::route('/import',array('Greeting','import'));
Flight::start();
// -----------------------------------------------------------------
2) 应用程序/视图/问候.php
<?php
require_once ("csv_manipulate.php");
require_once ("mongo_manipulate.php");
// -----------------------------------------------------------------
class Greeting {
// -----------------------------------------------------------------
public static function hello() {
echo '*** Greeting ***<p />';
echo '<a href="/mongodb">mongodb</a><p />';
echo '<a href="/csv">csv</a><p />';
echo '<a href="/export">export</a><p />';
echo '<a href="/import">import</a><p />';
echo '*** Jul/31/2018 *** AM 10:37<p />';
}
// -----------------------------------------------------------------
public static function export()
{
$file_csv = "data/cities_out.csv";
$user = 'scott';
$password = 'tiger123';
$dict_aa = Mongo_manipulate::mongo_to_dict_proc ($user,$password);
Csv_manipulate::dict_display_proc ($dict_aa);
Csv_manipulate::csv_write_proc ($file_csv,$dict_aa);
echo '<p />';
echo '*** Jul/31/2018 *** AM 10:40<p />';
}
// -----------------------------------------------------------------
public static function import()
{
$file_csv = "data/cities.csv";
$dict_aa = Csv_manipulate::csv_read_proc ($file_csv);
Csv_manipulate::dict_display_proc ($dict_aa);
$user = 'scott';
$password = 'tiger123';
Mongo_manipulate::dict_to_mongo_proc ($dict_aa,$user,$password);
$dbcon=null;
echo '<p />';
echo '*** Jul/31/2018 *** PM 15:26<p />';
}
// -----------------------------------------------------------------
public static function csv()
{
$file_csv = "data/cities.csv";
$dict_aa = Csv_manipulate::csv_read_proc ($file_csv);
Csv_manipulate::dict_display_proc ($dict_aa);
echo '<p />';
echo '*** Jul/31/2018 *** AM 10:55<p />';
}
// -----------------------------------------------------------------
public static function mongodb()
{
$user = 'scott';
$password = 'tiger123';
$dict_aa = Mongo_manipulate::mongo_to_dict_proc ($user,$password);
Csv_manipulate::dict_display_proc ($dict_aa);
echo '<p />';
echo '*** Jul/31/2018 *** AM 10:55<p />';
}
// -----------------------------------------------------------------
}
// -----------------------------------------------------------------
?>
3)应用/视图/操纵mongo.php
<?php
// --------------------------------------------------------------------
/*
mongo_manipulate.php
Aug/01/2018
*/
// --------------------------------------------------------------------
class Mongo_manipulate {
// --------------------------------------------------------------------
function dict_append_proc ($dict_aa,$id,$name,$population,$date_mod)
{
$dict_unit = array ();
$dict_unit['name'] = $name;
$dict_unit['population'] = $population;
$dict_unit['date_mod'] = $date_mod;
$dict_aa[(string)$id]= $dict_unit;
return $dict_aa;
}
// --------------------------------------------------------------------
function mongo_to_dict_proc ($user,$password)
{
$dict_aa = array ();
$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";
$manager = new MongoDB\Driver\Manager($str_connect);
$filter = [];
$options = [
'projection' => ['_id' => 0],
'sort' => ['_id' => -1],
];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('city.saitama', $query);
$dict_aa = array ();
foreach ($cursor as $document) {
$dict_aa = Mongo_manipulate::dict_append_proc ($dict_aa,$document->key,$document->name,$document->population,$document->date_mod);
}
return $dict_aa;
}
// --------------------------------------------------------------------
function mongo_update_proc ($id_in,$population_in,$user,$password)
{
$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";
$manager = new MongoDB\Driver\Manager($str_connect);
$bulk = new MongoDB\Driver\BulkWrite;
date_default_timezone_set('Asia/Tokyo');
$today = date ("Y-m-d");
$newdata = array('$set' => array("population" => $population_in,"date_mod" => $today));
$bulk->update(array("key" => $id_in), $newdata);
$manager->executeBulkWrite('city.saitama', $bulk);
}
// --------------------------------------------------------------------
function mongo_delete_proc ($id_in,$user,$password)
{
$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";
$manager = new MongoDB\Driver\Manager($str_connect);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['key' => $id_in]);
$result = $manager->executeBulkWrite('city.saitama', $bulk);
}
// --------------------------------------------------------------------
function dict_to_mongo_proc ($dict_aa,$user,$password)
{
$str_connect = "mongodb://" . $user . ":" . $password . "@localhost:27017";
$manager = new MongoDB\Driver\Manager($str_connect);
$manager->executeCommand('city', new \MongoDB\Driver\Command(["drop" => "saitama"]));
$bulk = new MongoDB\Driver\BulkWrite;
foreach ($dict_aa as $key => $value)
{
$bulk->insert(['key' => $key, 'name' => $value["name"], 'population' => $value['population'], 'date_mod' => $value['date_mod']]);
}
$manager->executeBulkWrite('city.saitama', $bulk);
}
// --------------------------------------------------------------------
}
// --------------------------------------------------------------------
?>
4) app/views/csv_manipulate.php
这个文件是使用微型框架 Flight 来操作 MariaDB 和 csv 的相同内容。