Skip to content

Commit 6cab3c5

Browse files
committed
simplified code now, using generic file writing service
1 parent dc6e0e7 commit 6cab3c5

5 files changed

Lines changed: 124 additions & 40 deletions

File tree

Annotations/AnnotationGenerator.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
use Piwik\API\NoDefaultValue;
2121
use Piwik\API\Proxy;
2222
use Piwik\API\Request;
23-
use Piwik\Filesystem;
2423
use Piwik\Http;
2524
use Piwik\Piwik;
2625
use Piwik\Plugin\Manager;
26+
use Piwik\Plugins\OpenApiDocs\Artifact\ArtifactWriter;
2727
use Piwik\Plugins\OpenApiDocs\OpenApiDocs;
2828
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
2929
use Piwik\SettingsPiwik;
@@ -79,6 +79,11 @@ class AnnotationGenerator
7979
*/
8080
protected $pathResolver;
8181

82+
/**
83+
* @var ArtifactWriter
84+
*/
85+
protected $artifactWriter;
86+
8287
/**
8388
* @var array[]
8489
*/
@@ -89,10 +94,14 @@ class AnnotationGenerator
8994
*/
9095
protected $missingImportantDataWarnings;
9196

92-
public function __construct(DocumentationGenerator $generator, ?PathResolver $pathResolver = null)
93-
{
97+
public function __construct(
98+
DocumentationGenerator $generator,
99+
?PathResolver $pathResolver = null,
100+
?ArtifactWriter $artifactWriter = null
101+
) {
94102
$this->generator = $generator;
95103
$this->pathResolver = $pathResolver ?? new PathResolver();
104+
$this->artifactWriter = $artifactWriter ?? new ArtifactWriter();
96105
$this->missingImportantDataWarnings = [];
97106
$this->currentPluginDir = Manager::getInstance()::getPluginDirectory('OpenApiDocs');
98107
}
@@ -1029,10 +1038,7 @@ protected function getCachedExampleResponseFile(string $pluginName, string $meth
10291038

10301039
protected function writeFile(string $filePath, string $contents)
10311040
{
1032-
$directory = dirname($filePath);
1033-
Filesystem::mkdir($directory);
1034-
1035-
return file_put_contents($filePath, $contents);
1041+
return $this->artifactWriter->writeFile($filePath, $contents);
10361042
}
10371043

10381044
/**

Annotations/ApiMethodInfoExtractor.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Piwik\Exception\PluginNotFoundException;
1515
use Piwik\API\Proxy;
1616
use Piwik\API\Request;
17-
use Piwik\Filesystem;
1817
use Piwik\Plugin\Manager;
18+
use Piwik\Plugins\OpenApiDocs\Artifact\ArtifactWriter;
1919
use Piwik\Plugins\OpenApiDocs\Specs\PathResolver;
2020
use Piwik\Validators\BaseValidator;
2121
use Piwik\Validators\NotEmpty;
@@ -27,9 +27,15 @@ class ApiMethodInfoExtractor
2727
*/
2828
private $pathResolver;
2929

30-
public function __construct(?PathResolver $pathResolver = null)
30+
/**
31+
* @var ArtifactWriter
32+
*/
33+
private $artifactWriter;
34+
35+
public function __construct(?PathResolver $pathResolver = null, ?ArtifactWriter $artifactWriter = null)
3136
{
3237
$this->pathResolver = $pathResolver ?? new PathResolver();
38+
$this->artifactWriter = $artifactWriter ?? new ArtifactWriter();
3339
}
3440

3541
/**
@@ -73,8 +79,7 @@ public function extractMethodInfo(string $pluginName, bool $writeToFile = false)
7379

7480
if ($writeToFile) {
7581
$pluginSpecPath = $this->pathResolver->getApiMethodInfoFilePath($fileBaseName);
76-
Filesystem::mkdir(dirname($pluginSpecPath));
77-
file_put_contents($pluginSpecPath, $methodInfoString);
82+
$this->artifactWriter->writeFile($pluginSpecPath, $methodInfoString);
7883
}
7984

8085
return $methodInfoString;

Artifact/ArtifactWriter.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\OpenApiDocs\Artifact;
13+
14+
use Piwik\Filesystem;
15+
16+
class ArtifactWriter
17+
{
18+
/**
19+
* @return false|int
20+
*/
21+
public function writeFile(string $filePath, string $contents)
22+
{
23+
Filesystem::mkdir(dirname($filePath));
24+
25+
return file_put_contents($filePath, $contents);
26+
}
27+
}

Specs/SpecGenerator.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use Matomo\Dependencies\OpenApiDocs\OpenApi\Generator;
1414
use Piwik\Container\StaticContainer;
1515
use Piwik\Exception\PluginNotFoundException;
16-
use Piwik\Filesystem;
1716
use Piwik\Log\LoggerInterface;
1817
use Piwik\Log\NullLogger;
1918
use Piwik\Plugin\Manager;
19+
use Piwik\Plugins\OpenApiDocs\Artifact\ArtifactWriter;
2020
use Piwik\Plugins\OpenApiDocs\OpenApiDocs;
2121
use Piwik\SettingsPiwik;
2222
use Piwik\Validators\BaseValidator;
@@ -29,9 +29,15 @@ class SpecGenerator
2929
*/
3030
private $specPathResolver;
3131

32-
public function __construct(?PathResolver $specPathResolver = null)
32+
/**
33+
* @var ArtifactWriter
34+
*/
35+
private $artifactWriter;
36+
37+
public function __construct(?PathResolver $specPathResolver = null, ?ArtifactWriter $artifactWriter = null)
3338
{
3439
$this->specPathResolver = $specPathResolver ?? new PathResolver();
40+
$this->artifactWriter = $artifactWriter ?? new ArtifactWriter();
3541

3642
// Set the constant for the current instance's URL
3743
if (!defined('LOCAL_MATOMO_SERVER_URL')) {
@@ -126,35 +132,9 @@ public function generateSpec(array $pluginNames, string $format = 'json', string
126132
$specContents = $lowercaseFormat === 'yaml' ? $openapi->toYaml() : $openapi->toJson();
127133
if ($writeToFile) {
128134
$pluginSpecPath = $this->specPathResolver->getSpecFilePath($specFileBaseName, $version, $lowercaseFormat);
129-
$this->writeSpecFile($pluginSpecPath, $specContents);
135+
$this->artifactWriter->writeFile($pluginSpecPath, $specContents);
130136
}
131137

132138
return $specContents;
133139
}
134-
135-
private function writeSpecFile(string $pluginSpecPath, string $specContents): void
136-
{
137-
$directory = dirname($pluginSpecPath);
138-
Filesystem::mkdir($directory);
139-
140-
if (!is_dir($directory) || !is_writable($directory)) {
141-
throw new \RuntimeException('OpenAPI spec output directory is not writable: ' . $directory);
142-
}
143-
144-
$temporaryFile = tempnam($directory, 'openapi_spec_');
145-
if ($temporaryFile === false) {
146-
throw new \RuntimeException('Could not create temporary file for OpenAPI spec output in ' . $directory);
147-
}
148-
149-
$bytesWritten = @file_put_contents($temporaryFile, $specContents, LOCK_EX);
150-
if ($bytesWritten === false) {
151-
@unlink($temporaryFile);
152-
throw new \RuntimeException('Could not write OpenAPI spec file to temporary path ' . $temporaryFile);
153-
}
154-
155-
if (!@rename($temporaryFile, $pluginSpecPath)) {
156-
@unlink($temporaryFile);
157-
throw new \RuntimeException('Could not move OpenAPI spec file into place at ' . $pluginSpecPath);
158-
}
159-
}
160140
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\OpenApiDocs\tests\Unit\Artifact;
13+
14+
require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php';
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Piwik\Plugins\OpenApiDocs\Artifact\ArtifactWriter;
18+
19+
/**
20+
* @group OpenApiDocs
21+
* @group OpenApiDocs_Unit
22+
* @group OpenApiDocs_ArtifactWriterTest
23+
*/
24+
class ArtifactWriterTest extends TestCase
25+
{
26+
private $temporaryDirectory;
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->temporaryDirectory = sys_get_temp_dir() . '/openapidocs_artifact_writer_' . uniqid('', true);
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
if (is_dir($this->temporaryDirectory)) {
38+
$files = scandir($this->temporaryDirectory);
39+
if (is_array($files)) {
40+
foreach ($files as $file) {
41+
if ($file === '.' || $file === '..') {
42+
continue;
43+
}
44+
45+
@unlink($this->temporaryDirectory . '/' . $file);
46+
}
47+
}
48+
49+
@rmdir($this->temporaryDirectory . '/nested');
50+
@rmdir($this->temporaryDirectory);
51+
}
52+
53+
parent::tearDown();
54+
}
55+
56+
public function testWriteFileCreatesDirectoryAndWritesContents(): void
57+
{
58+
$writer = new ArtifactWriter();
59+
$filePath = $this->temporaryDirectory . '/nested/example.json';
60+
61+
$result = $writer->writeFile($filePath, '{"status":"ok"}');
62+
63+
$this->assertIsInt($result);
64+
$this->assertSame('{"status":"ok"}', file_get_contents($filePath));
65+
}
66+
}

0 commit comments

Comments
 (0)