Skip to content

Commit b59f51e

Browse files
committed
Replace file_put_contents (#76)
1 parent 56c2b40 commit b59f51e

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

src/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Dapr\PubSub\Subscription;
2323
use Dapr\PubSub\Topic;
2424
use Dapr\State\Attributes\StateStore;
25+
use Dapr\State\FileWriter;
2526
use Dapr\State\StateManager;
2627
use Dapr\State\TransactionalState;
2728
use DI\ContainerBuilder;
@@ -422,7 +423,7 @@ function (
422423
) {
423424
$logger->critical('Received an event: {subject}', ['subject' => $event->subject]);
424425
touch('/tmp/sub-received');
425-
file_put_contents('/tmp/sub-received', $event->to_json());
426+
FileWriter::write('/tmp/sub-received', $event->to_json());
426427

427428
return [
428429
'status' => 'SUCCESS',

src/lib/Actors/Generators/CachedGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dapr\Actors\Generators;
44

5+
use Dapr\State\FileWriter;
56
use DI\FactoryInterface;
67
use JetBrains\PhpStorm\Pure;
78
use Psr\Container\ContainerInterface;
@@ -48,7 +49,7 @@ public function get_proxy(string $id): object
4849
mkdir($this->cache_dir);
4950
}
5051
$filename = $this->cache_dir.$this->get_short_class_name();
51-
file_put_contents($filename, $file);
52+
FileWriter::write($filename, $file);
5253
require_once $filename;
5354
}
5455

src/lib/Actors/Internal/Caches/FileCache.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Dapr\Actors\Internal\Caches;
44

5+
use Dapr\State\FileWriter;
6+
use phpDocumentor\Reflection\File;
7+
58
/**
69
* Class FileCache
710
* @package Dapr\Actors\Internal\Caches
@@ -78,6 +81,6 @@ private function serialize_cache()
7881
return;
7982
}
8083

81-
file_put_contents($this->cache_file, serialize($this->data));
84+
FileWriter::write($this->cache_file, serialize($this->data));
8285
}
8386
}

src/lib/State/FileWriter.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

tests/ActorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Dapr\exceptions\DaprException;
1414
use Dapr\exceptions\Http\NotFound;
1515
use Dapr\exceptions\SaveStateFailure;
16+
use Dapr\State\FileWriter;
1617
use DI\DependencyException;
1718
use DI\NotFoundException;
1819
use Fixtures\ActorClass;
@@ -357,7 +358,7 @@ public function testGeneratedClassIsCorrect()
357358
$generated_class = (string)FileGenerator::generate(ITestActor::class, $this->container);
358359
$take_snapshot = false;
359360
if ($take_snapshot) {
360-
file_put_contents(__DIR__.'/Fixtures/GeneratedProxy.php', $generated_class);
361+
FileWriter::write(__DIR__.'/Fixtures/GeneratedProxy.php', $generated_class);
361362
}
362363
$expected_proxy = file_get_contents(__DIR__.'/Fixtures/GeneratedProxy.php');
363364
$this->assertSame($expected_proxy, $generated_class);

0 commit comments

Comments
 (0)