From 2258a4632d4f032f1e1a0d6af2f6f6aa7230370a Mon Sep 17 00:00:00 2001 From: wing328 Date: Sat, 27 Jun 2015 22:54:37 +0800 Subject: [PATCH] fix test.php, fix default configuration --- .../resources/php/ObjectSerializer.mustache | 15 +++------ .../main/resources/php/configuration.mustache | 32 +++++++++++++++++-- .../SwaggerClient-php/lib/Configuration.php | 17 +++++----- .../lib/ObjectSerializer.php | 13 +++----- samples/client/petstore/php/test.php | 24 +++++++------- 5 files changed, 60 insertions(+), 41 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache b/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache index 4463236063..930313865c 100644 --- a/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache @@ -134,22 +134,15 @@ class ObjectSerializer { settype($data, $class); $deserialized = $data; } elseif ($class === '\SplFileObject') { - # determine temp folder path - if (!isset(Configuration::getDefaultConfig->$tempFolderPath) || '' === Configuration::$tempFolderPath) { - $tmpFolderPath = sys_get_temp_dir(); - } else { - $tmpFolderPath = Configuration::tempFolderPath; - } - # determine file name - if (preg_match('/Content-Disposition: inline; filename=(.*)/i', $httpHeader, $match)) { - $filename = $tmpFolderPath.$match[1]; + if (preg_match('/Content-Disposition: inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader, $match)) { + $filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1]; } else { - $filename = tempnam($tmpFolderPath, ''); + $filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), ''); } $deserialized = new \SplFileObject($filename, "w"); $byte_written = $deserialized->fwrite($data); - error_log("[INFO] Written $byte_written byte to $filename. Please move the file to a proper folder or delete the temp file afterwards", 3, Configuration::getDefaultConfiguration()->getDebugFile()); + error_log("[INFO] Written $byte_written byte to $filename. Please move the file to a proper folder or delete the temp file after processing.\n" , 3, Configuration::getDefaultConfiguration()->getDebugFile()); } else { $instance = new $class(); diff --git a/modules/swagger-codegen/src/main/resources/php/configuration.mustache b/modules/swagger-codegen/src/main/resources/php/configuration.mustache index 94e0165b4a..710edab55b 100644 --- a/modules/swagger-codegen/src/main/resources/php/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/php/configuration.mustache @@ -50,6 +50,16 @@ class Configuration { /** @var string Debug file location (log to STDOUT by default) */ protected $debugFile = 'php://output'; + + /** @var string Debug file location (log to STDOUT by default) */ + protected $tempFolderPath; + + /** + * @param string $tempFolderPath + */ + public function __construct() { + $this->tempFolderPath = sys_get_temp_dir(); + } /** * @param string $key @@ -248,13 +258,30 @@ class Configuration { public function getDebugFile() { return $this->debugFile; } - + + /** + * @param string $tempFolderPath + * @return Configuration + */ + public function setTempFolderPath($tempFolderPath) { + $this->tempFolderPath = $tempFolderPath; + return $this; + } + + /** + * @return string + */ + public function getTempFolderPath() { + return $this->tempFolderPath; + } + + /** * @return Configuration */ public static function getDefaultConfiguration() { if (self::$defaultConfiguration == null) { - return new Configuration(); + self::$defaultConfiguration = new Configuration(); } return self::$defaultConfiguration; @@ -276,6 +303,7 @@ class Configuration { $report .= " PHP Version: ".phpversion()."\n"; $report .= " Swagger Spec Version: {{version}}\n"; $report .= " SDK Package Version: {{version}}\n"; + $report .= " Temp Folder Path: ".self::getDefaultConfiguration()->getTempFolderPath()."\n"; return $report; } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php index 5f40dcfe3b..664293f1a1 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php @@ -50,18 +50,17 @@ class Configuration { /** @var string Debug file location (log to STDOUT by default) */ protected $debugFile = 'php://output'; - + /** @var string Debug file location (log to STDOUT by default) */ protected $tempFolderPath; /** * @param string $tempFolderPath - */ + */ public function __construct() { $this->tempFolderPath = sys_get_temp_dir(); } - - + /** * @param string $key * @param string $value @@ -259,16 +258,16 @@ class Configuration { public function getDebugFile() { return $this->debugFile; } - + /** - * @param string $debugFile + * @param string $tempFolderPath * @return Configuration */ public function setTempFolderPath($tempFolderPath) { $this->tempFolderPath = $tempFolderPath; return $this; } - + /** * @return string */ @@ -276,12 +275,13 @@ class Configuration { return $this->tempFolderPath; } + /** * @return Configuration */ public static function getDefaultConfiguration() { if (self::$defaultConfiguration == null) { - return new Configuration(); + self::$defaultConfiguration = new Configuration(); } return self::$defaultConfiguration; @@ -303,6 +303,7 @@ class Configuration { $report .= " PHP Version: ".phpversion()."\n"; $report .= " Swagger Spec Version: 1.0.0\n"; $report .= " SDK Package Version: 1.0.0\n"; + $report .= " Temp Folder Path: ".self::getDefaultConfiguration()->getTempFolderPath()."\n"; return $report; } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php index dd68d067c6..3f78ea55bb 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -134,20 +134,15 @@ class ObjectSerializer { settype($data, $class); $deserialized = $data; } elseif ($class === '\SplFileObject') { - # determine temp folder path - $tmpFolderPath = Configuration::getDefaultConfiguration()->getTempFolderPath(); - print_r($tmpFolderPath); - # determine file name - if (preg_match('/Content-Disposition: inline; filename=(.*)$/i', $httpHeader, $match)) { - $filename = $tmpFolderPath.$match[1]; + if (preg_match('/Content-Disposition: inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader, $match)) { + $filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1]; } else { - print_r($tmpFolderPath); - $filename = tempnam($tmpFolderPath, ''); + $filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), ''); } $deserialized = new \SplFileObject($filename, "w"); $byte_written = $deserialized->fwrite($data); - error_log("[INFO] Written $byte_written byte to $filename. Please move the file to a proper folder or delete the temp file afterwards", 3, Configuration::getDefaultConfiguration()->getDebugFile()); + error_log("[INFO] Written $byte_written byte to $filename. Please move the file to a proper folder or delete the temp file after processing.\n" , 3, Configuration::getDefaultConfiguration()->getDebugFile()); } else { $instance = new $class(); diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index bec9f4e6e7..79cc4ca4ad 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -5,22 +5,24 @@ require_once(__DIR__ . '/SwaggerClient-php/autoload.php'); //ini_set('display_errors', 1); //error_reporting(~0); -// initialize the API client -//$api_client = new SwaggerClient\ApiClient('http://petstore.swagger.io/v2'); -//$api_client->addDefaultHeader("test1", "value1"); - // to enable logging -//SwaggerClient\Configuration::$debug = true; -//SwaggerClient\Configuration::$debug_file = '/var/tmp/php_debug.log'; +//Swagger\Client\Configuration::$debug = true; +//Swagger\Client\Configuration::$debug_file = '/var/tmp/php_debug.log'; + +// to debug report +print Swagger\Client\Configuration::toDebugReport(); + +// to change temp folder path Swagger\Client\Configuration::getDefaultConfiguration()->setTempFolderPath('/var/tmp/php/'); $petId = 10005; // ID of pet that needs to be fetched try { // get pet by id - //$pet_api = new SwaggerClient\PetAPI($api_client); + //$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2'); + //$api_client->getConfig()->addDefaultHeader("test1", "value1"); + //$pet_api = new Swagger\Client\PetAPI($api_client); $pet_api = new Swagger\Client\Api\PetAPI(); $pet_api->getApiClient()->getConfig()->setTempFolderPath('/var/tmp/php/'); - print_r($pet_api->getApiClient()->getConfig()->getTempFolderPath()); // test default header //$pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903"); // return Pet (model) @@ -49,10 +51,10 @@ try { // add a new pet (model) $add_response = $pet_api->addPet($new_pet); - // test upload file (exception) - //$upload_response = $pet_api->uploadFile($petId, "test meta", NULL); + // test upload file (should return exception) + $upload_response = $pet_api->uploadFile($petId, "test meta", NULL); -} catch (Swagger\Client\Exception $e) { +} catch (Swagger\Client\ApiException $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; echo 'HTTP response headers: ', $e->getResponseHeaders(), "\n"; echo 'HTTP response body: ', $e->getResponseBody(), "\n";