1
+ <?php
2
+
3
+ namespace Arduino ;
4
+
5
+ class Wrapper
6
+ {
7
+
8
+ private static $ wrapperName = 'arduino ' ;
9
+ private $ path ;
10
+
11
+ public function __construct ()
12
+ {
13
+ self ::register ();
14
+ }
15
+
16
+ public function stream_open ($ path , $ mode , $ options = null , &$ opened_path = null )
17
+ {
18
+ $ realPath = str_replace ('arduino:// ' , '' , $ path );
19
+
20
+ if (!file_exists ($ realPath )) {
21
+ throw new \InvalidArgumentException ('Could not find Arduino connection in ' . $ realPath );
22
+ }
23
+
24
+ $ this ->path = fopen ($ realPath , 'r+ ' );
25
+
26
+ return true ;
27
+ }
28
+
29
+ public function stream_read ($ count )
30
+ {
31
+ return fread ($ this ->path , $ count );
32
+ }
33
+
34
+ public function stream_write ($ data )
35
+ {
36
+ return fwrite ($ this ->path , $ data );
37
+ }
38
+
39
+ public function stream_eof ()
40
+ {
41
+ return fclose ($ this ->path );
42
+ }
43
+
44
+ public static function register ()
45
+ {
46
+ // if we already defined the wrapper just return false
47
+ foreach (stream_get_wrappers () as $ wrapper ) {
48
+ if ($ wrapper == self ::$ wrapperName ) {
49
+ return false ;
50
+ }
51
+ }
52
+
53
+ stream_wrapper_register (self ::$ wrapperName , self ::class);
54
+ }
55
+ }
0 commit comments