Skip to content

Commit 311cc2b

Browse files
committed
Fix types.json generation
1 parent 2a2ecd7 commit 311cc2b

File tree

3 files changed

+5862
-6
lines changed

3 files changed

+5862
-6
lines changed

templates/json/ApiRenderer.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @link https://www.yiiframework.com/
45
* @copyright Copyright (c) 2008 Yii Software LLC
@@ -13,7 +14,6 @@
1314
use yii\apidoc\models\TraitDoc;
1415
use yii\apidoc\renderers\ApiRenderer as BaseApiRenderer;
1516
use yii\base\ViewContextInterface;
16-
use Yii;
1717

1818
/**
1919
* The class for outputting documentation data structures as a JSON text.
@@ -32,8 +32,8 @@ class ApiRenderer extends BaseApiRenderer implements ViewContextInterface
3232
public function render($context, $targetDir)
3333
{
3434
$types = array_merge($context->classes, $context->interfaces, $context->traits);
35-
foreach($types as $name => $type) {
36-
$types[$name] = (array) $type;
35+
foreach ($types as $name => $type) {
36+
$types[$name] = (array) $this->removeParentFieldsRecursive($type);
3737
if ($type instanceof ClassDoc) {
3838
$types[$name]['type'] = 'class';
3939
} elseif ($type instanceof InterfaceDoc) {
@@ -42,7 +42,41 @@ public function render($context, $targetDir)
4242
$types[$name]['type'] = 'trait';
4343
}
4444
}
45-
file_put_contents($targetDir . '/types.json', json_encode($types, JSON_PRETTY_PRINT));
45+
file_put_contents($targetDir . '/types.json', json_encode($types, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
46+
}
47+
48+
/**
49+
* Removes the `parent` fields recursively so that the data could be converted to JSON format..
50+
* @param array|object $data
51+
* @return array|object
52+
*/
53+
private function removeParentFieldsRecursive($data)
54+
{
55+
if (!is_array($data) && !is_object($data)) {
56+
return $data;
57+
}
58+
59+
if (is_array($data)) {
60+
foreach ($data as $key => $value) {
61+
if ($key === 'parent') {
62+
unset($data[$key]);
63+
} elseif (is_object($value) || is_array($value)) {
64+
$data[$key] = $this->removeParentFieldsRecursive($value);
65+
}
66+
}
67+
68+
return $data;
69+
}
70+
71+
foreach (get_object_vars($data) as $key => $value) {
72+
if ($key === 'parent') {
73+
unset($data->$key);
74+
} elseif (is_object($value) || is_array($value)) {
75+
$data->$key = $this->removeParentFieldsRecursive($value);
76+
}
77+
}
78+
79+
return $data;
4680
}
4781

4882
/**

tests/commands/ApiControllerTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ protected function generateApi($sourceDirs, $targetDir = '@runtime', array $args
5656

5757
// Tests :
5858

59-
public function testNoFiles()
59+
public function testNoFiles(): void
6060
{
6161
$output = $this->generateApi(Yii::getAlias('@yiiunit/apidoc/data/guide'));
6262

6363
$this->assertNotEmpty($output);
6464
$this->assertStringContainsString('Error: No files found to process', $output);
6565
}
6666

67-
public function testGenerateBootstrap()
67+
public function testGenerateBootstrap(): void
6868
{
6969
$sourceFilesDir = Yii::getAlias('@yiiunit/apidoc/data/api');
7070
$output = $this->generateApi($sourceFilesDir, '@runtime', ['template' => 'bootstrap']);
@@ -111,4 +111,15 @@ public function testGenerateBootstrap()
111111
$errorsContent = preg_replace('/(\s*\[file\] => ).*(\/tests\/.*\.php)/', '$1$2', $errorsContent);
112112
$this->assertMatchesTextSnapshot($errorsContent);
113113
}
114+
115+
public function testGenerateJson(): void
116+
{
117+
$sourceFilesDir = Yii::getAlias('@yiiunit/apidoc/data/api');
118+
$output = $this->generateApi($sourceFilesDir, '@runtime', ['template' => 'json']);
119+
120+
$this->assertNotEmpty($output);
121+
$this->assertStringContainsString('Updating cross references and backlinks... done.', $output);
122+
123+
$this->assertMatchesJsonSnapshot(file_get_contents(Yii::getAlias('@runtime') . '/types.json'));
124+
}
114125
}

0 commit comments

Comments
 (0)