diff options
author | Roger Meier <roger@apache.org> | 2012-09-05 19:47:14 +0000 |
---|---|---|
committer | Roger Meier <roger@apache.org> | 2012-09-05 19:47:14 +0000 |
commit | 21c0a85079e3e1b5ee475bd6654512079fe5df68 (patch) | |
tree | 4cff5730f356949cb848fef0b110a92a56541ff6 /tutorial | |
parent | f88f9c231ebcd8f19bb040f78bc03e6b33a2982c (diff) | |
download | thrift-21c0a85079e3e1b5ee475bd6654512079fe5df68.tar.gz |
THRIFT-1615 PHP Namespace (was Thrift for Symfony 2)
Patch: Xavier HAUSHERR
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1381323 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tutorial')
-rwxr-xr-x | tutorial/php/PhpClient.php | 55 | ||||
-rwxr-xr-x | tutorial/php/PhpServer.php | 64 |
2 files changed, 58 insertions, 61 deletions
diff --git a/tutorial/php/PhpClient.php b/tutorial/php/PhpClient.php index a6639d5fd..d262b8fe9 100755 --- a/tutorial/php/PhpClient.php +++ b/tutorial/php/PhpClient.php @@ -1,5 +1,22 @@ #!/usr/bin/env php <?php + +namespace tutorial\php; + +error_reporting(E_ALL); + +require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; + +use Thrift\ClassLoader\ThriftClassLoader; + +$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php'; + +$loader = new ThriftClassLoader(); +$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib'); +$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 @@ -19,29 +36,11 @@ * under the License. */ -$GLOBALS['THRIFT_ROOT'] = '../../lib/php/src'; - -require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; - -/** - * Suppress errors in here, which happen because we have not installed into - * $GLOBALS['THRIFT_ROOT'].'/packages/tutorial' like we are supposed to! - * - * Normally we would only have to include Calculator.php which would properly - * include the other files from their packages/ folder locations, but we - * include everything here due to the bogus path setup. - */ -error_reporting(E_NONE); -$GEN_DIR = '../gen-php'; -require_once $GEN_DIR.'/shared/SharedService.php'; -require_once $GEN_DIR.'/shared/shared_types.php'; -require_once $GEN_DIR.'/tutorial/Calculator.php'; -require_once $GEN_DIR.'/tutorial/tutorial_types.php'; -error_reporting(E_ALL); +use Thrift\Protocol\TBinaryProtocol; +use Thrift\Transport\TSocket; +use Thrift\Transport\THttpClient; +use Thrift\Transport\TBufferedTransport; +use Thrift\Exception\TException; try { if (array_search('--http', $argv)) { @@ -51,7 +50,7 @@ try { } $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); - $client = new CalculatorClient($protocol); + $client = new \tutorial\CalculatorClient($protocol); $transport->open(); @@ -61,20 +60,20 @@ try { $sum = $client->add(1,1); print "1+1=$sum\n"; - $work = new tutorial_Work(); + $work = new \tutorial\Work(); - $work->op = tutorial_Operation::DIVIDE; + $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) { + } catch (\tutorial\InvalidOperation $io) { print "InvalidOperation: $io->why\n"; } - $work->op = tutorial_Operation::SUBTRACT; + $work->op = \tutorial\Operation::SUBTRACT; $work->num1 = 15; $work->num2 = 10; $diff = $client->calculate(1, $work); diff --git a/tutorial/php/PhpServer.php b/tutorial/php/PhpServer.php index 235b15bab..4af70a488 100755 --- a/tutorial/php/PhpServer.php +++ b/tutorial/php/PhpServer.php @@ -1,5 +1,22 @@ #!/usr/bin/env php <?php + +namespace tutorial\php; + +error_reporting(E_ALL); + +require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; + +use Thrift\ClassLoader\ThriftClassLoader; + +$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php'; + +$loader = new ThriftClassLoader(); +$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib'); +$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 @@ -31,30 +48,11 @@ if (php_sapi_name() == 'cli') { ini_set("display_errors", "stderr"); } -$GLOBALS['THRIFT_ROOT'] = realpath(dirname(__FILE__).'/../..').'/lib/php/src'; - -require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/transport/TPhpStream.php'; -require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; - -/** - * Suppress errors in here, which happen because we have not installed into - * $GLOBALS['THRIFT_ROOT'].'/packages/tutorial' like we are supposed to! - * - * Normally we would only have to include Calculator.php which would properly - * include the other files from their packages/ folder locations, but we - * include everything here due to the bogus path setup. - */ -error_reporting(E_NONE); -$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php'; -require_once $GEN_DIR.'/shared/SharedService.php'; -require_once $GEN_DIR.'/shared/shared_types.php'; -require_once $GEN_DIR.'/tutorial/Calculator.php'; -require_once $GEN_DIR.'/tutorial/tutorial_types.php'; -error_reporting(E_ALL); +use Thrift\Protocol\TBinaryProtocol; +use Thrift\Transport\TPhpStream; +use Thrift\Transport\TBufferedTransport; -class CalculatorHandler implements CalculatorIf { +class CalculatorHandler implements \tutorial\CalculatorIf { protected $log = array(); public function ping() { @@ -66,21 +64,21 @@ class CalculatorHandler implements CalculatorIf { return $num1 + $num2; } - public function calculate($logid, $w) { + public function calculate($logid, \tutorial\Work $w) { error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})"); switch ($w->op) { - case tutorial_Operation::ADD: + case \tutorial\Operation::ADD: $val = $w->num1 + $w->num2; break; - case tutorial_Operation::SUBTRACT: + case \tutorial\Operation::SUBTRACT: $val = $w->num1 - $w->num2; break; - case tutorial_Operation::MULTIPLY: + case \tutorial\Operation::MULTIPLY: $val = $w->num1 * $w->num2; break; - case tutorial_Operation::DIVIDE: + case \tutorial\Operation::DIVIDE: if ($w->num2 == 0) { - $io = new tutorial_InvalidOperation(); + $io = new \tutorial\InvalidOperation(); $io->what = $w->op; $io->why = "Cannot divide by 0"; throw $io; @@ -88,13 +86,13 @@ class CalculatorHandler implements CalculatorIf { $val = $w->num1 / $w->num2; break; default: - $io = new tutorial_InvalidOperation(); + $io = new \tutorial\InvalidOperation(); $io->what = $w->op; $io->why = "Invalid Operation"; throw $io; } - $log = new SharedStruct(); + $log = new \shared\SharedStruct(); $log->key = $logid; $log->value = (string)$val; $this->log[$logid] = $log; @@ -107,7 +105,7 @@ class CalculatorHandler implements CalculatorIf { // This actually doesn't work because the PHP interpreter is // restarted for every request. //return $this->log[$key]; - return new SharedStruct(array("key" => $key, "value" => "PHP is stateless!")); + return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!")); } public function zip() { @@ -122,7 +120,7 @@ if (php_sapi_name() == 'cli') { } $handler = new CalculatorHandler(); -$processor = new CalculatorProcessor($handler); +$processor = new \tutorial\CalculatorProcessor($handler); $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); $protocol = new TBinaryProtocol($transport, true, true); |