-
Notifications
You must be signed in to change notification settings - Fork 4
/
structural-proxy.php
136 lines (119 loc) · 3.28 KB
/
structural-proxy.php
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
<?php
/**
* This example just to simulate how Proxy can be work
* 2nees.com
*/
/**
* Interface TextFilesControl - The interface for original service and proxy
*/
interface TextFilesControl {
public function read(): void;
public function write(string $txt): void;
public function open(): void;
public function close(): void;
public function getFile();
}
/**
* Class TxtFile - Original Server which its a concrete implementation
*/
class TxtFile implements TextFilesControl {
private $file;
private string $fileName;
/**
* txtFile constructor.
* @param string $fileName
*/
public function __construct(string $fileName)
{
$this->fileName = $fileName;
}
public function read(): void
{
// Output one line until end-of-file
echo file_get_contents("{$this->fileName}.txt") ?: "Empty" . PHP_EOL;
}
public function write(string $txt): void
{
fwrite($this->file, $txt);
}
public function open(): void
{
$this->file = fopen("{$this->fileName}.txt", "w+");
}
public function close(): void
{
fclose($this->file);
}
public function getFile()
{
return $this->file;
}
}
/**
* Class TxtFileProxy - This is the Proxy class which implements the same interface for Original Class
*/
class TxtFileProxy implements TextFilesControl {
private TextFilesControl $textFilesControl;
private int $role;
/**
* TxtFileProxy constructor.
* @param TextFilesControl $textFilesControl
* @param int $role
*/
public function __construct(TextFilesControl $textFilesControl, int $role)
{
$this->textFilesControl = $textFilesControl;
$this->role = $role;
}
public function read(): void
{
if($this->role !== 1){
echo "User Has Read Access Start Reading" . PHP_EOL;
}
echo "Read Full Data: " . PHP_EOL;
$this->textFilesControl->read();
}
public function write(string $txt): void
{
if($this->role === 1){
$this->textFilesControl->write($txt);
}else {
echo "Proxy Prevent You from Write to File" . PHP_EOL;
}
}
public function open(): void
{
if(!$this->getFile()){
$this->textFilesControl->open();
echo "File Open OR Created!" . PHP_EOL;
}else {
echo "File Already Open!" . PHP_EOL;
}
}
public function close(): void
{
$this->textFilesControl->close();
echo "File Closed!" . PHP_EOL;
}
public function getFile()
{
return $this->textFilesControl->getFile();
}
}
$file1 = new TxtFile("2neesFile1");
$fileProxy1 = new TxtFileProxy($file1, 1);
$fileProxy1->open();
$fileProxy1->read();
$fileProxy1->write("2nees.com explain Proxy Pattern! - 1\n");
$fileProxy1->write("2nees.com explain Proxy Pattern! - 2\n");
$fileProxy1->read();
$fileProxy1->close();
echo "==================================================" . PHP_EOL;
$file2 = new TxtFile("2neesFile2");
$fileProxy2 = new TxtFileProxy($file2, 2);
$fileProxy2->open();
$fileProxy2->read();
$fileProxy2->write("2nees.com explain Proxy Pattern! - 1\n");
$fileProxy2->write("2nees.com explain Proxy Pattern! - 2\n");
$fileProxy2->read();
$fileProxy2->close();