Commit 45df2d33 authored by tufengqi's avatar tufengqi

init

parent 50ad8055
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ClassLoader to load Thrift library and definitions
* Inspired from UniversalClassLoader from Symfony 2
*
* @package thrift.classloader
*/
class FpfClassLoader
{
/**
* Namespaces path
* @var array
*/
protected $namespaces = array();
/**
* Thrift definition paths
* @var type
*/
protected $definitions = array();
/**
* Do we use APC cache ?
* @var boolean
*/
protected $apc = false;
/**
* APC Cache prefix
* @var string
*/
protected $apc_prefix;
/**
* Set autoloader to use APC cache
* @param boolean $apc
* @param string $apc_prefix
*/
public function __construct($apc = false, $apc_prefix = null)
{
$this->apc = $apc;
$this->apc_prefix = $apc_prefix;
}
/**
* Registers a namespace.
*
* @param string $namespace The namespace
* @param array|string $paths The location(s) of the namespace
*/
public function registerNamespace($namespace, $paths)
{
$this->namespaces[$namespace] = (array)$paths;
}
/**
* Registers a Thrift definition namespace.
*
* @param string $namespace The definition namespace
* @param array|string $paths The location(s) of the definition namespace
*/
public function registerDefinition($namespace, $paths)
{
$this->definitions[$namespace] = (array)$paths;
}
/**
* Registers this instance as an autoloader.
*
* @param Boolean $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Loads the given class, definition or interface.
*
* @param string $class The name of the class
*/
public function loadClass($class)
{
if ((true === $this->apc && ($file = $this->findFileInApc($class))) or ($file = $this->findFile($class))) {
require_once $file;
}
}
/**
* Loads the given class or interface in APC.
* @param string $class The name of the class
* @return string
*/
protected function findFileInApc($class)
{
if (false === $file = apc_fetch($this->apc_prefix . $class)) {
apc_store($this->apc_prefix . $class, $file = $this->findFile($class));
}
return $file;
}
/**
* Find class in namespaces or definitions directories
* @param string $class
* @return string
*/
public function findFile($class)
{
// Remove first backslash
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
if (false !== $pos = strrpos($class, '\\')) {
// Namespaced class name
$namespace = substr($class, 0, $pos);
// Iterate in normal namespaces
foreach ($this->namespaces as $ns => $dirs) {
//Don't interfere with other autoloaders
if (0 !== strpos($namespace, $ns)) {
continue;
}
foreach ($dirs as $dir) {
$className = substr($class, $pos + 1);
$file = $dir . DIRECTORY_SEPARATOR .
str_replace('\\', DIRECTORY_SEPARATOR, $namespace) .
DIRECTORY_SEPARATOR .
$className . '.php';
if (file_exists($file)) {
return $file;
}
}
}
// Iterate in Thrift namespaces
// Remove first part of namespace
$m = explode('\\', $class);
// Ignore wrong call
if (count($m) <= 1) {
return;
}
$class = array_pop($m);
$namespace = implode('\\', $m);
foreach ($this->definitions as $ns => $dirs) {
//Don't interfere with other autoloaders
if (0 !== strpos($namespace, $ns)) {
continue;
}
foreach ($dirs as $dir) {
/**
* Available in service: Interface, Client, Processor, Rest
* And every service methods (_.+)
*/
if (0 === preg_match('#(.+)(if|client|processor|rest)$#i', $class, $n) and
0 === preg_match('#(.+)_[a-z0-9]+_(args|result)$#i', $class, $n)) {
$className = 'Types';
} else {
$className = $n[1];
}
$file = $dir . DIRECTORY_SEPARATOR .
str_replace('\\', DIRECTORY_SEPARATOR, $namespace) .
DIRECTORY_SEPARATOR .
$className . '.php';
if (file_exists($file)) {
return $file;
}
}
}
}
}
}
<?php
namespace fpf;
error_reporting(E_ALL);
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/autoload.php';
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/thrift_33cn/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
echo $root_path . '/classes/';
$loader->registerNamespace('fpf\\thrift', $root_path . '/classes');
$loader->register();
namespace fpf\thrift;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TJSONProtocol;
......@@ -41,16 +10,11 @@ use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use service\market\constant\CoinCate;
use service\market\constant\DataSource;
use fpf\thrift\ThriftServiceFactory;
try {
/**
* @var $base_market_service_client \service\market\BaseMarketServiceClient
* @var \service\market\BaseMarketServiceClient $base_market_service_client
*/
$base_market_service_client = ThriftServiceFactory::getService('\service\market\BaseMarketServiceClient');
// $request = new \service\market\object\base\BaseListRequest();
......
<?php
namespace tutorial\php;
error_reporting(E_ALL);
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/thrift_33cn/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift/gen-php/';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $root_path . '/vendor/thrift_33cn');
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
try {
$socket = new THttpClient('localhost', 80, '/classes/fpf/thrift/Server.php');
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TJSONProtocol($transport);
$client = new \tutorial\CalculatorClient($protocol);
$transport->open();
// $client->ping();
// print "ping()\n";
// $sum = $client->add(1, 1);
// print "1+1=$sum\n";
$std = new \tutorial\MemberInfoRequest();
$std->type_id = 1;
$std->id = 5;
$arr[] = $std;
$arr[] = $std;
$sum = $client->addList($arr);
print "5+5=$sum\n";
// $work = new \tutorial\Work();
// $work->op = \tutorial\Operation::DIVIDE;
// $work->num1 = 1;
// $work->num2 = 0;
// try {
// $client->calculate(1, $work);
// print "Whoa! We can divide by zero?\n";
// } catch (\tutorial\InvalidOperation $io) {
// print "InvalidOperation: $io->why\n";
// }
// $work->op = \tutorial\Operation::SUBTRACT;
// $work->num1 = 15;
// $work->num2 = 10;
// $diff = $client->calculate(1, $work);
// print "15-10=$diff\n";
// $log = $client->getStruct(1);
// print "Log: $log->value\n";
$transport->close();
} catch (TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
<?php
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
use Thrift\TMultiplexedProcessor;
class Launcher
{
public function run()
{
$uri = $_SERVER['REQUEST_URI'];
$pos = strpos($uri, '?');
if ($pos) {
$uri = substr($uri, 0, $pos);
}
list(, $module_name, $biz_name) = explode('/', $uri);
}
}
\ No newline at end of file
<?php
namespace tutorial\php;
error_reporting(E_ERROR);
$data = file_get_contents("php://input");
file_put_contents("./a.txt", $data, FILE_APPEND);
//echo 'from server';
$root_path = dirname(dirname(dirname(__DIR__)));
require_once $root_path . '/vendor/thrift_33cn/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = $root_path . '/vendor/thrift_33cn/Thrift/gen-php/';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $root_path . '/vendor/thrift_33cn');
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* This is not a stand-alone server. It should be run as a normal
* php web script (like through Apache's mod_php) or as a cgi script
* (like with the included runserver.py). You can connect to it with
* THttpClient in any language that supports it. The PHP tutorial client
* will work if you pass it the argument "--http".
*/
if (php_sapi_name() == 'cli') {
ini_set("display_errors", "stderr");
}
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TJSONProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
class CalculatorHandler implements \tutorial\CalculatorIf
{
protected $log = array();
public function ping()
{
error_log("ping()");
}
public function add($num1, $num2)
{
// error_log("add({$num1}, {$num2})");
return $num1 + $num2;
}
public function addList(array $arr)
{
$sum = 0;
foreach ($arr as $item){
$sum+=$item->id;
}
return $sum;
}
public function calculate($logid, \tutorial\Work $w)
{
error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
switch ($w->op) {
case \tutorial\Operation::ADD:
$val = $w->num1 + $w->num2;
break;
case \tutorial\Operation::SUBTRACT:
$val = $w->num1 - $w->num2;
break;
case \tutorial\Operation::MULTIPLY:
$val = $w->num1 * $w->num2;
break;
case \tutorial\Operation::DIVIDE:
if ($w->num2 == 0) {
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Cannot divide by 0";
throw $io;
}
$val = $w->num1 / $w->num2;
break;
default:
$io = new \tutorial\InvalidOperation();
$io->whatOp = $w->op;
$io->why = "Invalid Operation";
throw $io;
}
$log = new \shared\SharedStruct();
$log->key = $logid;
$log->value = (string)$val;
$this->log[$logid] = $log;
return $val;
}
public function getStruct($key)
{
error_log("getStruct({$key})");
// This actually doesn't work because the PHP interpreter is
// restarted for every request.
//return $this->log[$key];
return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!"));
}
public function zip()
{
error_log("zip()");
}
}
;
header('Content-Type', 'application/x-thrift');
if (php_sapi_name() == 'cli') {
echo "\r\n";
}
$handler = new CalculatorHandler();
$processor = new \tutorial\CalculatorProcessor($handler);
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TJSONProtocol($transport, true, true);
$transport->open();
$processor->process($protocol, $protocol);
$transport->close();
......@@ -99,8 +99,8 @@ class THttpClient extends \Thrift\Transport\TTransport
*/
public function __construct($host, $port = 80, $uri_no_func = '', $scheme = 'http')
{
if ((TStringFuncFactory::create()->strlen($uri_no_func) > 0) && ($uri_no_func{
0} != '/')) {
if ((TStringFuncFactory::create()->strlen($uri_no_func) > 0) && ($uri_no_func {
0} != '/')) {
$uri_no_func = '/' . $uri_no_func;
}
$this->scheme_ = $scheme;
......@@ -169,18 +169,18 @@ class THttpClient extends \Thrift\Transport\TTransport
public function read($len)
{
$data = @fread($this->handle_, $len);
if ($data === false || $data === ' ') {
if ($data === false || $data === '') {
$md = stream_get_meta_data($this->handle_);
if ($md[' timed_out ']) {
if ($md['timed_out']) {
throw new TTransportException(
' THttpClient : timed out reading ' . $len . ' bytes from ' .
$this->host_ . ' :' . $this->port_ . $this->uri_,
'THttpClient: timed out reading ' . $len . ' bytes from ' .
$this->host_ . ':' . $this->port_ . $this->uri_,
TTransportException::TIMED_OUT
);
} else {
throw new TTransportException(
' THttpClient : Could not read ' . $len . ' bytes from ' .
$this->host_ . ' : ' . $this->port_ . $this->uri_,
'THttpClient: Could not read ' . $len . ' bytes from ' .
$this->host_ . ':' . $this->port_ . $this->uri_,
TTransportException::UNKNOWN
);
}
......@@ -208,34 +208,34 @@ class THttpClient extends \Thrift\Transport\TTransport
public function flush()
{
// God, PHP really has some esoteric ways of doing simple things.
$host = $this->host_ . ($this->port_ != 80 ? ' :' . $this->port_ : ' ');
$host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : '');
$headers = array();
$defaultHeaders = array(
' Host ' => $host,
' Accept ' => ' application / x - thrift ',
' User - Agent ' => ' PHP / THttpClient ',
' Content - Type ' => ' application / x - thrift ',
' Content - Length ' => TStringFuncFactory::create()->strlen($this->buf_)
'Host' => $host,
'Accept' => 'application/x-thrift',
'User-Agent' => 'PHP/THttpClient',
'Content-Type' => 'application/x-thrift',
'Content-Length' => TStringFuncFactory::create()->strlen($this->buf_)
);
foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) {
$headers[] = "$key: $value";
}
$options = array(
' method ' => ' POST ',
' header ' => implode("\r\n", $headers),
' max_redirects ' => 1,
' content ' => $this->buf_
'method' => 'POST',
'header' => implode("\r\n", $headers),
'max_redirects' => 1,
'content' => $this->buf_
);
if ($this->timeout_ > 0) {
$options[' timeout '] = $this->timeout_;
$options['timeout'] = $this->timeout_;
}
$this->buf_ = ' ';
$this->buf_ = '';
$contextid = stream_context_create(array(' http ' => $options));
$contextid = stream_context_create(array('http' => $options));
$this->handle_ = @fopen(
$this->scheme_ . ' ://' . $host . $this->uri_,
$this->scheme_ . '://' . $host . $this->uri_,
'r',
false,
$contextid
......
......@@ -2,22 +2,11 @@
"name": "system",
"type": "library",
"require": {},
"require-dev": {
"thrift_33cn": "@dev",
"service/market": "@dev"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
},
"thrift_33cn": {
"type": "git",
"url": "git@gitlab.33.cn:_site-res/thrift_33cn.git"
},
"service/market": {
"type": "git",
"url": "git@gitlab.33.cn:_site-service/market-service-intf.git"
}
"require-dev": {},
"repositories": {},
"autoload": {
"classmap": [
"./classes"
]
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment