Issues (27)

src/PhpFile.php (1 issue)

1
<?php
2
3
/*
4
 * This file is part of the Cache package.
5
 *
6
 * Copyright (c) Daniel González
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Daniel González <[email protected]>
12
 * @author Arnold Daniels <[email protected]>
13
 */
14
15
declare(strict_types=1);
16
17
namespace Desarrolla2\Cache;
18
19
use Desarrolla2\Cache\AbstractFile;
20
use Desarrolla2\Cache\Packer\PackerInterface;
21
use Desarrolla2\Cache\Packer\SerializePacker;
22
use Desarrolla2\Cache\File\BasicFilename;
23
24
/**
25
 * Cache file as PHP script.
26
 */
27
class PhpFile extends AbstractFile
28
{
29
    /**
30
     * Create the default packer for this cache implementation.
31
     *
32
     * @return PackerInterface
33
     */
34 80
    protected static function createDefaultPacker(): PackerInterface
35
    {
36 80
        return new SerializePacker();
37
    }
38
39
    /**
40
     * Get the filename callable
41
     *
42
     * @return callable
43
     */
44 197
    protected function getFilenameOption(): callable
45
    {
46 197
        if (!isset($this->filename)) {
47 197
            $this->filename = new BasicFilename('%s.php');
48
        }
49
50 197
        return $this->filename;
51
    }
52
53
    /**
54
     * Create a PHP script returning the cached value
55
     *
56
     * @param mixed    $value
57
     * @param int|null $ttl
58
     * @return string
59
     */
60 60
    public function createScript($value, ?int $ttl): string
61
    {
62 60
        $macro = var_export($value, true);
63
64 60
        if (strpos($macro, 'stdClass::__set_state') !== false) {
65
            $macro = preg_replace_callback("/('([^'\\\\]++|''\\.)')|stdClass::__set_state/", $macro, function($match) {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
66
                return empty($match[1]) ? '(object)' : $match[1];
67
            });
68
        }
69
70 60
        return $ttl !== null
71 4
            ? "<?php return time() < {$ttl} ? {$macro} : false;"
72 60
            : "<?php return {$macro};";
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 98
    public function get($key, $default = null)
79
    {
80 98
        $cacheFile = $this->getFilename($key);
81
82 62
        if (!file_exists($cacheFile)) {
83 27
            return $default;
84
        }
85
86 39
        $packed = include $cacheFile;
87
88 39
        return $packed === false ? $default : $this->unpack($packed);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 21
    public function has($key)
95
    {
96 21
        return $this->get($key) !== null;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 98
    public function set($key, $value, $ttl = null)
103
    {
104 98
        $cacheFile = $this->getFilename($key);
105
106 80
        $packed = $this->pack($value);
107 80
        $script = $this->createScript($packed, $this->ttlToTimestamp($ttl));
108
109 60
        return $this->writeFile($cacheFile, $script);
110
    }
111
}
112