1818 *
1919 * @see https://www.php.net/manual/en/class.streamwrapper.php
2020 */
21- class CloudStorageStreamWrapper
21+ abstract class AbstractCloudStorageStreamWrapper
2222{
23- /**
24- * Name of the protocol used by the wrapper.
25- *
26- * @var string
27- */
28- public const PROTOCOL = 'cloudstorage ' ;
29-
3023 /**
3124 * The resource handled by the stream which is set by PHP.
3225 *
@@ -39,69 +32,77 @@ class CloudStorageStreamWrapper
3932 *
4033 * @var \ArrayObject|null
4134 */
42- private $ cache ;
35+ protected $ cache ;
4336
4437 /**
4538 * The cloud storage objects retrieved with "dir_opendir".
4639 *
4740 * @var \ArrayIterator|null
4841 */
49- private $ openedDirectoryObjects ;
42+ protected $ openedDirectoryObjects ;
5043
5144 /**
5245 * The path when "dir_opendir" was called.
5346 *
5447 * @var string|null
5548 */
56- private $ openedDirectoryPath ;
49+ protected $ openedDirectoryPath ;
5750
5851 /**
5952 * The prefix used to get the cloud storage objects with "dir_opendir".
6053 *
6154 * @var string|null
6255 */
63- private $ openedDirectoryPrefix ;
56+ protected $ openedDirectoryPrefix ;
6457
6558 /**
6659 * Mode used when the stream was opened.
6760 *
6861 * @var string
6962 */
70- private $ openedStreamMode ;
63+ protected $ openedStreamMode ;
7164
7265 /**
7366 * The key for the cloud storage object opened by "stream_open".
7467 *
7568 * @var string
7669 */
77- private $ openedStreamObjectKey ;
70+ protected $ openedStreamObjectKey ;
7871
7972 /**
8073 * The resource containing the cloud storage object opened by "stream_open".
8174 *
8275 * @var resource|null
8376 */
84- private $ openedStreamObjectResource ;
77+ protected $ openedStreamObjectResource ;
78+
79+ /**
80+ * Get the protocol used by the stream wrapper.
81+ */
82+ public static function getProtocol (): string
83+ {
84+ throw new \RuntimeException ('Must overrider "getProtocol" method ' );
85+ }
8586
8687 /**
8788 * Register the cloud storage stream wrapper.
8889 */
8990 public static function register (CloudStorageClientInterface $ client , \ArrayObject $ cache = null )
9091 {
91- if (in_array (self :: PROTOCOL , stream_get_wrappers ())) {
92- stream_wrapper_unregister (self :: PROTOCOL );
92+ if (in_array (static :: getProtocol () , stream_get_wrappers ())) {
93+ stream_wrapper_unregister (static :: getProtocol () );
9394 }
9495
95- stream_wrapper_register (self :: PROTOCOL , self ::class, STREAM_IS_URL );
96+ stream_wrapper_register (static :: getProtocol (), static ::class, STREAM_IS_URL );
9697
9798 $ defaultOptions = stream_context_get_options (stream_context_get_default ());
9899
99- $ defaultOptions [self :: PROTOCOL ]['client ' ] = $ client ;
100+ $ defaultOptions [static :: getProtocol () ]['client ' ] = $ client ;
100101
101102 if ($ cache instanceof \ArrayObject) {
102- $ defaultOptions [self :: PROTOCOL ]['cache ' ] = $ cache ;
103- } elseif (!isset ($ defaultOptions [self :: PROTOCOL ]['cache ' ])) {
104- $ defaultOptions [self :: PROTOCOL ]['cache ' ] = new \ArrayObject ();
103+ $ defaultOptions [static :: getProtocol () ]['cache ' ] = $ cache ;
104+ } elseif (!isset ($ defaultOptions [static :: getProtocol () ]['cache ' ])) {
105+ $ defaultOptions [static :: getProtocol () ]['cache ' ] = new \ArrayObject ();
105106 }
106107
107108 stream_context_set_default ($ defaultOptions );
@@ -312,7 +313,7 @@ public function stream_flush()
312313
313314 $ this ->getClient ()->putObject ($ this ->openedStreamObjectKey , stream_get_contents ($ this ->openedStreamObjectResource ), $ this ->getMimetype ());
314315
315- $ this ->removeCacheValue (self :: PROTOCOL .':// ' .$ this ->openedStreamObjectKey );
316+ $ this ->removeCacheValue (static :: getProtocol () .':// ' .$ this ->openedStreamObjectKey );
316317 });
317318 }
318319
@@ -356,7 +357,7 @@ public function stream_open(string $path, string $mode): bool
356357 // Remove the cache value in case we interacted with the file before using something
357358 // like "file_exists". If we don't write to the file, there won't be any cache busting
358359 // so this is the only opportunity to do so.
359- $ this ->removeCacheValue (self :: PROTOCOL .':// ' .$ this ->openedStreamObjectKey );
360+ $ this ->removeCacheValue (static :: getProtocol () .':// ' .$ this ->openedStreamObjectKey );
360361 }
361362
362363 $ this ->openedStreamObjectResource = fopen ('php://temp ' , 'r+ ' );
@@ -649,8 +650,8 @@ private function getOptions(): array
649650 $ context = stream_context_get_options ($ this ->context );
650651 }
651652
652- $ context = $ context [self :: PROTOCOL ] ?? [];
653- $ default = $ default [self :: PROTOCOL ] ?? [];
653+ $ context = $ context [static :: getProtocol () ] ?? [];
654+ $ default = $ default [static :: getProtocol () ] ?? [];
654655
655656 return $ context + $ default ;
656657 }
@@ -737,7 +738,7 @@ private function parseMode(string $key, string $mode): string
737738 */
738739 private function parsePath (string $ path ): string
739740 {
740- $ protocol = self :: PROTOCOL .':// ' ;
741+ $ protocol = static :: getProtocol () .':// ' ;
741742
742743 if (0 !== strpos ($ path , $ protocol )) {
743744 throw new \InvalidArgumentException (sprintf ('Invalid protocol for "%s" ' , $ path ));
0 commit comments