1111
1212namespace Piwik \Plugins \OpenApiDocs \Specs ;
1313
14- use Piwik \Container \Container ;
15- use Piwik \Container \StaticContainer ;
1614use Piwik \Plugin \Manager ;
15+ use Piwik \Piwik ;
1716use Piwik \Plugins \OpenApiDocs \OpenApiDocs ;
1817
1918class PathResolver
2019{
21- private const SHARED_BASE_SUBDIRECTORY = '/OpenApiDocs / ' ;
20+ private const ARTIFACT_BASE_SUBDIRECTORY = '/tmp / ' ;
2221
23- private const SHARED_SPECS_SUBDIRECTORY = '/OpenApiDocs/ specs/ ' ;
22+ private const SPECS_SUBDIRECTORY = 'specs/ ' ;
2423
25- private const SHARED_ANNOTATIONS_SUBDIRECTORY = '/OpenApiDocs/ annotations/ ' ;
24+ private const ANNOTATIONS_SUBDIRECTORY = 'annotations/ ' ;
2625
27- private const SHARED_RESPONSES_SUBDIRECTORY = '/OpenApiDocs/ responses/ ' ;
26+ private const RESPONSES_SUBDIRECTORY = 'responses/ ' ;
2827
2928 private $ pluginDirectory ;
3029
31- private $ isCloudActivated ;
32-
33- private $ container ;
34-
35- public function __construct (?string $ pluginDirectory = null , ?bool $ isCloudActivated = null , ?Container $ container = null )
30+ public function __construct (?string $ pluginDirectory = null )
3631 {
3732 $ this ->pluginDirectory = $ pluginDirectory ?? Manager::getInstance ()::getPluginDirectory ('OpenApiDocs ' );
38- $ this ->isCloudActivated = $ isCloudActivated ?? Manager::getInstance ()->isPluginActivated ('Cloud ' );
39- $ this ->container = $ container ?? $ this ->getStaticContainer ();
4033 }
4134
4235 public function getSpecDirectory (): string
4336 {
44- return $ this ->getArtifactDirectory (self ::SHARED_SPECS_SUBDIRECTORY , OpenApiDocs:: GENERATED_SPECS_PATH );
37+ return $ this ->getArtifactDirectory (self ::SPECS_SUBDIRECTORY );
4538 }
4639
4740 public function getSpecFilePath (
@@ -54,7 +47,7 @@ public function getSpecFilePath(
5447
5548 public function getAnnotationsDirectory (): string
5649 {
57- return $ this ->getArtifactDirectory (self ::SHARED_ANNOTATIONS_SUBDIRECTORY , OpenApiDocs:: GENERATED_ANNOTATIONS_PATH );
50+ return $ this ->getArtifactDirectory (self ::ANNOTATIONS_SUBDIRECTORY );
5851 }
5952
6053 public function getAnnotationFilePath (string $ pluginName ): string
@@ -69,53 +62,34 @@ public function getApiMethodInfoFilePath(string $fileBaseName): string
6962
7063 public function getResponsesDirectory (): string
7164 {
72- return $ this ->getArtifactDirectory (self ::SHARED_RESPONSES_SUBDIRECTORY , OpenApiDocs:: EXAMPLE_RESPONSES_PATH );
65+ return $ this ->getArtifactDirectory (self ::RESPONSES_SUBDIRECTORY );
7366 }
7467
7568 public function getExampleResponseFilePath (string $ pluginName , string $ methodName , string $ format ): string
7669 {
7770 return $ this ->getResponsesDirectory () . $ pluginName . '. ' . $ methodName . '. ' . strtolower ($ format );
7871 }
7972
80- private function getArtifactDirectory (string $ sharedSubdirectory , string $ fallbackPath ): string
73+ private function getArtifactDirectory (string $ subdirectory ): string
8174 {
82- $ sharedPath = $ this ->getSharedArtifactDirectory ($ sharedSubdirectory );
83- if ($ sharedPath !== null ) {
84- return $ sharedPath ;
85- }
86-
87- return $ this ->pluginDirectory . $ fallbackPath ;
75+ return $ this ->getArtifactBasePath () . $ subdirectory ;
8876 }
8977
90- private function getSharedArtifactDirectory ( string $ sharedSubdirectory ): ? string
78+ private function getArtifactBasePath ( ): string
9179 {
92- if (! $ this -> isCloudActivated || $ this -> container === null || ! $ this ->container -> has ( ' CloudDistributedCachePath ' )) {
93- return null ;
94- }
80+ $ defaultArtifactBasePath = $ this ->pluginDirectory . self :: ARTIFACT_BASE_SUBDIRECTORY ;
81+ $ artifactBasePath = $ defaultArtifactBasePath ;
82+ $ this -> dispatchArtifactBasePathEvent ( $ artifactBasePath );
9583
96- $ sharedBasePath = trim ((string ) $ this ->container ->get ('CloudDistributedCachePath ' ));
97- if ($ sharedBasePath === '' ) {
98- return null ;
84+ if (empty ($ artifactBasePath )) {
85+ $ artifactBasePath = $ defaultArtifactBasePath ;
9986 }
10087
101- if (!$ this ->isUsableSharedBasePath ($ sharedBasePath )) {
102- return null ;
103- }
104-
105- return rtrim ($ sharedBasePath , '/ \\' ) . self ::SHARED_BASE_SUBDIRECTORY . ltrim (substr ($ sharedSubdirectory , strlen (self ::SHARED_BASE_SUBDIRECTORY )), '/ \\' );
88+ return rtrim ($ artifactBasePath , '/ \\' ) . '/ ' ;
10689 }
10790
108- protected function isUsableSharedBasePath ( string $ sharedBasePath ): bool
91+ protected function dispatchArtifactBasePathEvent (? string & $ artifactBasePath ): void
10992 {
110- return is_dir ($ sharedBasePath ) && is_writable ($ sharedBasePath );
111- }
112-
113- private function getStaticContainer (): ?Container
114- {
115- try {
116- return StaticContainer::getContainer ();
117- } catch (\Throwable $ e ) {
118- return null ;
119- }
93+ Piwik::postEvent ('OpenApiDocs.getArtifactBasePath ' , [&$ artifactBasePath ]);
12094 }
12195}
0 commit comments