-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathStreamsJsonToFile.php
More file actions
138 lines (122 loc) · 2.7 KB
/
StreamsJsonToFile.php
File metadata and controls
138 lines (122 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace ProcessMaker\Traits;
use Illuminate\Support\Facades\Storage;
use Log;
trait StreamsJsonToFile
{
/**
* The file resource.
*
* @var object
*/
private $file;
/**
* The path where our file should be stored.
*
* @var string
*/
protected $filePath;
/**
* The name of our file.
*
* @var string
*/
protected $fileName;
/**
* Open the file resource.
*
* @param string $name
* @param string|null $directory
*
* @return void
*/
private function openFile($name, $directory = null)
{
$this->fileName = $name;
Storage::put("{$directory}{$this->fileName}", '');
$this->filePath = storage_path("app/{$directory}{$this->fileName}");
$this->file = fopen($this->filePath, 'w+');
fwrite($this->file, '{');
}
/**
* Write to the file resource.
*
* @param string $data
*
* @return void
*/
private function write($data)
{
fwrite($this->file, $data);
}
/**
* Seek within the file resource.
*
* @param int $offset
* @param int $whence one of SEEK_SET, SEEK_CUR, SEEK_END
*
* @return void
*/
private function seek($offset, $whence)
{
fseek($this->file, $offset, $whence);
}
/**
* Push a key/value pair.
*
* @param string|array|object $data
* @param string|array|object|null $value
* @param bool $isLast
*
* @return void
*/
private function push($data, $value = null, $isLast = false)
{
if ($value && (is_string($value) || is_array($value) || is_object($value))) {
$data = preg_replace('/^{|}$/', '', json_encode([$data => $value]));
} else {
$data = json_encode($data);
}
if (!$isLast) {
$data .= ', ';
}
fwrite($this->file, $data);
}
/**
* Push a JSON key.
*
* @param string $data
*
* @return void
*/
private function pushKey($data)
{
$data = preg_replace('/^{|}$/', '', json_encode($data));
$data .= ': ';
fwrite($this->file, $data);
}
/**
* Push a JSON value.
*
* @param string|array|object $data
*
* @return void
*/
private function pushValue($data)
{
$data = preg_replace('/^{|}$/', '', json_encode($data));
fwrite($this->file, $data);
}
/**
* Close and save the file.
*
* @return bool
*/
protected function closeFile($noBracket = false)
{
if (!$noBracket) {
fwrite($this->file, '}');
}
return fclose($this->file);
}
}