blob: f4b3b319f2a67a50018a1b5ddd51ed0cdfc80584 [file] [log] [blame]
David Reiss755c8152009-03-26 06:15:05 +00001#!/usr/bin/env php
2<?php
Roger Meier21c0a852012-09-05 19:47:14 +00003
4namespace tutorial\php;
5
6error_reporting(E_ALL);
7
Kengo Seki3b5c40c2019-10-09 06:53:14 +09008require_once __DIR__.'/../../vendor/autoload.php';
Roger Meier21c0a852012-09-05 19:47:14 +00009
10use Thrift\ClassLoader\ThriftClassLoader;
11
Kengo Seki538e36c2019-10-18 22:09:01 +090012$GEN_DIR = realpath(dirname(__FILE__)).'/gen-php';
Roger Meier21c0a852012-09-05 19:47:14 +000013
14$loader = new ThriftClassLoader();
15$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
Kengo Seki3b5c40c2019-10-09 06:53:14 +090016$loader->registerNamespace('shared', $GEN_DIR);
17$loader->registerNamespace('tutorial', $GEN_DIR);
Roger Meier21c0a852012-09-05 19:47:14 +000018$loader->register();
19
David Reissea2cba82009-03-30 21:35:00 +000020/*
21 * Licensed to the Apache Software Foundation (ASF) under one
22 * or more contributor license agreements. See the NOTICE file
23 * distributed with this work for additional information
24 * regarding copyright ownership. The ASF licenses this file
25 * to you under the Apache License, Version 2.0 (the
26 * "License"); you may not use this file except in compliance
27 * with the License. You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing,
32 * software distributed under the License is distributed on an
33 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
34 * KIND, either express or implied. See the License for the
35 * specific language governing permissions and limitations
36 * under the License.
37 */
David Reiss755c8152009-03-26 06:15:05 +000038
39/*
40 * This is not a stand-alone server. It should be run as a normal
41 * php web script (like through Apache's mod_php) or as a cgi script
42 * (like with the included runserver.py). You can connect to it with
43 * THttpClient in any language that supports it. The PHP tutorial client
44 * will work if you pass it the argument "--http".
45 */
46
47if (php_sapi_name() == 'cli') {
48 ini_set("display_errors", "stderr");
49}
50
Roger Meier21c0a852012-09-05 19:47:14 +000051use Thrift\Protocol\TBinaryProtocol;
52use Thrift\Transport\TPhpStream;
53use Thrift\Transport\TBufferedTransport;
David Reiss755c8152009-03-26 06:15:05 +000054
Roger Meier21c0a852012-09-05 19:47:14 +000055class CalculatorHandler implements \tutorial\CalculatorIf {
David Reiss755c8152009-03-26 06:15:05 +000056 protected $log = array();
57
58 public function ping() {
59 error_log("ping()");
60 }
61
62 public function add($num1, $num2) {
63 error_log("add({$num1}, {$num2})");
64 return $num1 + $num2;
65 }
66
Roger Meier21c0a852012-09-05 19:47:14 +000067 public function calculate($logid, \tutorial\Work $w) {
David Reiss755c8152009-03-26 06:15:05 +000068 error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
69 switch ($w->op) {
Roger Meier21c0a852012-09-05 19:47:14 +000070 case \tutorial\Operation::ADD:
David Reiss755c8152009-03-26 06:15:05 +000071 $val = $w->num1 + $w->num2;
72 break;
Roger Meier21c0a852012-09-05 19:47:14 +000073 case \tutorial\Operation::SUBTRACT:
David Reiss755c8152009-03-26 06:15:05 +000074 $val = $w->num1 - $w->num2;
75 break;
Roger Meier21c0a852012-09-05 19:47:14 +000076 case \tutorial\Operation::MULTIPLY:
David Reiss755c8152009-03-26 06:15:05 +000077 $val = $w->num1 * $w->num2;
78 break;
Roger Meier21c0a852012-09-05 19:47:14 +000079 case \tutorial\Operation::DIVIDE:
David Reiss755c8152009-03-26 06:15:05 +000080 if ($w->num2 == 0) {
Roger Meier21c0a852012-09-05 19:47:14 +000081 $io = new \tutorial\InvalidOperation();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020082 $io->whatOp = $w->op;
David Reiss755c8152009-03-26 06:15:05 +000083 $io->why = "Cannot divide by 0";
84 throw $io;
85 }
86 $val = $w->num1 / $w->num2;
87 break;
88 default:
Roger Meier21c0a852012-09-05 19:47:14 +000089 $io = new \tutorial\InvalidOperation();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020090 $io->whatOp = $w->op;
David Reiss755c8152009-03-26 06:15:05 +000091 $io->why = "Invalid Operation";
92 throw $io;
93 }
94
Roger Meier21c0a852012-09-05 19:47:14 +000095 $log = new \shared\SharedStruct();
David Reiss755c8152009-03-26 06:15:05 +000096 $log->key = $logid;
97 $log->value = (string)$val;
98 $this->log[$logid] = $log;
99
100 return $val;
101 }
102
103 public function getStruct($key) {
104 error_log("getStruct({$key})");
105 // This actually doesn't work because the PHP interpreter is
106 // restarted for every request.
107 //return $this->log[$key];
Roger Meier21c0a852012-09-05 19:47:14 +0000108 return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!"));
David Reiss755c8152009-03-26 06:15:05 +0000109 }
110
111 public function zip() {
112 error_log("zip()");
113 }
114
115};
116
117header('Content-Type', 'application/x-thrift');
118if (php_sapi_name() == 'cli') {
119 echo "\r\n";
120}
121
122$handler = new CalculatorHandler();
Roger Meier21c0a852012-09-05 19:47:14 +0000123$processor = new \tutorial\CalculatorProcessor($handler);
David Reiss755c8152009-03-26 06:15:05 +0000124
125$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
126$protocol = new TBinaryProtocol($transport, true, true);
127
128$transport->open();
129$processor->process($protocol, $protocol);
130$transport->close();