Skip to content

Commit 1e33912

Browse files
authored
Merge pull request #4 from wave-framework/php-8.4-compatibility
Php 8.4 compatibility
2 parents 91a233e + edaf155 commit 1e33912

File tree

9 files changed

+104
-46
lines changed

9 files changed

+104
-46
lines changed

bin/model-gen

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env php
22
<?php
3+
declare(strict_types=1);
34

45

56
use Wave\SDK\ModelGenerator\Generator;
67
use Wave\SDK\ModelGenerator\Input\Swagger;
78

89
ini_set('display_errors', '1');
9-
error_reporting(E_ALL | E_STRICT);
10+
error_reporting(E_ALL);
1011

1112
if(!file_exists('bootstrap.php'))
1213
sg_error("This file should be run from the application root");

bin/schema-gen

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env php
22
<?php
3+
declare(strict_types=1);
34

45
use Wave\Config;
56
use Wave\SDK\SchemaGenerator\Output\Swagger;
67
use Wave\SDK\SchemaGenerator\Parser\FromRoutes;
78
use Symfony\Component\Yaml\Yaml;
89

910
ini_set('display_errors', '1');
10-
error_reporting(E_ALL | E_STRICT);
11+
error_reporting(E_ALL);
1112

1213
if(!file_exists('bootstrap.php'))
1314
sg_error("This file should be run from the application root");
@@ -39,8 +40,18 @@ if (Config::get('app')->offsetExists('meta')){
3940
$meta_info = Config::get('app')->meta->getArrayCopy();
4041
}
4142

