1#!/usr/bin/env php
2<?php
3
4namespace tutorial\php;
5
6error_reporting(E_ALL);
7
8require_once __DIR__.'/../../vendor/autoload.php';
9
10use Thrift\ClassLoader\ThriftClassLoader;
11
12$GEN_DIR = realpath(dirname(__FILE__)).'/gen-php';
13
14$loader = new ThriftClassLoader();
15$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
16$loader->registerNamespace('shared', $GEN_DIR);
17$loader->registerNamespace('tutorial', $GEN_DIR);
18$loader->register();
19
20/*
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 */
38
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
51use Thrift\Protocol\TBinaryProtocol;
52use Thrift\Transport\TPhpStream;
53use Thrift\Transport\TBufferedTransport;
54
55class CalculatorHandler implements \tutorial\CalculatorIf {
56  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
67  public function calculate($logid, \tutorial\Work $w) {
68    error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
69    switch ($w->op) {
70      case \tutorial\Operation::ADD:
71        $val = $w->num1 + $w->num2;
72        break;
73      case \tutorial\Operation::SUBTRACT:
74        $val = $w->num1 - $w->num2;
75        break;
76      case \tutorial\Operation::MULTIPLY:
77        $val = $w->num1 * $w->num2;
78        break;
79      case \tutorial\Operation::DIVIDE:
80        if ($w->num2 == 0) {
81          $io = new \tutorial\InvalidOperation();
82          $io->whatOp = $w->op;
83          $io->why = "Cannot divide by 0";
84          throw $io;
85        }
86        $val = $w->num1 / $w->num2;
87        break;
88      default:
89        $io = new \tutorial\InvalidOperation();
90        $io->whatOp = $w->op;
91        $io->why = "Invalid Operation";
92        throw $io;
93    }
94
95    $log = new \shared\SharedStruct();
96    $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];
108    return new \shared\SharedStruct(array("key" => $key, "value" => "PHP is stateless!"));
109  }
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();
123$processor = new \tutorial\CalculatorProcessor($handler);
124
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();
131