Skip to content

Commit

Permalink
Update to ensure still works on php7.4 and simplify use of psr7
Browse files Browse the repository at this point in the history
  • Loading branch information
M1ke committed Aug 28, 2024
1 parent f588b9c commit 61ca194
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 999 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
}
],
"require": {
"php": ">=7.2 <8.3",
"php": ">=7.4 <8.3",
"nette/php-generator": "3.6.9",
"symfony/yaml": "5.4.35",
"symfony/serializer": "5.4.36",
"psr/http-message": "^1.0"
"psr/http-message": "^1.1 || ^2.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest"
Expand Down
301 changes: 189 additions & 112 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"dir",
"more-specificity",
];
$options = getopt("", array_map(function($option){
return "{$option}:";
}, $opt_fields));
$options = getopt("", array_map(static fn($option) => "{$option}:", $opt_fields));

$opt_errors = [];
foreach ($opt_fields as $opt_field){
Expand Down
54 changes: 3 additions & 51 deletions src/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ abstract class ClassGenerator {
public const NAMESPACE_MODEL = 'Models';
public const NAMESPACE_REQUEST = 'Requests';

/**
* @var string
*/
private $namespace_name;
private string $namespace_name;

/**
* @var ClassType[]
*/
public $classes = [];
public array $classes = [];

/**
* Creates a generator for a specific namespace
*
* @param string $namespace_name
*/
public function __construct(string $namespace_name){
$this->namespace_name = $this->stringNotEndWith($namespace_name, '\\');
Expand All @@ -55,11 +50,6 @@ abstract public function saveClasses(string $dir);

abstract public function generate(string $file_path);

/**
* @param string $yaml_path
* @param string $dir
* @return int
*/
public function runFull(string $yaml_path, string $dir): int{
$this->generate($yaml_path);

Expand All @@ -69,13 +59,7 @@ public function runFull(string $yaml_path, string $dir): int{
return count($this->classes);
}

/**
* @param string $yaml_path
* @param string $dir
*
* @return int
*/
public function runFullWithMoreSpecificity(string $yaml_path, string $dir){
public function runFullWithMoreSpecificity(string $yaml_path, string $dir): int{
$this->generate($yaml_path, true);

$this->saveClasses($dir);
Expand All @@ -87,9 +71,6 @@ public function runFullWithMoreSpecificity(string $yaml_path, string $dir){
/**
* Saves generated classes down as PHP files
*
* @param string $dir
* @param string $namespace_name
* @param string $use
* @throws RuntimeException
* @throws FileNotFoundException
*/
Expand All @@ -110,10 +91,6 @@ protected function saveClassesInternal(string $dir, string $namespace_name, stri
abstract public function dumpParentClass(string $dir);

/**
* @param string $dir
* @param string $file
* @param string $namespace
* @param string $namespace_use
* @throws FileNotFoundException
*/
protected function dumpParentInternal(string $dir, string $file, string $namespace, string $namespace_use = ''): void{
Expand All @@ -128,21 +105,10 @@ protected function dumpParentInternal(string $dir, string $file, string $namespa
file_put_contents("$dir/$file_name", $content);
}

/**
* Utility function
*
* @param string $string
* @param string $char
* @return string
*/
protected function stringNotEndWith(string $string, string $char): string{
return $string[strlen($string)-1]===$char ? substr($string, 0, -1) : $string;
}

/**
* @param string $string
* @return string
*/
protected function unPlural(string $string): string{
if (substr($string, -3)==='ies'){
return substr($string, 0, -3).'y';
Expand All @@ -156,9 +122,6 @@ protected function unPlural(string $string): string{

/**
* Changes a Swagger definition into a type
*
* @param array $property
* @return string
*/
protected function typeFromRef(array $property): string{
if (!isset($property['$ref'])){
Expand All @@ -168,20 +131,13 @@ protected function typeFromRef(array $property): string{
return str_replace('#/definitions/', '', $property['$ref']);
}

/**
* @param string $dir
* @param string $namespace
* @return string
*/
protected function dirNamespace(string $dir, string $namespace): string{
$dir = $this->stringNotEndWith($dir, '/');

return "$dir/$namespace";
}

/**
* @param string $dir
* @return string
* @throws FileNotFoundException
*/
private function checkDir(string $dir): string{
Expand All @@ -195,10 +151,6 @@ private function checkDir(string $dir): string{
return $dir;
}

/**
* @param string $type
* @return bool
*/
protected function notScalarType(string $type): bool{
return !in_array($type, ['integer', 'string', 'boolean', 'number', 'null']);
}
Expand Down
9 changes: 2 additions & 7 deletions src/GenerateAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace SwaggerGen;

class GenerateAll {
public $saved_models = 0;
public $saved_requests = 0;
public int $saved_models = 0;
public int $saved_requests = 0;

/**
* Generates all required files into the specified directory
*
* @param string $namespace
* @param string $yaml_path
* @param string $dir
* @param bool $more_specificity
*/
public function __construct(string $namespace, string $yaml_path, string $dir, bool $more_specificity = false){
$generate_models = new GenerateModels($namespace);
Expand Down
23 changes: 0 additions & 23 deletions src/GenerateModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace SwaggerGen;

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PhpNamespace;
use Nette\PhpGenerator\Property;
use RuntimeException;
Expand All @@ -16,8 +15,6 @@ class GenerateModels extends ClassGenerator {

/**
* Generates classes in the "classes" field
*
* @param string $file_path
* @throws RuntimeException
*/
public function generate(string $file_path) :void{
Expand Down Expand Up @@ -55,9 +52,6 @@ public function generate(string $file_path) :void{
}

/**
* @param array $properties
* @param ClassType $class
* @param array $required
* @throws RuntimeException
*/
private function classProperties(array $properties, ClassType $class, ?array $required): void{
Expand All @@ -83,9 +77,6 @@ private function classProperties(array $properties, ClassType $class, ?array $re
$typehint = $type;
}

/**
* @var Property $property
*/
$property = $class->addProperty($property_name)->setVisibility('protected');
$property->addComment($property_details['description'] ?? "\n");

Expand Down Expand Up @@ -136,9 +127,6 @@ private function classProperties(array $properties, ClassType $class, ?array $re
$class->addMethod('get'.$capital_case)
->setBody("return \$this->$property_name;")
->addComment("@return $comment_type");
/**
* @var Method $setter
*/
$setter = $class->addMethod('set'.$capital_case)
->setBody("\$this->$property_name = \$$property_name;\n\nreturn \$this;")
->addComment("@param $comment_type \$$property_name")
Expand All @@ -153,9 +141,6 @@ private function classProperties(array $properties, ClassType $class, ?array $re
if ($sub_type){
$property_name_singular = $this->unPlural($property_name);
$capital_case_singular = $this->unPlural($capital_case);
/**
* @var Method $add_to
*/
$add_to = $class->addMethod('add'.$capital_case_singular)
->setBody("\$this->{$property_name}[] = \$$property_name_singular;\n\nreturn \$this;");

Expand All @@ -174,25 +159,17 @@ private function classProperties(array $properties, ClassType $class, ?array $re
}
}

/**
* @param string $dir
*/
public function saveClasses(string $dir) :void{
$dir = $this->dirNamespace($dir, self::NAMESPACE_MODEL);
$this->saveClassesInternal($dir, $this->namespaceModel());
}

/**
* @param string $dir
*/
public function dumpParentClass(string $dir) :void{
$dir = $this->dirNamespace($dir, self::NAMESPACE_MODEL);
$this->dumpParentInternal($dir, __DIR__.'/SwaggerModel.php', $this->namespaceModel());
}

/**
* @param Property $property
* @param string $type
* @throws RuntimeException
*/
private function blankValue(Property $property, string $type): void{
Expand Down
2 changes: 1 addition & 1 deletion src/GenerateRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ protected function handleResponse(array $method_details, ClassType $class): void
$type = "''";
}

$response_models[] = "'$code_string' => '$type'";
$response_models[] = "'$code_string' => $type";

if ((int)$code_string>0 && (int)$code_string<400){
$has_2xx = true;
Expand Down
10 changes: 7 additions & 3 deletions src/SwaggerClient.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php
namespace SwaggerGen;

use Psr\Http\Message\RequestInterface;
use SwaggerGen\SwaggerModel;

interface SwaggerClient {

/**
* @param SwaggerRequest $request
* @param string[] $response_models
* @return SwaggerModel|SwaggerModel[]
* @template T of SwaggerModel
*
* @param array<array-key, class-string<T>> $response_models
* @return T|list<T>
*/
public function make(SwaggerRequest $request, array $response_models);

public function messageFromRequest(SwaggerRequest $swagger):RequestInterface;
}
18 changes: 3 additions & 15 deletions src/SwaggerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
class SwaggerModel implements JsonSerializable {
public const DATE_FORMAT = 'Y-m-d H:i:s';

private $_is_error = false;
private bool $_is_error = false;

/**
* @return array
*/
public function toArray(): array{
$this->preOutput();

Expand All @@ -24,9 +21,7 @@ public function toArray(): array{
$data[$key] = $val;
}

$data = $this->toArrayData($data);

return $data;
return $this->toArrayData($data);
}

/**
Expand All @@ -38,15 +33,11 @@ protected function preOutput(): void{
/**
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize(){
return $this->toArray();
}

/**
* @param array $data
*
* @return array
*/
private function toArrayData(array $data): array{
foreach ($data as &$val){

Expand All @@ -70,9 +61,6 @@ private function toArrayData(array $data): array{
return $data;
}

/**
* @return bool
*/
public function isError(): bool{
return $this->_is_error;
}
Expand Down
Loading

0 comments on commit 61ca194

Please sign in to comment.