42-
if('' !== $git_path = trim(shell_exec('which git'))){
43-
$meta_info['version'] = trim(shell_exec(sprintf('%s rev-parse --verify --short HEAD', $git_path)));
43+
$git_path_output = shell_exec('command -v git');
44+
if(is_string($git_path_output)){
45+
$git_path = trim($git_path_output);
46+
if($git_path !== ''){
47+
$git_version_output = shell_exec(sprintf('%s rev-parse --verify --short HEAD', escapeshellarg($git_path)));
48+
if(is_string($git_version_output)){
49+
$git_version_output = trim($git_version_output);
50+
if($git_version_output !== ''){
51+
$meta_info['version'] = $git_version_output;
52+
}
53+
}
54+
}
4455
}
4556

4657
$output = $generator->generate($meta_info);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"description": "Generates a schema file describing a wave app, then builds a PHP SDK from it",
44

55
"require": {
6-
"phindmarsh/statham": "1.0.1",
6+
"php": "^8.1",
7+
"phindmarsh/statham": "1.0.3",
78
"twig/twig": "3.*"
89
},
910

src/ModelGenerator/Generator.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
declare(strict_types=1);
34

45
namespace Wave\SDK\ModelGenerator;
56

@@ -9,16 +10,20 @@
910

1011
class Generator {
1112

12-
private static $defaults = [];
13+
private static array $defaults = [];
1314

14-
private $twig;
15+
private Schema $input;
16+
17+
private array $args;
18+
19+
private Environment $twig;
1520

1621
public function __construct(Schema $input, array $args = []) {
1722

1823
$this->input = $input;
1924
$this->args = array_merge(self::$defaults, $args);
2025

21-
$loader = new FilesystemLoader(__DIR__ . DS . 'Templates');
26+
$loader = new FilesystemLoader(__DIR__ . DIRECTORY_SEPARATOR . 'Templates');
2227
$this->twig = new Environment($loader, array(
2328
'autoescape' => false,
2429
'debug' => true
@@ -33,27 +38,30 @@ public function __construct(Schema $input, array $args = []) {
3338

3439
}));
3540
$this->twig->addFilter(new TwigFilter('print_r', 'print_r'));
36-
$this->twig->addFilter(new TwigFilter('explode', function($a, $d){
41+
$this->twig->addFilter(new TwigFilter('explode', function($a, $d){
3742
if ($a === null) {
3843
return [""];
3944
}
4045

41-
return explode($d, $a);
46+
return explode($d, $a);
4247
}));
4348
}
4449

45-
public function generate($output_directory, $base_namespace, $base_model_class){
50+
/**
51+
* @return array<string, array<int, string>>
52+
*/
53+
public function generate(string $output_directory, string $base_namespace, string $base_model_class): array{
4654

4755
$schemas = $this->input->getSchemaNames();
4856

4957
$base_namespace = trim($base_namespace, '\\');
50-
$output_directory = rtrim($output_directory, '/') . '/';
58+
$output_directory = rtrim($output_directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
5159

5260
$files_written = [];
5361
foreach($schemas as $schema){
5462

5563
$namespace = $base_namespace . '\\' . ucfirst($schema);
56-
$directory = $output_directory . ucfirst($schema) . DS;
64+
$directory = $output_directory . ucfirst($schema) . DIRECTORY_SEPARATOR;
5765

5866
$files = $this->generateModelsFor($schema, $directory, $namespace, $base_model_class);
5967
$files_written[$schema] = $files;
@@ -63,7 +71,10 @@ public function generate($output_directory, $base_namespace, $base_model_class){
6371

6472
}
6573

66-
public function generateModelsFor($schema, $base_directory, $base_namespace, $base_model_class){
74+
/**
75+
* @return array<int, string>
76+
*/
77+
public function generateModelsFor(string $schema, string $base_directory, string $base_namespace, string $base_model_class): array{
6778

6879
$models = $this->input->getModels($schema);
6980
$meta = $this->input->getMeta($schema);
@@ -78,7 +89,7 @@ public function generateModelsFor($schema, $base_directory, $base_namespace, $ba
7889
$ns = substr($class, 0, $split);
7990
$class = substr($class, $split + 1);
8091
$namespace = $base_namespace . '\\' . $ns;
81-
$directory = $base_directory . str_replace('\\', '/', $ns) . DS;
92+
$directory = $base_directory . str_replace('\\', DIRECTORY_SEPARATOR, $ns) . DIRECTORY_SEPARATOR;
8293
}
8394

8495
$template_data = [
@@ -90,20 +101,22 @@ public function generateModelsFor($schema, $base_directory, $base_namespace, $ba
90101
];
91102

92103
$base_model = $this->twig->render('base-model.phpt', $template_data);
93-
$base_filename = sprintf('%sBase/%s.php', $directory, $class);
104+
$base_filename = $directory . 'Base' . DIRECTORY_SEPARATOR . $class . '.php';
94105
$this->createDirectory(dirname($base_filename));
95106
$written = file_put_contents($base_filename, $base_model);
96107

97-
if($written > 0)
108+
if($written > 0) {
98109
$files_written[] = $base_filename;
110+
}
99111

100-
$stub_filename = sprintf('%s%s.php', $directory, $class);
112+
$stub_filename = $directory . $class . '.php';
101113
if(!file_exists($stub_filename)){
102114
$stub_model = $this->twig->render('stub-model.phpt', $template_data);
103115
$written = file_put_contents($stub_filename, $stub_model);
104116

105-
if($written > 0)
117+
if($written > 0) {
106118
$files_written[] = $stub_filename;
119+
}
107120
}
108121

109122
}
@@ -112,15 +125,17 @@ public function generateModelsFor($schema, $base_directory, $base_namespace, $ba
112125

113126
}
114127

115-
private function createDirectory($directory){
128+
private function createDirectory(string $directory): bool{
116129
if(file_exists($directory) && !is_dir($directory)) {
117130
throw new \RuntimeException("[{$directory}] already exists and is not a directory");
118131
}
119132
else if(!is_dir($directory)){
120-
@mkdir($directory, 0770, true);
121-
if(!is_dir($directory) || !is_writable($directory)) {
133+
if(!@mkdir($directory, 0770, true) && !is_dir($directory)) {
122134
throw new \RuntimeException("Failed to create directory [{$directory}]");
123135
}
136+
if(!is_writable($directory)) {
137+
throw new \RuntimeException("Directory [{$directory}] is not writable");
138+
}
124139
return true;
125140
}
126141

src/ModelGenerator/Loader.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
* @author Michael Calcinai <[email protected]>
66
*/
77

8-
namespace Wave\SDK\ModelGenerator;
8+
declare(strict_types=1);
99

10+
namespace Wave\SDK\ModelGenerator;
1011

1112
abstract class Loader {
1213

13-
14-
1514
public static function create(array $config){
1615

1716
switch($config['type']){
1817
case 'git':
1918
return new Loader\Git($config);
2019
case 'file':
2120
return new Loader\File($config);
21+
case 'string':
22+
return new Loader\StringLoader($config);
2223
default:
23-
throw new \InvalidArgumentException(sprintf('[%s] is not a supported loader type'));
24+
$type = $config['type'] ?? 'unknown';
25+
throw new \InvalidArgumentException(sprintf('[%s] is not a supported loader type', $type));
2426
}
2527

2628

2729
}
2830

2931
abstract public function getContent();
3032

31-
}
33+
}

src/ModelGenerator/Loader/File.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,33 @@
44
* @author Michael Calcinai <[email protected]>
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Wave\SDK\ModelGenerator\Loader;
810

911
use Wave\SDK\ModelGenerator\Loader;
1012

1113
class File extends Loader {
1214

13-
private $file;
15+
private string $file;
1416

1517
/**
16-
* File constructor.
17-
* @param array $config
18+
* @param array{file: string} $config
1819
*/
19-
public function __construct($config) {
20+
public function __construct(array $config) {
21+
if(empty($config['file'])){
22+
throw new \InvalidArgumentException('File loader configuration requires a "file" key');
23+
}
24+
2025
$this->file = $config['file'];
2126
}
2227

23-
public function getContent() {
24-
return file_get_contents($this->file);
28+
public function getContent(): string{
29+
$content = @file_get_contents($this->file);
30+
if($content === false){
31+
throw new \RuntimeException(sprintf('Failed to read file [%s]', $this->file));
32+
}
33+
34+
return $content;
2535
}
26-
}
36+
}

src/ModelGenerator/Loader/Git.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,43 @@
44
* @author Michael Calcinai <[email protected]>
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Wave\SDK\ModelGenerator\Loader;
810

911
use Wave\SDK\ModelGenerator\Loader;
1012

1113
class Git extends Loader {
1214

13-
private $repository;
14-
private $file;
15+
private string $repository;
16+
private string $file;
1517

1618
/**
17-
* Git constructor.
18-
* @param array $config
19+
* @param array{repository: string, file: string} $config
1920
*/
20-
public function __construct($config) {
21+
public function __construct(array $config) {
22+
if(empty($config['repository']) || empty($config['file'])){
23+
throw new \InvalidArgumentException('Git loader configuration requires both "repository" and "file" keys');
24+
}
25+
2126
$this->repository = $config['repository'];
2227
$this->file = $config['file'];
2328
}
2429

25-
public function getContent() {
30+
public function getContent(): string{
31+
32+
$command = sprintf(
33+
'git archive --remote=%s HEAD %s | tar -xO',
34+
escapeshellarg($this->repository),
35+
escapeshellarg($this->file)
36+
);
37+
38+
$result = shell_exec($command);
39+
40+
if($result === null){
41+
throw new \RuntimeException(sprintf('Failed to retrieve [%s] from repository [%s]', $this->file, $this->repository));
42+
}
2643

27-
$command = sprintf('git archive --remote=%s HEAD %s | tar -xO', $this->repository, $this->file);
28-
return trim(shell_exec($command));
44+
return trim($result);
2945
}
30-
}
46+
}

src/ModelGenerator/Loader/String.php renamed to src/ModelGenerator/Loader/StringLoader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
* @author Michael Calcinai <[email protected]>
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Wave\SDK\ModelGenerator\Loader;
810

911
use Wave\SDK\ModelGenerator\Loader;
1012

11-
class String extends Loader {
13+
class StringLoader extends Loader {
1214

1315
public function getContent() {
1416
// TODO: Implement getContent() method.
1517
}
16-
}
18+
}

src/SchemaGenerator/Definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Wave\SDK\SchemaGenerator;
55

6-
6+
#[\AllowDynamicProperties]
77
class Definition {
88

99
public function __construct($data = null){

0 commit comments

Comments
 (0)