# sfThrift plugin A simple [Apache Thrift](http://incubator.apache.org/thrift/) plugin. Base for other Thrift plugins. ## Installation * Install the plugin $ symfony plugin:install sfThriftPlugin ## Configuration * Configuration is in app.yml: all: thrift_plugin: default: connector: class: TSocket param: host: 127.0.0.1 port: 9090 transport: class: TBufferedTransport protocol: class: TBinaryProtocol ## Connectors * **THttpClient** HTTP client _Params:_ * **host _[required]_** The host to connect to * **port _[optional, default: 80]_** The port to connect on * **uri _[optional, default: '']_** The URI to request * **scheme _[optional, default: 'http']_** The scheme to use for the request, i.e. http, https * **timeout _[optional, default: null]_** Read timeout * **TMemoryBuffer** A memory buffer is a tranpsort that simply reads from and writes to an in-memory string buffer. Anytime you call write on it, the data is simply placed into a buffer, and anytime you call read, data is read from that buffer. _Params:_ * **buf _[optional, default: '']_** Initial buffer value * **TPhpStream** Php stream transport. Reads to and writes from the php standard streams php://input and php://output _Params:_ * **mode _[required]_** * **TServerSocket** _Params:_ * **host _[optional, default: 'localhost']_** Host to listen on * **port _[optional: default: 9090]_** Port to listen on * **TSocket** _Params:_ * **host _[optional, default: 'localhost']_** Remote hostname * **port _[optional: default: 9090]_** Remote port * **persist _[optional, default: false]_** Whether to use a persistent socket * **send_timeout _[optional, default: 100]_** Send timeout in milliseconds * **recv_timeout _[optional, default: 750]_** Recv timeout in milliseconds * **TSocketPool** _Params:_ * **hosts _[optional, default: array('localhost')]_** List of remote hostnames * **ports _[optional default: array(9090)]_** List of remote ports, or a single common port * **persist _[optional, default: false]_** Whether to use a persistent socket * **send_timeout _[optional, default: 100]_** Send timeout in milliseconds * **recv_timeout _[optional, default: 750]_** Recv timeout in milliseconds ## Transports * **TBufferedTransport** Buffered transport. Stores data to an internal buffer that it doesn't actually write out until flush is called. For reading, we do a greedy read and then serve data out of the internal buffer. _Params:_ * **read_buf_size _[optional, default: 512]_** The receive buffer size * **write_buf_size _[optional, default: 512]_** The write buffer size * **TFramedTransport** Framed transport. Writes and reads data in chunks that are stamped with their length. _Params:_ * **read _[optional, default: false]_** Buffer for read data. * **write _[optional, default: false]_** Buffer for queued output data * **TNullTransport** Transport that only accepts writes and ignores them. This is useful for measuring the serialized size of structures. ## Protocols * **TBinaryProtocol** Binary protocol. _Params:_ * **strict_read _[optional, default: false]_** * **strict_write _[optional, default: true]_** * **TBinaryProtocolAccelerated** Accelerated binary protocol. _Params:_ * **strict_read _[optional, default: false]_** * **strict_write _[optional, default: true]_** * **TCompactProtocol** Compact protocol. ## Use * Generate files $ thrift --gen php example.thrift * Copy those generated files to your project lib directory * Remove `include ...` lines from generated files * Create a client object: $service = new example_serviceClient(ThriftProtocolFactory::factory()); ## More Thrift services We can create many named configurations: all: thrift_plugin: # First service configuration service1: connector: class: TSocket param: host: 127.0.0.1 port: 9090 transport: class: TBufferedTransport protocol: class: TBinaryProtocol # Second service configuration service2: connector: class: TSocket param: host: 192.168.1.1 port: 9091 transport: class: TFramedTransport protocol: class: TBinaryProtocolAccelerated Now we can use it: $service1 = new FirstClient(ThriftProtocolFactory::factory('service1')); $service2 = new SecondClient(ThriftProtocolFactory::factory('service2')); ## Example This is example from Thrift project site: 1. Create `UserStorage.thrift` file: struct UserProfile { 1: i32 uid, 2: string name, 3: string blurb } service UserStorage { void store(1: UserProfile user), UserProfile retrieve(1: i32 uid) } 2. Generate UserStorage service files for PHP: thrift --gen php UserStorage.thrift 3. Move generated files to proper place (like lib/thrift folder) 4. Remove `include ...` lines from generated files 5. Use client: $service = new UserStorageClient(ThriftProtocolFactory::factory()); $service->store($user); $user2 = $service->retrieve(1);