1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\CacheDI;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class CacheExtension extends Nette\DI\CompilerExtension
17: {
18:
19: private $tempDir;
20:
21:
22: public function __construct($tempDir)
23: {
24: $this->tempDir = $tempDir;
25: }
26:
27:
28: public function loadConfiguration()
29: {
30: $builder = $this->getContainerBuilder();
31:
32: $builder->addDefinition($this->prefix('journal'))
33: ->setClass(Nette\Caching\Storages\IJournal::class)
34: ->setFactory(Nette\Caching\Storages\SQLiteJournal::class, [$this->tempDir . '/cache/journal.s3db']);
35:
36: $builder->addDefinition($this->prefix('storage'))
37: ->setClass(Nette\Caching\IStorage::class)
38: ->setFactory(Nette\Caching\Storages\FileStorage::class, [$this->tempDir . '/cache']);
39:
40: if ($this->name === 'cache') {
41: $builder->addAlias('nette.cacheJournal', $this->prefix('journal'));
42: $builder->addAlias('cacheStorage', $this->prefix('storage'));
43: }
44: }
45:
46:
47: public function afterCompile(Nette\PhpGenerator\ClassType $class)
48: {
49: if (!$this->checkTempDir($this->tempDir . '/cache')) {
50: $class->getMethod('initialize')->addBody('Nette\Caching\Storages\FileStorage::$useDirectories = FALSE;');
51: }
52: }
53:
54:
55: private function checkTempDir($dir)
56: {
57: @mkdir($dir);
58:
59:
60: $uniq = uniqid('_', TRUE);
61: if (!@mkdir("$dir/$uniq")) {
62: throw new Nette\InvalidStateException("Unable to write to directory '$dir'. Make this directory writable.");
63: }
64:
65:
66: $isWritable = @file_put_contents("$dir/$uniq/_", '') !== FALSE;
67: if ($isWritable) {
68: unlink("$dir/$uniq/_");
69: }
70: rmdir("$dir/$uniq");
71: return $isWritable;
72: }
73:
74: }
75: