Completed
Push — 1.x ( 2f995a...fa8c59 )
by Samuel
18:04
created

CacheTool::setLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4285
nc 2
cc 2
eloc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
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 CacheTool;
13
14
use CacheTool\Adapter\AbstractAdapter;
15
use CacheTool\Proxy\ProxyInterface;
16
use Psr\Log\LoggerInterface;
17
use Monolog\Logger;
18
19
class CacheTool
20
{
21
    /**
22
     * @var AbstractAdapter
23
     */
24
    protected $adapter;
25
26
    /**
27
     * @var array
28
     */
29
    protected $proxies = array();
30
31
    /**
32
     * @var array
33
     */
34
    protected $functions = array();
35
36
    /**
37
     * @var string
38
     */
39
    protected $tempDir;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    protected $logger;
45
46
    /**
47
     * @param string          $tempDir
48
     * @param LoggerInterface $logger
49
     */
50
    public function __construct($tempDir = null, LoggerInterface $logger = null)
51
    {
52
        $this->logger = $logger ?: new Logger('cachetool');
53
54
        $tempDirs = array($tempDir, '/dev/shm', '/var/run', sys_get_temp_dir());
55
56
        foreach ($tempDirs as $tempDir) {
57
            if (is_dir($tempDir) && is_writable($tempDir)) {
58
                $this->tempDir = $tempDir;
59
                break;
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param  AbstractAdapter $adapter
66
     * @param  string          $tempDir
67
     * @param  LoggerInterface $logger
68
     * @return CacheTool
69
     */
70
    public static function factory(AbstractAdapter $adapter = null, $tempDir = null, LoggerInterface $logger = null)
71
    {
72
        $cacheTool = new static($tempDir, $logger);
73
        $cacheTool->addProxy(new Proxy\ApcProxy());
74
        $cacheTool->addProxy(new Proxy\ApcuProxy());
75
        $cacheTool->addProxy(new Proxy\PhpProxy());
76
        $cacheTool->addProxy(new Proxy\OpcacheProxy());
77
78
        if ($adapter instanceof AbstractAdapter) {
79
            $cacheTool->setAdapter($adapter);
80
        }
81
82
        return $cacheTool;
83
    }
84
85
    /**
86
     * @param  AbstractAdapter $adapter
87
     * @return CacheTool
88
     */
89
    public function setAdapter(AbstractAdapter $adapter)
90
    {
91
        $this->logger->info(sprintf('Setting adapter: %s', get_class($adapter)));
92
93
        $this->adapter = $adapter;
94
        $this->adapter->setLogger($this->logger);
95
        $this->adapter->setTempDir($this->tempDir);
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return AbstractAdapter
102
     */
103
    public function getAdapter()
104
    {
105
        return $this->adapter;
106
    }
107
108
    /**
109
     * @param ProxyInterface $proxy
110
     * @return CacheTool
111
     */
112
    public function addProxy(ProxyInterface $proxy)
113
    {
114
        $this->logger->info(sprintf('Adding Proxy: %s', get_class($proxy)));
115
116
        $this->proxies[] = $proxy;
117
118
        // reset functions (to be built when needed)
119
        $this->functions = array();
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getProxies()
128
    {
129
        return $this->proxies;
130
    }
131
132
    /**
133
     * @param  LoggerInterface $logger
134
     * @return CacheTool
135
     */
136
    public function setLogger(LoggerInterface $logger)
137
    {
138
        $this->logger = $logger;
139
140
        if ($this->adapter instanceof AbstractAdapter) {
141
            $this->adapter->setLogger($logger);
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * @return LoggerInterface
149
     */
150
    public function getLogger()
151
    {
152
        return $this->logger;
153
    }
154
155
    /**
156
     * Calls proxy functions
157
     *
158
     * @param  string $name
159
     * @param  array $arguments
160
     * @return mixed
161
     */
162
    public function __call($name, $arguments)
163
    {
164
        $this->logger->notice(sprintf('Executing: %s(%s)', $name, implode(', ', array_map('json_encode', $arguments))));
165
166
        if ($this->getFunction($name)) {
167
            return call_user_func_array($this->getFunction($name), $arguments);
168
        }
169
    }
170
171
    /**
172
     * Initializes functions and return callable
173
     *
174
     * @param  string $name
175
     * @return callable
176
     */
177
    protected function getFunction($name)
178
    {
179
        if (empty($this->functions)) {
180
            foreach ($this->proxies as $proxy) {
181
                $this->logger->info(sprintf('Loading Proxy: %s', get_class($proxy)));
182
183
                // lazily set adapter
184
                $proxy->setAdapter($this->adapter);
185
186
                foreach ($proxy->getFunctions() as $fn) {
187
                    $this->logger->debug(sprintf('Loading Function: %s', $fn));
188
                    $this->functions[$fn] = array($proxy, $fn);
189
                }
190
            }
191
        }
192
193
        if (isset($this->functions[$name])) {
194
            return $this->functions[$name];
195
        }
196
197
        throw new \InvalidArgumentException("Function with name: {$name} is not provided by any Proxy.");
198
    }
199
}
200