2020use Piwik \API \NoDefaultValue ;
2121use Piwik \API \Proxy ;
2222use Piwik \API \Request ;
23+ use Piwik \Filesystem ;
2324use Piwik \Http ;
2425use Piwik \Piwik ;
2526use Piwik \Plugin \Manager ;
2627use Piwik \Plugins \OpenApiDocs \OpenApiDocs ;
28+ use Piwik \Plugins \OpenApiDocs \Specs \PathResolver ;
2729use Piwik \SettingsPiwik ;
2830use Piwik \Url ;
2931use Piwik \UrlHelper ;
@@ -72,6 +74,11 @@ class AnnotationGenerator
7274 */
7375 protected $ generator ;
7476
77+ /**
78+ * @var PathResolver
79+ */
80+ protected $ pathResolver ;
81+
7582 /**
7683 * @var array[]
7784 */
@@ -82,9 +89,10 @@ class AnnotationGenerator
8289 */
8390 protected $ missingImportantDataWarnings ;
8491
85- public function __construct (DocumentationGenerator $ generator )
92+ public function __construct (DocumentationGenerator $ generator, ? PathResolver $ pathResolver = null )
8693 {
8794 $ this ->generator = $ generator ;
95+ $ this ->pathResolver = $ pathResolver ?? new PathResolver ();
8896 $ this ->missingImportantDataWarnings = [];
8997 $ this ->currentPluginDir = Manager::getInstance ()::getPluginDirectory ('OpenApiDocs ' );
9098 }
@@ -115,8 +123,7 @@ public function generatePluginApiAnnotations(string $pluginName, bool $writeToFi
115123 }
116124
117125 $ rules = require $ this ->currentPluginDir . '/Annotations/config.php ' ;
118- $ pluginAnnotationDir = $ this ->currentPluginDir . OpenApiDocs::GENERATED_ANNOTATIONS_PATH ;
119- $ pluginAnnotationPath = $ pluginAnnotationDir . "/ {$ pluginName }GeneratedAnnotations.php " ;
126+ $ pluginAnnotationPath = $ this ->pathResolver ->getAnnotationFilePath ($ pluginName );
120127
121128 $ className = Request::getClassNameAPI ($ pluginName );
122129
@@ -231,8 +238,7 @@ public function getContentForGeneratedAnnotationsFile(array $annotations, string
231238 */
232239 protected function writeAnnotationsToFile (array $ annotations , string $ filePath , string $ pluginName )
233240 {
234- // Create or overwrite the annotations file
235- return file_put_contents ($ filePath , $ this ->getContentForGeneratedAnnotationsFile ($ annotations , $ pluginName ));
241+ return $ this ->writeFile ($ filePath , $ this ->getContentForGeneratedAnnotationsFile ($ annotations , $ pluginName ));
236242 }
237243
238244 /**
@@ -903,11 +909,11 @@ protected function getExampleIfAvailable(string $url, bool $useLocalToken = fals
903909 }
904910 $ method = $ queryParams ['method ' ];
905911 $ format = strtolower ($ queryParams ['format ' ]);
906- $ exampleFilePath = $ this ->currentPluginDir . OpenApiDocs::EXAMPLE_RESPONSES_PATH . $ method . '. ' . $ format ;
912+ [$ pluginName , $ methodName ] = explode ('. ' , $ method );
913+ $ exampleFilePath = $ this ->pathResolver ->getExampleResponseFilePath ($ pluginName , $ methodName , $ format );
907914 // If there's already a file, use that instead of making a new server call. Ignore the file when the flag is set.
908915 if (!$ ignoreCached ) {
909916 // If an example file is found, return its contents instead of making the server call.
910- [$ pluginName , $ methodName ] = explode ('. ' , $ method );
911917 $ exampleContents = $ this ->getCachedExampleResponseFile ($ pluginName , $ methodName , $ format );
912918 if (!empty ($ exampleContents )) {
913919 return $ exampleContents ;
@@ -962,7 +968,7 @@ protected function getExampleIfAvailable(string $url, bool $useLocalToken = fals
962968 $ body = $ response ['data ' ];
963969
964970 // Write the example response to file as a cache and reference.
965- file_put_contents ($ exampleFilePath , $ body );
971+ $ this -> writeFile ($ exampleFilePath , $ body );
966972
967973 // Convert the XML responses into a JSON object and then encode it into a string. This is helpful for building schemas.
968974 if ($ format === 'xml ' ) {
@@ -994,7 +1000,7 @@ protected function getExampleIfAvailable(string $url, bool $useLocalToken = fals
9941000 */
9951001 protected function getCachedExampleResponseFile (string $ pluginName , string $ methodName , string $ format , bool $ rawResult = false , bool $ applyMaxLength = true ): string
9961002 {
997- $ exampleFilePath = $ this ->currentPluginDir . OpenApiDocs:: EXAMPLE_RESPONSES_PATH . $ pluginName . ' . ' . $ methodName . ' . ' . $ format ;
1003+ $ exampleFilePath = $ this ->pathResolver -> getExampleResponseFilePath ( $ pluginName, $ methodName, $ format) ;
9981004 // Simply return an empty string if the file doesn't exist yet.
9991005 if (!file_exists ($ exampleFilePath )) {
10001006 return '' ;
@@ -1021,6 +1027,14 @@ protected function getCachedExampleResponseFile(string $pluginName, string $meth
10211027 return $ exampleContents ;
10221028 }
10231029
1030+ protected function writeFile (string $ filePath , string $ contents )
1031+ {
1032+ $ directory = dirname ($ filePath );
1033+ Filesystem::mkdir ($ directory );
1034+
1035+ return file_put_contents ($ filePath , $ contents );
1036+ }
1037+
10241038 /**
10251039 * Try to build an example URL for a specific API method using report metadata. This queries the demo server for
10261040 * report metadata to get examples of existing reports which can be used as example URLS. If no metadata matches the
0 commit comments