1 | <?php |
||
17 | class VarnishProxy extends AbstractProxy |
||
18 | { |
||
19 | protected $binary = 'varnishd'; |
||
20 | protected $port = 6181; |
||
21 | protected $managementPort = 6182; |
||
22 | protected $pid = '/tmp/foshttpcache-varnish.pid'; |
||
23 | protected $configFile; |
||
24 | protected $configDir; |
||
25 | protected $cacheDir; |
||
26 | protected $allowInlineC = false; |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | * |
||
31 | * @param string $configFile Path to VCL file |
||
32 | */ |
||
33 | 26 | public function __construct($configFile) |
|
34 | { |
||
35 | 26 | $this->setConfigFile($configFile); |
|
36 | 25 | $this->setCacheDir(sys_get_temp_dir().DIRECTORY_SEPARATOR.'foshttpcache-varnish'); |
|
37 | 25 | } |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 25 | public function start() |
|
43 | { |
||
44 | $args = [ |
||
45 | 25 | '-a', $this->ip.':'.$this->getPort(), |
|
46 | 25 | '-T', $this->ip.':'.$this->getManagementPort(), |
|
47 | 25 | '-f', $this->getConfigFile(), |
|
48 | 25 | '-n', $this->getCacheDir(), |
|
49 | 25 | '-p', 'vcl_dir='.$this->getConfigDir(), |
|
50 | |||
51 | 25 | '-P', $this->pid, |
|
52 | 25 | ]; |
|
53 | 25 | if ($this->getAllowInlineC()) { |
|
54 | $args[] = '-p'; |
||
55 | $args[] = 'vcc_allow_inline_c=on'; |
||
56 | } |
||
57 | |||
58 | 25 | $this->runCommand($this->getBinary(), $args); |
|
59 | |||
60 | 25 | $this->waitFor($this->ip, $this->getPort(), 5000); |
|
61 | 24 | } |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 23 | public function stop() |
|
67 | { |
||
68 | 23 | if (file_exists($this->pid)) { |
|
69 | try { |
||
70 | 23 | $this->runCommand('kill', ['-9', file_get_contents($this->pid)]); |
|
71 | 23 | } catch (\RuntimeException $e) { |
|
72 | // Ignore if command fails when Varnish wasn't running |
||
73 | } |
||
74 | 23 | unlink($this->pid); |
|
75 | 23 | $this->waitUntil($this->ip, $this->getPort(), 5000); |
|
76 | 23 | } |
|
77 | 23 | } |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 23 | public function clear() |
|
83 | { |
||
84 | 23 | $this->stop(); |
|
85 | 23 | $this->start(); |
|
86 | 23 | } |
|
87 | |||
88 | /** |
||
89 | * @param string $configDir |
||
90 | */ |
||
91 | 1 | public function setConfigDir($configDir) |
|
92 | { |
||
93 | 1 | $this->configDir = $configDir; |
|
94 | 1 | } |
|
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 25 | public function getConfigDir() |
|
100 | { |
||
101 | 25 | if (null === $this->configDir && null !== $this->configFile) { |
|
102 | 24 | return dirname(realpath($this->getConfigFile())); |
|
103 | } |
||
104 | |||
105 | 1 | return $this->configDir; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set Varnish management port (defaults to 6182). |
||
110 | * |
||
111 | * @param int $managementPort |
||
112 | */ |
||
113 | 1 | public function setManagementPort($managementPort) |
|
114 | { |
||
115 | 1 | $this->managementPort = $managementPort; |
|
116 | 1 | } |
|
117 | |||
118 | /** |
||
119 | * Get Varnish management port. |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | 25 | public function getManagementPort() |
|
124 | { |
||
125 | 25 | return $this->managementPort; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Set Varnish cache directory. |
||
130 | * |
||
131 | * @param string $cacheDir |
||
132 | */ |
||
133 | 25 | public function setCacheDir($cacheDir) |
|
134 | { |
||
135 | 25 | $this->cacheDir = $cacheDir; |
|
136 | 25 | } |
|
137 | |||
138 | /** |
||
139 | * Get Varnish cache directory. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 25 | public function getCacheDir() |
|
144 | { |
||
145 | 25 | return $this->cacheDir; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Whether the inline C flag should be set. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 25 | public function getAllowInlineC() |
|
157 | |||
158 | /** |
||
159 | * Set whether the inline c flag should be on or off. |
||
160 | * |
||
161 | * @param bool $allowInlineC True for on, false for off |
||
162 | */ |
||
163 | public function setAllowInlineC($allowInlineC) |
||
167 | } |
||
168 |