aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar mlumish <mlumish@google.com>2015-01-02 14:59:16 -0800
committerGravatar Craig Tiller <ctiller@google.com>2015-01-05 17:43:10 -0800
commit156e67d417f4569b61241793156b8b17d2960dbe (patch)
tree009e4789acd666f1b43415b4c6e8cefa51b7696e
parent8f91163795e5858fa31d05f25543139996b13b10 (diff)
Added PSR-4 compatible autoloader as defined at http://www.php-fig.org/psr/psr-4/.
SurfaceActiveCall.php was split into four files. Each file must contain exactly one PHP class because of the following line in the spec: > The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name. Other changes were made to correctly import the library using the autoloader. Change on 2015/01/02 by mlumish <mlumish@google.com> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=83150065
-rwxr-xr-xsrc/php/lib/Grpc/AbstractSurfaceActiveCall.php64
-rwxr-xr-xsrc/php/lib/Grpc/ActiveCall.php1
-rwxr-xr-xsrc/php/lib/Grpc/BaseStub.php2
-rwxr-xr-xsrc/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php43
-rwxr-xr-xsrc/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php39
-rwxr-xr-xsrc/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php42
-rwxr-xr-xsrc/php/lib/Grpc/SimpleSurfaceActiveCall.php39
-rwxr-xr-xsrc/php/lib/Grpc/SurfaceActiveCall.php211
-rwxr-xr-xsrc/php/lib/autoload.php21
-rwxr-xr-xsrc/php/tests/generated_code/GeneratedCodeTest.php4
-rwxr-xr-xsrc/php/tests/interop/interop_client.php4
11 files changed, 252 insertions, 218 deletions
diff --git a/src/php/lib/Grpc/AbstractSurfaceActiveCall.php b/src/php/lib/Grpc/AbstractSurfaceActiveCall.php
new file mode 100755
index 0000000000..53c7d4cd1a
--- /dev/null
+++ b/src/php/lib/Grpc/AbstractSurfaceActiveCall.php
@@ -0,0 +1,64 @@
+<?php
+namespace Grpc;
+
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
+
+/**
+ * Represents an active call that allows sending and recieving messages.
+ * Subclasses restrict how data can be sent and recieved.
+ */
+abstract class AbstractSurfaceActiveCall {
+ private $active_call;
+ private $deserialize;
+
+ /**
+ * Create a new surface active call.
+ * @param Channel $channel The channel to communicate on
+ * @param string $method The method to call on the remote server
+ * @param callable $deserialize The function to deserialize a value
+ * @param array $metadata Metadata to send with the call, if applicable
+ * @param long $flags Write flags to use with this call
+ */
+ public function __construct(Channel $channel,
+ $method,
+ callable $deserialize,
+ $metadata = array(),
+ $flags = 0) {
+ $this->active_call = new ActiveCall($channel, $method, $metadata, $flags);
+ $this->deserialize = $deserialize;
+ }
+
+ /**
+ * @return The metadata sent by the server
+ */
+ public function getMetadata() {
+ return $this->metadata();
+ }
+
+ /**
+ * Cancels the call
+ */
+ public function cancel() {
+ $this->active_call->cancel();
+ }
+
+ protected function _read() {
+ $response = $this->active_call->read();
+ if ($response == null) {
+ return null;
+ }
+ return call_user_func($this->deserialize, $response);
+ }
+
+ protected function _write($value) {
+ return $this->active_call->write($value->serialize());
+ }
+
+ protected function _writesDone() {
+ $this->active_call->writesDone();
+ }
+
+ protected function _getStatus() {
+ return $this->active_call->getStatus();
+ }
+} \ No newline at end of file
diff --git a/src/php/lib/Grpc/ActiveCall.php b/src/php/lib/Grpc/ActiveCall.php
index fb307c76c4..aa66dbb848 100755
--- a/src/php/lib/Grpc/ActiveCall.php
+++ b/src/php/lib/Grpc/ActiveCall.php
@@ -1,5 +1,6 @@
<?php
namespace Grpc;
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
/**
* Represents an active call that allows sending and recieving binary data
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php
index 7aa0c4ac4d..e1feb1206b 100755
--- a/src/php/lib/Grpc/BaseStub.php
+++ b/src/php/lib/Grpc/BaseStub.php
@@ -1,6 +1,6 @@
<?php
-
namespace Grpc;
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
/**
* Base class for generated client stubs. Stub methods are expected to call
diff --git a/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php
new file mode 100755
index 0000000000..b5d557e02d
--- /dev/null
+++ b/src/php/lib/Grpc/BidiStreamingSurfaceActiveCall.php
@@ -0,0 +1,43 @@
+<?php
+namespace Grpc;
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
+
+/**
+ * Represents an active call that allows for sending and recieving messages in
+ * streams in any order.
+ */
+class BidiStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
+
+ /**
+ * Reads the next value from the server.
+ * @return The next value from the server, or null if there is none
+ */
+ public function read() {
+ return $this->_read();
+ }
+
+ /**
+ * Writes a single message to the server. This cannot be called after
+ * writesDone is called.
+ * @param $value The message to send
+ */
+ public function write($value) {
+ $this->_write($value);
+ }
+
+ /**
+ * Indicate that no more writes will be sent
+ */
+ public function writesDone() {
+ $this->_writesDone();
+ }
+
+ /**
+ * Wait for the server to send the status, and return it.
+ * @return object The status object, with integer $code and string $details
+ * members
+ */
+ public function getStatus() {
+ return $this->_getStatus();
+ }
+} \ No newline at end of file
diff --git a/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php
new file mode 100755
index 0000000000..fa643e50a8
--- /dev/null
+++ b/src/php/lib/Grpc/ClientStreamingSurfaceActiveCall.php
@@ -0,0 +1,39 @@
+<?php
+namespace Grpc;
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
+
+/**
+ * Represents an active call that sends a stream of messages and then gets a
+ * single response.
+ */
+class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
+ /**
+ * Create a new simple (single request/single response) active call.
+ * @param Channel $channel The channel to communicate on
+ * @param string $method The method to call on the remote server
+ * @param callable $deserialize The function to deserialize a value
+ * @param Traversable $arg_iter The iterator of arguments to send
+ * @param array $metadata Metadata to send with the call, if applicable
+ */
+ public function __construct(Channel $channel,
+ $method,
+ callable $deserialize,
+ $arg_iter,
+ $metadata = array()) {
+ parent::__construct($channel, $method, $deserialize, $metadata, 0);
+ foreach($arg_iter as $arg) {
+ $this->_write($arg);
+ }
+ $this->_writesDone();
+ }
+
+ /**
+ * Wait for the server to respond with data and a status
+ * @return [response data, status]
+ */
+ public function wait() {
+ $response = $this->_read();
+ $status = $this->_getStatus();
+ return array($response, $status);
+ }
+}
diff --git a/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php b/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php
new file mode 100755
index 0000000000..082f995d8a
--- /dev/null
+++ b/src/php/lib/Grpc/ServerStreamingSurfaceActiveCall.php
@@ -0,0 +1,42 @@
+<?php
+namespace Grpc;
+
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
+
+/**
+ * Represents an active call that sends a single message and then gets a stream
+ * of reponses
+ */
+class ServerStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
+ /**
+ * Create a new simple (single request/single response) active call.
+ * @param Channel $channel The channel to communicate on
+ * @param string $method The method to call on the remote server
+ * @param callable $deserialize The function to deserialize a value
+ * @param $arg The argument to send
+ * @param array $metadata Metadata to send with the call, if applicable
+ */
+ public function __construct(Channel $channel,
+ $method,
+ callable $deserialize,
+ $arg,
+ $metadata = array()) {
+ parent::__construct($channel, $method, $deserialize, $metadata,
+ \Grpc\WRITE_BUFFER_HINT);
+ $this->_write($arg);
+ $this->_writesDone();
+ }
+
+ /**
+ * @return An iterator of response values
+ */
+ public function responses() {
+ while(($response = $this->_read()) != null) {
+ yield $response;
+ }
+ }
+
+ public function getStatus() {
+ return $this->_getStatus();
+ }
+}
diff --git a/src/php/lib/Grpc/SimpleSurfaceActiveCall.php b/src/php/lib/Grpc/SimpleSurfaceActiveCall.php
new file mode 100755
index 0000000000..f352573f34
--- /dev/null
+++ b/src/php/lib/Grpc/SimpleSurfaceActiveCall.php
@@ -0,0 +1,39 @@
+<?php
+namespace Grpc;
+
+require_once realpath(dirname(__FILE__) . '/../autoload.php');
+
+/**
+ * Represents an active call that sends a single message and then gets a single
+ * response.
+ */
+class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall {
+ /**
+ * Create a new simple (single request/single response) active call.
+ * @param Channel $channel The channel to communicate on
+ * @param string $method The method to call on the remote server
+ * @param callable $deserialize The function to deserialize a value
+ * @param $arg The argument to send
+ * @param array $metadata Metadata to send with the call, if applicable
+ */
+ public function __construct(Channel $channel,
+ $method,
+ callable $deserialize,
+ $arg,
+ $metadata = array()) {
+ parent::__construct($channel, $method, $deserialize, $metadata,
+ \Grpc\WRITE_BUFFER_HINT);
+ $this->_write($arg);
+ $this->_writesDone();
+ }
+
+ /**
+ * Wait for the server to respond with data and a status
+ * @return [response data, status]
+ */
+ public function wait() {
+ $response = $this->_read();
+ $status = $this->_getStatus();
+ return array($response, $status);
+ }
+}
diff --git a/src/php/lib/Grpc/SurfaceActiveCall.php b/src/php/lib/Grpc/SurfaceActiveCall.php
deleted file mode 100755
index 5f94303357..0000000000
--- a/src/php/lib/Grpc/SurfaceActiveCall.php
+++ /dev/null
@@ -1,211 +0,0 @@
-<?php
-namespace Grpc;
-
-/**
- * Represents an active call that allows sending and recieving messages.
- * Subclasses restrict how data can be sent and recieved.
- */
-abstract class AbstractSurfaceActiveCall {
- private $active_call;
- private $deserialize;
-
- /**
- * Create a new surface active call.
- * @param Channel $channel The channel to communicate on
- * @param string $method The method to call on the remote server
- * @param callable $deserialize The function to deserialize a value
- * @param array $metadata Metadata to send with the call, if applicable
- * @param long $flags Write flags to use with this call
- */
- public function __construct(Channel $channel,
- $method,
- callable $deserialize,
- $metadata = array(),
- $flags = 0) {
- $this->active_call = new ActiveCall($channel, $method, $metadata, $flags);
- $this->deserialize = $deserialize;
- }
-
- /**
- * @return The metadata sent by the server
- */
- public function getMetadata() {
- return $this->metadata();
- }
-
- /**
- * Cancels the call
- */
- public function cancel() {
- $this->active_call->cancel();
- }
-
- protected function _read() {
- $response = $this->active_call->read();
- if ($response == null) {
- return null;
- }
- return call_user_func($this->deserialize, $response);
- }
-
- protected function _write($value) {
- return $this->active_call->write($value->serialize());
- }
-
- protected function _writesDone() {
- $this->active_call->writesDone();
- }
-
- protected function _getStatus() {
- return $this->active_call->getStatus();
- }
-}
-
-/**
- * Represents an active call that sends a single message and then gets a single
- * response.
- */
-class SimpleSurfaceActiveCall extends AbstractSurfaceActiveCall {
- /**
- * Create a new simple (single request/single response) active call.
- * @param Channel $channel The channel to communicate on
- * @param string $method The method to call on the remote server
- * @param callable $deserialize The function to deserialize a value
- * @param $arg The argument to send
- * @param array $metadata Metadata to send with the call, if applicable
- */
- public function __construct(Channel $channel,
- $method,
- callable $deserialize,
- $arg,
- $metadata = array()) {
- parent::__construct($channel, $method, $deserialize, $metadata,
- \Grpc\WRITE_BUFFER_HINT);
- $this->_write($arg);
- $this->_writesDone();
- }
-
- /**
- * Wait for the server to respond with data and a status
- * @return [response data, status]
- */
- public function wait() {
- $response = $this->_read();
- $status = $this->_getStatus();
- return array($response, $status);
- }
-}
-
-/**
- * Represents an active call that sends a stream of messages and then gets a
- * single response.
- */
-class ClientStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
- /**
- * Create a new simple (single request/single response) active call.
- * @param Channel $channel The channel to communicate on
- * @param string $method The method to call on the remote server
- * @param callable $deserialize The function to deserialize a value
- * @param Traversable $arg_iter The iterator of arguments to send
- * @param array $metadata Metadata to send with the call, if applicable
- */
- public function __construct(Channel $channel,
- $method,
- callable $deserialize,
- $arg_iter,
- $metadata = array()) {
- parent::__construct($channel, $method, $deserialize, $metadata, 0);
- foreach($arg_iter as $arg) {
- $this->_write($arg);
- }
- $this->_writesDone();
- }
-
- /**
- * Wait for the server to respond with data and a status
- * @return [response data, status]
- */
- public function wait() {
- $response = $this->_read();
- $status = $this->_getStatus();
- return array($response, $status);
- }
-}
-
-/**
- * Represents an active call that sends a single message and then gets a stream
- * of reponses
- */
-class ServerStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
- /**
- * Create a new simple (single request/single response) active call.
- * @param Channel $channel The channel to communicate on
- * @param string $method The method to call on the remote server
- * @param callable $deserialize The function to deserialize a value
- * @param $arg The argument to send
- * @param array $metadata Metadata to send with the call, if applicable
- */
- public function __construct(Channel $channel,
- $method,
- callable $deserialize,
- $arg,
- $metadata = array()) {
- parent::__construct($channel, $method, $deserialize, $metadata,
- \Grpc\WRITE_BUFFER_HINT);
- $this->_write($arg);
- $this->_writesDone();
- }
-
- /**
- * @return An iterator of response values
- */
- public function responses() {
- while(($response = $this->_read()) != null) {
- yield $response;
- }
- }
-
- public function getStatus() {
- return $this->_getStatus();
- }
-}
-
-/**
- * Represents an active call that allows for sending and recieving messages in
- * streams in any order.
- */
-class BidiStreamingSurfaceActiveCall extends AbstractSurfaceActiveCall {
-
- /**
- * Reads the next value from the server.
- * @return The next value from the server, or null if there is none
- */
- public function read() {
- return $this->_read();
- }
-
- /**
- * Writes a single message to the server. This cannot be called after
- * writesDone is called.
- * @param $value The message to send
- */
- public function write($value) {
- $this->_write($value);
- }
-
- /**
- * Indicate that no more writes will be sent
- */
- public function writesDone() {
- $this->_writesDone();
- }
-
- /**
- * Wait for the server to send the status, and return it.
- * @return object The status object, with integer $code and string $details
- * members
- */
- public function getStatus() {
- return $this->_getStatus();
- }
-} \ No newline at end of file
diff --git a/src/php/lib/autoload.php b/src/php/lib/autoload.php
new file mode 100755
index 0000000000..8ae094730a
--- /dev/null
+++ b/src/php/lib/autoload.php
@@ -0,0 +1,21 @@
+<?php
+function grpcAutoloader($class) {
+ $prefix = 'Grpc\\';
+
+ $base_dir = __DIR__ . '/Grpc/';
+
+ $len = strlen($prefix);
+ if (strncmp($prefix, $class, $len) !== 0) {
+ return;
+ }
+
+ $relative_class = substr($class, $len);
+
+ $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
+
+ if (file_exists($file)) {
+ include $file;
+ }
+}
+
+spl_autoload_register('grpcAutoloader'); \ No newline at end of file
diff --git a/src/php/tests/generated_code/GeneratedCodeTest.php b/src/php/tests/generated_code/GeneratedCodeTest.php
index d8d726e21a..42d25e4614 100755
--- a/src/php/tests/generated_code/GeneratedCodeTest.php
+++ b/src/php/tests/generated_code/GeneratedCodeTest.php
@@ -1,7 +1,5 @@
<?php
-require __DIR__ . '/../../lib/Grpc/ActiveCall.php';
-require __DIR__ . '/../../lib/Grpc/SurfaceActiveCall.php';
-require __DIR__ . '/../../lib/Grpc/BaseStub.php';
+require_once realpath(dirname(__FILE__) . '/../../lib/autoload.php');
require 'DrSlump/Protobuf.php';
\DrSlump\Protobuf::autoload();
require 'math.php';
diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php
index 9810c86272..43da47fd53 100755
--- a/src/php/tests/interop/interop_client.php
+++ b/src/php/tests/interop/interop_client.php
@@ -1,7 +1,5 @@
<?php
-require __DIR__ . '/../../lib/Grpc/ActiveCall.php';
-require __DIR__ . '/../../lib/Grpc/SurfaceActiveCall.php';
-require __DIR__ . '/../../lib/Grpc/BaseStub.php';
+require_once realpath(dirname(__FILE__) . '/../../lib/autoload.php');
require 'DrSlump/Protobuf.php';
\DrSlump\Protobuf::autoload();
require 'empty.php';