Completed
Pull Request — master (#444)
by Yanick
02:29
created

LiteSpeed::purge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\ProxyClient;
13
14
use FOS\HttpCache\ProxyClient\Invalidation\ClearCapable;
15
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
16
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
17
18
/**
19
 * LiteSpeed Web Server (LSWS) invalidator.
20
 *
21
 * @author Yanick Witschi <[email protected]>
22
 */
23
class LiteSpeed extends HttpProxyClient implements PurgeCapable, TagCapable, ClearCapable
24
{
25
    private $headerLines = [];
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function clear()
31
    {
32
        $this->headerLines[] = 'X-LiteSpeed-Purge: *';
33
34
        return $this;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function purge($url, array $headers = [])
41
    {
42
        $this->headerLines[] = 'X-LiteSpeed-Purge: '.$url;
43
44
        return $this;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function configureOptions()
51
    {
52
        $resolver = parent::configureOptions();
53
54
        $resolver->setRequired(['target_dir']);
55
        $resolver->setAllowedTypes('target_dir', 'string');
56
57
        return $resolver;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function invalidateTags(array $tags)
64
    {
65
        $this->headerLines[] = 'X-LiteSpeed-Purge: '.implode(', ', preg_filter('/^/', 'tag=', $tags));
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function flush()
74
    {
75
        $filename = $this->createFile();
76
77
        $url = '/'.$filename;
78
79
        $this->queueRequest('GET', $url, []);
80
81
        $result = parent::flush();
82
83
        // Reset
84
        $this->headerLines = [];
85
        unlink($this->options['target_dir'].'/'.$filename);
86
87
        return $result;
88
    }
89
90
    /**
91
     * Creates the file and returns the file name.
92
     *
93
     * @return string
94
     */
95
    private function createFile()
96
    {
97
        $content = '<?php'."\n\n";
98
99
        foreach ($this->headerLines as $header) {
100
            $content .= sprintf('header(\'%s\');', $header)."\n";
101
        }
102
103
        // Generate a reasonably random file name, no need to be cryptographically safe here
104
        $filename = 'fos_cache_litespeed_purger_'.substr(sha1(uniqid('', true).mt_rand()), 0, mt_rand(10, 40)) . '.php';
105
106
        file_put_contents($this->options['target_dir'].'/'.$filename, $content);
107
108
        return $filename;
109
    }
110
}
111