File tree 5 files changed +46
-4
lines changed 5 files changed +46
-4
lines changed Original file line number Diff line number Diff line change 22
22
use Dapr \PubSub \Subscription ;
23
23
use Dapr \PubSub \Topic ;
24
24
use Dapr \State \Attributes \StateStore ;
25
+ use Dapr \State \FileWriter ;
25
26
use Dapr \State \StateManager ;
26
27
use Dapr \State \TransactionalState ;
27
28
use DI \ContainerBuilder ;
@@ -422,7 +423,7 @@ function (
422
423
) {
423
424
$ logger ->critical ('Received an event: {subject} ' , ['subject ' => $ event ->subject ]);
424
425
touch ('/tmp/sub-received ' );
425
- file_put_contents ('/tmp/sub-received ' , $ event ->to_json ());
426
+ FileWriter:: write ('/tmp/sub-received ' , $ event ->to_json ());
426
427
427
428
return [
428
429
'status ' => 'SUCCESS ' ,
Original file line number Diff line number Diff line change 2
2
3
3
namespace Dapr \Actors \Generators ;
4
4
5
+ use Dapr \State \FileWriter ;
5
6
use DI \FactoryInterface ;
6
7
use JetBrains \PhpStorm \Pure ;
7
8
use Psr \Container \ContainerInterface ;
@@ -48,7 +49,7 @@ public function get_proxy(string $id): object
48
49
mkdir ($ this ->cache_dir );
49
50
}
50
51
$ filename = $ this ->cache_dir .$ this ->get_short_class_name ();
51
- file_put_contents ($ filename , $ file );
52
+ FileWriter:: write ($ filename , $ file );
52
53
require_once $ filename ;
53
54
}
54
55
Original file line number Diff line number Diff line change 2
2
3
3
namespace Dapr \Actors \Internal \Caches ;
4
4
5
+ use Dapr \State \FileWriter ;
6
+ use phpDocumentor \Reflection \File ;
7
+
5
8
/**
6
9
* Class FileCache
7
10
* @package Dapr\Actors\Internal\Caches
@@ -78,6 +81,6 @@ private function serialize_cache()
78
81
return ;
79
82
}
80
83
81
- file_put_contents ($ this ->cache_file , serialize ($ this ->data ));
84
+ FileWriter:: write ($ this ->cache_file , serialize ($ this ->data ));
82
85
}
83
86
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Dapr \State ;
4
+
5
+ /**
6
+ * Class FileWriter
7
+ * @package Dapr\State
8
+ */
9
+ class FileWriter
10
+ {
11
+ /**
12
+ * @param string $filename The filename to write to
13
+ * @param string $contents The contents of the file to write
14
+ */
15
+ public static function write (string $ filename , string $ contents )
16
+ {
17
+ $ handle = fopen ($ filename , 'w ' );
18
+ if ($ handle === false ) {
19
+ throw new \RuntimeException ('Unable to open ' .$ filename .' for writing! ' );
20
+ }
21
+ $ content_length = strlen ($ contents );
22
+ $ write_result = fwrite ($ handle , $ contents , $ content_length );
23
+ if ($ write_result !== $ content_length ) {
24
+ throw new \RuntimeException ('Failed to write all content to ' .$ filename );
25
+ }
26
+ $ flush_result = fflush ($ handle );
27
+ if ($ flush_result === false ) {
28
+ throw new \RuntimeException ('Failed to flush ' .$ filename .' to disk. ' );
29
+ }
30
+ $ close_result = fclose ($ handle );
31
+ unset($ handle );
32
+ if ($ close_result === false ) {
33
+ throw new \RuntimeException ('Failed to close ' .$ filename );
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change 13
13
use Dapr \exceptions \DaprException ;
14
14
use Dapr \exceptions \Http \NotFound ;
15
15
use Dapr \exceptions \SaveStateFailure ;
16
+ use Dapr \State \FileWriter ;
16
17
use DI \DependencyException ;
17
18
use DI \NotFoundException ;
18
19
use Fixtures \ActorClass ;
@@ -357,7 +358,7 @@ public function testGeneratedClassIsCorrect()
357
358
$ generated_class = (string )FileGenerator::generate (ITestActor::class, $ this ->container );
358
359
$ take_snapshot = false ;
359
360
if ($ take_snapshot ) {
360
- file_put_contents (__DIR__ .'/Fixtures/GeneratedProxy.php ' , $ generated_class );
361
+ FileWriter:: write (__DIR__ .'/Fixtures/GeneratedProxy.php ' , $ generated_class );
361
362
}
362
363
$ expected_proxy = file_get_contents (__DIR__ .'/Fixtures/GeneratedProxy.php ' );
363
364
$ this ->assertSame ($ expected_proxy , $ generated_class );
You can’t perform that action at this time.
0 commit comments