From d46a529b6a0f4e9994bb513e39f6dc672529ee19 Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Tue, 29 Nov 2016 10:49:41 -0500 Subject: [PATCH] Add support for Thrift-generated PHP code. (#3329) --- lib/linguist/generated.rb | 6 +-- samples/PHP/ThriftGenerated.php | 93 +++++++++++++++++++++++++++++++++ test/test_blob.rb | 1 + 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 samples/PHP/ThriftGenerated.php diff --git a/lib/linguist/generated.rb b/lib/linguist/generated.rb index a9ca4914..de16e683 100644 --- a/lib/linguist/generated.rb +++ b/lib/linguist/generated.rb @@ -275,16 +275,14 @@ module Linguist return lines[0].include?("Generated by the protocol buffer compiler. DO NOT EDIT!") end - APACHE_THRIFT_EXTENSIONS = ['.rb', '.py', '.go', '.js', '.m', '.java', '.h', '.cc', '.cpp'] + APACHE_THRIFT_EXTENSIONS = ['.rb', '.py', '.go', '.js', '.m', '.java', '.h', '.cc', '.cpp', '.php'] # Internal: Is the blob generated by Apache Thrift compiler? # # Returns true or false def generated_apache_thrift? return false unless APACHE_THRIFT_EXTENSIONS.include?(extname) - return false unless lines.count > 1 - - return lines[0].include?("Autogenerated by Thrift Compiler") || lines[1].include?("Autogenerated by Thrift Compiler") + return lines.first(6).any? { |l| l.include?("Autogenerated by Thrift Compiler") } end # Internal: Is the blob a C/C++ header generated by the Java JNI tool javah? diff --git a/samples/PHP/ThriftGenerated.php b/samples/PHP/ThriftGenerated.php new file mode 100644 index 00000000..0f031ffd --- /dev/null +++ b/samples/PHP/ThriftGenerated.php @@ -0,0 +1,93 @@ + array( + 'var' => 'title', + 'type' => TType::STRING, + ), + ); + } + if (is_array($vals)) { + if (isset($vals['title'])) { + $this->title = $vals['title']; + } + } + } + + public function getName() { + return 'PullRequest'; + } + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) + { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) + { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->title); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) { + $xfer = 0; + $xfer += $output->writeStructBegin('PullRequest'); + if ($this->title !== null) { + $xfer += $output->writeFieldBegin('title', TType::STRING, 1); + $xfer += $output->writeString($this->title); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } + +} diff --git a/test/test_blob.rb b/test/test_blob.rb index 588360ff..42945941 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -202,6 +202,7 @@ class TestBlob < Minitest::Test assert sample_blob_memory("JavaScript/gen-js-linguist-thrift.js").generated? assert sample_blob_memory("Ruby/gen-rb-linguist-thrift.rb").generated? assert sample_blob_memory("Objective-C/gen-cocoa-linguist-thrift.m").generated? + assert sample_blob_memory("PHP/ThriftGenerated.php").generated? # Generated JNI assert sample_blob_memory("C/jni_layer.h").generated?