Skip to content

Commit c082b2f

Browse files
authored
Merge pull request #13 from matomo-org/PG-4540-scope-dependencies
Upgrade dependencies, scope, and downgrade to match minimum PHP
2 parents d351900 + efa688a commit c082b2f

916 files changed

Lines changed: 33983 additions & 39614 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Annotations/GlobalApiComponents.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
/**
1717
* @OA\OpenApi(
18-
* openapi="3.1.0",
1918
* security={{"MatomoToken": {}}},
2019
* @OA\ExternalDocumentation(
2120
* description="Matomo Reporting API developer page",
@@ -58,7 +57,7 @@
5857
* description="Generic Matomo success payload.",
5958
* required={"result","message"},
6059
* additionalProperties=true,
61-
* @OA\Property(property="result", type="string", enum={"success"}, example="success"),
60+
* @OA\Property(property="result", type="string", example="success"),
6261
* @OA\Property(property="message", type="string", example="ok"),
6362
* @OA\Property(property="code", type="integer", example="200")
6463
* )
@@ -70,7 +69,7 @@
7069
* description="Generic Matomo error payload.",
7170
* required={"result","message"},
7271
* additionalProperties=true,
73-
* @OA\Property(property="result", type="string", enum={"error"}, example="error"),
72+
* @OA\Property(property="result", type="string", example="error"),
7473
* @OA\Property(property="message", type="string", example="There was an error"),
7574
* @OA\Property(property="code", type="integer")
7675
* )

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,31 @@
33
## Description
44

55
Allow generating OpenAPI documentation for the Matomo public APIs.
6+
7+
## Dependencies
8+
This plugin had its vendored dependencies scoped using [matomo scoper](https://github.com/matomo-org/matomo-scoper). This means that composer packages are prefixed so that they won't conflict with the same libraries used by other plugins.
9+
If you need to update a dependency, you should be able to run `composer install` to populate the vendor directory and then follow the [instructions for scoping a plugin](https://github.com/matomo-org/matomo-scoper#how-to-scope-a-matomo-plugin). Since the scoper.inc.php file already exists, it will hopefully be as simple as running the scoper for this plugin. Once that's done, you'll also need to make some of the dependencies compatible with Matomo's minimum supported version of PHP.
10+
This is done using the [Rector library](https://github.com/rectorphp/rector-downgrade-php). It's preferable that you install the composer package in a separate project and point to this project so that it doesn't get committed in this project. You should also have a config file saved containing the following:
11+
```php
12+
<?php
13+
14+
use Rector\Config\RectorConfig;
15+
16+
return static function (RectorConfig $rectorConfig): void {
17+
// Matomo requires PHP >= 7.2.5, but PHP 7.3 is close enough. We don't want to downgrade further than necessary.
18+
$rectorConfig->sets([
19+
\Rector\Set\ValueObject\DowngradeLevelSetList::DOWN_TO_PHP_73
20+
]);
21+
22+
$rectorConfig->skip([
23+
\Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector::class,
24+
// Skip downgrading Token for php-parser since it already provides a polyfill
25+
\Rector\DowngradePhp80\Rector\StaticCall\DowngradePhpTokenRector::class => [
26+
'*/vendor/prefixed/nikic/php-parser/*',
27+
],
28+
]);
29+
};
30+
```
31+
With all that in place, you should be able to run Rector like so: `vendor/bin/rector process {path_to_this_plugin/vendor/prefixed} --config={path_to_config_file}`
32+
33+
> **_NOTE:_** For Matomo developers, there's an internal DevPluginCommands plugin with a command that handles scoping and running Rector. See the SearchEngineKeywordsPerformance plugin's README.md for more details.

Specs/SpecGenerator.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace Piwik\Plugins\OpenApiDocs\Specs;
1111

12-
use OpenApi\Annotations\OpenApi;
13-
use OpenApi\Generator;
12+
use Matomo\Dependencies\OpenApiDocs\OpenApi\Annotations\OpenApi;
13+
use Matomo\Dependencies\OpenApiDocs\OpenApi\Generator;
1414
use Piwik\Container\StaticContainer;
1515
use Piwik\Log\LoggerInterface;
1616
use Piwik\Log\NullLogger;
@@ -60,6 +60,7 @@ public function generatePluginDoc(string $pluginName, string $format = 'json', s
6060
* @throws \Piwik\Exception\DI\DependencyException
6161
* @throws \Piwik\Exception\DI\NotFoundException
6262
* @throws \Piwik\Exception\PluginDeactivatedException
63+
* @throws \Exception
6364
*/
6465
public function generateSpec(array $pluginNames, string $format = 'json', string $version = OpenApiDocs::DEFAULT_SPEC_VERSION, bool $writeToFile = false): string
6566
{
@@ -73,17 +74,21 @@ public function generateSpec(array $pluginNames, string $format = 'json', string
7374

7475
$pluginDir = Manager::getInstance()::getPluginDirectory($pluginName);
7576
$pluginAnnotationsSource = $pluginDir . '/API.php';
76-
$openapi = (new Generator(StaticContainer::get(NullLogger::class)))->generate([
77-
$pluginAnnotationsSource,
78-
]);
77+
try {
78+
$openapi = (new Generator(StaticContainer::get(NullLogger::class)))->generate([
79+
$pluginAnnotationsSource,
80+
]);
81+
} catch (\Throwable $e) {
82+
throw new \Exception('There was an error testing the API annotations for plugin ' . $pluginName, 0, $e);
83+
}
7984
if (trim($openapi->toYaml()) === 'openapi: ' . OpenApi::DEFAULT_VERSION) {
8085
throw new \Exception("The $pluginName plugin's API class does not appear to be annotated yet.");
8186
}
8287
$pluginDirs[$pluginName] = $pluginAnnotationsSource;
8388
}
8489

8590
$generator = new Generator(StaticContainer::get(LoggerInterface::class));
86-
$openapi = $generator->generate(array_merge([
91+
$openapi = $generator->setVersion(OpenApi::VERSION_3_1_0)->generate(array_merge([
8792
$currentPluginDir . '/Annotations/GlobalApiComponents.php',
8893
], $pluginDirs));
8994

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"require": {
3-
"zircote/swagger-php": "^5.1.4",
4-
"doctrine/annotations": "^2.0",
5-
"symfony/yaml": "^6.4",
6-
"symfony/finder": "^6.4"
3+
"zircote/swagger-php": "^5.4",
4+
"doctrine/annotations": "^2.0"
75
},
86
"replace": {
97
"monolog/monolog": "*",

composer.lock

Lines changed: 46 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"license": "GPL v3+",
1313
"homepage": "https:\/\/matomo.org",
1414
"require": {
15-
"php": ">=8.1.0",
1615
"matomo": ">=5.0.0-stable,<6.0.0-b1"
1716
},
1817
"authors": [

scoper.inc.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
->exclude('lang')
2929
->exclude('javascripts')
3030
->exclude('vue')
31-
->notName(['scoper.inc.php', 'Controller.php'])
31+
->notName(['scoper.inc.php'])
3232
->filter(function (\SplFileInfo $file) {
3333
return !($file->isLink() && $file->isDir());
3434
})
@@ -56,7 +56,26 @@
5656
'force-no-global-alias' => $forceNoGlobalAlias,
5757
'prefix' => 'Matomo\\Dependencies\\' . $pluginName,
5858
'finders' => $finders,
59-
'patchers' => [],
59+
'patchers' => [
60+
// Patcher for making sure that AbstractAnnotation is looking for the correct root
61+
static function (string $filePath, string $prefix, string $content) use ($isRenamingReferences): string {
62+
if ($isRenamingReferences) {
63+
return $content;
64+
}
65+
66+
// Fix the string reference of a scoped dependency in the AbstractAnnotation class
67+
$escapedPrefix = str_replace('\\', '\\\\', $prefix);
68+
if ($filePath === __DIR__ . '/vendor/zircote/swagger-php/src/Annotations/AbstractAnnotation.php') {
69+
$content = str_replace(
70+
'OpenApi\\\\Annotations\\\\',
71+
"{$escapedPrefix}\\\\OpenApi\\\\Annotations\\\\",
72+
$content
73+
);
74+
}
75+
76+
return $content;
77+
},
78+
],
6079
'include-namespaces' => $namespacesToIncludeRegexes,
6180
'exclude-namespaces' => $namespacesToExclude,
6281
'exclude-constants' => [

0 commit comments

Comments
 (0)