Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
#!/usr/bin/env php
2
<?php
3
/*
4
 * Licensed to the Apache Software Foundation (ASF) under one
5
 * or more contributor license agreements. See the NOTICE file
6
 * distributed with this work for additional information
7
 * regarding copyright ownership. The ASF licenses this file
8
 * to you under the Apache License, Version 2.0 (the
9
 * "License"); you may not use this file except in compliance
10
 * with the License. You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing,
15
 * software distributed under the License is distributed on an
16
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
 * KIND, either express or implied. See the License for the
18
 * specific language governing permissions and limitations
19
 * under the License.
20
 */
21
 
22
/*
23
 * This is not a stand-alone server.  It should be run as a normal
24
 * php web script (like through Apache's mod_php) or as a cgi script
25
 * (like with the included runserver.py).  You can connect to it with
26
 * THttpClient in any language that supports it.  The PHP tutorial client
27
 * will work if you pass it the argument "--http".
28
 */
29
 
30
if (php_sapi_name() == 'cli') {
31
  ini_set("display_errors", "stderr");
32
}
33
 
34
$GLOBALS['THRIFT_ROOT'] = realpath(dirname(__FILE__).'/../..').'/lib/php/src';
35
 
36
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
37
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
38
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TPhpStream.php';
39
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
40
 
41
/**
42
 * Suppress errors in here, which happen because we have not installed into
43
 * $GLOBALS['THRIFT_ROOT'].'/packages/tutorial' like we are supposed to!
44
 *
45
 * Normally we would only have to include Calculator.php which would properly
46
 * include the other files from their packages/ folder locations, but we
47
 * include everything here due to the bogus path setup.
48
 */
49
error_reporting(E_NONE);
50
$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php';
51
require_once $GEN_DIR.'/SharedService.php';
52
require_once $GEN_DIR.'/shared_types.php';
53
require_once $GEN_DIR.'/Calculator.php';
54
require_once $GEN_DIR.'/tutorial_types.php';
55
error_reporting(E_ALL);
56
 
57
class CalculatorHandler implements CalculatorIf {
58
  protected $log = array();
59
 
60
  public function ping() {
61
    error_log("ping()");
62
  }
63
 
64
  public function add($num1, $num2) {
65
    error_log("add({$num1}, {$num2})");
66
    return $num1 + $num2;
67
  }
68
 
69
  public function calculate($logid, $w) {
70
    error_log("calculate({$logid}, {{$w->op}, {$w->num1}, {$w->num2}})");
71
    switch ($w->op) {
72
      case tutorial_Operation::ADD:
73
        $val = $w->num1 + $w->num2;
74
        break;
75
      case tutorial_Operation::SUBTRACT:
76
        $val = $w->num1 - $w->num2;
77
        break;
78
      case tutorial_Operation::MULTIPLY:
79
        $val = $w->num1 * $w->num2;
80
        break;
81
      case tutorial_Operation::DIVIDE:
82
        if ($w->num2 == 0) {
83
          $io = new tutorial_InvalidOperation();
84
          $io->what = $w->op;
85
          $io->why = "Cannot divide by 0";
86
          throw $io;
87
        }
88
        $val = $w->num1 / $w->num2;
89
        break;
90
      default:
91
        $io = new tutorial_InvalidOperation();
92
        $io->what = $w->op;
93
        $io->why = "Invalid Operation";
94
        throw $io;
95
    }
96
 
97
    $log = new SharedStruct();
98
    $log->key = $logid;
99
    $log->value = (string)$val;
100
    $this->log[$logid] = $log;
101
 
102
    return $val;
103
  }
104
 
105
  public function getStruct($key) {
106
    error_log("getStruct({$key})");
107
    // This actually doesn't work because the PHP interpreter is
108
    // restarted for every request.
109
    //return $this->log[$key];
110
    return new SharedStruct(array("key" => $key, "value" => "PHP is stateless!"));
111
  }
112
 
113
  public function zip() {
114
    error_log("zip()");
115
  }
116
 
117
};
118
 
119
header('Content-Type', 'application/x-thrift');
120
if (php_sapi_name() == 'cli') {
121
  echo "\r\n";
122
}
123
 
124
$handler = new CalculatorHandler();
125
$processor = new CalculatorProcessor($handler);
126
 
127
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
128
$protocol = new TBinaryProtocol($transport, true, true);
129
 
130
$transport->open();
131
$processor->process($protocol, $protocol);
132
$transport->close();