1<?php
2/**
3 * A flat-file filesystem cache.
4 *
5 * @category  Xamin
6 * @package   Handlebars
7 * @author    Alex Soncodi <alex@brokerloop.com>
8 * @author    Behrooz Shabani <everplays@gmail.com>
9 * @author    Mardix <https://github.com/mardix>
10 * @copyright 2013 (c) Brokerloop, Inc.
11 * @copyright 2013 (c) Behrooz Shabani
12 * @copyright 2013 (c) Mardix
13 * @license   MIT
14 * @link      http://voodoophp.org/docs/handlebars
15 */
16
17namespace Handlebars\Cache;
18use Handlebars\Cache;
19use InvalidArgumentException;
20use RuntimeException;
21
22class Disk implements Cache
23{
24
25    private $path = '';
26    private $prefix = '';
27    private $suffix = '';
28
29    /**
30     * Construct the disk cache.
31     *
32     * @param string $path   Filesystem path to the disk cache location
33     * @param string $prefix optional file prefix, defaults to empty string
34     * @param string $suffix optional file extension, defaults to empty string
35     *
36     * @throws \RuntimeException
37     * @throws \InvalidArgumentException
38     */
39    public function __construct($path, $prefix = '', $suffix = '')
40    {
41        if (empty($path)) {
42            throw new InvalidArgumentException('Must specify disk cache path');
43        } elseif (!is_dir($path)) {
44            @mkdir($path, 0777, true);
45
46            if (!is_dir($path)) {
47                throw new RuntimeException('Could not create cache file path');
48            }
49        }
50
51        $this->path = $path;
52        $this->prefix = $prefix;
53        $this->suffix = $suffix;
54    }
55
56    /**
57     * Gets the full disk path for a given cache item's file,
58     * taking into account the cache path, optional prefix,
59     * and optional extension.
60     *
61     * @param string $name Name of the cache item
62     *
63     * @return string full disk path of cached item
64     */
65    private function getPath($name)
66    {
67        return $this->path . DIRECTORY_SEPARATOR .
68            $this->prefix . $name . $this->suffix;
69    }
70
71    /**
72     * Get cache for $name if it exists.
73     *
74     * @param string $name Cache id
75     *
76     * @return mixed data on hit, boolean false on cache not found
77     */
78    public function get($name)
79    {
80        $path = $this->getPath($name);
81
82        return (file_exists($path)) ?
83            unserialize(file_get_contents($path)) : false;
84    }
85
86    /**
87     * Set a cache
88     *
89     * @param string $name  cache id
90     * @param mixed  $value data to store
91     *
92     * @return void
93     */
94    public function set($name, $value)
95    {
96        $path = $this->getPath($name);
97
98        file_put_contents($path, serialize($value));
99    }
100
101    /**
102     * Remove cache
103     *
104     * @param string $name Cache id
105     *
106     * @return void
107     */
108    public function remove($name)
109    {
110        $path = $this->getPath($name);
111
112        unlink($path);
113    }
114
115}
116